Closed ghost closed 1 year ago
Please fix. Enums are useless right now
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.
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
$enumValue
needs to be a string that is validated against EnumValue
EnumValue
needs to have a property that actually stores the value@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 enum
s introduced in PHP 8.1:
https://php.watch/versions/8.1/enums
This could be some new enhancement request. :)
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:
Steps to reproduce
Generate the code
Related issues/PRs
Suggest a fix
https://github.com/OpenAPITools/openapi-generator/pull/14105