swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.01k stars 6.03k forks source link

[PHP] Enum with type integer creates invalid const names #11311

Open paulvandermeijs opened 2 years ago

paulvandermeijs commented 2 years ago
Description

When a model is generated for a schema defined as an enum with type integer a class constant is included for each value. These constants will use the integer value as their name which is not valid PHP. For enums with type string the names for numeric values seem to be prefixed with _ but this does not happen for type integer.

Swagger-codegen version

3.0.30

Swagger declaration file content or url

The following schema definition:

"ResponseCode": {
    "enum": [
        1,
        2
    ],
    "type": "integer",
    "format": "int32"
}

Results in:

class ResponseCode
{
    /**
     * Possible values of this enum
     */
    const 1 = 1;
const 2 = 2;
    /**
     * Gets allowable values of the enum
     * @return string[]
     */
    public static function getAllowableEnumValues()
    {
        return [
            self::1,
self::2,        ];
    }
}

While the following schema definition:

"ResponseCode": {
    "enum": [
        "1",
        "2"
    ],
    "type": "string"
}

Results in:

class ResponseCode
{
    /**
     * Possible values of this enum
     */
    const _1 = '1';
const _2 = '2';
    /**
     * Gets allowable values of the enum
     * @return string[]
     */
    public static function getAllowableEnumValues()
    {
        return [
            self::_1,
self::_2,        ];
    }
}
jmauzyk commented 1 year ago

Hi @paulvandermeijs, I'm running into the same issue with a PHP client that was generated by Swagger Codegen. Did you find any workaround to correct this without having to manually change each one of these?