0

I use the swagger-codegen-maven-plugin to generate Java classes and have trouble with the getter of an entity class field / member which type is an enum. Until version 2.2.3 the getter returns the enum value but with all versions above the getter just returns String.

The field in question looks like follows in the swagger file:

"status": {
    "type": "string",
    "enum": [
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G"
    ]
},

Code generated for version until 2.2.3:

@XmlType(name="StatusEnum")
@XmlEnum(String.class)
public enum StatusEnum {

    @XmlEnumValue("A") A(String.valueOf("A")), @XmlEnumValue("B") B(String.valueOf("B")), @XmlEnumValue("C") C(String.valueOf("C")), @XmlEnumValue("D") D(String.valueOf("D")), @XmlEnumValue("E") E(String.valueOf("E")), @XmlEnumValue("F") F(String.valueOf("F")), @XmlEnumValue("G") G(String.valueOf("G"));

    private String value;

    // omitted further details ...
}

/**
  * Get status
  * @return status
 **/
public StatusEnum getStatus() {
    return status;
}

public void setStatus(StatusEnum status) {
    this.status = status;
}

Everything > 2.2.3 (tried 2.3.0, 2.4.0, 3.0.34):

public enum StatusEnum {
    A("A"),
    B("B"),
    C("C"),
    D("D"),
    E("E"),
    F("F"),
    G("G");

    private String value;

    // omitted further details ...
}

/**
  * Get status
  * @return status
 **/
@JsonProperty("status")
public String getStatus() {
    if (status == null) {
        return null;
    }
    return status.getValue();
}

public void setStatus(StatusEnum status) {
    this.status = status;
}

How to configure the generator plugin to get the same method signature with version > 2.2.3 as before? Would be a lot of work to change the code in the existing projects everywhere where it calls getter of enum typed members.

AnnetteC
  • 490
  • 2
  • 5
  • 20
  • You can use custom templates with swagger codegen, you will make some changes in the getter/setter of enum templates for your technology/language(ex [pojo.mustache](https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/JavaJaxRS/cxf/pojo.mustache) for java cxf ) and provide them through some config options to your codegen maven plugin or cli – Akshay May 27 '22 at 15:29
  • One important point in above method, you only need to provide those files to the plugin which you've changed, the default ones are used for all that are not provided – Akshay May 27 '22 at 15:38

0 Answers0