Skip to content

Commit 3bafc2a

Browse files
committed
Add applying annotations with [Description] attributes for types
1 parent 6cd0d41 commit 3bafc2a

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ internal sealed class OpenApiSchemaService(
101101
{
102102
schema.ApplyNullabilityContextInfo(jsonPropertyInfo);
103103
}
104+
if (context.TypeInfo.Type.GetCustomAttributes(inherit: false).OfType<DescriptionAttribute>().LastOrDefault() is DescriptionAttribute typeDescriptionAttribute)
105+
{
106+
schema[OpenApiSchemaKeywords.DescriptionKeyword] = typeDescriptionAttribute.Description;
107+
}
104108
if (context.PropertyInfo is { AttributeProvider: { } attributeProvider })
105109
{
106110
var isInlinedSchema = schema["x-schema-id"] is null;
@@ -128,7 +132,7 @@ internal sealed class OpenApiSchemaService(
128132
}
129133
}
130134

131-
return schema;
135+
return schema;
132136
}
133137
};
134138

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.Annotations.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ await VerifyOpenApiDocument(builder, document =>
5050
Assert.Null(reference.Reference.Description);
5151
});
5252

53-
var childSchema = document.Components.Schemas["DescribedChildDto"];
54-
// TODO: Handle descriptions on classes
55-
// Assert.Equal("Class: DescribedChildDto", "DescribedChildDto");
53+
var referencedSchema = document.Components.Schemas["DescribedChildDto"];
54+
Assert.Equal("Class: DescribedChildDto", referencedSchema.Description);
5655
});
5756

5857
}
@@ -118,8 +117,7 @@ await VerifyOpenApiDocument(builder, options, document =>
118117
{
119118
Assert.Equal("inlinedNoDescription", property.Key);
120119
var inlinedSchema = Assert.IsType<OpenApiSchema>(property.Value);
121-
// TODO: Handle descriptions on classes
122-
// Assert.Equal("Class: DescribedInlinedDto", inlinedSchema.Description);
120+
Assert.Equal("Class: DescribedInlinedDto", inlinedSchema.Description);
123121
});
124122
});
125123
}

0 commit comments

Comments
 (0)