swagger-api / swagger-api/swagger-codegen
[PHP] Enum with type integer creates invalid const names
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
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, ];
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the integer enum schema and inspect the PHP enum generation path in the template-driven code generator. Compare it with string enum generation; done means generated PHP uses valid constant names while preserving the integer values and allowable-value references.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100