OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] Nullable on lists that will never be null (with Jackson)

Open
#20,858 0 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

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

Actual: openapi-generator will put @Nullable on lists, while the array is effectively not nullable and being absent.

Expected: no nullable on a list unless explicitly requested via nullable: true or types: [..., null]

openapi-generator version

7.11.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: foo API
  version: 0.0.1
paths:
  /foo:
    get:
      responses:
        200:
          description: foo
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Response"
components:
  schemas:
    Response:
      properties:
        names:
          type: array
          items:
            type: string
Steps to reproduce
  1. openapi-generator generate -g java --library webclient -i test.yaml
grep -A3 'public class' src/main/java/org/openapitools/client/model/Response.java
public class Response {
  public static final String JSON_PROPERTY_NAMES = "names";
  @javax.annotation.Nullable
  private List<String> names = new ArrayList<>();
  1. you can test that it will not be null with this:
	@Test
	public void testDeserializeResponse() throws Exception {
		String requestBody = "{}";
		Response response = new ObjectMapper().readValue(requestBody, Response.class);

		assertEquals(Collections.emptyList(), response.getNames());
	}
Related issues/PRs
Suggest a fix

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 by running openapi-generator generate -g java --library webclient -i test.yaml and inspect the generated Response.java, especially the names field. Use the provided testDeserializeResponse scenario to verify the generated list remains non-null and that @Nullable is absent unless the schema explicitly permits null.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.