swagger-api / swagger-api/swagger-codegen

[JAXRS-CXF] When setting dateLibrary to "legacy" java.util.Date is overridden by javax.xml.datatype.XMLGregorianCalendar

Open
#4,422 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

General: Suggestion help wanted Server: Java
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Running config-help for the jaxrs-cxf generator shows the following options for dateLibrary:

$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l jaxrs-cxf
...
    dateLibrary
            Option. Date library to use
                joda - Joda
                legacy - Legacy java.util.Date
                java8-localdatetime - Java 8 using LocalDateTime (for legacy app only)
                java8 - Java 8 native
...

When using joda, java8 and java8-localdatetime everything looks great.
However, when using legacy, instead of getting java.util.Date type for dates, I get javax.xml.datatype.XMLGregorianCalendar.

Looking at the code, it looks like DateTime is purposefuly overridden (see modules\swagger-codegen\src\main\java\io\swagger\codegen\languages\JavaCXFServerCodegen.java +79.
I don't really understand why DateTime is overridden - not sure if this is a bug or just something intentional that I don't understand. In any case, the help message and actual behavior of the generator are not consistent, so this should be treated as a bug.

Using swagger-codegen-2.2.1

Example swagger definition:

---
swagger: "2.0"
info:
  version: "1.0.2"
host: "localhost:8080"
basePath: "/api"
schemes:
- "http"
paths:
  /book:
    get:
      summary: "get a book"
      description: ""
      operationId: "getBook"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters: []
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/Book"
definitions:
  Book:
    type: "object"
    properties:
      Author:
        type: "string"
      Length:
        type: "integer"
      Published:
        type: "string"
        format: "date-time"

And resulting Book model:

public class Book  {
  @ApiModelProperty(example = "null", value = "")
  private String author = null;

  @ApiModelProperty(example = "null", value = "")
  private Integer length = null;

  @ApiModelProperty(example = "null", value = "")
  private javax.xml.datatype.XMLGregorianCalendar published = null; 

  // Code truncated for brevity

Generated using the following command:

$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ./tmp/swagger.yaml -o ./tmp/output/ -l jaxrs-cxf -DdateLibrary=legacy

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

Read modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaCXFServerCodegen.java around line 79, then run the supplied jaxrs-cxf config-help and generate commands with dateLibrary=legacy. Trace how the DateTime type is selected for the Published date in the example. Done means the documented legacy option and generated model use the same date type.

Written by the indexing model from the issue text.

Assessment

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