openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [SPRING] Generation of JSonNullable for OpenApi 3.1 stops working from version 7.3

Open jan-mawh opened this issue 2 years ago • 4 comments

Description

when running spring-generator with version 7.3 or 7.4 the generator does not generate fields of type jsonNullable anymore

Desired outcome as produced with version 7.2 :

package org.example.swagger.model;

// skipp imports

/**
 * BugRequest
 */
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)

@JsonTypeName("bugRequest")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-04-16T13:30:01.556701506+02:00[Europe/Berlin]")
public class BugRequest {

  private JsonNullable<TransportDate> transportDate = JsonNullable.<TransportDate>undefined();

  public BugRequest transportDate(TransportDate transportDate) {
    this.transportDate = JsonNullable.of(transportDate);
    return this;
  }

  /**
   * Get transportDate
   * @return transportDate
  */
  @Valid 
  @Schema(name = "transportDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("transportDate")
  public JsonNullable<TransportDate> getTransportDate() {
    return transportDate;
  }

  public void setTransportDate(JsonNullable<TransportDate> transportDate) {
    this.transportDate = transportDate;
  }

}

Actual outcome as produced with version 7.3 and 7.4 :

package org.example.swagger.model;

// skip imports


/**
 * BugRequest
 */
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)

@JsonTypeName("bugRequest")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-04-16T13:34:18.957871524+02:00[Europe/Berlin]")
public class BugRequest {

  private BugRequestTransportDate transportDate;

  public BugRequest transportDate(BugRequestTransportDate transportDate) {
    this.transportDate = transportDate;
    return this;
  }

  /**
   * Get transportDate
   * @return transportDate
  */
  @Valid 
  @Schema(name = "transportDate", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("transportDate")
  public BugRequestTransportDate getTransportDate() {
    return transportDate;
  }

  public void setTransportDate(BugRequestTransportDate transportDate) {
    this.transportDate = transportDate;
  }

}

note that the generated class with version 7.2 has a field of type JsonNullable<TransportDate> (as disired)
the generated class with version 7.3 only has a field of type TransportDate

openapi-generator version

version 7.3 and 7.4

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: bug-report
  version: 0.2.8
servers:
  - url: https://test.test.org
paths:
  /bug-report/v0/:
    post:
      summary: summery
      operationId: bug-endpoint
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/bugRequest'
        required: true
      responses:
        '200':
          description: Successful operation
        '400':
          description: Invalid input

components:
  schemas:
    bugRequest:
      $schema: https://spec.openapis.org/oas/3.1/dialect/base
      title: calculateTransportRequest
      type: object
      properties:
        transportDate:
          type: object
          anyOf:
            - $ref: '#/components/schemas/transportDate'
            - type: 'null'
    transportDate:
      title: transportDate
      type: object
      properties:
        year:
          type: integer
        month:
          type: integer
        day:
          type: integer
      required:
        - year
        - month
        - day

Command line used for generation

spring generator via maven plugin - maven plugin config:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.3.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${yaml.file}</inputSpec>
                            <generatorName>spring</generatorName>
                            <modelPackage>${project.groupId}.swagger.model</modelPackage>
                            <apiPackage>${project.groupId}.swagger.api</apiPackage>
                            <invokerPackage>${project.groupId}.handler</invokerPackage>
                            <generateApiTests>false</generateApiTests>
                            <skipOperationExample>false</skipOperationExample>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <additionalModelTypeAnnotations>
                                    @com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
                                </additionalModelTypeAnnotations>
                                <openApiNullable>true</openApiNullable>
                                <useSpringBoot3>true</useSpringBoot3>
                                <virtualService>false</virtualService>
                                <delegatePattern>false</delegatePattern>
                                <sourceFolder>${project.build.directory}/generated-sources</sourceFolder>
                                <configHelp>true</configHelp>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

Run generator for Spring with above OpenApi specification Check created DTO classes

Suggest a fix/enhancement

unsure

jan-mawh avatar Apr 16 '24 11:04 jan-mawh

Still having issues on 7.20.0 and openapi 3.1.1

benjyAuthi avatar Mar 20 '26 01:03 benjyAuthi

does it mean it still work for openapi 3.0.x spec?

if that's the case, please downgrade the spec for the time being while we look into it.

wing328 avatar Mar 20 '26 03:03 wing328

We tried 3.0 as well with no success :/

Our normal setup is 7.9.0 and 3.0 (failing)

benjyAuthi avatar Mar 23 '26 04:03 benjyAuthi

I tested this on latest master (7.25.0-SNAPSHOT, built locally) with the spring generator, openApiNullable=true, and an OpenAPI 3.1 spec.

A property declared the 3.1 way generates JsonNullable correctly:

transportDate:
  type: [string, "null"]
  format: date
private JsonNullable<LocalDate> transportDate = JsonNullable.<LocalDate>undefined();

One note: the old 3.0 keyword nullable: true was removed in OpenAPI 3.1, so in a 3.1 document it is ignored and such a property generates a plain field instead of JsonNullable. Specs converted from 3.0 to 3.1 that kept nullable: true will see the behavior reported here. The fix is to declare the null type in the schema, like above.

So the regression itself looks fixed on master. I think this issue can be closed.

SubhamAshok avatar Aug 02 '26 11:08 SubhamAshok