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

[BUG] php-symfony enum generation generates empty class #14115

Closed
5 of 6 tasks
ghost opened this issue Nov 25, 2022 · 4 comments
Closed
5 of 6 tasks

[BUG] php-symfony enum generation generates empty class #14115

ghost opened this issue Nov 25, 2022 · 4 comments

Comments

@ghost
Copy link

ghost commented Nov 25, 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

when declaring an enum in OAS specification file, php-symfony generator generates an empty DTO class instead of a usable implementation.

I proposed a fix in the following PR: #14105

openapi-generator version

Version 6.2.1

OpenAPI declaration file content or url

Generate "php-symfony" code with gradle plugin v6.2.1

Here is the generated code for StatusDto:

...
class StatusDto 
{
        /**
     * Constructor
     * @param array|null $data Associated array of property values initializing the model
     */
    public function __construct(array $data = null)
    {
    }
}
Steps to reproduce

Generate the code

Related issues/PRs
Suggest a fix

#14105

@ghost ghost added the Issue: Bug label Nov 25, 2022
@paulmyer
Copy link

Please fix. Enums are useless right now

@ghost
Copy link
Author

ghost commented May 11, 2023

Please fix. Enums are useless right now

@paulmyer can you please detail your statement (rational, recommended usage, etc...) ?

By the way, I close this issue as the PR has been merge few times ago.

@ghost ghost closed this as completed May 11, 2023
@paulmyer
Copy link

Schema snippet:

        TestItem:
      type: object
      properties:
        enumValue:
          $ref: "#/components/schemas/EnumValue"

    EnumValue:
      type: string
      enum: [ FIRST, SECOND, THIRD ]

Generated classes:

class TestItem 
{
        /**
     * @var EnumValue|null
     * @SerializedName("enumValue")
     * @Assert\Type("Test\Model\EnumValue")
     * @Type("Test\Model\EnumValue")
     */
    protected ?EnumValue $enumValue = null;

    /**
     * Constructor
     * @param array|null $data Associated array of property values initializing the model
     */
    public function __construct(array $data = null)
    {
        $this->enumValue = $data['enumValue'] ?? null;
    }

    /**
     * Gets enumValue.
     *
     * @return EnumValue|null
     */
    public function getEnumValue(): ?EnumValue
    {
        return $this->enumValue;
    }

    /**
     * Sets enumValue.
     *
     * @param EnumValue|null $enumValue
     *
     * @return $this
     */
    public function setEnumValue(?EnumValue $enumValue = null): self
    {
        $this->enumValue = $enumValue;

        return $this;
    }
}
class EnumValue
{
    /**
     * Possible values of this enum
     */
    const FIRST = "FIRST";
    const SECOND = "SECOND";
    const THIRD = "THIRD";
    
    /**
     * Gets allowable values of the enum
     * @return string[]
     */
    public static function getAllowableEnumValues()
    {
        return [
            self::FIRST,
            self::SECOND,
            self::THIRD,
        ];
    }
}

$enumValue in TestItem requires a EnumValue object. However, an EnumValue has no parameters. So either

  1. $enumValue needs to be a string that is validated against EnumValue
  2. EnumValue needs to have a property that actually stores the value

@ghost
Copy link
Author

ghost commented May 12, 2023

@paulmyer thanks for your feedback.
If I understand well, you say that there's no added value using those custom made enums.
But still, even if it is a workaround, the referenced PR make the generated code usable (which wasn't the case before).

Regarding the real fix, I think the generator should use enums introduced in PHP 8.1:
https://php.watch/versions/8.1/enums

This could be some new enhancement request. :)

This issue was closed.
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

1 participant