-
Notifications
You must be signed in to change notification settings - Fork 6k
[csharp] Clean up ref/inner enum structure #6887
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
Conversation
C# works differently from most languages in that enums are not considered objects. This means default(EnumType) will choose a default of the first enum option. This isn't desirable because it breaks the required = false functionality of swagger specs, which defines a property which isn't required to exist in the message body. Rather than force consumers to use enum values such as UNSPECIFIED, UNKNOWN, NOT_SET, etc... we can treat enums as primitives. This means any non-required enum will become Nullable<EnumType> regardless of whether it is defined as an inline enum or a referenced enum model.
…ferent enum usage cases.
Enums defines as ref models have a different object structure (CodegenModel) than those defined as inner enums (CodegenProperty). To make these look as similar as possible, we walk all ref model enums and reassign enumVars with the same properties inherited from the top level CodegenProperty so CodegenModel enums can use the same templates as inner enums.
Test case against spec at: Generate with:
Then, compile:
Evaluate:
|
I've removed the |
* master: (101 commits) [Swift4] Allow for custom dateformatter to be used (swagger-api#6672) [haskell-http-client] fix bug when generating models-only (swagger-api#6931) fix typo: crediential => credential minor typo fix [csharp] fix enum serialization of first value (swagger-api#6873) [PHP] Improve docs and README (swagger-api#6935) Binary mode for file deserialization in python (swagger-api#6936) add python tornado test to travis [Python/tornado] add integration tests and fix bugs (swagger-api#6925) Fix PHP passes response body to ApiException (swagger-api#6923) [TypeScript][Node] Resolve TS2532 error (swagger-api#6932) skip "all" shell script minor formatting change Fixes Issue swagger-api#6841, Map for accessing additionalProperties is generated. (swagger-api#6886) add tsloughter as owner erlang WIP: initial commit for Erlang client generator (swagger-api#6502) add back php client test Switch Travis image from MacOS to Linux (swagger-api#6937) add link to ebook [Scala] Default case class Option types to None for non-required fields (swagger-api#6790) ...
I haven't regenerated samples in this branch because there are multiple issues that I fixed in #6899. |
Hi, I just tried C# Enums again after having trouble before. I am experiencing the same issue as #5352 specifically https://github.com/swagger-api/swagger-codegen/issues/5352#issuecomment-335444175 where generating netcore code from editor.swagger.io with the following schema
is generating
Each of the values has a [double quote] "" on either side of the value which fails at compile time. Please advise if there is any way to correctly format the codegen. |
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.3.0.0
branch for changes related to OpenAPI spec 3.0. Default:master
.Description of the PR
This is a continuation of #6851, filed as a separate PR so we can have discussion of whether this is an appropriate workaround without blocking #6851. If this gets merged, it'll likely cause conflicts with #6873 (another continuation PR of #6851 for community discussion).
Enums defines as ref models have a different object structure
(CodegenModel) than those defined as inner enums (CodegenProperty). To
make these look as similar as possible, we walk all ref model enums and
reassign enumVars with the same properties inherited from the top level
CodegenProperty so CodegenModel enums can use the same templates as
inner enums
This PR is an attempt to resolve a question/comment in #5352 by @larssb.
/cc @mandrean