Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/OpenApi/src/Schemas/OpenApiJsonSchema.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ internal sealed partial class OpenApiJsonSchema
{
type = JsonSchemaType.Array;
var array = new JsonArray();
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,52 @@ 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);
});
}

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();
}
}
}
Loading