Skip to content

Fix OpenApiJsonSchema array parsing #62051

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/OpenApi/src/Schemas/OpenApiJsonSchema.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ internal sealed partial class OpenApiJsonSchema
{
type = JsonSchemaType.Array;
var array = new JsonArray();
// Read to process JsonTokenType.StartArray before advancing
reader.Read();
while (reader.TokenType != JsonTokenType.EndArray)
{
array.Add(ReadJsonNode(ref reader));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,4 +722,93 @@ public static bool TryParse(string value, out Student result)
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];
var param = Assert.Single(operation.Parameters);
Assert.NotNull(param.Schema);
Assert.IsType<JsonArray>(param.Schema.Default);
// 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];
var param = Assert.Single(operation.Parameters);
Assert.NotNull(param.Schema);
Assert.IsType<JsonObject>(param.Schema.Default);
// 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>
{
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();
}
}
}
Loading