-
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
Exception when using System.Text.Json version 7 with System.Text.Json source generator and enums #2593
Comments
Hello |
Same issue also. Getting ready for net8, and moving to the source generation. the type is an enum, Stack Trace shows SwaggerGen/Swashbuckle has the type: I think the fault falls here though. JsonConverterFunc takes the first value of the enum, and it becomes an object (boxed?). Shouldn't it be the I have not tried rolling this back to package release In short - using the System.Test.Json Serialization Source Gen, this will cause the throw. |
As a work around -
For the fix: On this line: Have it call a new function with the type passed in
|
In case this helps anyone else, in my case for some reason the The workaround for me, so far working, has been to explicitly add
|
This works in DotSwashbuckle, tested in 3.0.9 |
Pass the `Type` value to `JsonSerializer` to give a hint as to how to serialize the value. See domaindrivendev#2593 (comment).
Use JsonOptions for Minimal APIs in .NET 8+ if not available from MVC. Use `JsonSerializerOptions.Default` where available. See domaindrivendev#2593 (comment).
Thanks for the hints here in the thread.
I've applied the suggestion from #2593 (comment) to #2800. The reason #2593 (comment) doesn't work out of the box is because Swashbuckle hadn't been taught to understand the other |
Use JsonOptions for Minimal APIs in .NET 8+ if not available from MVC. Use `JsonSerializerOptions.Default` where available. See domaindrivendev#2593 (comment).
Pass the `Type` value to `JsonSerializer` to give a hint as to how to serialize the value. See domaindrivendev#2593 (comment).
Pass the `Type` value to `JsonSerializer` to give a hint as to how to serialize the value. See domaindrivendev#2593 (comment).
Use JsonOptions for Minimal APIs in .NET 8+ if not available from MVC. Use `JsonSerializerOptions.Default` where available. See domaindrivendev#2593 (comment).
Pass the `Type` value to `JsonSerializer` to give a hint as to how to serialize the value. See domaindrivendev#2593 (comment).
Use JsonOptions for Minimal APIs in .NET 8+ if not available from MVC. Use `JsonSerializerOptions.Default` where available. See domaindrivendev#2593 (comment).
Pass the `Type` value to `JsonSerializer` to give a hint as to how to serialize the value. See domaindrivendev#2593 (comment).
Use JsonOptions for Minimal APIs in .NET 8+ if not available from MVC. Use `JsonSerializerOptions.Default` where available. See domaindrivendev#2593 (comment).
Pass the `Type` value to `JsonSerializer` to give a hint as to how to serialize the value. See domaindrivendev#2593 (comment).
Pass the `Type` value to `JsonSerializer` to give a hint as to how to serialize the value. See domaindrivendev#2593 (comment).
Hello, My workaround, as suggested by @Pablissimo, is to add the following line: services.AddTransient<ISerializerDataContractResolver>(sp =>
{
var opts = sp.GetRequiredService<IOptions<JsonOptions>>().Value?.SerializerOptions
?? new JsonSerializerOptions(JsonSerializerDefaults.Web);
return new JsonSerializerDataContractResolver(opts);
}); This resolves the error, but the enum example is still showing null. To solve this part, I've created the following class: public class EnumSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type.IsEnum)
{
schema.Enum.Clear();
foreach (var name in Enum.GetNames(context.Type))
{
schema.Enum.Add(new OpenApiString(name));
}
}
}
} And I configured it here: builder.Services.AddSwaggerGen(c =>
{
c.SchemaFilter<EnumSchemaFilter>();
}); |
Hi! |
I am using Swashbuckle.AspNetCore version 6.5.0. I have created a class inherited from JsonSerializerContext to use System.Text.Json source generator, and added the context at startup:
The ApiJsonContext looks like this:
I have a controller that returns a simple object with an enum:
This works great when using System.Text.Json version 6, but breaks with this error if I upgrade to System.Text.Json version 7:
This error occurs only if using enum. No error if I change to string:
Example source code
The text was updated successfully, but these errors were encountered: