OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.87k stars 6.59k forks source link

encapsulated date component not genereted #12503

Open jbkervyn opened 2 years ago

jbkervyn commented 2 years ago

Since the version 5.4.0, the code generation of the following component via the spring generator generates the following model

schemas: 
    Person:
      type: object
      properties:
        id:
          $ref: '#/components/schemas/ID'
        birthDate:
           $ref: '#/components/schemas/Date'
    ID:
      type: integer
      format: int64
      description: Unique identifier
      example: 1255266
    Date:
      type: string
      format: date
      description: A particular point in the progression of time in a calendar year expressed in the YYYY-MM-DD format. This representation is aligned with ISO 8601.
      example: "2020-02-15"
public class PersonApi   {

  @JsonProperty("id")
  private Long id;

  @JsonProperty("birthDate")
  private DateApi birthDate;

  public PersonApi id(Long id) {
    this.id = id;
    return this;
  }

And the DateApi object is not being genertated or present in the import

Here is the configuration

<generatorName>spring</generatorName>
<modelNameSuffix>Api</modelNameSuffix>

In version 5.3.1, the following is being generated

public class PersonApi   {
  @JsonProperty("id")
  private Integer id;

  @JsonProperty("birthDate")
  private java.util.Date birthDate;

It's worth noting that a quick workaround would be to inline the date directly.

juergencodes commented 2 years ago

Very interesting for me how sometimes things happen. I was debugging the similar (or even same) thing the last couple of hours. Unfortunatelly, I could not decide if it is a bug or feature, Hence, I'm requesting the contributors to give their 2 cents.

My settings is as follows: I use the schema

      type: string
      format: date-time

that I expect to render as OffsetDateTime in case of jdk8 and use a prefix for model generation. The prefix (and obviously the suffix) are key to this issue. Without it, it works as I would expect.

To reproduce the issue I used the following schema

openapi: 3.0.1
paths:

components:
  schemas:
    MyObject:
      type: object
      properties:
        date1:
          $ref: '#/components/schemas/OffsetDateTimeX'
        date2:
          $ref: '#/components/schemas/OffsetDateTime'
    OffsetDateTimeX:
      type: string
      format: date-time
    OffsetDateTime:
      type: string
      format: date-time

Basically, both attributes date1 and date2 are quite similar except that for date2 I've hit a somehow known word/type. Using the prefix Prefix, in the resulting Java model, the two attributes are rendered as follows:

  @SerializedName(SERIALIZED_NAME_DATE1)
  private OffsetDateTime date1;

  public static final String SERIALIZED_NAME_DATE2 = "date2";
  @SerializedName(SERIALIZED_NAME_DATE2)
  private PrefixOffsetDateTime date2;

date1 is correct for me, however, I would have expected date2 also to be rendered as OffsetDateTime. Please note that there is no class PrefixOffsetDateTime generated.

Hint for @jbkervyn: As my example shows, probably another workaround for you is to rename the schema Date to something else. Maybe give that a try.

jbkervyn commented 2 years ago

@juergencodes I don't know why I didn't think about that :) Indeed, renaming the Component to something else than Date fixes it !

juergencodes commented 2 years ago

Here is a patch for reproducing the issue in a unit test.

12503.txt