OpenAPITools / OpenAPITools/openapi-generator

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

Open
#7,038 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

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

openapi-generator version

v5.0.0-beta

OpenAPI declaration file content or url
{
    "openapi": "3.0.1",
    "info": {
        "title": "Test",
        "version": "1.0"
      },
    "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 openapi-generator-cli-5.0.0-beta.jar generate -i test.json -g 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 = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-07-24T10:22:22.238+10:00[Australia/Sydney]")
public class Request {
  public static final String SERIALIZED_NAME_END_DATE = "endDate";
  @SerializedName(SERIALIZED_NAME_END_DATE)
  private LocalDate endDate = 1900-01-01; // <--- Type mismatch: cannot convert from int to LocalDate

  public static final String SERIALIZED_NAME_START_DATE = "startDate";
  @SerializedName(SERIALIZED_NAME_START_DATE)
  private LocalDate startDate;
  ...
Related issues/PRs

None known.

Suggest a fix

In the toDefaultValue(Schema) method of org.openapitools.codegen.languages.AbstractJavaCodegen the default value for a date is returned as a String representation of the date, but should probably be a call to LocalDate.parse or similar.

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 org.openapitools.codegen.languages.AbstractJavaCodegen, specifically the toDefaultValue(Schema) method, and reproduce the issue with the supplied OpenAPI declaration and Java generation command. Inspect the generated Request.java and verify that a date default produces valid Java for LocalDate rather than the shown invalid initialization.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.