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] Fixed bug for handling optional header parameters #3776
Conversation
Refactored code to handle optional header parameters.
{{/headerParams}} | ||
{{#headerParams}} | ||
{{^required}} | ||
if ({{paramName}} != null) {{paramName}}.map { v => headerParams += "{{baseName}}" -> v.toString } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use 2-space indentation instead?
And what if {{paramName}} (variable) is a non-string? Shall we use {{#isString}} ... {{/isString}} to do the null check only if it's a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we should use 2-space indentation here.
null check is done just to make sure that the code won't break, if the user has provided null values when calling the Api. |
@clasnake when you've time, please review this change. Thanks! |
{{/required}} | ||
{{#required}} | ||
if ({{paramName}} != null) headerParams += "{{baseName}}" -> {{paramName}}.toString | ||
{{/required}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the type of {{paramName}} is an Option, why do we need to do the null check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that {{paramName}} is an Option if it's an optional parameter.
If it's required, then we still need the null check (if the data type is string
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. But why do we use Option for non-required and raw type for required params? Required or not can be achieved by whether providing default value or not. It doesn't matter whether we use Option or not. It's orthogonal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know the reason (I was not the one who created the async-scala generator).
scala
generator (API client) also follows similar convention: Option for non-required and raw type for required.
If Option should be used for both required and non-required parameters, I welcome the (breaking) change as long as it conforms to Scala style guide or best practices.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, basically we will never see null in Scala code. And actually that's the reason why we have Option.
@wing328 @geetikagupta16 Just finished review. We don't need any null check here because {{paramName}} is an Option. Use pattern matching instead and do the operation if it matches Some(xxx). |
@geetikagupta16 thanks for the contribution. PR merged into master. In the future, we may merge the |
@wing328 Thanks for merging PR |
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)2.3.0
branch for breaking (non-backward compatible) changes.Description of the PR
Refactored code to handle optional header parameters