swagger-api / swagger-api/swagger-codegen

[Java, Jaxrs Spec] Ommitted type in swagger enum generates corrupted Java Enum

Open
#8,323 0 comments 1 reaction 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

When you leave out the type in a enum declaration in a swagger file (which apparently is valid), the generated enum class does not compile because codegen inserts empty string as the enum's type.

Swagger-codegen version

2.4.0 snapshot

Swagger declaration file content or url
  BackendType:
    #type: string
    enum:
      - ILON
      - HUE

leads to:

public enum BackendType {
  
  ILON("ILON"),
  
  HUE("HUE");

  private  value; <------------------------------------

  BackendType( value) { <----------------------------
    this.value = value;
  }

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

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

Command line used for generation

invoked via gradle, language is jax-rs spec
used options:

{
  "interfaceOnly": true,
  "dateLibrary":"java8",
  "invokerPackage": "ch.package.impl",
  "apiPackage" : "ch.package.webapi",
  "modelPackage" : "ch.package.webapi.model",
  "java8" : "true",
  "title" : "App server",
  "returnResponse": true
}

add above definition to a yaml file and generate jax-rs spec server code.

Related issues/PRs

to some degree: #8275

Suggest a fix/enhancement

One of those:

  • don't generate a value attribute in the enum (and also ommit constructors) if there is no type specified
  • Use string as default if no type specified

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 with the Gradle JAX-RS spec generation entry point and reproduce the issue using the provided Swagger YAML enum without a type. Inspect the generated Java enum and ensure the output compiles with a valid enum value type or without the unnecessary value field and constructor.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.