From f7d181e65b2104f3f61172cac4a166e625409682 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 14 Apr 2022 18:36:15 +0100 Subject: [PATCH] Handle null additional properties more gracefully Closes gh-30654 --- .../boot/gradle/tasks/buildinfo/BuildInfo.java | 4 ++-- .../boot/gradle/tasks/buildinfo/BuildInfoTests.java | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) 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 2246248790fd..c66453b1ad09 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-2020 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. @@ -110,7 +110,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 61d2629dfd22..5c601c4bc9e7 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}. @@ -129,6 +130,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();