OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Numeric String enums cause duplicate enum keys

Open
#7,288 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)?
  • What's the version of OpenAPI Generator used? 4.3.1
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

An openapi spec containing a string enum with numeric values has the possibility to generate duplicate enum keys, because the generator gives the strings "1" and "-1" the same key (_1). The declaration below will produce;

public class Foo {
  /**
   * Gets or Sets bar
   */
  @JsonAdapter(BarEnum.Adapter.class)
  public enum BarEnum {
    _1("1"),
    _1("-1");
  
  //...
  }
}
openapi-generator version

4.3.1

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: Example spec with numeric string enums
  version: 1.0.0
  title: OpenAPI Petstore
  license:
    name: Apache-2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
paths:
  /foo:
    get:
      responses:
        default:
          description: response
          content:
            application/json:
              schema:
                type: object
                properties:
                  string:
                    $ref: '#/components/schemas/Foo'
components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          type: string
          enum:
            - 1
            - -1
Generation Details

Applies to java generator, see AbstractJavaCodegen.

Steps to reproduce

Use yaml above openapi-generator -g java -i yaml.

Related issues/PRs

Could not find any through searching open issues / prs

Suggest a fix

Detecting numeric strings here and handling them as if they were a numeric type for enum var naming would fix the issue by producing NUMBER_1("1"), NUMBER_MINUS_1("-1"); or similar but this would be a large BC break for anyone with numeric string enums.

Adding a special case to the string handling for negative numeric strings to provide _1("1"), _MINUS_1("-1") reduces BC break (though still breaks in case of negative numeric strings) but is less consistent.

Switching behaviour on a config setting may be possible but is probably also undesireable.

It may also be possible be able to detect duplicate enum keys at generation time.

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 by reproducing the issue with the YAML specification and openapi-generator -g java -i yaml. Then inspect the enum variable naming logic in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java around the referenced lines. Done means the generated Java enum has unique keys for numeric string values such as "1" and "-1".

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.