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: 1 addition & 1 deletion src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ static bool ShouldIgnoreParameter(ApiParameterDescription parameter)
private static bool IsRequired(ApiParameterDescription parameter)
{
var hasRequiredAttribute = parameter.ParameterDescriptor is IParameterInfoParameterDescriptor parameterInfoDescriptor &&
parameterInfoDescriptor.ParameterInfo.GetCustomAttributes(inherit: true).Any(attr => attr is RequiredAttribute);
parameterInfoDescriptor.ParameterInfo.IsDefined(typeof(RequiredAttribute), inherit: true);
// Per the OpenAPI specification, parameters that are sourced from the path
// are always required, regardless of the requiredness status of the parameter.
return parameter.Source == BindingSource.Path || parameter.IsRequired || hasRequiredAttribute;
Expand Down
45 changes: 34 additions & 11 deletions src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,45 +107,68 @@ internal sealed class OpenApiSchemaService(
schema.ApplyNullabilityContextInfo(jsonPropertyInfo);
}
var underlyingType = Nullable.GetUnderlyingType(context.TypeInfo.Type) ?? context.TypeInfo.Type;
var typeAttributes = underlyingType.GetCustomAttributes(inherit: false);
if (typeAttributes.OfType<DescriptionAttribute>().LastOrDefault() is { } typeDescriptionAttribute)
// Only materialize the attributes we look for instead of every attribute declared on the type.
var typeDescriptionAttributes = underlyingType.GetCustomAttributes(typeof(DescriptionAttribute), inherit: false);
if (typeDescriptionAttributes.Length > 0)
{
schema[OpenApiSchemaKeywords.DescriptionKeyword] = typeDescriptionAttribute.Description;
schema[OpenApiSchemaKeywords.DescriptionKeyword] = ((DescriptionAttribute)typeDescriptionAttributes[^1]).Description;
}
if (typeAttributes.OfType<ObsoleteAttribute>().Any())
if (underlyingType.IsDefined(typeof(ObsoleteAttribute), inherit: false))
{
schema[OpenApiSchemaKeywords.DeprecatedKeyword] = true;
}
if (context.PropertyInfo is { AttributeProvider: { } attributeProvider })
{
var propertyAttributes = attributeProvider.GetCustomAttributes(inherit: false);
if (propertyAttributes.OfType<ValidationAttribute>() is { } validationAttributes)
var hasValidationAttribute = false;
var isObsolete = false;
DefaultValueAttribute? defaultValueAttribute = null;
DescriptionAttribute? descriptionAttribute = null;
foreach (var attribute in propertyAttributes)
{
schema.ApplyValidationAttributes(validationAttributes);
switch (attribute)
{
case ValidationAttribute:
hasValidationAttribute = true;
break;
case DefaultValueAttribute defaultValue:
defaultValueAttribute = defaultValue;
break;
case DescriptionAttribute description:
descriptionAttribute = description;
break;
case ObsoleteAttribute:
isObsolete = true;
break;
}
}
if (hasValidationAttribute)
{
schema.ApplyValidationAttributes(propertyAttributes.OfType<ValidationAttribute>());
}
if (propertyAttributes.OfType<DefaultValueAttribute>().LastOrDefault() is { } defaultValueAttribute)
if (defaultValueAttribute is not null)
{
schema.ApplyDefaultValue(defaultValueAttribute.Value, context.TypeInfo);
}
var isInlinedSchema = !schema.WillBeComponentized();
if (isInlinedSchema)
{
if (propertyAttributes.OfType<DescriptionAttribute>().LastOrDefault() is { } descriptionAttribute)
if (descriptionAttribute is not null)
{
schema[OpenApiSchemaKeywords.DescriptionKeyword] = descriptionAttribute.Description;
}
if (propertyAttributes.OfType<ObsoleteAttribute>().Any())
if (isObsolete)
{
schema[OpenApiSchemaKeywords.DeprecatedKeyword] = true;
}
}
else
{
if (propertyAttributes.OfType<DescriptionAttribute>().LastOrDefault() is { } descriptionAttribute)
if (descriptionAttribute is not null)
{
schema[OpenApiConstants.RefDescriptionAnnotation] = descriptionAttribute.Description;
}
if (propertyAttributes.OfType<ObsoleteAttribute>().Any())
if (isObsolete)
{
schema[OpenApiConstants.RefDeprecatedAnnotation] = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ public class DescribedInlinedDto
public string ChildValue { get; set; }
}

[Fact]
public async Task SchemaDescriptions_HandlesDerivedDescriptionAttributes()
{
var builder = CreateBuilder();

builder.MapPost("/", (DerivedDescriptionDto dto) => { });

await VerifyOpenApiDocument(builder, document =>
{
var schema = document.Components.Schemas["DerivedDescriptionDto"];
Assert.Equal("Derived class description", schema.Description);
var property = Assert.Single(schema.Properties);
Assert.Equal("value", property.Key);
Assert.Equal("Derived property description", property.Value.Description);
});
}

private sealed class DerivedDescriptionAttribute(string description) : DescriptionAttribute(description);

[DerivedDescription("Derived class description")]
public class DerivedDescriptionDto
{
[DerivedDescription("Derived property description")]
public string Value { get; set; }
}

[Fact]
public async Task SchemaDeprecated_HandlesObsoleteType()
{
Expand Down
Loading