Skip to content

Commit 1a613ba

Browse files
committed
Merge pull request #22646 from dreis2211
* gh-22646: Polish "Allow DurationFormat and PeriodFormat to be used on parameters" Allow DurationFormat and PeriodFormat to be used on parameters Closes gh-22646
2 parents c7bfc01 + f96a688 commit 1a613ba

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationFormat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -30,7 +30,7 @@
3030
* @author Phillip Webb
3131
* @since 2.0.0
3232
*/
33-
@Target(ElementType.FIELD)
33+
@Target({ ElementType.FIELD, ElementType.PARAMETER })
3434
@Retention(RetentionPolicy.RUNTIME)
3535
@Documented
3636
public @interface DurationFormat {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/PeriodFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @author Edson Chávez
3232
* @since 2.3.0
3333
*/
34-
@Target(ElementType.FIELD)
34+
@Target({ ElementType.FIELD, ElementType.PARAMETER })
3535
@Retention(RetentionPolicy.RUNTIME)
3636
@Documented
3737
public @interface PeriodFormat {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@
5656
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
5757
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
5858
import org.springframework.boot.convert.DataSizeUnit;
59+
import org.springframework.boot.convert.DurationFormat;
60+
import org.springframework.boot.convert.DurationStyle;
5961
import org.springframework.boot.convert.DurationUnit;
62+
import org.springframework.boot.convert.PeriodFormat;
63+
import org.springframework.boot.convert.PeriodStyle;
6064
import org.springframework.boot.convert.PeriodUnit;
6165
import org.springframework.boot.testsupport.system.CapturedOutput;
6266
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
@@ -808,6 +812,51 @@ void loadWhenBindingToConstructorParametersWithDefaultDataUnitShouldBind() {
808812
assertThat(bean.getPeriod()).isEqualTo(Period.ofYears(4));
809813
}
810814

815+
@Test
816+
void loadWhenBindingToConstructorParametersWithCustomDataFormatShouldBind() {
817+
MutablePropertySources sources = this.context.getEnvironment().getPropertySources();
818+
Map<String, Object> source = new HashMap<>();
819+
source.put("test.duration", "12d");
820+
source.put("test.period", "13y");
821+
sources.addLast(new MapPropertySource("test", source));
822+
load(ConstructorParameterWithFormatConfiguration.class);
823+
ConstructorParameterWithFormatProperties bean = this.context
824+
.getBean(ConstructorParameterWithFormatProperties.class);
825+
assertThat(bean.getDuration()).isEqualTo(Duration.ofDays(12));
826+
assertThat(bean.getPeriod()).isEqualTo(Period.ofYears(13));
827+
}
828+
829+
@Test
830+
void loadWhenBindingToConstructorParametersWithNotMatchingCustomDurationFormatShouldFail() {
831+
MutablePropertySources sources = this.context.getEnvironment().getPropertySources();
832+
Map<String, Object> source = new HashMap<>();
833+
source.put("test.duration", "P12D");
834+
sources.addLast(new MapPropertySource("test", source));
835+
assertThatExceptionOfType(Exception.class)
836+
.isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class)).havingCause()
837+
.isInstanceOf(BindException.class);
838+
}
839+
840+
@Test
841+
void loadWhenBindingToConstructorParametersWithNotMatchingCustomPeriodFormatShouldFail() {
842+
MutablePropertySources sources = this.context.getEnvironment().getPropertySources();
843+
Map<String, Object> source = new HashMap<>();
844+
source.put("test.period", "P12D");
845+
sources.addLast(new MapPropertySource("test", source));
846+
assertThatExceptionOfType(Exception.class)
847+
.isThrownBy(() -> load(ConstructorParameterWithFormatConfiguration.class)).havingCause()
848+
.isInstanceOf(BindException.class);
849+
}
850+
851+
@Test
852+
void loadWhenBindingToConstructorParametersWithDefaultDataFormatShouldBind() {
853+
load(ConstructorParameterWithFormatConfiguration.class);
854+
ConstructorParameterWithFormatProperties bean = this.context
855+
.getBean(ConstructorParameterWithFormatProperties.class);
856+
assertThat(bean.getDuration()).isEqualTo(Duration.ofDays(2));
857+
assertThat(bean.getPeriod()).isEqualTo(Period.ofYears(3));
858+
}
859+
811860
@Test
812861
void loadWhenBindingToConstructorParametersShouldValidate() {
813862
assertThatExceptionOfType(Exception.class)
@@ -2007,6 +2056,31 @@ Period getPeriod() {
20072056

20082057
}
20092058

2059+
@ConstructorBinding
2060+
@ConfigurationProperties(prefix = "test")
2061+
static class ConstructorParameterWithFormatProperties {
2062+
2063+
private final Duration duration;
2064+
2065+
private final Period period;
2066+
2067+
ConstructorParameterWithFormatProperties(
2068+
@DefaultValue("2d") @DurationFormat(DurationStyle.SIMPLE) Duration duration,
2069+
@DefaultValue("3y") @PeriodFormat(PeriodStyle.SIMPLE) Period period) {
2070+
this.duration = duration;
2071+
this.period = period;
2072+
}
2073+
2074+
Duration getDuration() {
2075+
return this.duration;
2076+
}
2077+
2078+
Period getPeriod() {
2079+
return this.period;
2080+
}
2081+
2082+
}
2083+
20102084
@ConstructorBinding
20112085
@ConfigurationProperties(prefix = "test")
20122086
@Validated
@@ -2035,6 +2109,11 @@ static class ConstructorParameterWithUnitConfiguration {
20352109

20362110
}
20372111

2112+
@EnableConfigurationProperties(ConstructorParameterWithFormatProperties.class)
2113+
static class ConstructorParameterWithFormatConfiguration {
2114+
2115+
}
2116+
20382117
@EnableConfigurationProperties(ConstructorParameterValidatedProperties.class)
20392118
static class ConstructorParameterValidationConfiguration {
20402119

0 commit comments

Comments
 (0)