Using a ListBox for a nullable value

28 views
Skip to first unread message

Tim Van Holder

unread,
Aug 11, 2024, 5:11:20 PM8/11/24
to jenkin...@googlegroups.com
Hi,

I have a property that is a Boolean, where null is a distinct and important value ("inherit from parent context").

I currently have code like this

    @NonNull
    @POST
    public ListBoxModel doFillFooItems(@CheckForNull @AncestorInPath Item item) {
      if (item != null) {
        item.checkPermission(Item.CONFIGURE);
      }
      final ListBoxModel model = new ListBoxModel();
      model.add("description 1", null);
      model.add("description 2", "true");
      model.add("description 3", "false");
      return model;
    }

which works exactly as I want it to, setting the Boolean property to null, true or false.

However, this is hitting a SpotBugs check, claiming that the value in that add() call must not be null.
But if I change it to another value, like "", it breaks - that sets the property to false.

So I am wondering how I resolve this - is that value really not supposed to be null (especially given that it works fine)?
If so, that means the code is unsafe and I should not disable the SpotBugs diagnostic (if it would even let me). Is there a way around it? Like can I configure my own string-to-value mapping method for the property (which could map "" to null)?

And if it's not really required to be non-null, should I file a PR to change the annotation for the parameter? But then that would mean the SpotBugs failure would not go away unless I set the required Jenkins version to the latest, which isn't exactly desirable either.

The strange thing is that I don't really remember seeing this issue two years ago when I originally wrote that code.
Looking at the history, the annotations were added in https://github.com/jenkinsci/jenkins/pull/5930 - but that just adds @Nonull to the value without really justifying why null is bad for it.

Jesse Glick

unread,
Aug 12, 2024, 7:26:21 AM8/12/24
to jenkin...@googlegroups.com
On Sun, Aug 11, 2024 at 5:11 PM Tim Van Holder <tim.va...@gmail.com> wrote:
this is hitting a SpotBugs check, claiming that the value in that add() call must not be null.

Names and values in list boxes are expected to non-null strings. If your property’s GUI is a select, its type should be `String` not `Boolean`.
Reply all
Reply to author
Forward
0 new messages