-
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
Nullability gets ignored for return types and dictionary values #2680
Comments
@kpko, you can add it to swaggergen options. Hope can help you
|
Hi, I tried enabling this option but this results in the same schema. builder.Services.AddSwaggerGen(c =>
{
c.SupportNonNullableReferenceTypes();
c.UseAllOfToExtendReferenceSchemas();
}); It all seems to work if I try the same thing as in my first example in a model object. // With model object
app.MapPost("model-object-nullable-test", (Test parameters) => "hello")
.Produces<Test>();
public class Test
{
public Dictionary<string, object> NonNullableObjectDict { get; set; } = new();
public Dictionary<string, object?> NullableObjectDict { get; set; } = new();
public string NonNullableString { get; set; } = string.Empty;
public string? NullableString { get; set; }
} This results in the expected schema. {
"openapi": "3.0.1",
"info": {
"title": "WebApplication3",
"version": "1.0"
},
"paths": {
"/model-object-nullable-test": {
"post": {
"tags": [
"WebApplication3"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/Test"
}
]
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Test"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Test": {
"type": "object",
"properties": {
"nonNullableObjectDict": {
"type": "object",
"additionalProperties": { }
},
"nullableObjectDict": {
"type": "object",
"additionalProperties": {
"nullable": true
}
},
"nonNullableString": {
"type": "string"
},
"nullableString": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
}
}
} But somehow, once I use the types like |
This might be fixed in DotSwashbuckle, can you test using it please. |
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. |
I have two API methods like this in a fresh ASP.NET Core Web API project using .NET 7 and Swashbuckle.AspNetCore v6.5.0
Nullable is enabled in my project, but nullability settings seem to get ignored when the OpenAPI document is generated. Both definitions are exactly the same.
Is there anything I must enable to support nullable reference types in return types and parameters? It seems to work in models once I wrap the return/request type in classes.
Thanks!
The text was updated successfully, but these errors were encountered: