swagger-api / swagger-api/swagger-codegen

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

Open
#9,572 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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
  • Create an API that use Swagger2
  • Create an GET endpoint that use java.time.Instant
  • run 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
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java around line 989 and inspect how AllImport is assembled for API classes. Reproduce with the provided CLI command and verify that the generated EmployeeControllerApi.java includes the java.time.Instant import and compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.