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

[JAVA] Import mapping don't affect API generated classes in swagger codegen 2.4.7 #9572

Open Hbeau opened 5 years ago

Hbeau commented 5 years ago
Description

I have an Api that use java.time.Instant as request parameter.

@GetMapping("/employees/search")
Resources<Employee> search(@RequestParam(name ="Search after", defaultValue =  "2019-07-16T07:30:00Z") Instant hireDate) {

but when CodeGen generate the Java client code, it set date as OffsetDateTime. So to use Instant instead I use options typeMapping and importMapping to change Types. It work well for generated models classes but not for Api classes. Where type mapping work but not import mapping and the code does not compile.

output from mvn package

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/hugo/IdeaProjects/perso/tut-rest/rest/src/main/java/com/test/generated/api/EmployeeControllerApi.java:[262,45] cannot find symbol
  symbol:   class Instant
  location: class com.test.generated.api.EmployeeControllerApi

And the compiled code Have the rigth type mapping

  public ResourcesEmployee searchUsingGET(Instant searchAfter) throws RestClientException 

But Instant is not imported

Swagger-codegen version

Tried with swagger-codegen-maven-plugin version 2.4.7

Command line used for generation

I use the maven plugin to generate the code but this command line to the trick

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate --library=resttemplate -i http://localhost:8080/v2/api-docs -l java --type-mappings OffsetDateTime=Instant --import-mappings java.time.OffsetDateTime=java.time.Instant -DdateLibrary=java8
Steps to reproduce
Suggest a fix/enhancement

I don't really know how to solve this now but I'm pretty sure it's around this line https://github.com/swagger-api/swagger-codegen/blob/83a3de0a970f0fd23cbc44ae87e0484aaaacd450/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java#L989

if I print the values in AllImport the value specified in --import-mapping is missing

tarunsama commented 3 years ago

Same issue I am facing as well. Any work around this ?