Skip to content
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]: SwaggerGeneratorException (Failed to generate schema for type - System.Nullable`1[{enum_type}]) when using UseAllOfToExtendedReferenceSchemas #2904

Closed
marcominerva opened this issue May 16, 2024 · 3 comments · Fixed by #2895
Assignees
Labels
Milestone

Comments

@marcominerva
Copy link

Describe the bug

If I use the UseAllOfToExtendedReferenceSchemas extension method:

builder.Services.AddSwaggerGen(options =>
{
    options.UseAllOfToExtendReferenceSchemas();
});

And then I use a nullable enum type:

[ApiController]
[Route("api/[controller]")]
public class EnumController : ControllerBase
{
    [HttpGet]
    public IActionResult Get(LogLevel? logLevel = LogLevel.Error) => Ok(new { logLevel });
}

public enum LogLevel
{
    Verbose,
    Debug,
    Information,
    Warning,
    Error,
    Fatal
}

When I run the application, I get the following exception:

SwaggerGeneratorException: Failed to generate schema for type - System.Nullable1[LogLevel]`

With this inner exception:

ArgumentException: The specified type LogLevel must derive from the specific value's type System.Int32.

Expected behavior

Swashbuckle.AspNetCore should not throw any exception.

Actual behavior

No response

Steps to reproduce

Minimal repro: https://github.com/marcominerva/NullableEnumIssue

Exception(s) (if any)

Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Failed to generate Operation for action - EnumController.Get (NullableEnumIssue). See inner exception
 ---> Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Failed to generate schema for type - System.Nullable`1[LogLevel]. See inner exception
 ---> System.ArgumentException: The specified type LogLevel must derive from the specific value's type System.Int32.
   at System.Text.Json.ThrowHelper.ThrowArgumentException_DeserializeWrongType(Type type, Object value)
   at System.Text.Json.JsonSerializer.ValidateInputType(Object value, Type inputType)
   at System.Text.Json.JsonSerializer.Serialize(Object value, Type inputType, JsonSerializerOptions options)
   at Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver.JsonConverterFunc(Object value, Type type)
   at Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver.<>c__DisplayClass2_0.<GetDataContractForType>b__4(Object value)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchemaForParameter(Type modelType, SchemaRepository schemaRepository, ParameterInfo parameterInfo, ApiParameterRouteInfo routeInfo)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchema(Type modelType, SchemaRepository schemaRepository, MemberInfo memberInfo, ParameterInfo parameterInfo, ApiParameterRouteInfo routeInfo)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository, PropertyInfo propertyInfo, ParameterInfo parameterInfo, ApiParameterRouteInfo routeInfo)
   --- End of inner exception stack trace ---
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateSchema(Type type, SchemaRepository schemaRepository, PropertyInfo propertyInfo, ParameterInfo parameterInfo, ApiParameterRouteInfo routeInfo)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateParameter(ApiParameterDescription apiParameter, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.<>c__DisplayClass16_0.<GenerateParameters>b__1(ApiParameterDescription apiParam)
   at System.Linq.Enumerable.WhereSelectListIterator`2.ToList()
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateParameters(ApiDescription apiDescription, SchemaRepository schemaRespository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
   --- End of inner exception stack trace ---
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperation(ApiDescription apiDescription, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GeneratePaths(IEnumerable`1 apiDescriptions, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerDocumentWithoutFilters(String documentName, String host, String basePath)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwaggerAsync(String documentName, String host, String basePath)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Swashbuckle.AspNetCore version

6.6.1

.NET Version

8.0.5

Anything else?

No response

@martincostello
Copy link
Collaborator

Looks similar to #2885, but I think it's a different case but the same root cause.

@martincostello martincostello self-assigned this May 16, 2024
martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue May 16, 2024
- Fix schema generation of default values for nullable enums with System.Text.Json.
- Resolve some IDE refactoring suggestions.
- Render the response if an integration test fails.
Resolves domaindrivendev#2904.
@martincostello martincostello added this to the v6.6.2 milestone May 16, 2024
@martincostello
Copy link
Collaborator

Thanks for the report, I've pushed the fix into the existing PR to fix the other issue.

martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue May 19, 2024
- Fix schema generation of default values for nullable enums with System.Text.Json.
- Resolve some IDE refactoring suggestions.
- Render the response if an integration test fails.
Resolves domaindrivendev#2904.
@martincostello
Copy link
Collaborator

Thanks for reporting this issue - the fix is available in Swashbuckle.AspNetCore 6.6.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants