-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Apply only a SchemaFilter to create the Description on the SwaggerUi #2943
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 12 commits into
domaindrivendev:master
from
jgarciadelanoceda:issue_1685_EnumDescriptions
Jun 18, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cc50dac
Apply only a SchemaFilter to create the Description on the SwaggerUi
5ff5576
Fix possible null Reference
36d5b70
Fix error on Property EnumArray
96f5509
Another issue with enumArray property
0df3e01
IntegrationTest on Basic example
7b5c408
Test using verify
f2eca3e
Do not use verify and assert.equal to the description of the component
5adda0a
try verify again
aac4e2b
Removed again the Verify
907e422
Delete the GeneratedSchema from Verify Test
126c610
Merge branch 'master' into issue_1685_EnumDescriptions
6163ff4
Another try using verify
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
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
34 changes: 34 additions & 0 deletions
34
test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/TestEnumSchemaFilter.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,34 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| namespace Swashbuckle.AspNetCore.SwaggerGen.Test.Fixtures | ||
| { | ||
| internal class TestEnumSchemaFilter : ISchemaFilter | ||
| { | ||
| public void Apply(OpenApiSchema schema, SchemaFilterContext context) | ||
| { | ||
| bool isEnumArgument = (context.Type?.GenericTypeArguments?.Length ?? 0) == 1 && context.Type.GenericTypeArguments.All(b => b.IsEnum); | ||
| var isEnumArray = context.Type.IsArray && context.Type.GetElementType().IsEnum; | ||
| if (context.Type.IsEnum || isEnumArgument || isEnumArray) | ||
| { | ||
| var enumType = (context.Type.IsEnum, isEnumArgument, isEnumArray) switch | ||
| { | ||
| (true, _, _) => context.Type, | ||
| (_, true, _) => context.Type.GenericTypeArguments.First(), | ||
| _ => context.Type.GetElementType() | ||
| }; | ||
| StringBuilder stringBuilder = new("<p>Members:</p><ul>"); | ||
| foreach (var enumValue in Enum.GetValues(enumType)) | ||
| { | ||
| if (enumValue is Enum value) | ||
| { | ||
| stringBuilder.Append($"<li>{value} - {value:d}</li>"); | ||
| } | ||
| } | ||
| schema.Description = stringBuilder.Append("</ul>").ToString(); | ||
| } | ||
| } | ||
| } | ||
| } |
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
130 changes: 130 additions & 0 deletions
130
...aggerGeneratorVerifyTests.GetSwagger_Copies_Description_From_GeneratedSchema.verified.txt
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,130 @@ | ||
| { | ||
| Info: { | ||
| Title: Test API, | ||
| Version: V1 | ||
| }, | ||
| Paths: { | ||
| /resource: { | ||
| Operations: { | ||
| Post: { | ||
| Tags: [ | ||
| { | ||
| Name: Fake, | ||
| UnresolvedReference: false | ||
| } | ||
| ], | ||
| Parameters: [ | ||
| { | ||
| UnresolvedReference: false, | ||
| Name: EnumWithDefault, | ||
| In: Query, | ||
| Description: <p>Members:</p><ul><li>Value2 - 2</li><li>Value4 - 4</li><li>Value8 - 8</li></ul>, | ||
| Required: false, | ||
| Deprecated: false, | ||
| AllowEmptyValue: false, | ||
| Style: Form, | ||
| Explode: true, | ||
| AllowReserved: false, | ||
| Schema: { | ||
| ReadOnly: false, | ||
| WriteOnly: false, | ||
| AdditionalPropertiesAllowed: true, | ||
| Nullable: false, | ||
| Deprecated: false, | ||
| UnresolvedReference: false, | ||
| Reference: { | ||
| IsFragrament: false, | ||
| Type: Schema, | ||
| Id: IntEnum, | ||
| IsExternal: false, | ||
| IsLocal: true, | ||
| ReferenceV3: #/components/schemas/IntEnum, | ||
| ReferenceV2: #/definitions/IntEnum | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| UnresolvedReference: false, | ||
| Name: EnumArrayWithDefault, | ||
| In: Query, | ||
| Description: <p>Members:</p><ul><li>Value2 - 2</li><li>Value4 - 4</li><li>Value8 - 8</li></ul>, | ||
| Required: false, | ||
| Deprecated: false, | ||
| AllowEmptyValue: false, | ||
| Style: Form, | ||
| Explode: true, | ||
| AllowReserved: false, | ||
| Schema: { | ||
| Type: array, | ||
| Description: <p>Members:</p><ul><li>Value2 - 2</li><li>Value4 - 4</li><li>Value8 - 8</li></ul>, | ||
| Default: [ | ||
| { | ||
| Value: 4 | ||
| } | ||
| ], | ||
| ReadOnly: false, | ||
| WriteOnly: false, | ||
| Items: { | ||
| ReadOnly: false, | ||
| WriteOnly: false, | ||
| AdditionalPropertiesAllowed: true, | ||
| Nullable: false, | ||
| Deprecated: false, | ||
| UnresolvedReference: false, | ||
| Reference: { | ||
| IsFragrament: false, | ||
| Type: Schema, | ||
| Id: IntEnum, | ||
| IsExternal: false, | ||
| IsLocal: true, | ||
| ReferenceV3: #/components/schemas/IntEnum, | ||
| ReferenceV2: #/definitions/IntEnum | ||
| } | ||
| }, | ||
| AdditionalPropertiesAllowed: true, | ||
| Nullable: false, | ||
| Deprecated: false, | ||
| UnresolvedReference: false | ||
| } | ||
| } | ||
| ], | ||
| Responses: { | ||
| 200: { | ||
| Description: OK, | ||
| UnresolvedReference: false | ||
| } | ||
| }, | ||
| Deprecated: false | ||
| } | ||
| }, | ||
| UnresolvedReference: false | ||
| } | ||
| }, | ||
| Components: { | ||
| Schemas: { | ||
| IntEnum: { | ||
| Type: integer, | ||
| Format: int32, | ||
| Description: <p>Members:</p><ul><li>Value2 - 2</li><li>Value4 - 4</li><li>Value8 - 8</li></ul>, | ||
| ReadOnly: false, | ||
| WriteOnly: false, | ||
| AdditionalPropertiesAllowed: true, | ||
| Enum: [ | ||
| { | ||
| Value: 2 | ||
| }, | ||
| { | ||
| Value: 4 | ||
| }, | ||
| { | ||
| Value: 8 | ||
| } | ||
| ], | ||
| Nullable: false, | ||
| Deprecated: false, | ||
| UnresolvedReference: false | ||
| } | ||
| } | ||
| }, | ||
| HashCode: 29A4A89ADE3B75E921A9EF9CE77F3E1517293B167A0B1513F55C57F3E84194193E8B24F875170DE0524C73CF5AB9DDCDF6556F1BDC2E6EA1F8171340F4C0F0B1 | ||
| } |
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.