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] PHPdoc Enum types #12331

Open
5 of 6 tasks
emajo opened this issue May 10, 2022 · 3 comments
Open
5 of 6 tasks

[PHP] PHPdoc Enum types #12331

emajo opened this issue May 10, 2022 · 3 comments

Comments

@emajo
Copy link
Contributor

emajo commented May 10, 2022

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

In the generated classes if an attribute is an enum, in its getter/setter, the phpdoc type is the class that contains the constants with the enum values instead of string:

enum class:

class Enum
{
    /**
     * Possible values of this enum
     */
    const FIRST = 'first';

    const SECOND = 'second';

Attribute setter(Enum type):

    /**
     * Sets type
     *
     * @param \Proj\Model\Enum|null $type type
     *
     * @return self
     */
    public function setType($type)
    {
        $this->container['type'] = $type;

        return $this;
    }

Here we set the property:

$obj->setType(Enum::FIRST);

As you can see the actual type passed is string and not Enum, so we will get a warning from the IDE.
So the phpdoc of the setter should be:

    /**
     * Sets type, see the accepted values in the Enum class
     *
     * @param string|null $type type
     *
     * @return self
     */
    public function setType($type)
    {
        $this->container['type'] = $type;

        return $this;
    }
openapi-generator version

5.4.0

@black-silence
Copy link

Actually the problem is deeper: when a server sends a response that includes an enum type, the actual string value is still deserialized through json_decode resulting in a null value and finally a class-not-found exception.

IMO enums should never be created as classes/objects/models at least in php 8.0

@wing328
Copy link
Member

wing328 commented Nov 4, 2022

if anyone would like to sponsor this task, please us know by replying with the amount you would like to sponsor.

@CommanderAlchemy
Copy link

Actually the problem is deeper: when a server sends a response that includes an enum type, the actual string value is still deserialized through json_decode resulting in a null value and finally a class-not-found exception.

IMO enums should never be created as classes/objects/models at least in php 8.0

I'we just encountered this in a project of mine that uses enums. Where it sets

@param Enum|null $type TestProp type: (TESTA = TESTING ENUM A, TESTB = TESTING ENUM B).

And when parsing response from the server

PHP Fatal error: Uncaught Error: Class 'Enum' not found in ....ObjectSerializer.php on line 470

Any way to fix this or is the only way to not use enum's in the api doc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants