Skip to content

Possible swagger hub codegen issue with Spring Data Mongo #3509

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
mherb63 opened this issue Aug 2, 2016 · 23 comments
Closed

Possible swagger hub codegen issue with Spring Data Mongo #3509

mherb63 opened this issue Aug 2, 2016 · 23 comments

Comments

@mherb63
Copy link

mherb63 commented Aug 2, 2016

Description

Mongo not able to map swagger code generated fields with domain model code generated from swaggerhub.

The following JSON is used on swaggerhub:
"frameTransmissionPeriod": {
"description": "SLM period for frame transmission",
"type": "string",
"enum": [
"_10ms",
"_100ms",
"_1sec",
"_10sec"
],
"default": "_100ms"
},

And here is the generated code (Server->Spring), note the uppercase "MS" and "SEC"
public enum FrameTransmissionPeriodEnum {
_100MS("_100ms"),
_1SEC("_1sec"),
_10SEC("_10sec");

private String value;

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

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

}

When reading from Spring Data mongo we have what's in Mongo:
"ethOamDmmCfg" : {
"testId" : "5555",
"pmCosId" : 1,
"vlanId" : 100,
"frameTransmissionPeriod" : "_100ms",
"frameSize" : "1028"
},

And the following Mongo error:
2016-08-02 15:46:21.804 ERROR 79727 --- [nio-8680-exec-8] c.c.l.p.exception.ApiExceptionHandler : No enum constant com.ciena.lion.mef.pm.domain.EthOamDmmCfg.FrameTransmissionPeriodEnum._100ms
java.lang.IllegalArgumentException: No enum constant com.ciena.lion.mef.pm.domain.EthOamDmmCfg.FrameTransmissionPeriodEnum._100ms
at java.lang.Enum.valueOf(Enum.java:238)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:819)

Swagger-codegen version

swaggerhub.com

Swagger declaration file content or url
Command line used for generation
Steps to reproduce
Related issues
Suggest a Fix

at some point in time, the generated Spring code has upper-cased the enums and I believe this has caused the issue. If I change "MS" and "SEC" to "ms" and "sec" in the generated domain model code, Mongo is happy.

@wing328
Copy link
Contributor

wing328 commented Aug 3, 2016

@mherb63 are you saying the following enum name and value mapping is incorrect?

_100MS("_100ms"),
_1SEC("_1sec"),
_10SEC("_10sec");

If yes, what would the correct mapping looks like?

@mherb63
Copy link
Author

mherb63 commented Aug 3, 2016

it seems like Mongo is looking for "_100ms" (with lower case).

c.c.l.p.exception.ApiExceptionHandler : No enum constant com.ciena.lion.mef.pm.domain.EthOamDmmCfg.FrameTransmissionPeriodEnum._100ms
java.lang.IllegalArgumentException: No enum constant com.ciena.lion.mef.pm.domain.EthOamDmmCfg.FrameTransmissionPeriodEnum._100ms
at java.lang.Enum.valueOf(Enum.java:238)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:819)

@wing328
Copy link
Contributor

wing328 commented Aug 5, 2016

@mherb63 this should be fixed by #3531. Please pull the latest master to give it a try (I assume you're using Jersey1.x or 2.x Java API client and you will need to use the --library option to specify that as Java API client now defaults to use okhttp-gson)

@wing328 wing328 modified the milestones: v2.2.1, v2.2.2 Aug 8, 2016
@wing328
Copy link
Contributor

wing328 commented Aug 8, 2016

Please try the stable version 2.2.1 and let us know if you still experience the same issue.

@wing328 wing328 closed this as completed Aug 8, 2016
@mherb63
Copy link
Author

mherb63 commented Aug 9, 2016

I'm generating Spring code from swaggerhub. Just tried and its still uppercasing the enums.

@wing328
Copy link
Contributor

wing328 commented Aug 9, 2016

@mherb63 thanks for checking again. I'll take another look at the issue for Spring (the fix I mentinoed was for Java client)

@wing328 wing328 reopened this Aug 9, 2016
@wing328 wing328 modified the milestones: v2.2.2, v2.2.1 Aug 9, 2016
@mherb63
Copy link
Author

mherb63 commented Aug 9, 2016

Great. Thanks!

Please let me know when you want me to re-test from swaggerhub.

@mherb63
Copy link
Author

mherb63 commented Aug 18, 2016

any updates on this issue? When will fixes be pushed to swaggerhub for Spring server code generation?

Thanks!

@wing328
Copy link
Contributor

wing328 commented Aug 18, 2016

@mherb63 To be clear, the fix will still convert the enum name to upper case. The fix we did with Java added the fromValue method so that the deserialization works: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/java/jersey1/src/main/java/io/swagger/client/model/EnumArrays.java#L95

I've been very busy these days and I'll try to file a PR for this issue tomorrow (Fri).

@wing328
Copy link
Contributor

wing328 commented Aug 19, 2016

UPDATE: Filed #3615 to add the fromValue method

@wing328
Copy link
Contributor

wing328 commented Aug 22, 2016

#3615 merged into master. Please pull the latest master and build the JAR locally to use the fix before stable version 2.2.2 is released.

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

mherb63 commented Aug 23, 2016

I'm curious why you are converting the enum name to upper case. This is recent new behavior which has broken our existing code. Can you not use the enum value as the name (without uppercasing)? That's how it was working up until about a month ago or so.

Thanks!

@wing328
Copy link
Contributor

wing328 commented Aug 24, 2016

@mherb63 we do it to conform the enum naming convention in Java: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html, http://stackoverflow.com/a/3069863/677735

@mherb63
Copy link
Author

mherb63 commented Aug 30, 2016

I just discovered another side effect of the uppercasing. The Model Schema on the Swagger UI page shows the lowercase version. Notice the "interval" below:

image

and the generated Spring server code:

image

@wing328
Copy link
Contributor

wing328 commented Aug 30, 2016

I believe the "Model Schema" shows the JSON presentation of the model.

@mherb63
Copy link
Author

mherb63 commented Aug 30, 2016

yea but the Enums should be all caps there, otherwise it fails. For example:

image

@wing328
Copy link
Contributor

wing328 commented Aug 30, 2016

Did you get the error using the latest master, which should have addressed the deserialization for spring model with enum property if I'm not mistaken?

@mherb63
Copy link
Author

mherb63 commented Aug 30, 2016

I generated the code from Swagger hub on Aug 24th (6 days ago). What new code did you add that I can check if its there?

@wing328
Copy link
Contributor

wing328 commented Aug 30, 2016

@mherb63
Copy link
Author

mherb63 commented Aug 30, 2016

I just generated our code from swaggerhub and I'm not seeing any fromValue method.

  public enum FrameTransmissionPeriodEnum {
    _10MS("_10ms"),

    _100MS("_100ms"),

    _1SEC("_1sec"),

    _10SEC("_10sec");

    private String value;

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

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

Michaels-iMac:spring-server-generated mherb$ pwd
/Users/mherb/Downloads/spring-server-generated
Michaels-iMac:spring-server-generated mherb$ find . -name *.java | xargs grep -i fromValue
Michaels-iMac:spring-server-generated mherb$

@wing328
Copy link
Contributor

wing328 commented Aug 30, 2016

I don't think SwaggerHub is using the latest master of Swagger Codegen.

@mherb63
Copy link
Author

mherb63 commented Oct 4, 2016

are you sure this bug is fixed for Spring Server code generation? I just did the below and I'm still not seeing any fromValue methods in the generated model code.

brew install swagger-codegen
swagger-codegen generate -i Y1731PMActivation-1.1.12-swagger.yaml -l spring-mvc

Mike

@wing328
Copy link
Contributor

wing328 commented Oct 4, 2016

brew is not installing the latest master either. It's using the latest stable version 2.2.1 instead.

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

2 participants