Skip to content

[csharp] Use default rather than null in ctor#4145

Merged
wing328 merged 1 commit intoswagger-api:masterfrom
jimschubert:cs/3608
Jan 3, 2017
Merged

[csharp] Use default rather than null in ctor#4145
wing328 merged 1 commit intoswagger-api:masterfrom
jimschubert:cs/3608

Conversation

@jimschubert
Copy link
Copy Markdown
Contributor

@jimschubert jimschubert commented Nov 6, 2016

PR checklist

  • Read the contribution guildelines.
  • Ran the shell/batch 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)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

See #3608

This improves the defaults on constructor parameters by using defaults(type) rather than null for 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:

  1. Default to a default if present
  2. Set to default(TheType) if no default value is defined

Examples

Old Behavior: Dog

public Dog(string ClassName = null, string Color = null, string Breed = null)
{
    // to ensure "ClassName" is required (not null)
    if (ClassName == null)
    {
        throw new InvalidDataException("ClassName is a required property for Dog and cannot be null");
    }
    else
    {
        this.ClassName = ClassName;
    }
    // use default value if no "Color" provided
    if (Color == null)
    {
        this.Color = "red";
    }
    else
    {
        this.Color = Color;
    }
    this.Breed = Breed;
}

New Behavior: Dog

 public Dog(string ClassName = default(string), string Color = "red", string Breed = default(string))
{
    // to ensure "ClassName" is required (not null)
    if (ClassName == null)
    {
        throw new InvalidDataException("ClassName is a required property for Dog and cannot be null");
    }
    else
    {
        this.ClassName = ClassName;
    }
    // use default value if no "Color" provided
    if (Color == null)
    {
        this.Color = "red";
    }
    else
    {
        this.Color = Color;
    }
    this.Breed = Breed;
}

New Behavior: Other constructor examples

public Cat(string ClassName = default(string), 
            string Color = "red", 
            bool? Declawed = default(bool?)
) { }

public EnumArrays(JustSymbolEnum? JustSymbol = default(JustSymbolEnum?), 
            List<ArrayEnumEnum> ArrayEnum = default(List<ArrayEnumEnum>)
) { }

public Pet(long? Id = default(long?), 
           Category Category = default(Category), 
           string Name = default(string), 
           List<string> PhotoUrls = default(List<string>), 
           List<Tag> Tags = default(List<Tag>), 
           StatusEnum? Status = default(StatusEnum?)
) { }

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?

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