Skip to content

[csharp] Treat enum models consistently #6851

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

Merged
merged 4 commits into from
Nov 9, 2017

Conversation

jimschubert
Copy link
Contributor

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 regardless of whether
it is defined as an inline enum or a referenced enum model.

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./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\.
  • Filed the PR against the correct branch: 3.0.0 branch for changes related to OpenAPI spec 3.0. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming langauge.

Description of the PR

This allows us to reference enums defined as RefModel using enum logic, rather than object logic. That is, default(EnumType?) rather than default(EnumType).

I've made some test-specific modifications to DefaultGenerator, such that integration tests can override the switches we pull from System properties and exclude the swagger codegen metadata (for test maintainability).

See #6690 et al.

/cc @mandrean @wing328

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.
@jimschubert
Copy link
Contributor Author

I'll rebuild samples after others have reviewed the changes, to limit noise.

@jimschubert jimschubert force-pushed the csharp-refmodel-enums branch from fdd60c7 to 72b9ab6 Compare October 31, 2017 18:37
@jimschubert
Copy link
Contributor Author

See linked issue, reporter has verified this branch against about 400 models with no additional concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants