-
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
[Bug]: All enum values show up as "null" when using JSON Source Generation #3217
Comments
The nulls come from this code: Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/OpenApiAnyFactory.cs Lines 8 to 19 in 2fd6b39
The There's no workaround for this - we need to do some non-trivial refactoring to get this passed through for System.Text.Json while still making sense for the abstractions associated with Newtonsoft.Json. The problem is that we convert the enum to a JSON string using the relevant settings, but we then convert it back to a Getting this to work properly in all cases doesn't look like a trivial piece of work, so I don't think there's a quick fix for this. |
Actually, you should be able to use a Schema Filter to post-process the schema to fill in the values yourself:
Obviously that isn't ideal, but it should work until we can make this work. A short-term solution would be to add the ability to pass internal class EnumSchemaFilter(IOptions<JsonOptions> options) : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type.IsEnum)
{
schema.Enum = context.Type.GetEnumValues()
.Cast<object>()
.Select(ToJson)
.Distinct()
.Select(FromJson)
.ToList();
}
string ToJson(object value)
=> JsonSerializer.Serialize(value, options.Value.JsonSerializerOptions);
IOpenApiAny FromJson(string value)
=> OpenApiAnyFactory.CreateFromJson(value, options.Value.JsonSerializerOptions);
}
} |
- Add overload to `OpenApiAnyFactory.CreateFromJson()` that supports passing in a `JsonSerializerOptions` as a workaround for domaindrivendev#3217. - Use concrete types as suggested by analyzers. - Move unshipped APIs to shipped. - Bump version to 7.3.0.
Thank you for the workaround, it seems to work fine for now. For anyone stumbling across this, you also need to add [JsonSerializable(typeof(JsonElement))] to the JsonSerializerContext for the SchemaFilter to work |
- Add overload to `OpenApiAnyFactory.CreateFromJson()` that supports passing in a `JsonSerializerOptions` as a workaround for #3217. - Use concrete types as suggested by analyzers. - Move unshipped APIs to shipped. - Bump version to 7.3.0.
Describe the bug
When using this setting in the .csproj file:
All enum values are null
Expected behavior
Enum values are serialized to integers by default (or strings when using the JSON string enum converter)
Actual behavior
All enum values are null:
UI: "Available values : null, null, null"
swagger.json:
Steps to reproduce
Exception(s) (if any)
No response
Swashbuckle.AspNetCore version
7.2.0
.NET Version
8.0.401
Anything else?
No response
The text was updated successfully, but these errors were encountered: