swagger-api / swagger-api/swagger-codegen

java8 / java8-localdatetime options are ignored

Open
#7,730 5 comments 6 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

Hi. I'm using the command line codegen to generate a Java model using the Java 8 options for dates/times, but I can see that the generated Java classes have references to Joda instead, like this:

@SerializedName("timeOfDayFrom")
private org.joda.time.* timeOfDayFrom = null;

I've also noticed this in the output:

[main] INFO io.swagger.codegen.DefaultGenerator - Model LocalTime not imported due to import mapping
Swagger-codegen version

I'm using the current master branch, which is 2.4.0-SNAPSHOT.

Swagger declaration file content or url

I'm using a JSON declaration generated by camel-rest-swagger. The relevant part is this one:

...

"timeOfDayFrom" : {
  "description" : "Minimum time of day (inclusive) to include in search results. Null means no minimum. ISO 8601 format, e.g.: 12:34:56",
  "$ref" : "#/definitions/LocalTime"
},

...

"LocalTime" : {
  "type" : "object",
  "properties" : {
    "hour" : {
      "type" : "integer",
      "format" : "int32"
    },
    "minute" : {
      "type" : "integer",
      "format" : "int32"
    },
    "second" : {
      "type" : "integer",
      "format" : "int32"
    },
    "nano" : {
      "type" : "integer",
      "format" : "int32"
    }
  }
},

The original model is also Java, and the variable looks like this:

@ApiModelProperty("Minimum time of day (inclusive) to include in search results. Null means no minimum. ISO 8601 format, e.g.: 12:34:56")
private LocalTime timeOfDayFrom;
Command line used for generation
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i http://10.17.1.65:8102/api/doc -l java -o /tmp/java-client -DdateLibrary=java8
Related issues/PRs

This issue seems to be related, but it's closed already:
https://github.com/swagger-api/swagger-codegen/issues/7177

Suggest a fix/enhancement

I've found out that if I remove these lines from DefaultCodegen.java

importMapping.put("DateTime", "org.joda.time.*");
importMapping.put("LocalDateTime", "org.joda.time.*");
importMapping.put("LocalDate", "org.joda.time.*");
importMapping.put("LocalTime", "org.joda.time.*");

Then the Java model is created correctly:

@SerializedName("timeOfDayFrom")
private LocalTime timeOfDayFrom = null;

Is it really a bug, or am I doing something wrong? Thank you very much.

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 DefaultCodegen.java, focusing on the import mappings for DateTime, LocalDateTime, LocalDate, and LocalTime when the Java generator receives dateLibrary=java8. Reproduce the issue with the command and Swagger declaration in the report, then inspect the generated model and import output. Done means the Java 8 option no longer produces Joda references for the LocalTime definition.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
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.