Skip to content

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

Description

@gbataille

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions