Skip to content
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: 1 addition & 1 deletion src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal sealed class OpenApiSchemaService(
{
schema.ApplyNullabilityContextInfo(jsonPropertyInfo);
}
if (context.TypeInfo.Type.GetCustomAttributes(inherit: false).OfType<DescriptionAttribute>().LastOrDefault() is { } typeDescriptionAttribute)
if ((context.TypeInfo.ElementType ?? context.TypeInfo.Type).GetCustomAttributes(inherit: false).OfType<DescriptionAttribute>().LastOrDefault() is { } typeDescriptionAttribute)
{
schema[OpenApiSchemaKeywords.DescriptionKeyword] = typeDescriptionAttribute.Description;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ await VerifyOpenApiDocument(builder, document =>
Assert.Equal("Proposal", ((OpenApiSchemaReference)schema).Reference.Id);
var effectiveSchema = schema;
Assert.Collection(effectiveSchema.Properties,
property => {
property =>
{
Assert.Equal("proposalElement", property.Key);
Assert.Equal("Proposal", ((OpenApiSchemaReference)property.Value).Reference.Id);
},
property => {
property =>
{
Assert.Equal("stream", property.Key);
var targetSchema = property.Value;
Assert.Equal(JsonSchemaType.String | JsonSchemaType.Null, targetSchema.Type);
Expand Down Expand Up @@ -395,7 +397,15 @@ await VerifyOpenApiDocument(builder, document =>
Assert.Equal(JsonSchemaType.String, property.Value.Type);
Assert.Equal("date-time", property.Value.Format);
Assert.Equal("The date and time the todo item was created.", property.Value.Description);
},
property =>
{
var reference = Assert.IsType<OpenApiSchemaReference>(property.Value);
Assert.Equal("sampleEnum", property.Key);
Assert.Equal("The sample enum property.", reference.Description);
});
var sampleEnumSchema = document.Components.Schemas["SampleEnum"];
Assert.Equal("Enum: SampleEnum", sampleEnumSchema.Description);
});
}

Expand Down Expand Up @@ -735,6 +745,9 @@ private class DescriptionTodo

[Description("The date and time the todo item was created.")]
public DateTime CreatedAt { get; set; }

[Description("The sample enum property.")]
public SampleEnum SampleEnum { get; set; }
}

#nullable enable
Expand Down Expand Up @@ -999,4 +1012,36 @@ internal Status FormPostWithOptionalEnumParam(
[FromForm(Name = "status")] Status status = Status.Approved
) => status;
}

[Fact]
public async Task GetRequestBody_HandleNullableEnumDescription()
{
// Arrange
var builder = CreateBuilder();

// Act
builder.MapPost("/", (NullableEnumModel model) => { });

// Assert
await VerifyOpenApiDocument(builder, document =>
{
var paths = Assert.Single(document.Paths.Values);
var operation = paths.Operations[HttpMethod.Post];

var modelSchema = document.Components.Schemas["SampleEnum"];
Assert.Equal("Enum: SampleEnum", modelSchema.Description);
});
}

private class NullableEnumModel
{
public SampleEnum? EnumProperty { get; set; }
}

[Description("Enum: SampleEnum")]
public enum SampleEnum
{
FirstValue,
SecondValue
}
}
Loading