Skip to content
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

Dart - generate constructor with named params #6751

Merged
merged 2 commits into from
Jun 27, 2020

Conversation

agilob
Copy link
Contributor

@agilob agilob commented Jun 23, 2020

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/config/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

@ircecho (2017/07) @swipesight (2018/09) @jaumard (2018/09) @athornz (2019/12) @amondnet (2019/12)

@@ -6,7 +6,12 @@ class {{classname}} {
{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{
{{/allowableValues}}
{{/vars}}
{{classname}}();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agilob I'm no expert in Dart but shall we keep the default constructor (no argument) for backward compatibility?

(upcoming 5.0.0 allows breaking changes without fallbacks but it's still worth considering backward compatibility)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, happy to explain this feature. Dart has flexible syntax on method params, default values, and mandatory params.

Method/constructor params can be mandatory (as not named) or named (optional) or named AND mandatory. This change introduces constructors that have named optional parameters. Such parameters have input params inside {} brackets. Since all fields in these classes are public anyway (dart discourages use of setters and getters "just because" like in java), so we have this syntax, of all valid constructors.

Constructors with all optional params are backwards compatible, it's really just programmers' preference
image

This MR introduces optional named params in constructors, while cascade operators is already supported, it's fully backwards compatible.
https://dart.dev/guides/language/language-tour#optional-parameters
https://dart.dev/guides/language/language-tour#cascade-notation-

(upcoming 5.0.0 allows breaking changes without fallbacks but it's still worth considering backward compatibility)

I am myself a guardian of backwards compatibility ;)

This MR 6729 breaks backwards compatibility when creating enums (which now are just string without any validation), so I found it necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am myself a guardian of backwards compatibility ;)

👍

Thanks for the explanation. The change looks good to me.

If no further feedback or question, I'll merge it over the weekend.

@wing328 wing328 merged commit 1798fea into OpenAPITools:master Jun 27, 2020
@wing328 wing328 added this to the 5.0.0 milestone Jun 27, 2020
@Mike278
Copy link

Mike278 commented Jul 17, 2020

Constructors with all optional params are backwards compatible

not for properties with default values

for example:

      "Foo": {
        "required": [
          "bar"
        ],
        "properties": {
          "bar": {
            "type": "string",
            "default": "some default value"
          }
        }
      },

generates

class Foo {
  
  String bar = "some default value";

  Foo({
    this.bar,
  });

  // ... 
}

print(Foo().bar); // prints null, used to print 'some default value'

@agilob
Copy link
Contributor Author

agilob commented Jul 23, 2020

@Mike278 this is fixed now on master

@agilob agilob deleted the dart2-constructor-generation branch September 25, 2021 20:35
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.

3 participants