swagger-api / swagger-api/swagger-codegen

[JAVA] Problem generating model where default value is specified for date property

Open
#10,388 2 comments 1 reaction 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

Model properties that are dates generate invalid Java code where a default value is specified.

Swagger-codegen version

v3.0.20
(unknown whether this is a regression)

Swagger declaration file content or url
{
    "openapi": "3.0.1",
    "paths": {},
    "components": {
        "schemas": {
            "Request": {
                "type": "object",
                "properties": {
                    "endDate": {
                        "type": "string",
                        "format": "date",
                        "default": "1900-01-01"
                    },
                    "startDate": {
                        "type": "string",
                        "format": "date"
                    }
                }
            }
        }
    }
}
Command line used for generation
java -jar swagger-codegen-cli-3.0.20.jar generate -i test.json -l java -o .\output --additional-properties dateLibrary=java8-localdatetime
Steps to reproduce

Run the command above;
View generated Request.java which contains the following invalid variable initialization:

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2020-07-14T16:43:41.079+10:00[Australia/Sydney]")
public class Request {
  @SerializedName("endDate")
  private LocalDate endDate = Mon Jan 01 00:00:00 AEST 1900;
 
  @SerializedName("startDate")
  private LocalDate startDate = null;
  ...
Related issues/PRs

None known.

Suggest a fix/enhancement

Removing the default from the Swagger file stops the problem, but the file comes from a third party, and editing it is not desirable.

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 with the provided test.json and run the Swagger Codegen CLI command using dateLibrary=java8-localdatetime. Inspect the generated Request.java, especially the endDate initialization; done means the date default produces valid Java while retaining the declared default value.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.