forked from domaindrivendev/Swashbuckle.AspNetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix that XML comment examples do not show up if the type is string an…
…d the example contains quotation marks domaindrivendev#2088
- Loading branch information
Daniel
committed
Oct 13, 2023
1 parent
8f363f7
commit 618102c
Showing
10 changed files
with
96 additions
and
35 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 | ||
{ | ||
public static class XmlCommentsExampleHelper | ||
{ | ||
public static IOpenApiAny Create( | ||
SchemaRepository schemaRepository, | ||
OpenApiSchema schema, | ||
string exampleString) | ||
{ | ||
var isStringType = | ||
(schema.ResolveType(schemaRepository) == "string") && | ||
!exampleString.Equals("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
6 changes: 3 additions & 3 deletions
6
test/Swashbuckle.AspNetCore.SwaggerGen.Test/Swashbuckle.AspNetCore.SwaggerGen.Test.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
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() | ||
{ | ||
OpenApiSchema schema = new OpenApiSchema(); | ||
|
||
IOpenApiAny example = XmlCommentsExampleHelper.Create( | ||
schemaRepository, | ||
schema, | ||
"[\"one\",\"two\",\"three\"]"); | ||
|
||
|
||
var output = (OpenApiArray)example; | ||
Assert.Equal(3, output.Count); | ||
Assert.Equal("one", ((OpenApiString)output[0]).Value); | ||
Assert.Equal("two", ((OpenApiString)output[1]).Value); | ||
Assert.Equal("three", ((OpenApiString)output[2]).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.Equal(((OpenApiString)example).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.Equal(AnyType.Null, ((OpenApiNull)example).AnyType); | ||
} | ||
} | ||
} |
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