Swagger-codegen: Enums: @JsonValue annotation is missing from toString() in the generated model for language jaxrs

Created on 28 Sep 2017  路  7Comments  路  Source: swagger-api/swagger-codegen

Looked at issue #3611 #3615

It does not work for language jaxrs .
I tried using 2.2.3 and 2.3.0 lastest snapshot also.

java -jar swagger-codegen-cli-2.3.0.jar generate -i genapi.yaml -l jaxrs -o generated_server

public enum HealthType {

OVERALL("overall"),

CONNECTED("connected"),

ONBOARDED("onboarded");

private String value;

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

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

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

Most helpful comment

See the same issue..Any responses?

All 7 comments

See the same issue..Any responses?

Do not put a @JsonValue at the toString() method. Add a new method:

@JsonValue
public {{dataType}} getValue() {
    return value;
}

This will work for non-String enums, too.
Imaging enums of datatype Integer or Double.

This is a generated file.
Cannot add code here.
It should be fixed from the swagger codegen

@sdoeringNew please check this

https://github.com/swagger-api/swagger-codegen/issues/3611

Yeah, sure. I already fixed a similar issue for the Java code generation.

I just mean: It would be wrong to fix this issue by adding a @JsonValue at the toString() method.

I think it's better to use Jackson's WRITE_ENUMS_USING_TO_STRING property

@cbornet : You mean while deserializing? Could you give me an example?

Was this page helpful?
0 / 5 - 0 ratings