-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Add support for CreateSchemaReferenceId option #56753
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
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
201 changes: 201 additions & 0 deletions
201
src/OpenApi/test/Services/CreateSchemaReferenceIdTests.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,201 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json.Serialization.Metadata; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.OpenApi; | ||
| using Microsoft.OpenApi.Any; | ||
| using Microsoft.OpenApi.Models; | ||
|
|
||
| public class CreateSchemaReferenceIdTests : OpenApiDocumentServiceTestBase | ||
|
captainsafia marked this conversation as resolved.
|
||
| { | ||
| [Fact] | ||
| public async Task HandlesPolymorphicTypeWithCustomReferenceIds() | ||
| { | ||
| // Arrange | ||
| var builder = CreateBuilder(); | ||
|
|
||
| // Act | ||
| builder.MapPost("/api", (Shape shape) => { }); | ||
| string createReferenceId(JsonTypeInfo jsonTypeInfo) | ||
| { | ||
| return jsonTypeInfo.Type.Name switch | ||
| { | ||
| "Shape" => "MyShape", | ||
| "Triangle" => "MyTriangle", | ||
| "Square" => "MySquare", | ||
| _ => jsonTypeInfo.Type.Name, | ||
| }; | ||
| } | ||
| var options = new OpenApiOptions { CreateSchemaReferenceId = createReferenceId }; | ||
|
|
||
| // Assert | ||
| await VerifyOpenApiDocument(builder, options, document => | ||
| { | ||
| var operation = document.Paths["/api"].Operations[OperationType.Post]; | ||
| Assert.NotNull(operation.RequestBody); | ||
| var requestBody = operation.RequestBody.Content; | ||
| Assert.True(requestBody.TryGetValue("application/json", out var mediaType)); | ||
| var schema = mediaType.Schema.GetEffective(document); | ||
| // Assert discriminator mappings have been configured correctly | ||
| Assert.Equal("$type", schema.Discriminator.PropertyName); | ||
| Assert.Contains(schema.Discriminator.PropertyName, schema.Required); | ||
| Assert.Collection(schema.Discriminator.Mapping, | ||
| item => Assert.Equal("triangle", item.Key), | ||
| item => Assert.Equal("square", item.Key) | ||
| ); | ||
| Assert.Collection(schema.Discriminator.Mapping, | ||
| item => Assert.Equal("#/components/schemas/MyShapeMyTriangle", item.Value), | ||
| item => Assert.Equal("#/components/schemas/MyShapeMySquare", item.Value) | ||
| ); | ||
| // Assert the schemas with the discriminator have been inserted into the components | ||
| Assert.True(document.Components.Schemas.TryGetValue("MyShapeMyTriangle", out var triangleSchema)); | ||
| Assert.Contains(schema.Discriminator.PropertyName, triangleSchema.Properties.Keys); | ||
| Assert.Equal("triangle", ((OpenApiString)triangleSchema.Properties[schema.Discriminator.PropertyName].Enum.First()).Value); | ||
| Assert.True(document.Components.Schemas.TryGetValue("MyShapeMySquare", out var squareSchema)); | ||
| Assert.Equal("square", ((OpenApiString)squareSchema.Properties[schema.Discriminator.PropertyName].Enum.First()).Value); | ||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GeneratesSchemaForPoco_WithSchemaReferenceIdCustomization() | ||
| { | ||
| // Arrange | ||
| var builder = CreateBuilder(); | ||
|
|
||
| // Act | ||
| builder.MapPost("/", (Todo todo) => { }); | ||
| var options = new OpenApiOptions { CreateSchemaReferenceId = (type) => $"{type.Type.Name}Schema" }; | ||
|
|
||
| // Assert | ||
| await VerifyOpenApiDocument(builder, options, document => | ||
| { | ||
| var operation = document.Paths["/"].Operations[OperationType.Post]; | ||
| var requestBody = operation.RequestBody; | ||
|
|
||
| Assert.NotNull(requestBody); | ||
| var content = Assert.Single(requestBody.Content); | ||
| Assert.Equal("application/json", content.Key); | ||
| Assert.NotNull(content.Value.Schema); | ||
| Assert.Equal("TodoSchema", content.Value.Schema.Reference.Id); | ||
| var schema = content.Value.Schema.GetEffective(document); | ||
| Assert.Equal("object", schema.Type); | ||
| Assert.Collection(schema.Properties, | ||
| property => | ||
| { | ||
| Assert.Equal("id", property.Key); | ||
| Assert.Equal("integer", property.Value.Type); | ||
| }, | ||
| property => | ||
| { | ||
| Assert.Equal("title", property.Key); | ||
| Assert.Equal("string", property.Value.Type); | ||
| }, | ||
| property => | ||
| { | ||
| Assert.Equal("completed", property.Key); | ||
| Assert.Equal("boolean", property.Value.Type); | ||
| }, | ||
| property => | ||
| { | ||
| Assert.Equal("createdAt", property.Key); | ||
| Assert.Equal("string", property.Value.Type); | ||
| Assert.Equal("date-time", property.Value.Format); | ||
| }); | ||
|
|
||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GeneratesInlineSchemaForPoco_WithCustomNullId() | ||
| { | ||
| // Arrange | ||
| var builder = CreateBuilder(); | ||
|
|
||
| // Act | ||
| builder.MapPost("/", (Todo todo) => { }); | ||
| var options = new OpenApiOptions { CreateSchemaReferenceId = (type) => type.Type.Name == "Todo" ? null : $"{type.Type.Name}Schema" }; | ||
|
|
||
| // Assert | ||
| await VerifyOpenApiDocument(builder, options, document => | ||
| { | ||
| var operation = document.Paths["/"].Operations[OperationType.Post]; | ||
| var requestBody = operation.RequestBody; | ||
|
|
||
| Assert.NotNull(requestBody); | ||
| var content = Assert.Single(requestBody.Content); | ||
| Assert.Equal("application/json", content.Key); | ||
| Assert.NotNull(content.Value.Schema); | ||
| // Assert that no reference was created and the schema is inlined | ||
| var schema = content.Value.Schema; | ||
| Assert.Null(schema.Reference); | ||
| Assert.Equal("object", schema.Type); | ||
| Assert.Collection(schema.Properties, | ||
| property => | ||
| { | ||
| Assert.Equal("id", property.Key); | ||
| Assert.Equal("integer", property.Value.Type); | ||
| }, | ||
| property => | ||
| { | ||
| Assert.Equal("title", property.Key); | ||
| Assert.Equal("string", property.Value.Type); | ||
| }, | ||
| property => | ||
| { | ||
| Assert.Equal("completed", property.Key); | ||
| Assert.Equal("boolean", property.Value.Type); | ||
| }, | ||
| property => | ||
| { | ||
| Assert.Equal("createdAt", property.Key); | ||
| Assert.Equal("string", property.Value.Type); | ||
| Assert.Equal("date-time", property.Value.Format); | ||
| }); | ||
|
|
||
| }); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CanCallDefaultImplementationFromCustomOne() | ||
| { | ||
| var builder = CreateBuilder(); | ||
|
|
||
| builder.MapPost("/", (Todo todo) => new TodoWithDueDate(todo.Id, todo.Title, todo.Completed, todo.CreatedAt, DateTime.UtcNow)); | ||
| var options = new OpenApiOptions | ||
| { | ||
| CreateSchemaReferenceId = (type) => | ||
| { | ||
| if (type.Type.Name == "Todo") | ||
| { | ||
| return null; | ||
| } | ||
| return OpenApiOptions.CreateDefaultSchemaReferenceId(type); | ||
| } | ||
| }; | ||
|
|
||
| await VerifyOpenApiDocument(builder, options, document => | ||
| { | ||
| var operation = document.Paths["/"].Operations[OperationType.Post]; | ||
| var requestBody = operation.RequestBody; | ||
| var response = operation.Responses["200"]; | ||
|
|
||
| // Assert that no reference was created for the Todo type | ||
| Assert.NotNull(requestBody); | ||
| var content = Assert.Single(requestBody.Content); | ||
| Assert.Equal("application/json", content.Key); | ||
| Assert.NotNull(content.Value.Schema); | ||
| var schema = content.Value.Schema; | ||
| Assert.Null(schema.Reference); | ||
|
|
||
| // Assert that a reference was created for the TodoWithDueDate type | ||
| Assert.NotNull(response); | ||
| var responseContent = Assert.Single(response.Content); | ||
| Assert.Equal("application/json", responseContent.Key); | ||
| Assert.NotNull(responseContent.Value.Schema); | ||
| var responseSchema = responseContent.Value.Schema; | ||
| Assert.NotNull(responseSchema.Reference); | ||
| Assert.Equal("TodoWithDueDate", responseSchema.Reference.Id); | ||
| }); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
_openApiOptions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_openApiOptionsis an instance field so we can't use it in the initializer for_confguration. I could switch to standard constructors instead of primary constructors and initialize everything there but.... 😅 🤪