swagger-api / swagger-api/swagger-codegen

Faulty Transformation of Element with Type Boolean + Enum

Open
#10,849 0 comments 0 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

Reopen of ticket #10546 as the provided fix does not solve the issue. The generated Java code still looks as follows:

/**
 * True or False indicator
 */
@JsonAdapter(ModelBoolean.Adapter.class)
public enum ModelBoolean {
  
  TRUE("true"),
  
  FALSE("false");

  private Boolean value;

  ModelBoolean(Boolean value) {
    this.value = value;
  }

  public Boolean getValue() {
    return value;
  }

  @Override
  public String toString() {
    return String.valueOf(value);
  }

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

  public static class Adapter extends TypeAdapter<ModelBoolean> {
    @Override
    public void write(final JsonWriter jsonWriter, final ModelBoolean enumeration) throws IOException {
      jsonWriter.value(enumeration.getValue());
    }

    @Override
    public ModelBoolean read(final JsonReader jsonReader) throws IOException {
      Boolean value = jsonReader.nextBoolean();
      return ModelBoolean.fromValue(String.valueOf(value));
    }
  }
}

This is not a valid Java code. The constructor requires a value of type Boolean ModelBoolean(Boolean value), but is called with values of type String TRUE("true").

Tested with Swagegr Codegen Version 2.4.18 and version 3.0.24. Both sould include the fix.

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

No source file or test is named in the issue. Reproduce the invalid enum generation described for Swagger Codegen 2.4.18 and 3.0.24, then trace the generation path responsible for the Boolean/String mismatch and add a regression test; done means the generated Java code compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.