-
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
Feature Request: Provide more details on Type hierarchy when generation fails #2722
Comments
Did a little more research on this and found an easy fix for adding the additional context. Changed the private DataContract GetDataContractFor(Type modelType)
{
try
{
var effectiveType = Nullable.GetUnderlyingType(modelType) ?? modelType;
return _serializerDataContractResolver.GetDataContractForType(effectiveType);
}
catch (Exception ex)
{
throw new SwaggerGeneratorException(
message: $"Failed to generate data contract for type - {modelType}. See inner exception",
innerException: ex);
}
} This pointed me to a problem with how Swashbuckle is handling enums. Here's a simple Console program that documents the problem: using System.Text.Json;
using System.Text.Json.Serialization;
var options = new JsonSerializerOptions { TypeInfoResolver = MyJsonSerializerContext.Default };
// Serializing without JsonSerializerContext works
Console.WriteLine(JsonSerializer.Serialize(DrinkSize.Small));
// Serializing with JsonSerializerContext works
Console.WriteLine(JsonSerializer.Serialize(DrinkSize.Small, options));
// Swashbuckle uses this approach to get the first enum value
var enumValues = typeof(DrinkSize).GetEnumValues();
var firstValue = enumValues.GetValue(0);
// Using dynamic works
// dynamic firstValue = enumValues.GetValue(0);
// Serializing without JsonSerializerContext works
Console.WriteLine(JsonSerializer.Serialize(firstValue));
// Since firstValue is a System.Object instead of a DrinkSize, this fails
Console.WriteLine(JsonSerializer.Serialize(firstValue, options));
public enum DrinkSize
{
Small,
Medium,
Large
}
[JsonSerializable(typeof(DrinkSize))]
public partial class MyJsonSerializerContext : JsonSerializerContext
{
} The fix for this was even easier. Changed private string JsonConverterFunc(dynamic value)
{
return JsonSerializer.Serialize(value, _serializerOptions);
} I'll wrap both changes in a pull request and get it submitted. |
This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made. |
We've started using
System.Text.Json
source-code generation and it has basically killed our Swagger page. We're now getting the following from the Swashbuckle code that generates the JSON:As you can see, it's getting hung up because it thinks we're serializing a System.Object; we aren't. Unfortunately, the error message provided doesn't give us any clues about which object caused the exception.
Let's say we had classes like this:
Instead of this message:
It would be much more helpful to get a message showing the context of what the generator is working on. Maybe something like this:
The text was updated successfully, but these errors were encountered: