Skip to content

[JAVA] Bug generating models with enum discriminator #10441

@KamasamaK

Description

@KamasamaK
Description

The model for an object with an enum discriminator generates a constructor that causes a compilation error.

The following is an example generated model with some irrelevant code removed.

package com.alianza.client.model;

// imports

public class RingFailoverAction {
  /**
   * Gets or Sets type
   */
  @JsonAdapter(TypeEnum.Adapter.class)
  public enum TypeEnum {
    // values

    private String value;

    TypeEnum(String value) {
      this.value = value;
    }

    public String getValue() {
      return value;
    }

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

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

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

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

  @SerializedName("@type")
  private TypeEnum type = null;

  public RingFailoverAction() {
    this.type = this.getClass().getSimpleName();
  }
  public RingFailoverAction type(TypeEnum type) {
    this.type = type;
    return this;
  }

   /**
   * Get type
   * @return type
  **/
  @ApiModelProperty(required = true, value = "")
  public TypeEnum getType() {
    return type;
  }

  public void setType(TypeEnum type) {
    this.type = type;
  }

  // equals

  // hashCode

  // toString

  // toIndentedString
}

The statement causing the error is this.type = this.getClass().getSimpleName(), which is attempting to assign a String to a TypeEnum.

Swagger-codegen version

2.4.15 (not a regression)

Swagger declaration file content or url

Full file: https://api.alianza.com/v2/apidocs/swagger.json

Relevant example section

"RingFailoverAction" : {
  "type" : "object",
  "required" : [ "@type" ],
  "discriminator" : "@type",
  "properties" : {
    "@type" : {
      "type" : "string",
      "enum" : [ "VoicemailRingFailoverAction", "BusyRingFailoverAction", "ForwardRingFailoverAction" ]
    }
  }
}
Command line used for generation
java -jar swagger-codegen-cli-2.4.15.jar generate -i https://api.alianza.com/v2/apidocs/swagger.json --api-package com.alianza.client.api --model-package com.alianza.client.model --invoker-package com.alianza.client.invoker --group-id com.alianza -l java -o ./generated_java_client --additional-properties dateLibrary=java8
Steps to reproduce
  1. Run the command line
  2. Attempt to compile
Related issues/PRs

#10196 (comment)

Suggest a fix/enhancement

Wrapping this.getClass().getSimpleName() with the enum's valueOf() fixes the compilation error, but I don't know if this is the proper fix because I don't know what the use of that constructor even is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions