Skip to content

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

Open
@slobodator

Description

@slobodator

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

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