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.23k stars 6.43k forks source link

[BUG][Java][Spring][Bounty] allOf with nullable produces build method not expecting JsonNullable, Java cannot compile #15640

Closed jspetrak closed 11 months ago

jspetrak commented 1 year ago

OpenAPI Generator: 7.0.0 beta, template "spring"

OpenAPI Specification: 3.0.3

Given test file reproduces the issue.

openapi: 3.0.3

info:
  title: Test API specification
  version: 0.0.1

paths:
  /tests/{testId}:
    parameters:
      - name: testId
        in: path
        required: true
        schema:
          type: string
    patch:
      operationId: modifyTest
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ModifyTestRequest'
      responses:
        '200':
          description: Modifies a test
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ModifyTestResponse'

components:
  schemas:
    ModifyTestRequest:
      type: object
      additionalProperties: false
      discriminator:
        propertyName: objectType
      required:
        - oneRequired
      properties:
        oneRequired:
          type: string
          nullable: false
        twoOptional:
          type: string
          nullable: true
        threeOptionalBinaryContent:
          type: string
          format: byte
          nullable: true

    ModifyNamedTestRequest:
      allOf:
        - $ref: '#/components/schemas/ModifyTestRequest'
        - type: object
          additionalProperties: false
          properties:
            name:
              type: string
              nullable: true

    ModifyOrderedTestRequest:
      allOf:
        - $ref: '#/components/schemas/ModifyTestRequest'
        - type: object
          additionalProperties: false
          properties:
            ordinal:
              type: integer
              nullable: true

    ModifyTestResponse:
      type: object
      additionalProperties: false
      required:
        - oneRequired
      properties:
        oneRequired:
          type: string
          nullable: false
        twoOptional:
          type: string
          nullable: true
        threeOptionalBinaryContent:
          type: string
          format: byte
          nullable: true

The parent ModifyTestObject has following setter generated

public void setTwoOptional(JsonNullable<String> twoOptional) {
    this.twoOptional = twoOptional;
}

but the child object takes List instead and passes it to the super method.

public ModifyNamedTestRequest twoOptional(String twoOptional) {
    super.setTwoOptional(twoOptional);
    return this;
}

Expected solutions

Either ModifyNamedTestRequest::twoOptional should not be generated at all, or twoOptional should take JsonNullable<String> as its parameter.

Related Issues

Originally posted by @jspetrak in https://github.com/OpenAPITools/openapi-generator/issues/4128#issuecomment-1560978293

14765 [BUG][Java][Spring] openapiNullable - JsonNullable usage is based on nullable property instead of required property

14766 is potential fix

Bounty

This is a blocker for OSDM code generation into Java Spring Boot. Bileto is willing to sponsor the resolution of the issue by €500 bounty, other currency equivalent, or extra donation to Open API Collective.

jspetrak commented 1 year ago

Yet present in 7.0.0 beta.

martin-mfg commented 11 months ago

Hi @jspetrak, this issue should be fixed in the latest release, version 7.0.1. The fix was contributed by @robinjhector via https://github.com/OpenAPITools/openapi-generator/pull/16497 .


As an aside, you might want to fix the below lines of your input spec:

      discriminator:
        propertyName: objectType

According to https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

The discriminator is used with anyOf or oneOf keywords only.

and

It is important that all the models mentioned [...] contain the property that the discriminator specifies.

robinjhector commented 11 months ago

Does my fix solve the problem @jspetrak? Otherwise let me know!

jspetrak commented 11 months ago

@martin-mfg @robinjhector I tested the 7.0.1 version against the openapi files and I agree, it now produces compilable code. Thank you!

robinjhector commented 11 months ago

I had no idea this had a bounty on it, and I'm not sure it was still active, otherwise feel free to sponsor my github profile or paypal, or buy me a coffee 😄