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

[php-symfony] Support for default scalar value of properties in model #16605

Merged
merged 2 commits into from
Oct 1, 2023

Conversation

reznikartem
Copy link
Contributor

After PR #12532 in php-symfony model does not exist default values from OpenApi specification. This commit add support for default scalar value of properties in php-symfony model. Default values now returns in getter-functions, and return type in getter function changed to not nullable/

@jebentier (2017/07), @dkarlovi (2017/07), @mandrean (2017/08), @jfastnacht (2017/09), @ybelenko (2018/07), @renepardon (2018/12)

…els. Default values now returns in getter-functions
{
return $this->{{name}};
return {{#defaultValue}}$this->{{name}} ?? {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}$this->{{name}}{{/defaultValue}};
Copy link
Member

Choose a reason for hiding this comment

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

@reznikartem thanks for the PR. Should the default value be assigned in the constructor instead?

If we do this in the getter, that means we can't set a property to null (assuming the property is nullable)

Copy link
Contributor Author

@reznikartem reznikartem Sep 21, 2023

Choose a reason for hiding this comment

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

Hi, @wing328
Thank you for answer. You are right, i`ve missed this moment. Revert type-hinting in second commit to initial state. And move default value of properties into property initializator.

__constructor bad place for default values because of it`s not running when object created by jms serializer, from json string.

In constructor i`m added checks with array_key_exist. Now default values will be present in objects, and they can be replaced with null for nullable properties, if this null value presents in $data array. If key not present in $data array, then default value will be available from getter.

Copy link
Member

Choose a reason for hiding this comment

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

ok. let's give it a try.

…erty initialize.

Made changes in __construct:
Now value of properties rewrites only if key exist in $data array.
@reznikartem reznikartem requested a review from wing328 September 25, 2023 08:17
@wing328 wing328 added this to the 7.1.0 milestone Oct 1, 2023
@wing328 wing328 merged commit 039c169 into OpenAPITools:master Oct 1, 2023
12 checks passed
AlanCitrix pushed a commit to AlanCitrix/openapi-generator that referenced this pull request Oct 26, 2023
…OpenAPITools#16605)

* Add support for default scalar value of properties in php-symfony models. Default values now returns in getter-functions

* Revert nullable type-hinting. Move default values from getter to property initialize.
Made changes in __construct:
Now value of properties rewrites only if key exist in $data array.
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