-
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
There is a bug about Nullable enum with UseReferencedDefinitionsForEnums. #861
Comments
src/Swashbuckle.AspNetCore.SwaggerGen/Generator/SchemaRegistry.cs#L70 - (type.GetTypeInfo().IsEnum && _options.UseReferencedDefinitionsForEnums));
+ (_options.UseReferencedDefinitionsForEnums && (type.GetTypeInfo().IsEnum || Nullable.GetUnderlyingType(type)?.GetTypeInfo().IsEnum == true))); Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/Generator/SchemaRegistry.cs Lines 55 to 75 in 2a9efcf
|
@domaindrivendev |
It's not fix completely. |
@onionhammer Could you help me to review that? #864 |
Could you add a test please? |
Any news on this? |
@Rassilion My patched version works fine. But I'm too busy to make an unittest. |
I updated to 5.0.0-rc2 and looks like bug fixed, I was on 5.0.0-beta. |
@Rassilion that version has a different issue
will get translated as:
|
There is no release version of the package with this fix yet. When do we expect v5 release to be issued? |
@Rassilion if you've discovered a separate issue, please create a separate issue for it - it's too difficult to track issues within issues. @PMExtra - after further review, I'm not convinced that the generation of two separate enums should be considered a bug ... In OpenApi 3 (OA3), the schema definition has a |
Due to this unavoidable constraint, I don't believe this is an issue with SB. If it's causing issues in downstream tooling (e.g. Nswag) then I think the issue actually lies there. In closing, I can offer an alternative approach that may or may not work for you. I assume your goal with the non-nullable vs non-nullable properties is to make the value "required" in one case and not in the other. In OA3, this should be represented using the class Dto1 {
[Required]
public SomeEnum Some {get; set;}
}
class Dto2 {
public SomeEnum Some {get; set;}
} Of course the downside here is that if no value is submitted for the optional (non-required) property, then server side the value will default to the first value in your enum definition. So, you would need to introduce a value of |
@domaindrivendev I would like UseReferencedDefinitionsForEnums to work with nullable enums. Maybe I'm missing something, but I thought the semantics of C# worked like this:
So having to put a [Required] on an enum seems backward to the language.
It seems to me that the above should generate two references to MyEnum, one required one not.
|
In Swagger/OpenAPI, class MyClass
{
public MyEnum RequiredEnum { get; set; }
public MyEnum? OptionalEnum { get; set; }
} There's nothing in the code here that definitively indicates that |
I installed 5.0.0 release and haven't found UseReferencedDefinitionsForEnums in it, what did I miss? 5.0.0 still shows optional (nullable, not marked as [Required]) enums as required. |
@domaindrivendev the issue I have is that OptionalEnum isn't a 'MyEnum'. Given this code:
The swagger I get is for it is:
Since OptionalEnum is not a reference to "#/definitions/MyEnum", when I use Nswag to generate code, they come out as two different types. So parts of my interface use my "MyEnum" enum and parts use things with names like "MyClass1OptionalEnum". And then consumers of my SDK have to do weird casts. |
Which version of SB are you using? Using
|
I'm using 4.0.1 which was the latest stable release when I wrote that comment last September. We are in the process of moving from .Net core 2.1 to 3.1. It looks like the problem I see got fixed. |
I'm still slogging through upgrading 4.0.1 => 5.1.0.
|
I've worked through most of the issues moving to 5.1.0, but this is a blocker for me. I have a shared code project to test against both my original code and the SDK and this causes the generated SDK code to be incompatible. As it is, to build my C# SDK, I have to hand-edit the generated swagger, adding the I added a schema filter to set Nullable to true but it doesn't affect the generated swagger. Am I doing something wrong here? (Note: This and some other filters are here: https://github.com/TimothyByrd/swashbuckle_stuff )
|
@TimothyByrd the JSON that you're expecting is actually invalid Swagger/OpenAPI because a "reference" schema MUST NOT have any properties present other than the Swashbuckle gives you two options here which may or may not suit your needs. You can apply the suggested workaround from that issue with the
Or, you can force all enum schema's to be defined inline by setting
|
@domaindrivendev Thanks - after trying them out, I think UseAllOfToExtendReferenceSchemas will work. (As you'd expect UseInlineDefinitionsForEnums, gives me all individual types.) Now to do regression testing... One thing I noticed - using UseAllOfToExtendReferenceSchemas causes my references to also include the XML documentation in the generated swagger. So that's better. I suppose that's because a $ref doesn't allow any other properties, as you said above.
|
Yes exactly - the |
When enabling this option, what is the reason the Specifically, I mean in this code path: Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs Line 41 in 1b1d3da
I would have expected it to return a wrapped in Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs Line 260 in 1b1d3da
Is this by design? |
It will generate redundant enums.
The text was updated successfully, but these errors were encountered: