[PHP] Fully support referenced enums#4488
Open
hinrik wants to merge 9 commits intoOpenAPITools:masterfrom
Open
[PHP] Fully support referenced enums#4488hinrik wants to merge 9 commits intoOpenAPITools:masterfrom
hinrik wants to merge 9 commits intoOpenAPITools:masterfrom
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.
Contributor
Author
Contributor
|
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? |
Contributor
Author
|
Yeah I can do that. |
Contributor
|
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. |
Contributor
|
I have done the rebase work as of today https://github.com/Cubid/openapi-generator/tree/php_fix_referenced_enums |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).