From 5ee3fdab778dfc0e8a3821cbe4ae4563d95d5a1b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 4 Mar 2022 12:16:27 +0000 Subject: [PATCH] Polish "Allow @DefaultValue to be used on record components" See gh-29010 --- .../src/docs/asciidoc/features/external-config.adoc | 3 ++- .../ConfigurationMetadataAnnotationProcessorTests.java | 1 - .../boot/configurationsample/DefaultValue.java | 2 +- .../boot/context/properties/bind/ValueObjectBinderTests.java | 5 +---- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc index f23e8bc48a..2c971f3220 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc @@ -719,7 +719,8 @@ Unless your record has multiple constructors, there is no need to use `@Construc Nested members of a constructor bound class (such as `Security` in the example above) will also be bound through their constructor. -Default values can be specified using `@DefaultValue` and the same conversion service will be applied to coerce the `String` value to the target type of a missing property. +Default values can be specified using `@DefaultValue` on constructor parameters and record components. +The conversion service will be applied to coerce the annotation's `String` value to the target type of a missing property. By default, if no properties are bound to `Security`, the `MyProperties` instance will contain a `null` value for `security`. If you wish you return a non-null instance of `Security` even when no properties are bound to it, you can use an empty `@DefaultValue` annotation to do so: diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java index 381b7bf6ab..60eff8fb19 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java @@ -447,7 +447,6 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene } @Test - @EnabledForJreRange(min = JRE.JAVA_16) void recordPropertiesWithDefaultValues(@TempDir File temp) throws IOException { File exampleRecord = new File(temp, "ExampleRecord.java"); try (PrintWriter writer = new PrintWriter(new FileWriter(exampleRecord))) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/DefaultValue.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/DefaultValue.java index 5bdd5c8313..071e8a20ce 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/DefaultValue.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/DefaultValue.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. diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.java index b0628e2472..97bce00960 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.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,8 +33,6 @@ import java.util.Map; import java.util.Objects; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.EnabledForJreRange; -import org.junit.jupiter.api.condition.JRE; import org.junit.jupiter.api.io.TempDir; import org.springframework.boot.context.properties.source.ConfigurationPropertyName; @@ -371,7 +369,6 @@ class ValueObjectBinderTests { } @Test - @EnabledForJreRange(min = JRE.JAVA_16) void bindToRecordWithDefaultValue(@TempDir File tempDir) throws IOException, ClassNotFoundException { MockConfigurationPropertySource source = new MockConfigurationPropertySource(); source.put("test.record.property1", "value-from-config-1");