OpenAPITools / OpenAPITools/openapi-generator

[PHP] Invalid enum const names

Open
#1,475 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: PHP Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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 32b8d7fee7886c13a8ccd9523dd31791cdf6371b

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided openapi-generator-cli command against the Bungie OpenAPI v2 specification, then inspect the PHP generator path that produces the shown enum class and compare it with the swagger-codegen attempt. Done means integer-only enum names no longer generate invalid PHP constants while existing enum names do not acquire unintended extra underscores.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, php
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.