Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -30,7 +30,7 @@
* @author Phillip Webb
* @since 2.0.0
*/
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DurationFormat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @author Edson Chávez
* @since 2.3.0
*/
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PeriodFormat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.boot.convert.DurationFormat;
import org.springframework.boot.convert.DurationStyle;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.boot.convert.PeriodFormat;
import org.springframework.boot.convert.PeriodStyle;
import org.springframework.boot.convert.PeriodUnit;
import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
Expand Down Expand Up @@ -781,13 +785,16 @@ void loadWhenBindingToConstructorParametersWithCustomDataUnitShouldBind() {
source.put("test.duration", "12");
source.put("test.size", "13");
source.put("test.period", "14");
source.put("test.formattedDuration", "15d");
source.put("test.formattedPeriod", "16y");
sources.addLast(new MapPropertySource("test", source));
load(ConstructorParameterWithUnitConfiguration.class);
ConstructorParameterWithUnitProperties bean = this.context
.getBean(ConstructorParameterWithUnitProperties.class);
assertThat(bean.getDuration()).isEqualTo(Duration.ofDays(12));
assertThat(bean.getSize()).isEqualTo(DataSize.ofMegabytes(13));
assertThat(bean.getPeriod()).isEqualTo(Period.ofYears(14));
assertThat(bean.getFormattedDuration()).isEqualTo(Duration.ofDays(15));
assertThat(bean.getFormattedPeriod()).isEqualTo(Period.ofYears(16));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions succeed without @DurationFormat and @PeriodFormat on ConstructorParameterWithUnitProperties. I think that's because the converter successfully infers the style to use from the value that's being converted. To verify that the format annotations are working as expected, I think it would be necessary to trigger a failure by trying, for example, to bind an ISO-8601 value to a property that requires the simple style.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extended/reworked the testing a bit. Let me know if this is what you had in mind, @wilkinsona

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spot on. Thanks.

}

@Test
Expand Down Expand Up @@ -1985,12 +1992,20 @@ static class ConstructorParameterWithUnitProperties {

private final Period period;

private final Duration formattedDuration;

private final Period formattedPeriod;

ConstructorParameterWithUnitProperties(@DefaultValue("2") @DurationUnit(ChronoUnit.DAYS) Duration duration,
@DefaultValue("3") @DataSizeUnit(DataUnit.MEGABYTES) DataSize size,
@DefaultValue("4") @PeriodUnit(ChronoUnit.YEARS) Period period) {
@DefaultValue("4") @PeriodUnit(ChronoUnit.YEARS) Period period,
@DefaultValue("5") @DurationFormat(DurationStyle.SIMPLE) Duration formattedDuration,
@DefaultValue("6") @PeriodFormat(PeriodStyle.SIMPLE) Period formattedPeriod) {
this.size = size;
this.duration = duration;
this.period = period;
this.formattedDuration = formattedDuration;
this.formattedPeriod = formattedPeriod;
}

Duration getDuration() {
Expand All @@ -2005,6 +2020,14 @@ Period getPeriod() {
return this.period;
}

Duration getFormattedDuration() {
return this.formattedDuration;
}

Period getFormattedPeriod() {
return this.formattedPeriod;
}

}

@ConstructorBinding
Expand Down