-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for C# 11 'required' modifier. #2555
Comments
This did the trick for me: public class SwaggerRequiredMemberFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
var requiredProperties = context.Type.GetProperties().Where(p => p.HasAttribute<RequiredMemberAttribute>());
var schemaPropertyNames = schema.Properties.Keys.Where(
sp => requiredProperties.Any(rp => string.Equals(sp, rp.Name, StringComparison.InvariantCultureIgnoreCase))
);
foreach (var property in schemaPropertyNames)
{
schema.Required.Add(property);
}
}
} From a quick glance it looks like a proper implementation might get away with adding Line 15 in 73610b9
Edit: After a second look that is only for parameters, no idea how it works for properties. Edit 2: Original filter did not handle nullable required classes type properties well. This version handles that by not marking nullable ref types as required: public class SwaggerRequiredMemberFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
var nullabilityContext = new NullabilityInfoContext();
var properties = context.Type.GetProperties();
foreach (var key in schema.Properties.Keys)
{
// Keys that do not correspond to a property are ignored
var property = properties.SingleOrDefault(
p => string.Equals(p.Name, key, StringComparison.OrdinalIgnoreCase)
);
if (property == null)
continue;
// Do not mark if the property is not required
var required = property.HasAttribute<RequiredMemberAttribute>();
if (!required)
continue;
// Check nullability of non primitive types
var primitive = schema.Properties[key].Type != null;
if (!primitive)
{
// Do not mark nullable ref types.
// Ref types cannot be marked as nullable, so this would lead to them being non nullable.
var nullabilityInfo = nullabilityContext.Create(property);
if (nullabilityInfo.ReadState == NullabilityState.Nullable)
continue;
}
schema.Required.Add(key);
}
}
} |
I used the solution from @flostellbrink, but found one flaw. I found that not all properties where marked required and when I looked at the code I saw that this only works as long as the name of the property is the same as the json name, but if you use JsonPropertyName to use snakecase for the API while your C# property has PascalCase naming convention, this code does not work. So I changed the code slightly, basically do the foreach on the Properties of the class instead of Properties of the schema.
|
This isnt a C# 11 issue, it's a discrepancy between the ASP.NET validator treatment of non-nullable properties and swashbuckle's. |
Any movement on this issue? AFAICT this discrepancy still exists |
Would a PR to implement support for the C# 11 |
FYI this should work in DotSwashbuckle |
Is this related? I think the |
That has already been fixed for the next release: #2879 |
I think this can be closed as resolved? |
@martincostello this is a candidate to close |
Now that C# has inbuilt language support for required properties, it might be worth discussing how we can take advantage of this with SwashBuckle to provide more accurate, streamlined API documentation.
I think the below comment covers my thoughts on this well:
Since nullablility and required/not are different things where one does not necessarily imply the other, I think SwashBuckle should use the C# property nullability for swagger property nullable, and C# property required/not for the swagger property required.
The text was updated successfully, but these errors were encountered: