-
Notifications
You must be signed in to change notification settings - Fork 10.4k
OpenApi generates the same schema for generic types with a JsonConverterFactory. #59172
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
Comments
After some more investigation, I think this is caused by dotnet/runtime#110241 |
After even more investigation, the cause of this is probably fixed in dotnet/runtime#109868. I am fine if someone wants to close this, otherwise I will wait for the next servicing release to test this. |
@desjoerd Thanks for filing this issue and taking the time to investigate it yourself. I'll keep this open so that we can follow up once the next servicing release comes out. The reference PR does seem to solve the issue but it would be good to verify that there isn't a confounding issue in Microsoft.AspNetCore.OpenApi that is also causing the problem. In the meantime, sticking this in the backlog until we verify. |
@captainsafia I just checked with .NET Runtime 9.0.1 and I now get the correct output, a {
"openapi": "3.0.1",
"info": {
"title": "MinimalApi | v1",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"tags": [
"MinimalApi"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ExampleModel"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ExampleModel": {
"type": "object",
"properties": {
"guidValue": {
"$ref": "#/components/schemas/GenericValueOfGuid"
},
"stringValue": {
"$ref": "#/components/schemas/GenericValueOfstring"
}
}
},
"GenericValueOfGuid": { },
"GenericValueOfstring": { }
}
},
"tags": [
{
"name": "MinimalApi"
}
]
} |
There are still issues when using public readonly struct OptionalValue<T>
{
public T Value { get; init; }
public bool IsSet { get; init; }
public OptionalValue()
{
Value = default!;
IsSet = false;
}
public OptionalValue(T value)
{
Value = value;
IsSet = true;
}
} And here is an example of a model using this type: public record AccountPatchRequest
{
public OptionalValue<string> FirstName { get; init; }
public OptionalValue<string> LastName { get; init; }
public OptionalValue<Language?> PreferredLanguage { get; init; }
} Without the JsonConverterFactory, I get the correct schema: As soon as I add the JsonConverterFactory to the JsonOptions in my minimal API, the generated schema for the |
@Cyberzim you will have to do some unwrapping. I've done that for you with .NET 10 because it has the GetOrCreateSchema method: https://github.com/desjoerd/OptionalValues/blob/feature/net10/examples/OptionalValues.Examples.OpenApi/Program.cs It's the same for NSwag or Swashbuckle, which I've implemented and published. For .NET 10 I can publish a preview as well if you want. Edit: |
Thank you for sharing this. Unfortunately switching to .NET 10 is not an option for now until it is released. For now, the workaround for me is to just exclude the JsonConverterFactory when generating the OpenAPI specs. I should also note that I have a |
Is there an existing issue for this?
Describe the bug
When generating openapi containing Generic types, with a JsonConverterFactory, the same schema is generated for different types.
Given the following minimal example:
I am getting the following openapi:
There is only one schema generated for the GenericValue, namely
GenericValueOfstring
(which is the last defined type). I was expectingGenericValueOfstring
andGenericValueOfGuid
.Expected Behavior
No response
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
9.0.100
Anything else?
No response
The text was updated successfully, but these errors were encountered: