OpenAPITools / OpenAPITools/openapi-generator

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

Open
#17,538 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied OpenAPI declaration and reproduce generation for the User model using version 7.2.0, then inspect the generated Java RestUser/User model behavior for omitted and nullable phoneNumber values. Compare the serialized output with the expected null value and verify both non-required and nullable cases after the behavior is corrected.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.