diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java index f48226e5d843..4728caf87dc1 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 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. @@ -108,7 +108,7 @@ public void properties(Action action) { private Map coerceToStringValues(Map input) { Map output = new HashMap<>(); - input.forEach((key, value) -> output.put(key, value.toString())); + input.forEach((key, value) -> output.put(key, (value != null) ? value.toString() : null)); return output; } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoTests.java index 441ebd4d2459..e1c86f4ab761 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 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. @@ -33,6 +33,7 @@ import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Tests for {@link BuildInfo}. @@ -186,6 +187,14 @@ void additionalPropertiesAreReflectedInProperties() { assertThat(buildInfoProperties(task)).containsEntry("build.b", "bravo"); } + @Test + void nullAdditionalPropertyProducesInformativeFailure() { + BuildInfo task = createTask(createProject("test")); + task.getProperties().getAdditional().put("a", null); + assertThatThrownBy(() -> buildInfoProperties(task)) + .hasMessage("Additional property 'a' is illegal as its value is null"); + } + private Project createProject(String projectName) { File projectDir = new File(this.temp, projectName); Project project = GradleProjectBuilder.builder().withProjectDir(projectDir).withName(projectName).build();