[csharp] Use default rather than null in ctor#4145
Merged
wing328 merged 1 commit intoswagger-api:masterfrom Jan 3, 2017
Merged
[csharp] Use default rather than null in ctor#4145wing328 merged 1 commit intoswagger-api:masterfrom
wing328 merged 1 commit intoswagger-api:masterfrom
Conversation
Contributor
Author
|
@wing328 I'll try to get the same fix in place for aspnetcore and NancyFX within a day or so. |
jimschubert
added a commit
to jimschubert/swagger-codegen
that referenced
this pull request
Jan 16, 2017
See original issue swagger-api#3608 This adds same model constructor logic to aspnetcore as what was added to csharp generator by PR swagger-api#4145. This doesn't include NancyFX because model construction relies more on object initialization in that generator.
jimschubert
added a commit
to jimschubert/swagger-codegen
that referenced
this pull request
Jan 16, 2017
This follows up to swagger-api#4145, and modifies model constructors to use default(x) instead of initializing to nulls. default(x) works in all cases using intuitive default values it is intended to support. Example: csharp> public enum Color { RED = -1, BLUE = 0, GREEN } csharp> var color = default(Color) csharp> color BLUE In the above example, The default of BLUE=0 is expected. For nullable enums, this would be null as a default. The aspnetcore generated code is also updated to support enums and nested enums to account for changed to the petstore.yaml used to generate the sample.
3 tasks
wing328
pushed a commit
that referenced
this pull request
Jan 18, 2017
* [aspnetcore] Use default rather than null in ctor See original issue #3608 This adds same model constructor logic to aspnetcore as what was added to csharp generator by PR #4145. This doesn't include NancyFX because model construction relies more on object initialization in that generator. * [aspnetcore] ctor defaults + enum support This follows up to #4145, and modifies model constructors to use default(x) instead of initializing to nulls. default(x) works in all cases using intuitive default values it is intended to support. Example: csharp> public enum Color { RED = -1, BLUE = 0, GREEN } csharp> var color = default(Color) csharp> color BLUE In the above example, The default of BLUE=0 is expected. For nullable enums, this would be null as a default. The aspnetcore generated code is also updated to support enums and nested enums to account for changed to the petstore.yaml used to generate the sample. * [aspnetcore] Regenerate sample
davidgri
pushed a commit
to davidgri/swagger-codegen
that referenced
this pull request
May 11, 2017
davidgri
pushed a commit
to davidgri/swagger-codegen
that referenced
this pull request
May 11, 2017
…er-api#4573) * [aspnetcore] Use default rather than null in ctor See original issue swagger-api#3608 This adds same model constructor logic to aspnetcore as what was added to csharp generator by PR swagger-api#4145. This doesn't include NancyFX because model construction relies more on object initialization in that generator. * [aspnetcore] ctor defaults + enum support This follows up to swagger-api#4145, and modifies model constructors to use default(x) instead of initializing to nulls. default(x) works in all cases using intuitive default values it is intended to support. Example: csharp> public enum Color { RED = -1, BLUE = 0, GREEN } csharp> var color = default(Color) csharp> color BLUE In the above example, The default of BLUE=0 is expected. For nullable enums, this would be null as a default. The aspnetcore generated code is also updated to support enums and nested enums to account for changed to the petstore.yaml used to generate the sample. * [aspnetcore] Regenerate sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR checklist
./bin/to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.shand./bin/security/{LANG}-petstore.shif updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)2.3.0branch for breaking (non-backward compatible) changes.Description of the PR
See #3608
This improves the defaults on constructor parameters by using
defaults(type)rather thannullfor all inputs.There seems to have been some weird usage of nullable types, possibly on parameters that aren't defined in the target swagger definition as nullable although I'm not 100% sure and didn't spend a lot of time investigating this.Edit: I looked more closely and the nullable types seem to only occur when a primitive or enum is not marked as required. I had actually previously explained this behavior for properties, but wasn't thinking broadly enough when I made comment about constructor parameters above.
This PR changes constructor parameters in models to:
default(TheType)if no default value is definedExamples
Old Behavior: Dog
New Behavior: Dog
New Behavior: Other constructor examples
I didn't include regenerated samples in this PR.
Question
If this looks good, should I go ahead and update aspnetcore and NancyFx generators to do the same for models?