-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix that XML comment examples do not show up if the type is string and the example contains quotation marks #2727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martincostello
merged 6 commits into
domaindrivendev:master
from
dldl-cmd:2088_fix_string_examples_with_quotes
May 6, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a18bb77
Fix that XML comment examples do not show up if the type is string an…
dldl-cmd 6f6837c
allow schema to be null in example creation
dldl-cmd 8186b21
Adapt suggestion from code review
dldl-cmd 6e225b9
Make XmlCommentsExampleHelper internal
dldl-cmd 9020e18
Update asserts
dldl-cmd 7989435
PR suggestions, adapting asserts
dldl-cmd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Swashbuckle.AspNetCore.SwaggerGen/XmlComments/XmlCommentsExampleHelper.cs
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.