Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Nullable annotation is not copied to builder #3785

Open
lbenedetto opened this issue Nov 21, 2024 · 0 comments
Open

[BUG] Nullable annotation is not copied to builder #3785

lbenedetto opened this issue Nov 21, 2024 · 0 comments

Comments

@lbenedetto
Copy link

lbenedetto commented Nov 21, 2024

Describe the bug
When a builder is generated for a class with a field marked as Nullable, the nullability is not transferred to the builder, causing warnings when trying to build an instance of that object using nullable variables.

To Reproduce
With lombok.addNullAnnotations = spring and a package-info.java with this:

@NonNullApi
package com.example;

import org.springframework.lang.NonNullApi;

Then when you make an object like this:

import org.springframework.lang.Nullable;
import lombok.Builder;

@Builder
public class Thing {
  @Nullable
  String thing;
}

it becomes

import org.springframework.lang.Nullable;

public class Thingy {
  @Nullable
  String thing;

  Thingy(@Nullable String thing) {
    this.thing = thing;
  }

  @org.springframework.lang.NonNull
  public static ThingBuilder builder() {
    return new ThingBuilder();
  }

  public static class ThingyBuilder {
    private String thing;

    ThingyBuilder() {
    }

    @org.springframework.lang.NonNull
    public ThingyBuilder thing(String thing) {
      this.thing = thing;
      return this;
    }

    @org.springframework.lang.NonNull
    public Thingy build() {
      return new Thingy(this.thing);
    }

    @org.springframework.lang.NonNull
    public String toString() {
      return "Thingy.ThingyBuilder(thing=" + this.thing + ")";
    }
  }
}

Expected behavior
Nullable annotation is copied to the builder like so:

import org.springframework.lang.Nullable;

public class Thingy {
  @Nullable
  String thing;

  Thingy(@Nullable String thing) {
    this.thing = thing;
  }

  @org.springframework.lang.NonNull
  public static ThingyBuilder builder() {
    return new ThingyBuilder();
  }

  public static class ThingyBuilder {
    @Nullable
    private String thing;

    ThingyBuilder() {
    }

    @org.springframework.lang.NonNull
    public ThingyBuilder thing(@Nullable String thing) {
      this.thing = thing;
      return this;
    }

    @org.springframework.lang.NonNull
    public Thingy build() {
      return new Thingy(this.thing);
    }

    @org.springframework.lang.NonNull
    public String toString() {
      return "Thingy.ThingyBuilder(thing=" + this.thing + ")";
    }
  }
}

Doing this would not interfere with the implementation of Builder.Default, since you generate a separate field for tracking if the field has already been manually set. With these changes I can do the following without any nullability warnings:

return Thingy.builder()
  .thing(mightBeNull)
  .build()

Currently, I have to do this instead:

var builder = Thingy.builder();
if (mightBeNull != null) {
  builder.thing(mightBeNull);
}
return builder.build();

Version info (please complete the following information):

  • Lombok version: 1.18.36
  • Delombok version (used for examining the generated code): 1.18.4.5
  • Platform 21.0.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant