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]: Exception on start after upgrade to latest #2885

Closed
LucaCris opened this issue May 15, 2024 · 10 comments · Fixed by #2895
Closed

[Bug]: Exception on start after upgrade to latest #2885

LucaCris opened this issue May 15, 2024 · 10 comments · Fixed by #2895
Assignees
Labels
Milestone

Comments

@LucaCris
Copy link

Describe the bug

Rollbacked to 6.5.0 due to non-sense exception:

[09:41:23 ERR] An unhandled exception has occurred while executing the request.
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException: Failed to generate schema for type - EagleChartModel.EChart. See inner exception
 ---> System.ArgumentException: The specified type System.Double 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__1(Object value)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchemaForMember(Type modelType, SchemaRepository schemaRepository, MemberInfo memberInfo, DataProperty dataProperty)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.CreateObjectSchema(DataContract dataContract, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.<>c__DisplayClass11_0.<GenerateConcreteSchema>b__3()
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateReferencedSchema(DataContract dataContract, SchemaRepository schemaRepository, Func`1 definitionFactory)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateConcreteSchema(DataContract dataContract, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchemaForType(Type modelType, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchema(Type modelType, SchemaRepository schemaRepository, MemberInfo memberInfo, ParameterInfo parameterInfo, ApiParameterRouteInfo routeInfo)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.CreateArraySchema(DataContract dataContract, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.<>c__DisplayClass11_0.<GenerateConcreteSchema>b__1()
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateConcreteSchema(DataContract dataContract, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateSchemaForMember(Type modelType, SchemaRepository schemaRepository, MemberInfo memberInfo, DataProperty dataProperty)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.CreateObjectSchema(DataContract dataContract, SchemaRepository schemaRepository)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.<>c__DisplayClass11_0.<GenerateConcreteSchema>b__3()
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateReferencedSchema(DataContract dataContract, SchemaRepository schemaRepository, Func`1 definitionFactory)
   at Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator.GenerateConcreteSchema(DataContract dataContract, SchemaRepository schemaRepository)
   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.GenerateOpenApiOperationFromMetadata(ApiDescription apiDescription, SchemaRepository schemaRepository)
   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.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
[09:41:23 INF] Request finished HTTP/1.1 GET http://localhost:5500/swagger/v1/swagger.json - 500 null text/plain; charset=utf-8 84.4112ms

Expected behavior

As in 6.5.0 the standard swagger page.

Actual behavior

No response

Steps to reproduce

No response

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

6.6.1

.NET Version

net 8.0.5

Anything else?

No response

@LucaCris LucaCris added the bug label May 15, 2024
@martincostello
Copy link
Collaborator

Please provide a minimal reproducible example as a GitHub repository that demonstrates the issue you're experiencing so we can look into this further.

@martincostello martincostello added the needs-repro A minimal reproducible example is needed label May 15, 2024
@IvanMaskaliou
Copy link

IvanMaskaliou commented May 15, 2024

I have the same issue/exception. But I think I found how to workaround it - I have a property defined like:

[DataMember]
[Required]
[DefaultValue(1)]
public double Factor { get; set; } = 1;

and I have that exception in 6.6.1 (not in 6.5.0).
But if I remove DefaultValue attribute or set it as [DefaultValue(1D)] (explicit double instead of int) it is working fine.

@martincostello
Copy link
Collaborator

Interesting - what version of .NET are you using?

I wonder if this is something to do with changes in .NET 8 that Swashbuckle is now consuming because we have a net8.0 target now.

@IvanMaskaliou
Copy link

.NET 8.0.5

@martincostello
Copy link
Collaborator

Is it possible you could provide a stack trace that includes our line numbers when the exception happens?

I think the changes for #2800 is what's making this issue appear, I'm just having trouble reconciling exactly which code path it's going down when it hits that.

I'll try debugging things with the sample you've given me anyway as that will probably point to what's going wrong.

@martincostello
Copy link
Collaborator

I've gotten a repro for this based on #2885 (comment) now, thanks.

martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue May 15, 2024
Fix SwaggerGeneratorException if the type of a `[DefaultValue]` does not match the type of the property when using System.Text.Json for serialization.
Resolves domaindrivendev#2885.
@martincostello martincostello removed the needs-repro A minimal reproducible example is needed label May 15, 2024
@martincostello martincostello added this to the v6.6.2 milestone May 15, 2024
@LucaCris
Copy link
Author

I also have the same scheme:

    [XmlAttribute]
    [DefaultValue(0)]
    public double ParamVal1 { get; set; }

Not yet tested the workaround above.

@martincostello martincostello self-assigned this May 16, 2024
martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue May 16, 2024
Fix SwaggerGeneratorException if the type of a `[DefaultValue]` does not match the type of the property when using System.Text.Json for serialization.
Resolves domaindrivendev#2885.
martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue May 19, 2024
Fix SwaggerGeneratorException if the type of a `[DefaultValue]` does not match the type of the property when using System.Text.Json for serialization.
Resolves domaindrivendev#2885.
@kwakuduahc1
Copy link

Cost me hours. Happy for the fix

martincostello added a commit that referenced this issue May 21, 2024
- Fix SwaggerGeneratorException if the type of a `[DefaultValue]` does not match the type of the property when using System.Text.Json for serialization to resolve #2885.
- Fix schema generation of default values for nullable enums with System.Text.Json to resolve #2904.
- Resolve some IDE refactoring suggestions.
- Render the response if an integration test fails.
@martincostello
Copy link
Collaborator

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

@LucaCris
Copy link
Author

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

Just tried, it works! Thanks...

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.

4 participants