Skip to content

Conversation

@hinrik
Copy link
Contributor

@hinrik hinrik commented Nov 14, 2019

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, CodegenProperty has a new
field (referencedEnumType) which contains the enum value data type of a
referenced 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 indicating
whether 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).

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.
@hinrik hinrik changed the title [PHP] Support referenced enums [PHP] Fully support referenced enums Nov 14, 2019
@hinrik
Copy link
Contributor Author

hinrik commented Nov 15, 2019

@ybelenko
Copy link
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?

@hinrik
Copy link
Contributor Author

hinrik commented May 15, 2020

Yeah I can do that.

@etremblay
Copy link
Contributor

etremblay commented Mar 26, 2021

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.

@Cubid
Copy link
Contributor

Cubid commented Jan 7, 2022

I have done the rebase work as of today https://github.com/Cubid/openapi-generator/tree/php_fix_referenced_enums
If any one is interested to go further, she can use it.

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] Setters for referenced enums are unusable

4 participants