Swagger-codegen: Possible swagger hub codegen issue with Enums: @JsonValue annotation is missing from toString()

Created on 18 Aug 2016  Â·  15Comments  Â·  Source: swagger-api/swagger-codegen

Description

The @JsonValue annotation is missing when codegen creates java code (Server->jaxrs) for Enums.
This annotation should be on toString() method.
The following:
"state": {
"type": "string",
"enum": [
"active",
"inactive"
]
}

causes creation of the following Enum code:
public enum StateEnum {
ACTIVE("active"),

INACTIVE("inactive");

private String value;

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

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

}

Which causes "active" and "inactive" in JSON files to be unrecognized.
In order to correct it the annotation @JsonValue should be added before toString():
@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

It was OK in previous versions of swagger hub.
Annotations are used by jackson's fasterXml package.

Enum Bug Java

Most helpful comment

@wing328 , I still see the issue with version 2.2.3 maven plugin, the @jsonValue does not get generated .. Any ideas?

All 15 comments

@ralgor14 thanks for reporting the issue and suggesting a fix. I'll look into this tomorrow (Friday) and hopefully file a PR for the fix.

@ralgor14 I've filed #3615 to fix the issue.

@ralgor14 PR merged into master. Please pull the latest to give it a try.

I do not see this version under https://github.com/swagger-api/swagger-codegen
I can only download version 2.2.1 from 2016, Aug 7

@ralgor14 please build the JAR locally (mvn clean install) before 2.2.2 (next stable release) is released.

Because of security issues I cannot download maven into my host.
Could you please send me the compiler exe file just rename it from exe to something else?
Or even better create a temporary link for unreleased version that can be downloaded in the same way as a regular released version?

Can you use https://generator.swagger.io? If yes, I can setup another online code generator to run the latest version of Swagger Codegen.

I didn't succeed.
Following an input for swagger: Could you please compile it with your latest version and send me the Server->jaxrs created java files?
{
"swagger": "2.2.1",
"info": {
"description": "demo",
"version": "1.1.11",
"title": "demo",
"contact": {
"name": "Ruth Algor",
"email": "[email protected]"
}
},
"host": "A.B.C:8080",
"basePath": "/y1731pm/v1",
"schemes": [
"http"
],
"produces": [
"application/json"
],
"paths": {
"/api/pm/pmsession": {
"post": {
"tags": [
"PM_Session"
],
"operationId": "createPmSession",
"summary": "Create PM Session",
"description": "Create PM Session",
"deprecated": false,
"parameters": [
{
"name": "PM_Session",
"required": true,
"in": "body",
"description": "PM Session",
"schema": {
"$ref": "#/definitions/PM_Session"
}
}
],
"responses": {
"200": {
"description": "OK - Request was successful",
"schema": {
"$ref": "#/definitions/PM_Session"
}
},
"202": {
"description": "Accepted - The request has been accepted for processing, but the processing has not been completed for asynchronous operations.",
"schema": {
"$ref": "#/definitions/PM_Session"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/Error_Response"
}
},
"404": {
"description": "Not found",
"schema": {
"$ref": "#/definitions/Error_Response"
}
},
"422": {
"description": "Resource Issue",
"schema": {
"$ref": "#/definitions/Error_Response"
}
}
}
}
}
},
"definitions": {
"PM_Session": {
"description": "PM_Session",
"type": "object",
"properties": {
"id": {
"description": "Identifier for this PM Session",
"type": "string"
},
"state": {
"description": "State of PM Session",
"type": "string",
"enum": [
"active",
"inactive"
]
},
"ethOamPerfMonCfg": {
"$ref": "#/definitions/EthOamPerfMonCfg"
}
}
},
"EthOamPerfMonCfg": {
"description": "EthOamPerfMonCfg",
"type": "object",
"properties": {
"startTime": {
"description": "description",
"type": "string"
},
"enabled": {
"description": "State",
"type": "boolean"
},
"EthOam1SlCfg": {
"$ref": "#/definitions/EthOam1SlCfg"
},
"EthOamDmCfg": {
"$ref": "#/definitions/EthOamDmCfg"
}
},
"required": [
"startTime"
]
},
"EthOam1SlCfg": {
"description": "description",
"type": "object",
"properties": {
"testId": {
"type": "string"
},
"unicastDestMacAddr": {
"type": "string"
},
"unicastSrcMacAddr": {
"type": "string"
},
"frameTransmissionPeriod": {
"description": "DMM period for frame transmission",
"type": "string",
"enum": [
"_10ms",
"_100ms",
"_1sec",
"_10sec"
],
"default": "_1sec"
},
"frameSize": {
"type": "string"
}
}
},
"EthOamDmCfg": {
"description": "description",
"type": "object",
"properties": {
"testId": {
"type": "string"
},
"multicastDestMacAddr": {
"type": "string"
},
"vlanId": {
"type": "string"
},
"frameTransmissionPeriod": {
"description": "description",
"type": "string",
"enum": [
"_100ms",
"_1sec",
"_10sec"
],
"default": "_1sec"
},
"frameSize": {
"type": "string"
}
}
},
"Error_Response": {
"description": "Error addition information",
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
},
"description": {
"type": "string"
}
}
}
}
}

@wing328 I tried with the snapshot builds from here https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen-maven-plugin/2.2.2-SNAPSHOT/ Wed Nov 02 21:01:06

I am still not able to see the @JsonValue being there for the toString() method?

    private String value;

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

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

Am I missing anything here?

That's a snapshot for the maven plug-in (not swagger codegen cli).

Have you tried building the JAR locally based on the latest master?

@wing328 It has dependency with the 2.2.2-SNAPSHOT swagger-codegen which should contain this fix right?

https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen/2.2.2-SNAPSHOT/ - Wed Nov 02 21:00:06

@wing328 , I still see the issue with version 2.2.3 maven plugin, the @jsonValue does not get generated .. Any ideas?

@wing328, like other users commented this issue is still reproducible with 2.2.3. Even tried with 2.3.0 and is reproducible. Should add any options while generating the code? Using cli, java -jar swagger-codegen-cli-2.3.0.jar, to generate code.

@nmrao lets continue the discussion in https://github.com/swagger-api/swagger-codegen/issues/6587, ok?

Sorry for the delayed reply.

Yes, sure.

On Fri, Jan 5, 2018 at 3:39 PM, William Cheng notifications@github.com
wrote:

@nmrao https://github.com/nmrao lets continue the discussion in #6587
https://github.com/swagger-api/swagger-codegen/issues/6587, ok?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/swagger-api/swagger-codegen/issues/3611#issuecomment-355518434,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AEbSe6A6XP0Oe5vHXqG-IpivMrfdmCY5ks5tHfTYgaJpZM4JnoJ6
.

Was this page helpful?
0 / 5 - 0 ratings