Skip to content

[PHP] Invalid enum const names #1475

@AlwinGarside

Description

@AlwinGarside
Description

When generating clients for the Bungie OpenAPI v2 spec, certain enum types with unnamed integer values result in invalid constant names, consisting only of integers.

What I mean, concretely, is that this:

    "GroupsV2.GroupType": {
      "format": "int32",
      "enum": [
        "0",
        "1"
      ],
      "type": "integer",
      "x-enum-values": [
        {
          "numericValue": "0",
          "identifier": "General"
        },
        {
          "numericValue": "1",
          "identifier": "Clan"
        }
      ]
    }, 

results in this:

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

(note the const 0 = 0;)

For the complete spec file, check:
https://raw.githubusercontent.com/Bungie-net/api/master/openapi-2.json

openapi-generator version

3.3.4-SNAPSHOT, commit 32b8d7f

OpenAPI declaration file content or url

https://raw.githubusercontent.com/Bungie-net/api/master/openapi-2.json

Command line used for generation
java -jar openapi-generator-cli.jar generate \
      --generator-name php \
      --config         swagger-codegen-config.php.json \
      --input-spec     https://raw.githubusercontent.com/Bungie-net/api/master/openapi-2.json \
      --output         bungie-sdk-php \
      -DapiTests=false \
      -DmodelTests=false

swagger-codegen-config.php.json:

{
  "composerVendorName": "yogarine",
  "composerProjectName": "bungie-sdk-php",
  "artifactVersion": "2.3.2",
  "invokerPackage" : "Bungie",
  "modelPackage": "Model",
  "apiPackage": "API",
  "variableNamingConvention": "camelCase",
  "packagePath": "bungie-sdk-php",
  "srcBasePath": "src",
  "gitUserId": "Yogarine",
  "gitRepoId": "bungie-sdk-php",
  "validateSpec": false
}
Steps to reproduce

Generate with the command provided above.

Related issues/PRs

None I could find.

Suggest a fix/enhancement

The same issue is present in swagger-codegen and I tried to fix it there by also prefixing integers with underscore. However this breaks existing enums. You can see my attempt here, together with generated petstore files where enum constants suddenly have double underscores.
https://github.com/swagger-api/swagger-codegen/compare/master...Yogarine:fix-php-enums#diff-2f17f44f63e1286045038c552dcfcb66

I'm trying to figure out how to do this properly but I'm not really well-acquainted with the code. Perhaps someone can point me in the right direction?

@jebentier @dkarlovi @mandrean @jfastnacht @ackintosh @ybelenko

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions