OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java/spring] useOptional:true is applied only if openApiNullable:true

Open
#20,407 22 comments 3 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)
OpenAPI declaration file content or url
openapi: 3.0.0
components:
  schemas:
    Item:
      type: object
      properties:
        foo:
          type: string
Description

Assume we have a very minimal specification (see above) and use Java Spring Boot generator.

With useOptional:true everything is fine i.e.

public class Item {
  private Optional<String> foo = Optional.empty();

  public Item foo(String foo) {
    this.foo = Optional.ofNullable(foo);
    return this;
  }

  @JsonProperty("foo")
  public Optional<String> getFoo() {
    return foo;
  }

  public void setFoo(Optional<String> foo) {
    this.foo = foo;
  }
}

... but Optional wrappers are applied only if openApiNullable:true.
Luckily, it is a default behavior. Still, if I explicitly turn it off openApiNullable:false then useOptional:true doesn't have any effect

public class Item {
  private String foo;

  public Item foo(String foo) {
    this.foo = foo;
    return this;
  }

  @JsonProperty("foo")
  public String getFoo() {
    return foo;
  }

  public void setFoo(String foo) {
    this.foo = foo;
  }
}

I guess it is a bug. The output should be the very same as above.

Or am I missing an idea behind it? Is it written somewhere in documentation that useOptional makes sense only together with openApiNullable?

@MelleD as you seem to be an expert of using Optional, what are your thoughts of it?
Should I provide a PR to fix?

openapi-generator version

7.10.0

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 Spring Boot generator and reproduce the issue using the minimal OpenAPI schema in the report. Compare generated Item models with useOptional:true while toggling openApiNullable. Done means Optional wrappers are generated when useOptional is enabled, regardless of the openApiNullable setting, with coverage for both option combinations.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring-boot
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.