OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java] Nullable field failing when explicitly set to "null"

Open
#12,549 23 comments 5 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

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

In our OpenAPI spec, we have a model with a nullable field called "prev_page_url".
If we test the generated code (with generator v6.0.0) omitting the parameter, it works correctly.

If instead we test setting the field explicitly to null we obtain the following error:

Expected the field `prev_page_url` to be a primitive type in the JSON string but got `null`
openapi-generator version

v6.0.0

OpenAPI declaration file content or URL

The parameter is defined here, and the main spec can be found here

title: Pagination
type: object
description: ''
properties:
  current_page:
    type: integer
    description: Current page number.
    nullable: true
  first_page_url:
    type: string
    format: uri
    description: First page url.
    nullable: true
  from:
    type: integer
    nullable: true
    description: First result of the page.
  last_page:
    type: integer
    description: Last page number.
    nullable: true
  last_page_url:
    type: string
    format: uri
    description: Last page url.
    nullable: true
  next_page_url:
    type: string
    format: uri
    nullable: true
    description: Next page url
  path:
    type: string
    format: uri
    description: Request path.
    nullable: true
  per_page:
    type: integer
    description: Number of result per page.
    nullable: true
  prev_page_url:
    type: string
    format: uri
    nullable: true
    description: Previous page url.
  to:
    type: integer
    nullable: true
    description: Last result of the page.
  total:
    type: integer
    description: Total number of results
    nullable: true
Generation Details

Our github action can be found here
openapi-generator-cli generate -i ./openapi.yaml -g java -o ./generated/java/

Steps to reproduce

obtain a JSON response with the parameter set as follows:

{
  "prev_page_url":null
}
Related issues/PRs

I couldn't find any...

Suggest a fix

In this case, Gson doesn't set the param to null, but to an instance of JsonNull class.
To check it should be able to change the pojo.mustache template in this way:

if (({{^isRequired}}jsonObj.get("{{{baseName}}}") != null && {{^isRequired}}jsonObj.get("{{{baseName}}}").isJsonNull()) && {{/isRequired}}!jsonObj.get("{{{baseName}}}").isJsonPrimitive()) {

If you want, I can try to prepare a PR for this issue.

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 Java generator's pojo.mustache template and the nullable prev_page_url definition in Pagination.yaml. Regenerate the Java client with the documented openapi-generator-cli command, then verify that a response containing "prev_page_url": null is accepted without the primitive-type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.