Skip to content

Commit 6e2ce91

Browse files
committed
Allow @name to be used on record components
It's not actually necessary, but it's better to align with `@DefaultValue`. See spring-projects#29010 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 719af68 commit 6e2ce91

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
* @author Pavel Anisimov
8585
* @author Scott Frederick
8686
* @author Moritz Halbritter
87+
* @author Yanming Zhou
8788
*/
8889
class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGenerationTests {
8990

@@ -492,6 +493,32 @@ public record ExampleRecord(String someString, Integer someInteger) {
492493
assertThat(metadata).has(Metadata.withProperty("implicit.some-integer"));
493494
}
494495

496+
@Test
497+
void recordPropertiesWithName() {
498+
String source = """
499+
@org.springframework.boot.configurationsample.ConfigurationProperties("implicit")
500+
public record ExampleRecord(
501+
@org.springframework.boot.configurationsample.Name("for") String forString) {
502+
}
503+
""";
504+
ConfigurationMetadata metadata = compile(source);
505+
assertThat(metadata).has(Metadata.withProperty("implicit.for"));
506+
}
507+
508+
@Test
509+
void recordPropertiesWithNameAndDefaultValue() {
510+
String source = """
511+
@org.springframework.boot.configurationsample.ConfigurationProperties("implicit")
512+
public record ExampleRecord(
513+
@org.springframework.boot.configurationsample.Name("for")
514+
@org.springframework.boot.configurationsample.DefaultValue("example")
515+
String forString) {
516+
}
517+
""";
518+
ConfigurationMetadata metadata = compile(source);
519+
assertThat(metadata).has(Metadata.withProperty("implicit.for", String.class).withDefaultValue("example"));
520+
}
521+
495522
@Test
496523
void recordPropertiesWithDefaultValues() {
497524
String source = """

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationsample/Name.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,8 +27,9 @@
2727
* dependency on the real annotation).
2828
*
2929
* @author Phillip Webb
30+
* @author Yanming Zhou
3031
*/
31-
@Target({ ElementType.PARAMETER, ElementType.FIELD })
32+
@Target({ ElementType.FIELD, ElementType.PARAMETER })
3233
@Retention(RetentionPolicy.RUNTIME)
3334
@Documented
3435
public @interface Name {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/Name.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,10 +32,11 @@
3232
*
3333
* @author Phillip Webb
3434
* @author Lasse Wulff
35+
* @author Yanming Zhou
3536
* @since 2.4.0
3637
*/
3738
@Retention(RetentionPolicy.RUNTIME)
38-
@Target({ ElementType.FIELD, ElementType.PARAMETER })
39+
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.RECORD_COMPONENT })
3940
@Documented
4041
public @interface Name {
4142

0 commit comments

Comments
 (0)