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

[#13998][Bug][PHP] Move isNullable section to the top of the setter function in templates #14005

Merged

Conversation

MustansirS
Copy link
Contributor

@MustansirS MustansirS commented Nov 14, 2022

Resolves #13998

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    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/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
    @jebentier @dkarlovi @mandrean @jfastnacht @ybelenko @renepardon @wing328

@MustansirS
Copy link
Contributor Author

@jebentier @dkarlovi @mandrean @jfastnacht @ybelenko @renepardon @wing328 This PR is ready for review

{{#isEnum}}
$allowedValues = $this->{{getter}}AllowableValues();
{{^isContainer}}
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues, true)) {
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}!in_array(${{{name}}}, $allowedValues, true)) {
Copy link
Member

Choose a reason for hiding this comment

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

@MustansirS thanks for the PR. I notice that the check for optional field has been replaced by isNullable check instead.

Shall we keep the check for optional field?

Copy link
Contributor Author

@MustansirS MustansirS Nov 17, 2022

Choose a reason for hiding this comment

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

I noticed that a null value will never be passed to the setter function if it is not a nullable field:

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache#L516
Screenshot 2022-11-18 at 12 19 37 AM

And if we keep the check as the check for just the optional field, then the original problem of the issue will persist and an operation such as a preg_match on a null value will be attempted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

thanks for the explanation.

@wing328
Copy link
Member

wing328 commented Nov 21, 2022

@wing328
Copy link
Member

wing328 commented Nov 21, 2022

thanks for the fix. lgtm but i've not tested it locally

let's give it a try to see if anyone has further feedback on this fix.

@wing328 wing328 merged commit a96777b into OpenAPITools:master Nov 21, 2022
@MustansirS
Copy link
Contributor Author

Thank you!

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.

[BUG][PHP] Runtime exception when property is both nullable and required
2 participants