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

[Scala] Default Option parameters to None #6538

Merged
merged 2 commits into from
Nov 27, 2017

Conversation

gmarz
Copy link
Contributor

@gmarz gmarz commented Sep 21, 2017

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: master for non-breaking changes and 3.0.0 branch for breaking (non-backward compatible) changes.

Description of the PR

Defaults Option parameters to None which makes method calls and models less verbose.

@wing328
Copy link
Contributor

wing328 commented Sep 21, 2017

@gmarz thank for the PR.

cc @clasnake @foxmk @ramzimaalej

@@ -61,7 +61,7 @@ class {{classname}}(val defBasePath: String = "{{{basePath}}}",
{{#allParams}} * @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}} * @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
*/
def {{operationId}}({{#allParams}}{{paramName}}: {{#required}}{{dataType}}{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{/required}}{{^required}}Option[{{dataType}}]{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{^defaultValue}} = None{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}: Option[{{returnType}}]{{/returnType}} = {
def {{operationId}}({{#allParams}}{{paramName}}: {{#required}}{{dataType}}{{#defaultValue}} /* = {{{defaultValue}}}*/{{/defaultValue}}{{/required}}{{^required}}Option[{{dataType}}]{{#defaultValue}} = None /* = {{{defaultValue}}}*/{{/defaultValue}}{{^defaultValue}} = None{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}: Option[{{returnType}}]{{/returnType}} = {
Copy link
Contributor

Choose a reason for hiding this comment

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

@gmarz it's already default to None if defaultValue is not provided:

{{^defaultValue}} = None{{/defaultValue}}

I think the following should be changed from

/* = {{{defaultValue}}}*/

to

 = {{{defaultValue}}}

instead to use the default value provided in the spec.

(I do not recall why it's commented. Maybe it's not working for all default values)

Copy link
Contributor

Choose a reason for hiding this comment

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

@gmarz can you tell me what are the steps to reproduce the error, and I will test it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wing328 sorry for the slow reply...

(I do not recall why it's commented. Maybe it's not working for all default values)

Yea, I'm not sure. Maybe because it needs to be wrapped in Some...I think the current template will try and do Option[Boolean] = false which isn't valid?

@ramzimaalej there is no error/issue here. This change just defaults Option types to None for convenience.

Copy link
Contributor

Choose a reason for hiding this comment

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

@gmarz do you mind resolving the conflicts and I will take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ramzimaalej done :)

Copy link
Contributor

Choose a reason for hiding this comment

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

@gmarz I think the current implementation should be able to meet your requirement:

/* = {{{defaultValue}}}*/{{/defaultValue}}{{^defaultValue}} = None{{/defaultValue}}

which is default to None if defaultValue is not provided.

In your spec, do you have the defaultValue set to something?

Copy link
Contributor

Choose a reason for hiding this comment

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

@gmarz do you mind double checking if current version solves the problem or not. if the problem is not solved, please create a ticket (if it does not exists) and attach the swagger-doc you used to reproduce the issue

Copy link
Contributor

Choose a reason for hiding this comment

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

The default value is commented out anyway so it should not be a problem merging this PR into master if the spec contains default value. I'll create a separate "issue" to track the defaultValue enhancement to Scala API client.

Copy link
Contributor

Choose a reason for hiding this comment

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

@gmarz @wing328 @ramzimaalej the only issue I see with the previous default value is that if someone were to say defaultValue = null it would fail as Some(null) defines a value of null, which is unexpected. The optional value should be Option({{defaultValue}}) to account for this edge case, as Option(null) becomes None and Option(nonNull) becomes Some(nonNull).

@gmarz gmarz force-pushed the fix/scala-default-params branch 2 times, most recently from ee6af15 to d219577 Compare November 23, 2017 03:35
@ramzimaalej
Copy link
Contributor

@wing328 LGTM

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.

5 participants