-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix OpenApiJsonSchema array parsing (#62051) #62118
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -722,4 +722,93 @@ | |||||
| return true; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Regression test for https://github.com/dotnet/aspnetcore/issues/62023 | ||||||
| // Testing that the array parsing in our OpenApiJsonSchema works | ||||||
| [Fact] | ||||||
| public async Task CustomConverterThatOutputsArrayWithDefaultValue() | ||||||
| { | ||||||
| // Arrange | ||||||
| var serviceCollection = new ServiceCollection(); | ||||||
| serviceCollection.ConfigureHttpJsonOptions(options => | ||||||
| { | ||||||
| options.SerializerOptions.Converters.Add(new EnumArrayTypeConverter()); | ||||||
| }); | ||||||
| var builder = CreateBuilder(serviceCollection); | ||||||
|
|
||||||
| // Act | ||||||
| builder.MapPost("/api", (EnumArrayType e = EnumArrayType.None) => { }); | ||||||
|
|
||||||
| // Assert | ||||||
| await VerifyOpenApiDocument(builder, document => | ||||||
| { | ||||||
| var operation = document.Paths["/api"].Operations[HttpMethod.Post]; | ||||||
|
Check failure on line 745 in src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ParameterSchemas.cs
|
||||||
| var param = Assert.Single(operation.Parameters); | ||||||
| Assert.NotNull(param.Schema); | ||||||
| Assert.IsType<JsonArray>(param.Schema.Default); | ||||||
|
Check failure on line 748 in src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ParameterSchemas.cs
|
||||||
| // Type is null, it's up to the user to configure this via a custom schema | ||||||
| // transformer for types with a converter. | ||||||
| Assert.Null(param.Schema.Type); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| public async Task CustomConverterThatOutputsObjectWithDefaultValue() | ||||||
| { | ||||||
| // Arrange | ||||||
| var serviceCollection = new ServiceCollection(); | ||||||
| serviceCollection.ConfigureHttpJsonOptions(options => | ||||||
| { | ||||||
| options.SerializerOptions.Converters.Add(new EnumObjectTypeConverter()); | ||||||
| }); | ||||||
| var builder = CreateBuilder(serviceCollection); | ||||||
|
|
||||||
| // Act | ||||||
| builder.MapPost("/api", (EnumArrayType e = EnumArrayType.None) => { }); | ||||||
|
|
||||||
| // Assert | ||||||
| await VerifyOpenApiDocument(builder, document => | ||||||
| { | ||||||
| var operation = document.Paths["/api"].Operations[HttpMethod.Post]; | ||||||
|
Check failure on line 772 in src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ParameterSchemas.cs
|
||||||
| var param = Assert.Single(operation.Parameters); | ||||||
| Assert.NotNull(param.Schema); | ||||||
| Assert.IsType<JsonObject>(param.Schema.Default); | ||||||
|
Check failure on line 775 in src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ParameterSchemas.cs
|
||||||
| // Type is null, it's up to the user to configure this via a custom schema | ||||||
| // transformer for types with a converter. | ||||||
| Assert.Null(param.Schema.Type); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| public enum EnumArrayType | ||||||
| { | ||||||
| None = 1 | ||||||
| } | ||||||
|
|
||||||
| public class EnumArrayTypeConverter : JsonConverter<EnumArrayType> | ||||||
| { | ||||||
| public override EnumArrayType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||||||
| { | ||||||
| return new EnumArrayType(); | ||||||
| } | ||||||
|
|
||||||
| public override void Write(Utf8JsonWriter writer, EnumArrayType value, JsonSerializerOptions options) | ||||||
| { | ||||||
| writer.WriteStartArray(); | ||||||
| writer.WriteEndArray(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public class EnumObjectTypeConverter : JsonConverter<EnumArrayType> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
| public override EnumArrayType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||||||
| { | ||||||
| return new EnumArrayType(); | ||||||
| } | ||||||
|
|
||||||
| public override void Write(Utf8JsonWriter writer, EnumArrayType value, JsonSerializerOptions options) | ||||||
| { | ||||||
| writer.WriteStartObject(); | ||||||
| writer.WriteEndObject(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
The
EnumArrayTypeConverterandEnumObjectTypeConverterclasses has been used only in the tests. Can we not make those classes asprivate?