Skip to content

Java @Nullable component references lose scoped nullability #3362

Description

@janeklb

Description

For Java DTO properties, Springdoc does not preserve @Nullable when the property type is emitted as a component $ref.

This differs from Springdoc's KotlinNullablePropertyCustomizer, which wraps nullable $ref properties in a composed schema rather than modifying the referenced component.

The result is that a Java API can return null at runtime while the generated OpenAPI document declares a non-nullable property.

Minimal example

public record Outer(@Nullable Inner result) {}

public record Inner(String value) {}

With springdoc.api-docs.version=OPENAPI_3_0, the generated property is:

result:
  $ref: "#/components/schemas/Inner"

Expected OpenAPI 3.0-compatible output:

result:
  allOf:
  - $ref: "#/components/schemas/Inner"
  nullable: true

Runtime impact

The generated OpenAPI 3.0 document currently emits bare $ref properties for all of these fields.

Related issues

Proposed solution

Apply the same scoped wrapper strategy used by KotlinNullablePropertyCustomizer to recognized Java nullable annotations, including jakarta.annotation.Nullable:

  • OAS 3.0: replace the property with allOf: [ { $ref: ... } ] plus nullable: true.
  • OAS 3.1: replace the property with a non-mutating union of the $ref and the JSON Schema null type.

The component schema itself must remain unchanged, because it may be reused by non-nullable properties.

Environment

  • Springdoc: 3.1.0
  • Swagger Core: 2.2.52
  • Spring Boot: 4.1.0
  • Java: 21
  • OpenAPI mode: 3.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions