Skip to content
Merged
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 @@ -136,6 +136,8 @@ internal sealed partial class OpenApiJsonSchema
{
type = "array";
var array = new OpenApiArray();
// Read to process JsonTokenType.StartArray before advancing
reader.Read();
while (reader.TokenType != JsonTokenType.EndArray)
{
array.Add(ReadOpenApiAny(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[OperationType.Post];
var param = Assert.Single(operation.Parameters);
Assert.NotNull(param.Schema);
Assert.IsType<OpenApiArray>(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[OperationType.Post];
var param = Assert.Single(operation.Parameters);
Assert.NotNull(param.Schema);
Assert.IsType<OpenApiObject>(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>
Copy link

@Arnab-Developer Arnab-Developer Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EnumArrayTypeConverter and EnumObjectTypeConverter classes has been used only in the tests. Can we not make those classes as private?

Suggested change
public class EnumArrayTypeConverter : JsonConverter<EnumArrayType>
private 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>
Copy link

@Arnab-Developer Arnab-Developer Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class EnumObjectTypeConverter : JsonConverter<EnumArrayType>
private 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