-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix XML comment examples where example contains quotes (#2727)
Fix that XML comment examples do not show up if the type is string and the example contains quotation marks.
- Loading branch information
Showing
9 changed files
with
113 additions
and
32 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Text.Json; | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Swashbuckle.AspNetCore.SwaggerGen | ||
{ | ||
internal static class XmlCommentsExampleHelper | ||
{ | ||
public static IOpenApiAny Create( | ||
SchemaRepository schemaRepository, | ||
OpenApiSchema schema, | ||
string exampleString) | ||
{ | ||
var isStringType = | ||
(schema?.ResolveType(schemaRepository) == "string") && | ||
!string.Equals(exampleString, "null"); | ||
|
||
var exampleAsJson = isStringType | ||
? JsonSerializer.Serialize(exampleString) | ||
: exampleString; | ||
|
||
var example = OpenApiAnyFactory.CreateFromJson(exampleAsJson); | ||
|
||
return example; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
test/Swashbuckle.AspNetCore.SwaggerGen.Test/XmlComments/XmlCommentsExampleHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Models; | ||
using Xunit; | ||
|
||
namespace Swashbuckle.AspNetCore.SwaggerGen.Test | ||
{ | ||
public class XmlCommentsExampleHelperTests | ||
{ | ||
private readonly SchemaRepository schemaRepository = new SchemaRepository(); | ||
|
||
[Fact] | ||
public void Create_BuildsOpenApiArrayJson__When_NotStringTypeAndDataIsArray() | ||
{ | ||
var schema = new OpenApiSchema(); | ||
|
||
IOpenApiAny example = XmlCommentsExampleHelper.Create( | ||
schemaRepository, | ||
schema, | ||
"[\"one\",\"two\",\"three\"]"); | ||
|
||
Assert.NotNull(example); | ||
var actual = Assert.IsType<OpenApiArray>(example); | ||
Assert.Equal(3, actual.Count); | ||
|
||
var item1 = Assert.IsType<OpenApiString>(actual[0]); | ||
var item2 = Assert.IsType<OpenApiString>(actual[1]); | ||
var item3 = Assert.IsType<OpenApiString>(actual[2]); | ||
Assert.Equal("one", item1.Value); | ||
Assert.Equal("two", item2.Value); | ||
Assert.Equal("three", item3.Value); | ||
} | ||
|
||
[Fact] | ||
public void Create_BuildsOpenApiString_When_TypeString() | ||
{ | ||
string exampleString = "example string with special characters\"<>\r\n\""; | ||
OpenApiSchema schema = new OpenApiSchema { Type = "string" }; | ||
schemaRepository.AddDefinition("test", schema); | ||
|
||
IOpenApiAny example = XmlCommentsExampleHelper.Create( | ||
schemaRepository, schema, exampleString); | ||
|
||
Assert.NotNull(example); | ||
var actual = Assert.IsType<OpenApiString>(example); | ||
Assert.Equal(actual.Value, exampleString); | ||
} | ||
|
||
[Fact] | ||
public void Create_ReturnsNull_When_TypeString_and_ValueNull() | ||
{ | ||
OpenApiSchema schema = new OpenApiSchema { Type = "string" }; | ||
schemaRepository.AddDefinition("test", schema); | ||
|
||
IOpenApiAny example = XmlCommentsExampleHelper.Create( | ||
schemaRepository, schema, "null"); | ||
|
||
Assert.NotNull(example); | ||
var actual = Assert.IsType<OpenApiNull>(example); | ||
Assert.Equal(AnyType.Null, actual.AnyType); | ||
} | ||
|
||
[Fact] | ||
public void Create_AllowsSchemaToBeNull() | ||
{ | ||
OpenApiSchema schema = null; | ||
|
||
IOpenApiAny example = XmlCommentsExampleHelper.Create(schemaRepository, schema, "[]"); | ||
|
||
Assert.NotNull(example); | ||
var actual = Assert.IsType<OpenApiArray>(example); | ||
Assert.Empty(actual); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters