OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Unable to Set Null Values for Nullable Fields in Generated Java Code

Open
#21,504 4 comments 0 reactions 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

Description

When using the OpenAPI Generator to generate Java client code, nullable fields (defined with type: ["string", "null"]) cannot be set to null in the generated code, resulting in an incorrect request payload. Specifically, fields set to null are omitted from the JSON payload, which is problematic for PATCH requests where a missing field and a null field have distinct meanings.

openapi-generator version

7.13.0

OpenAPI declaration file content or url
{
  "openapi": "3.1.0",
  "info": {
    "title": "API Overview",
    "version": "1.0"
  },
  "servers": [
    {
      "url": "http://test.com",
      "description": "Test Server",
      "variables": {}
    }
  ],
  "paths": {
    "/users/{id}": {
      "parameters": [
        {
          "schema": {
            "type": "string"
          },
          "name": "id",
          "in": "path",
          "required": true,
          "description": "user ID"
        }
      ],
      "patch": {
        "summary": "Update a user by id",
        "operationId": "update-user-by-id",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "phone": {
                    "type": ["string", "null"]
                  },
                  "email": {
                    "type": ["string", "null"]
                  }
                },
                "required": [
                  "id",
                  "email"
                ]
              }
            }
          },
          "description": ""
        },
        "responses": {
          "200": {
            "description": "default response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "tags": [],
        "parameters": []
      }
    }
  },
  "tags": [],
  "components": {
    "schemas": {}
  },
  "security": []
}
Generation Details
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i ./api.json -g java -o ./java_api_client
Steps to reproduce
public class App {
    public static void main(String[] args) {
        ApiClient defaultClient = new ApiClient();
        DefaultApi apiInstance = new DefaultApi(defaultClient);
        try {
            UpdateUserByIdRequest req = new UpdateUserByIdRequest();
            req.setEmail(null);
            req.setPhone(null);
            UpdateUserById200Response resp = apiInstance.updateUserById("123", req);
            // The request payload will be:
            // {}
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
    }
}
Expected Request Payload
{"email": null, "phone": null}
Actual Request Payload
{}

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 generated UpdateUserByIdRequest and ApiClient serialization behavior, using the supplied OpenAPI declaration and generation command. Trace how null-valued email and phone fields are handled before the request payload is sent. Done means the generated PATCH request preserves both fields as explicit JSON null values rather than omitting them.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.