openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA][Spring] with jspecify - non-required field with default should be annotated with @Nullable

Open gbataille opened this issue 4 months ago • 4 comments

Bug Report Checklist

  • [ ] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When jspecify is in use, when a field is defined as not required in openApi, it should always be mapped to a @Nullable type pojo. Even if there is a defaultValue in the openApi spec, the caller can always force the value to null. The defaultValue applies to properties that are not passed, not to properties that are passed explicitly as null.

openapi-generator version

7.22.0. not a regression.

OpenAPI declaration file content or url
components:
  schemas:
    CobJobDto:
      type: object
      required: [ date ]
      properties:
        date:
          description: The Business Day to consider for the COB job.
          type: string
          format: date
          example: "2021-01-30"
        force:
          description: Force a new job to start whatever is the state of the previous.
          type: boolean
          default: false
          example: true
        synchronous:
          description: Wait for the end of the batch.
          type: boolean
          default: false
          example: true

Generation Details

generated through Maven. The DTO looks like this

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2026-07-14T08:25:00.429547+02:00[Europe/Paris]", comments = "Generator version: 7.22.0")
public class CobJobDto {

  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
  private LocalDate date;

  private Boolean force = false;

  private Boolean synchronous = false;

I expect it to look like this

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2026-07-14T08:25:00.429547+02:00[Europe/Paris]", comments = "Generator version: 7.22.0")
public class CobJobDto {

  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
  private LocalDate date;

  private @org.jspecify.annotations.Nullable Boolean force = false;

  private @org.jspecify.annotations.Nullable Boolean synchronous = false;
Steps to reproduce
Related issues/PRs
Suggest a fix

In the nullableAnnotation.mustache, I think the fact that we put the @Nullable annotation or not should not depend on the defaultValue flag

gbataille avatar Jul 14 '26 06:07 gbataille

I reproduced this on latest master (7.25.0-SNAPSHOT, built locally) with spring, useJspecify=true, openApiNullable=false, useSpringBoot3=true. A non-required boolean with default: false generates:

@JsonInclude(JsonInclude.Include.NON_NULL)
private Boolean force = false;

@JsonSetter(nulls = Nulls.SKIP)
@JsonProperty("force")
public void setForce(Boolean force) {

After looking at the generated code I am not sure the current typing is wrong. Three things keep the field from ever being null:

  1. It is initialized to the default, so it never starts null.
  2. The setter has @JsonSetter(nulls = Nulls.SKIP), so an explicit JSON null is skipped and the default is kept. Jackson cannot write null into the field.
  3. The package is @NullMarked, so the setter and fluent setter parameters are non-null and analyzers (NullAway, IntelliJ) stop Java callers from passing null.

So under jspecify rules the field really is never null, and the non-null getter seems correct. Making it @Nullable would force every consumer of getForce() to null check a value that cannot legally be null.

The case where an explicit null should be accepted and stored is nullable: true, and that already generates @Nullable.

Am I missing a path where the field can become null? If not, this may be working as intended. Would be good to have a maintainer confirm either way.

SubhamAshok avatar Aug 02 '26 11:08 SubhamAshok

Hey,

so I have not retested yet. we are on openapi-generator 7.23.0 I definetely don't have all these annotations popping up. I need to dig further if it's a change between 7.23.0 and 7.25.0 or if there might be something else different in our call to the generator 7.25.0

gbataille avatar Aug 03 '26 07:08 gbataille

Version difference, not your config. @JsonInclude and @JsonSetter(nulls = Nulls.SKIP) came in v7.24.0 via #23993. 7.23.0 does not have them.

And #24563 (in review) makes your case @Nullable. Tested your example on that branch:

private @Nullable Boolean force = false;
public void setForce(@Nullable Boolean force)

Resolved once #24563 merges.

SubhamAshok avatar Aug 03 '26 18:08 SubhamAshok

ok sorry. I had not followed. I'll try to test this week. thanks.

gbataille avatar Aug 04 '26 03:08 gbataille