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

ConditionMessage#items throws an NPE with a null list of items although the Javadoc states it is tolerated #22344

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -381,7 +381,8 @@ public ConditionMessage items(Style style, Collection<?> items) {
Assert.notNull(style, "Style must not be null");
StringBuilder message = new StringBuilder(this.reason);
items = style.applyTo(items);
if ((this.condition == null || items.size() <= 1) && StringUtils.hasLength(this.singular)) {
if ((this.condition == null || items == null || items.size() <= 1)
&& StringUtils.hasLength(this.singular)) {
message.append(" ").append(this.singular);
}
else if (StringUtils.hasLength(this.plural)) {
Expand Down Expand Up @@ -420,6 +421,9 @@ protected String applyToItem(Object item) {
};

public Collection<?> applyTo(Collection<?> items) {
if (items == null) {
return null;
}
List<Object> result = new ArrayList<>(items.size());
for (Object item : items) {
result.add(applyToItem(item));
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.condition;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -188,4 +189,19 @@ void availableShouldConstructMessage() {
assertThat(message.toString()).isEqualTo("@Test JMX is available");
}

@Test
void itemsTolerateNullInput() {
Collection<?> items = null;
ConditionMessage message = ConditionMessage.forCondition(Test.class).didNotFind("item").items(items);
assertThat(message.toString()).isEqualTo("@Test did not find item");
}

@Test
void quotedItemsTolerateNullInput() {
Collection<?> items = null;
ConditionMessage message = ConditionMessage.forCondition(Test.class).didNotFind("item").items(Style.QUOTE,
items);
assertThat(message.toString()).isEqualTo("@Test did not find item");
}

}