Skip to content

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

@valmoz

Description

@valmoz

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions