Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
seantreg opened this issue Jul 24, 2020 · 3 comments

Comments

@seantreg
Copy link

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.

@auto-labeler
Copy link

auto-labeler bot commented Jul 24, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@wing328
Copy link
Member

wing328 commented Jul 28, 2020

@seantreg thanks for reporting the issue. The correct code should look like the following?

  private LocalDate endDate = LocalDate.parse("1900-01-01"); 

UPDATE: just tested locally to confirm the above compiles without issues.

@parenko
Copy link
Contributor

parenko commented Dec 31, 2022

I think this issue is fixed with #11536
and can be closed, @wing328

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants