OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.35k stars 6.46k forks source link

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

Closed ghost closed 1 year ago

ghost commented 1 year ago

Bug Report Checklist

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: https://github.com/OpenAPITools/openapi-generator/pull/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

https://github.com/OpenAPITools/openapi-generator/pull/14105

paulmyer commented 1 year ago

Please fix. Enums are useless right now

ghost commented 1 year ago

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.

paulmyer commented 1 year ago

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 commented 1 year ago

@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. :)