-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Reserved word causes Java codegen to fail #305
Comments
this is by design--it is possible to change the templates to support reserved words. However, that will be addressed in the develop_2.0 branch of swagger-codegen. |
We are at v2.1.2 and the problem is still here. Need change the target ? |
Please try 2.1.3-SNAPSHOT. I believe it has been addressed there. |
I try with last code from develop_2.0 branch |
Do you mind showing us what the auto-generated code looks like ? |
No generetad code. $ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i http://127.0.0.1:3000/api/swagger_doc.json -l java -o dir [...] According to swagger spec (1.2) void is a valid return data type. |
Agreed void is valid return type in swagger spec 1.2. My guess is that there may be an issue converting the spec from 1.2 to 2.0 when generating the client I would suggest you to try generating the client in Ruby or Python to see if there's a model named "Void". If yes, that means likely the conversion from 1.2 to 2.0 treats "void" as an object, not a data type indicating void return. |
Ruby is not the right place to look at, since there is no return type nor return. In Python void is treated as type, but not no void entity are created. In C#, same ugly thing: |
CSharp petstore sample (using swagger spec 2.0) does not have the "void" so my guess is that the issue has to do with conversion from swagger spec On Fri, Apr 3, 2015 at 12:14 AM, Frédéric Rodrigo notifications@github.com
|
Regarding the issue of treating "void" as model name, the following is what I guess where the problem is: // default response type
Property responseProperty = propertyFromTypedObject(operation);
if(responseProperty != null) {
Response response = new Response()
.description("success")
.schema(responseProperty); // <===== here
if(output.getResponses() == null)
output.defaultResponse(response);
else
output.response(200, response);
} if(obj.getRef() != null)
output = new RefProperty(obj.getRef());
else if(type != null) // <===== type is "void" here
output = new RefProperty(type);
else
output = new RefProperty("void"); |
I've made it work via this PR: swagger-api/swagger-parser#34, please have a look. |
I have updated, rebuild all, (the dependency is now 1.0.5-SNAPSHOT), but the problem is still the same: |
the 1.0.5-SNAPSHOT version of swagger-parser does has the issue, it needs to publish a new version for swagger-parser and use that new version in swagger-codegen project |
Hi, I've just pushed an updated 1.0.5-SNAPSHOT. Please try again, and apologies for the delay. |
It's now OK for me. Thank you. |
@frodrigo do you mind closing this issue given that it's resolved ? |
Looks like @frodrigo confirmed already. Closing. |
The reserved keyword "void" is now OK. But the others still cause exceptions as reported when this issue was opened. |
Without a list it would be difficult to do much about it. |
@frodrigo If I understand it correctly, you would like to use Java reserved keyword (e.g. return) as the model name. Do you mind telling us a bit more about the use case? Can the model be renamed as something else (e.g. ReturnObject) ? Currently, in Java codegen, we check whether the model name is a reserved keyword and throw an exception if found. If we skip the check, Java codegen can generate the code but it won't compile since Java would complain about using "return" as the class name. |
It's not my issue initially. Have personally no more problem since void is OK. Just point you, you may not close this issue just because "void" is now OK. |
Can you please add also a fix for the return type "Byte" --> java.lang.RuntimeException: byte (reserved word) cannot be used as a model name |
@MarcinKwiatkowski1988 Do you mind sharing the spec with us ? A model named "Byte" won't compile in Java |
{"apiVersion":"1.0.0","swaggerVersion":"1.2","basePath":"/pluto/api-docs","resourcePath":"/retrieveperson","apis":[{"path":"/retrieve/{id}","operations":[{"method":"GET","summary":"The API allows to retrieve a person's data","notes":"More notes about this method (rest2 two)","type":"Person","nickname":"retrievePerson","produces":["application/json","application/xml"],"authorizations":{},"parameters":[{"name":"id","description":"ID of Person to fetch","required":true,"type":"string","paramType":"path"}]}]}],"models":{"Person":{"id":"Person","description":"Person resource representation","properties":{"byteField":{"type":"array","description":"ByteField","items":{"type":"byte"}},"name":{"type":"string","description":"Name"},"surname":{"type":"string","description":"Surname"},"phoneNumbers":{"type":"array","description":"List of phone numbers","items":{"$ref":"PhoneNumber"}},"numbers":{"type":"array","items":{"$ref":"PhoneNumber"}}}},"PhoneNumber":{"id":"PhoneNumber","description":"PhoneNumber resource representation","properties":{"prefix":{"type":"string","description":"Country prefix"},"number":{"type":"string","description":"Phone number"}}}}} |
@MarcinKwiatkowski1988 may I know how you obtain the swagger spec file ?
|
I obtained it using swagger's annotations - for the Byte part, using this piece of code: @apimodel(value = "Person", description = "Person resource representation") The whole projects works very well, I just need the codegen to works with reserved word "Byte" to create the client for java |
In your case you want to use |
So I just need to wait till it will be supported, or I have to figure out how to change my project to not use Byte[] ? |
For |
I have a java file (Person.java) that has a property of this type: Byte[]; what I'm supposed to do to make it work with codegen, to have a working client generated by swagger-codegen, instead of having the error of "reserved word"? |
What about skipping that property for the time being ? and you manually add it back after the code is generated (and clearly this is not the best solution) |
That's not an option, since the real project has multiple files with that kind of property; the spec I showed was from a small-simple sample. |
What about a temporary workaround to comment out the line that's throwing the exception so that you will still get the code generated (then you can remove the custom model "Byte" to avoid compile error) A permanent solution is to wait for the OAI/OpenAPI-Specification#50 to be resolved. |
JSON below causes the Java CodeGen to fail with the following exception because of the use of 'switch':
java.lang.Exception: reserved word "switch" not allowed at com.wordnik.swagger.codegen.BasicJavaGenerator.escapeReservedWord(BasicJavaGenerator.scala:240) at com.wordnik.swagger.codegen.language.CodegenConfig.toVarName(CodegenConfig.scala:116) at com.wordnik.swagger.codegen.BasicJavaGenerator.toVarName(BasicJavaGenerator.scala:112) at com.wordnik.swagger.codegen.Codegen$$anonfun$apiToMap$2.apply(Codegen.scala:166) at com.wordnik.swagger.codegen.Codegen$$anonfun$apiToMap$2.apply(Codegen.scala:115) at com.wordnik.swagger.codegen.Codegen.apiToMap(Codegen.scala:115) at com.wordnik.swagger.codegen.BasicGenerator$$anonfun$prepareApiBundle$2$$anonfun$apply$16.apply(BasicGenerator.scala:345) at com.wordnik.swagger.codegen.BasicGenerator$$anonfun$prepareApiBundle$2$$anonfun$apply$16.apply(BasicGenerator.scala:344) at scala.collection.TraversableLike$WithFilter$$anonfun$foreach$1.apply(TraversableLike.scala:772) at scala.collection.immutable.List.foreach(List.scala:318)
The text was updated successfully, but these errors were encountered: