-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[PHP] Fully support referenced enums #4488
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
base: master
Are you sure you want to change the base?
Conversation
Resolves issue OpenAPITools#4328 and allows reusing enums in definitions consumed by PHP applications. Without this change, referenced enums are unusable in PHP (and possibly other languages which don't have native enums). This is because the setter for such a property would expect an instnace of the external enum class as the value, and that class has no state (and was therefore a meaningless value). On properties which reference enum schemas, we now set `referencedEnumType` which contains the data type of the enum value. This allows languages which don't have native enums to handle referenced enums correctly. I adjusted the PHP model template to make use of this new variable, so that it now generates very similar code for referenced enums as it does for inline ones: the setter accepts a primitive type and validates the value. The only difference is that the allowed values are stored in the external enum class rather than inline on the model.
|
I would take a look at this PR. @hinrik can you rebase on latest master and resolve merge conflicts? Or it's not relevant already? |
|
Yeah I can do that. |
|
This feature would be awesome. psalm and phpstorm always complain about badly typed enum arguments generated by the php generator. Edit I can help with the rebase but I would need to open an new PR. |
|
I have done the rebase work as of today https://github.com/Cubid/openapi-generator/tree/php_fix_referenced_enums |
Resolves issue #4328 and allows full use of referenced enums for PHP.
This change might also make it easier to address the issue discussed in #3186.
Without this change, referenced enums are not fully usable in PHP (and
possibly other languages which don't have native enums). This is because the
setter for such a property would expect an instance of the external enum
class as the value, and that class has no state (and was therefore a
meaningless value) (see #4328).
On properties which reference enum schemas,
CodegenPropertyhas a newfield (
referencedEnumType) which contains the enum value data type of areferenced schema. This allows generators for languages that don't have native
enums to easily work with the primitive type instead.
I adjusted the PHP model template to make use of this new variable, so
that it now generates very similar code for referenced enum properties as it
does for inline ones: the setter accepts a primitive type and validates the
value. The only difference is that the allowed values are stored on the
external enum class rather than inline on the model.
There was also some syntactically invalid PHP code being generated for
referenced enums containing purely numeric values. To account for the
differences in code generation between inline and referenced enums, the
toEnumVarName()method now receives an extra parameter indicatingwhether the enum is on its own model class. In the case of PHP this allows
selectively prefixing the name with an underscore (while avoiding a
superfluous underscore on inline enums, which already have a prefix).