swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.98k stars 6.03k forks source link

[JavaSpring]swagger-codegen-maven-plugin with <async>false</async> #8759

Open raghuraman1 opened 6 years ago

raghuraman1 commented 6 years ago

Hi, I am using the swagger-codegen-maven-plugin with these versions: 2.3.1 and 2.4.0-SNAPSHOT (thats master i think). I came across this bug. Please see code snippet below of the pom.xml alongwith my suggestions for the fix.. I think its not resolved yet. If you feel its already fixed please excuse. Thanks. R Please note value of asynch: false.

`

io.swagger swagger-codegen-maven-plugin ${swagger.codegen.version} common generate ${project.basedir}/src/main/resources/api.yaml com.example.api.models com.example.api ${project.basedir} spring com.example.api com.example.api true foo Test API Test API https://api.example.com com.example.api 1 src/gen/java/main true java8 true false spring-boot true true true true

`

For the above configuration in swagger-codegen\src\main\resources\JavaSpring\apiDelegate.mustache an expression of {{#async}}CompletableFuture.completedFuture({{/async}} should resolve to empty. Unfortunately : In swagger-codegen\src\main\java\io\swagger\codegen\DefaultGenerator.java the value of async in this.config.additionalProperties() is a string.

This causes wrong code generation.

I can think of three solutions:

  1. In swagger-codegen\src\main\java\io\swagger\codegen\languages\SpringCodegen.java in public void processOpts() method change these lines: if (additionalProperties.containsKey(ASYNC)) { this.setAsync(Boolean.valueOf(additionalProperties.get(ASYNC).toString())); } TO if (additionalProperties.containsKey(ASYNC)) { this.setAsync(Boolean.valueOf(additionalProperties.get(ASYNC).toString())); convertPropertyToBooleanAndWriteBack(ASYNC); }

  2. Another way is in swagger-codegen\src\main\java\io\swagger\codegen\DefaultGenerator.java in method public Generator opts(ClientOptInput opts) right after this line "this.config = opts.getConfig();" invoke 'adjustToBoolean("async");' this second approach has benefit that you can use it from one place than do this change from many places. I also think there may be other similar boolean properties that need similar handling. //new method

` public void adjustToBoolean(String propertyName) { Object object = this.config.additionalProperties().get(propertyName); Boolean val=Boolean.FALSE; if(object!=null) { if(object instanceof String) { String string=(String) object; if(string.equalsIgnoreCase("true")) { val=true; } else { val=Boolean.FALSE; } } else if(object instanceof Boolean) { val=(Boolean) object; } else { val=Boolean.TRUE; } } else { val=Boolean.FALSE; } this.config.additionalProperties().put(propertyName, val);

} 

` Adding a a small google doc with more details using the 2nd approach https://docs.google.com/document/d/1N7ASyYPAuk5gO_kqcggXHOG_w5nGlKlToy2H1bbG6Tg/edit?usp=sharing

  1. A third approach could be to eliminate the booleans like false being created as strings in the first place. That is something havent pursued.
invitor commented 5 years ago

I can confirm that this issue still exists in version 3.0.11. When the async option is set to anything ("true", "false", "asdf", ...), the {{#async}} ... {{/async}} sections are added.