Skip to content

[JAXRS-CXF] Lowercase enums sent as uppercase #4045

Closed
@markus-wa

Description

@markus-wa
Description

When sending enums with lowercase 'value' attributes the uppercase enum name seems to be sent instead.

Swagger-codegen version

2.2.2-SNAPSHOT

Swagger declaration file content or url

http://petstore.swagger.io/v2/swagger.json

Command line used for generation

java -jar swagger2java/swagger-codegen-cli-2.2.2-SNAPSHOT.jar generate -i swagger.json -c config.json -l jaxrs-cxf -o out

Steps to reproduce

Example (petstore Pet.status) - note the lowercase values for the enum:

"status":{"type":"string","description":"pet status in the store","enum":["available","pending","sold"]}}

Generates this enum in the model (https://github.com/swagger-api/swagger-codegen/blob/master/samples/server/petstore/jaxrs-cxf-cdi/src/gen/java/io/swagger/model/Pet.java#L44):

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

    AVAILABLE(String.valueOf("available")), PENDING(String.valueOf("pending")), SOLD(String.valueOf("sold"));


    private String value;

    StatusEnum (String v) {
        value = v;
    }

    public String value() {
        return value;
    }

    public static StatusEnum fromValue(String v) {
        return valueOf(v);
    }
}

Implementation of getPetById():

    @Override
    public Response getPetById(Long arg0) {
        Pet result = new Pet();
        result.setId(arg0);
        result.setStatus(StatusEnum.AVAILABLE);
        result.setName("Test");
        return Response.ok().entity(result).type(MediaType.APPLICATION_JSON_TYPE).build();
    }

Results in uppercase status:

{
  "id": 5,
  "name": "Test",
  "status": "AVAILABLE"
}

The java client then expects a lowercase value (as specified) which results in a null value for status.

Related issues
Suggest a Fix

I assume this happens because the enum's toString() method should be overridden to return the result of value() which isn't done, so it returns the name of the enum, which is uppercase.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions