Open toreamun opened 1 year ago
Hello Same for me any workaround?
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 type
passed in?
I have not tried rolling this back to package release System.Text.Json
6.x yet, using 7.0.3. Have not tried the RC2 of v8 yet.
In short - using the System.Test.Json Serialization Source Gen, this will cause the throw.
As a work around -
[JsonSerializable(typeof(object))]
on top of your JsonSerializerContext
will get you past it.
For the fix:
On this line:
Have it call a new function with the type passed in
private string JsonConverterFunc(object value, Type t, JsonSerializerOptions options)
{
return JsonSerializer.Serialize(value, t, options);
}
In case this helps anyone else, in my case for some reason the JsonSerializerDataContractResolver
that was getting used by Swashbuckle to generate the swagger.json file was defaulting to a brand new JsonSerializerOptions, rather than correctly fetching the JsonSerializerOptions I'd already configured via a call to ConfigureHttpJsonOptions
during startup.
The workaround for me, so far working, has been to explicitly add ISerializerDataContractResolver
to the DI container before AddSwaggerGen
is called so that Swashbuckle is using the same JsonSerializerContext
as the rest of my application which is correctly configured with all the enums and other types I use in my API - though I'm not sure why it wasn't in the first place (possibly bad code our side):
services.AddTransient<ISerializerDataContractResolver>(sp =>
{
var opts = sp.GetRequiredService<IOptions<JsonOptions>>().Value?.SerializerOptions
?? new JsonSerializerOptions(JsonSerializerDefaults.Web);
return new JsonSerializerDataContractResolver(opts);
});
This works in DotSwashbuckle, tested in 3.0.9
Thanks for the hints here in the thread.
JsonSerializerDataContractResolver
seems to me to have fundamental issues with AoT compatibility with regards to be supported for all possible scenarios (or at least would need to be heavily annotated). It may however work for various simple use cases.
I've applied the suggestion from https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2593#issuecomment-1799837881 to #2800.
The reason https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2593#issuecomment-1836432544 doesn't work out of the box is because Swashbuckle hadn't been taught to understand the other JsonOptions
class that is specific to Minimal APIs (as opposed to MVC). While explicit configuration is the way to go to explicitly configure which options you want to use, I've updated the code in #2799 to fall through to the options for Minimal APIs if not resolvable from MVC.
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! In my case I just put the JsonSerializable WeatherType in the context. This isn't needed for Request/Response Bodys, but it's needed for FromPath/FromQuery/FromForm properties
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