swagger-api / swagger-api/swagger-codegen

[java] Request to support: enumPropertyNaming / or case conversion - generated fromValue() to use equalsIgnoreCase().

Open
#10,390 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

openapi: 3.0.0
Java client code generation for an enum defined to be flexible with respect to case:

    color:
      type: string
      maxLength: 10
      enum: [Red, RED, Blue, BLUE]
      example: RED

Will generates invalid code (that doesn't compile) of the form:

public enum ColorEnum {
RED("Red"),
RED("RED"),
BLUE("Blue"),
BLUE("BLUE");

Suggest a fix/enhancement

Two possible solutions:
(i) generate code that uses mixed/matching case for the enums. Example: public enum ColorEnum { Red("Red"), RED("RED"), Blue("Blue"), BLUE("BLUE"); ...

(ii) generate code where the .fromValue(String text) method is case insensitive using equalsIgnoreCase() instead of equals()

Example - code generated now:

public static ColorEnum fromValue(String text) {
  for (ColorEnum b : ColorEnum.values()) {
    if (String.valueOf(b.value).equals(text)) {
      return b;
    }
  }
  return null;
}

to - code that should be generated:

public static ColorEnum fromValue(String text) {
  for (ColorEnum b : ColorEnum.values()) {
    if (String.valueOf(b.value).equalsIgnoreCase(text)) {
      return b;
    }
  }
  return null;
}

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 supplied OpenAPI 3.0 schema and inspect the generated Java ColorEnum.fromValue() method. Trace the Java enum generation template or entry point; done means the generated enum compiles with case variants and its fromValue behavior matches the selected requirement, with a regression test if the project has one.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 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.