OpenAPITools / OpenAPITools/openapi-generator

[BUG] [PHP] Enum values starting with - have the same name as ones without, resolving in duplicate consts

Open
#7,441 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Currently enum values that are the same except for a starting - result in the same const names for php model files. This result in unexecutable files, that need manual editing before it can be used.

openapi-generator version

both on docker:
openapi-generator-cli 5.0.0-SNAPSHOT
commit : a28037d
built : 2020-09-01T09:02:56Z
openapi-generator-cli 5.0.0-beta2
commit : 212b4f7
built : 2020-09-04T05:08:30Z

OpenAPI declaration file content or url

https://gist.github.com/victorlap/44e351b72f5d9f26f0ac1943b4c6cf22

Command line used for generation
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v5.0.0-beta2 generate \
    -i /local/spec.json \
    -g php \
    -o /local/src
Steps to reproduce

Run the above command on the spec that is linked in the gist.
Check file src/lib/Model/SalesOrderBillingaddress.php and see duplicate const declaration:

    const CUSTOM_FORM__1049 = '-1049';
    const CUSTOM_FORM__1049 = '1049';
Related issues/PRs

I've not found any related issues/PRs

Suggest a fix/enhancement

I think the sanitizing of the name needs to change a bit.

It's probably somewhere in the toEnumVarName

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

Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java, at toEnumVarName. Reproduce with the linked OpenAPI declaration and the documented Docker command, then inspect src/lib/Model/SalesOrderBillingaddress.php. Done means enum values -1049 and 1049 generate distinct constant names and the PHP output is executable.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.