Swashbuckle.AspNetCore: There is a bug about Nullable enum with UseReferencedDefinitionsForEnums.
class Dto1 {
public SomeEnum Some {get; set;}
}
class Dto2 {
public SomeEnum? Some {get; set;}
}
It will generate redundant enums.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 26 (8 by maintainers)
Commits related to this issue
- Fix for Nullable enum. (#861) — committed to PMExtra/Swashbuckle.AspNetCore by PMExtra 6 years ago
- Fix for Nullable enum. (#861) — committed to PMExtra/Swashbuckle.AspNetCore by PMExtra 6 years ago
@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.
@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
$refproperty. This presents a bit of a dilemna. On the one hand you probably want enum schema’s to be referenced as opposed to being redefined inline with every usage. But, if they’re referenced then you can’t really apply something “contextual” likenullableto the definition schema because it can be shared across different contexts. In fact, there’s an issue in the Swagger/OpenAPI repo addressing this exact problem.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
UseAllOfToExtendReferenceSchemas()setting: This would generate the following JSON for your example:Or, you can force all enum schema’s to be defined inline by setting
UseInlineDefinitionsForEnums(). This would generate the following JSON for your example:I’m still slogging through upgrading 4.0.1 => 5.1.0. So now I’m asking why optionalEnum isn’t marked as nullable like this?
@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.