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

[BUG][Java][Spring] allOf with nullable produces JsonNullable<Object>

Open iNomaD opened this issue 7 years ago • 6 comments

Description

The generated code for definitions which include allOf annotation with nullable properties produces JsonNullable<Object> when the property type is defined in parent object.

public class UpdateFirmJson   {
  @JsonProperty("firmName")
  private JsonNullable<String> firmName = JsonNullable.undefined();

  @JsonProperty("addressId")
  private JsonNullable<Object> addressId = JsonNullable.undefined();
...
openapi-generator version

4.1.3

OpenAPI declaration file content or url
swagger: "2.0"

info:
  title: Test for JsonNullable
  version: 1.0.0

paths:
  /firm/{firmId}:
    patch:
      tags:
        - firm
      summary: Update a firm
      operationId: updateFirm
      consumes:
        - application/json
      parameters:
        - in: path
          name: firmId
          required: true
          description: "Firm ID"
          type: integer
          format: int64
        - in: body
          name: updateFirm
          description: Field to be updated
          schema:
            $ref: "#/definitions/UpdateFirm"
      responses:
        204:
          description: Updated
  
definitions:
  FirmProperties:
    properties:
      addressId:
        type: string

  UpdateFirm:
    allOf:
      - $ref: '#/definitions/FirmProperties'
      - properties:
          firmName:
            type: string
            x-nullable: true
          addressId:
            x-nullable: true

Command line used for generation

./gradlew openApiGenerate

Steps to reproduce

Just run the generator

Related issues/PRs

Nothing found.

Suggest a fix

Support deriving the type from the parent object, i.e. JsonNullabe<String>.

iNomaD avatar Oct 11 '19 12:10 iNomaD

Same

steklopod avatar Jan 27 '20 07:01 steklopod

I'm seeing this in 4.2.3 too.

Considering a way to re-write the contract so that this doesn't happen.

MinuraIddamalgoda avatar Mar 24 '20 00:03 MinuraIddamalgoda

I'm seeing this on 4.2.3 as well. The problem causes Jackson JSON serialization to generate extra fields for the same field. For example, assume there is a field called foo. Even though you set foo to null it is being serialized as:

{
  "foo": {"present": false}
}

TunaYagci avatar May 07 '20 12:05 TunaYagci

This is reproducible on the latest generator 6.6.0 using openapi specification of version 3.0.3. Below is the sample YML file.

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;
}

jspetrak avatar May 24 '23 11:05 jspetrak

Persisting in 7.0.0 beta

jspetrak avatar Jul 22 '23 09:07 jspetrak

Still reproduces on latest master, also with OpenAPI 3.0 syntax (nullable: true without repeating the type). Root cause: the allOf property merge replaces the typed schema with the constraint-only one. Fix in #24586.

For swagger 2.0 specs like the original report: the 2.0 to 3.0 converter itself rewrites the property to type: object, so the type is lost before the generator runs. Converting the spec to 3.0 avoids that.

SubhamAshok avatar Aug 03 '26 20:08 SubhamAshok