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
20.51k stars 6.27k forks source link

[BUG][Java/spring] Incorrect Handling of Nullable Fields in OpenAPI Generator #17538

Open Kavan72 opened 5 months ago

Kavan72 commented 5 months ago

Bug Report Checklist

Description

Essentially, I have one endpoint to fetch user data (see below). What I'm attempting to achieve is to make phoneNumber optional, or set it as nullable: true. Therefore, I have two options: 1) Omit mentioning that field in the required section. 2) Use nullable: true at the field level.

In addition, I have a method that converts a UserDTO (database object) to a RestUser (generated model). Take a look at this method:

public static RestUser toRestUser(UserDTO userDTO) {

    return new RestUser()
        .id(userDTO.getId())
        .name(userDTO.getName())
        .phoneNumber(userDTO.getPhoneNumber())
        .email(userDTO.getEmailAddress());
}

1) If I don't specify that field as required in Swagger, the generator will create that field with Optional.of. At that point, if my value is null, it will throw a NullPointerException. In my opinion, we should use Optional.ofNullable for non-required fields. 2) If I use nullable: true on the field, I get a completely different value. If I pass a value to a nullable field, I receive this below result.

Actual output

However, this is incorrect why getting "present": true; the corrected value should be null if i didn't pass the value.

{
  "id": 1,
  "name": "John",
  "phoneNumber": {
    "present": true
  },
  "mail": "test@gmail.com"
}
Expected output
{
  "id": 1,
  "name": "John",
  "phoneNumber": null,
  "mail": "test@gmail.com"
}
openapi-generator version

7.2.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Backend
  version: 1.0.0

paths:
  /users:
    get:
      tags:
       - user
      summary: get.users
      description: get all users.
      responses:
        200:
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'

components:
  schemas:
    User:
      type: object
      properties:
        id: 
          type: integer
        name:
          type: string
        phoneNumber:
          type: integer
          format: int64
          nullable: true
        email:
          type: string
Kavan72 commented 5 months ago

@wing328 can you have a look on this issue ? I'm ready to contribute on this issue.

Moribund7 commented 5 months ago

@Kavan72 @wing328 I'm facing the same issue right now and I'm also willing to contribute here.

MelleD commented 1 month ago

If I don't specify that field as required in Swagger, the generator will create that field with Optional.of. At that point, if my value is null, it will throw a NullPointerException. In my opinion, we should use Optional.ofNullable for non-required fields.

For me the handling is absolutely correct. I see no reason to create empty optionals to act with null values.

That was also a conscious decision for the current implementation and Jackson is doing it correctly so far. If so, you only need this shortcut in your own code and there are other ways.

For this reason it should be a setting if at all.

However, this is incorrect why getting "present": true; the corrected value should be null if i didn't pass the value.

This is because you have set your Jackson Objectmapper incorrectly. You already need the Java 8 module to trade optionals. see https://www.baeldung.com/jackson-optional

Expected output

Your expected output depends on you Jackson setting. There is a setting what should happen when the field is absent: a) is visible with null b) is absent in the payload

Here you have all settings: https://github.com/FasterXML/jackson-databind/wiki/Mapper-Features and examples https://www.baeldung.com/spring-boot-customize-jackson-objectmapper

Kavan72 commented 1 month ago

If I don't specify that field as required in Swagger, the generator will create that field with Optional.of. At that point, if my value is null, it will throw a NullPointerException. In my opinion, we should use Optional.ofNullable for non-required fields.

For me the handling is absolutely correct. I see no reason to create empty optionals to act with null values.

That was also a conscious decision for the current implementation and Jackson is doing it correctly so far. If so, you only need this shortcut in your own code and there are other ways.

For this reason it should be a setting if at all.

However, this is incorrect why getting "present": true; the corrected value should be null if i didn't pass the value.

This is because you have set your Jackson Objectmapper incorrectly. You already need the Java 8 module to trade optionals. see https://www.baeldung.com/jackson-optional

Expected output

Your expected output depends on you Jackson setting. There is a setting what should happen when the field is absent: a) is visible with null b) is absent in the payload

Here you have all settings: https://github.com/FasterXML/jackson-databind/wiki/Mapper-Features and examples https://www.baeldung.com/spring-boot-customize-jackson-objectmapper

https://github.com/OpenAPITools/openapi-generator/issues/14765#issuecomment-1437450306

MelleD commented 1 month ago

https://github.com/OpenAPITools/openapi-generator/issues/14765#issuecomment-1570215549 https://github.com/OpenAPITools/openapi-generator/issues/14765#issuecomment-1948266832 https://github.com/OpenAPITools/openapi-generator/issues/14765#issuecomment-1948357891

And off course: https://github.com/OpenAPITools/openapi-generator/pull/17202 https://github.com/OpenAPITools/openapi-generator/pull/17202#discussion_r1407664380

And the whole discussion is of no use if your Jackson incorrectly serializes an Optional/JsonNullable. You have to set the settings correctly to have a good JSON