Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ralgor14 opened this issue Aug 18, 2016 · 15 comments

Comments

@ralgor14
Copy link

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.

@wing328
Copy link
Contributor

wing328 commented Aug 18, 2016

@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.

@wing328
Copy link
Contributor

wing328 commented Aug 19, 2016

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

@wing328
Copy link
Contributor

wing328 commented Aug 22, 2016

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

@wing328 wing328 closed this as completed Aug 22, 2016
@ralgor14
Copy link
Author

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

@wing328
Copy link
Contributor

wing328 commented Aug 22, 2016

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

@ralgor14
Copy link
Author

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?

@wing328
Copy link
Contributor

wing328 commented Aug 22, 2016

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

@ralgor14
Copy link
Author

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"
}
}
}
}
}

@rajeshkamal5050
Copy link

@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?

@wing328
Copy link
Contributor

wing328 commented Nov 8, 2016

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?

@rajeshkamal5050
Copy link

rajeshkamal5050 commented Nov 8, 2016

@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

@ambikath
Copy link

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

@nmrao
Copy link

nmrao commented Jan 2, 2018

@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.

@wing328
Copy link
Contributor

wing328 commented Jan 5, 2018

@nmrao lets continue the discussion in #6587, ok?

@nmrao
Copy link

nmrao commented Jan 8, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants