-
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
SupportNonNullableReferenceTypes not working for nested record types #2758
Comments
I just encountered this issue as well. |
I can confirm this. Took me a while to find this issue. This is the really hacky patch I came up as a solution while this gets addressed: public sealed class NullableSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (schema.Properties.Count == 0)
return;
var nullabilityInfoContext = new NullabilityInfoContext();
var contextProperties = context.Type.GetProperties();
foreach (var (name, property) in schema.Properties)
{
var contextProperty = contextProperties.FirstOrDefault(x => string.Equals(name, x.Name, StringComparison.OrdinalIgnoreCase));
if (contextProperty is null)
continue;
var nullabilityInfo = nullabilityInfoContext.Create(contextProperty);
// If nullability is unknown or ambiguous, we continue.
if (nullabilityInfo is { ReadState: NullabilityState.Unknown, WriteState: NullabilityState.Unknown } || nullabilityInfo.ReadState != nullabilityInfo.WriteState)
continue;
var detectedNullability = property.Nullable;
var reflectedNullability = nullabilityInfo.ReadState == NullabilityState.Nullable;
if (detectedNullability != reflectedNullability)
property.Nullable = reflectedNullability;
}
}
} |
To make issue tracking a bit less overwhelming for the new maintainers (see #2778), I've created a new tracking issue to roll-up various nullability issues here: #2793. We'll refer back to this issue from there and include it as part of resolving that issue, but I'm going to close this one to help prune the backlog. |
Using Swashbuckle.AspNetCore 6.5.0
Given the following:
The schema for
Child
correctly infers nullabilityHowever the schema for
NestedChild
incorrectly marksnonNullable
asnullable
If I move these records outside of the
Request
class then nullability is inferred correctly for bothSchema
The text was updated successfully, but these errors were encountered: