OpenAPITools / OpenAPITools/openapi-generator

[REQ][spring] Null-Safety annotations

Open
#14,427 11 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Manage null safety for object properties with Spring annotations (@NonNull, @Nullable).
Example: https://www.baeldung.com/spring-null-safety-annotations

Describe the solution you'd like

I'd like to add a config option useSpringNullSafety for the spring generator (false by default), to add Spring Null-Safety annotations (either @NonNull or@Nullable, depending of the nullable attribute of the OAS model).

Example:

Pet:
  properties:
    name:
       type: string
       example: 'scooby-doo'
       nullable: false
    breed:
        type: string
        example: 'German mastiff'
        nullable: true

should generate a model:

import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Pet {

  @JsonProperty("name")
  @NonNull
  private String name;

  @JsonProperty("breed")
  @Nullable
  private String breed = null;

  public Pet name(@NonNull String name) {
    this.name = name;
    return this;
  }

  /**
   * Get name
   * @return name
  */
  
  @Schema(name = "name", example = "scooby-doo", required = false)
  @NonNull
  public String getName() {
    return name;
  }

  public void setName(@NonNull String name) {
    this.name = name;
  }

  public Pet breed(@Nullable String breed) {
    this.breed = breed;
    return this;
  }

  /**
   * Get breed
   * @return breed
  */
  
  @Schema(name = "breed", example = "German mastiff", required = false)
  @Nullable
  public String getBreed() {
    return breed;
  }

  public void setBreed(@Nullable String breed) {
    this.breed = breed;
  }

 ....
}

Describe alternatives you've considered

Maybe it should be similar to the useOptional config option, so that it should be based on the required OpenAPI attribute.

Additional context

It is better for Kotlin interoparibilty. As it is mentionned in Kotlin reference, Java object are natively resolved as platform type.
With contextual annotations, the Kotlin compiler can provide null-safety type checking.

https://kotlinlang.org/docs/java-interop.html#null-safety-and-platform-types

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 by locating the Spring generator's useOptional configuration and the model-generation entry points, then trace how OpenAPI nullable and required properties are handled. Done means an opt-in useSpringNullSafety setting generates the requested annotations consistently for fields, accessors, and mutators, with coverage for nullable and non-nullable models.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.