Skip to content

Commit aeac73b

Browse files
committed
#1920 - Support for JSR-303 @SiZe annotation in property metadata.
Using the JSR-303 @SiZe annotation is now reflected in the property being considered for input type range as well as exposed min and max values.
1 parent 71b27c4 commit aeac73b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/main/java/org/springframework/hateoas/mediatype/PropertyUtils.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.validation.constraints.NotBlank;
3535
import javax.validation.constraints.NotNull;
3636
import javax.validation.constraints.Pattern;
37+
import javax.validation.constraints.Size;
3738

3839
import org.reactivestreams.Publisher;
3940
import org.springframework.beans.BeanUtils;
@@ -558,6 +559,7 @@ private static class Jsr303AwarePropertyMetadata extends DefaultPropertyMetadata
558559

559560
Map<Class<? extends Annotation>, String> typeMap = new HashMap<>();
560561
typeMap.put(Email.class, "email");
562+
typeMap.put(Size.class, "range");
561563

562564
if (URL_ANNOTATION != null) {
563565
typeMap.put(URL_ANNOTATION, "url");
@@ -617,8 +619,9 @@ public Optional<String> getPattern() {
617619
@Override
618620
public Number getMin() {
619621

620-
return Optional.ofNullable(RANGE_ANNOTATION) //
621-
.flatMap(it -> getAnnotationAttribute(it, "min", Number.class)) //
622+
return getAnnotationAttribute(Size.class, "min", Number.class) //
623+
.or(() -> Optional.ofNullable(RANGE_ANNOTATION)
624+
.flatMap(it -> getAnnotationAttribute(it, "min", Number.class))) //
622625
.or(() -> getAnnotationAttribute(Min.class, "value", Number.class)) //
623626
.or(() -> parsePropertyAnnotationValue(DecimalMin.class)) //
624627
.orElse(null);
@@ -632,8 +635,9 @@ public Number getMin() {
632635
@Override
633636
public Number getMax() {
634637

635-
return Optional.ofNullable(RANGE_ANNOTATION) //
636-
.flatMap(it -> getAnnotationAttribute(it, "max", Number.class)) //
638+
return getAnnotationAttribute(Size.class, "max", Number.class) //
639+
.or(() -> Optional.ofNullable(RANGE_ANNOTATION)
640+
.flatMap(it -> getAnnotationAttribute(it, "max", Number.class))) //
637641
.or(() -> getAnnotationAttribute(Max.class, "value", Number.class)) //
638642
.or(() -> parsePropertyAnnotationValue(DecimalMax.class)) //
639643
.orElse(null);

src/test/java/org/springframework/hateoas/mediatype/PropertyUtilsTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import javax.validation.constraints.NotBlank;
3535
import javax.validation.constraints.NotNull;
3636
import javax.validation.constraints.Pattern;
37+
import javax.validation.constraints.Size;
3738

3839
import org.hibernate.validator.constraints.Range;
3940
import org.hibernate.validator.constraints.URL;
@@ -187,7 +188,8 @@ Stream<DynamicTest> exposesInputTypeForProperties() {
187188
InputTypes.of("url", HtmlInputType.URL), //
188189
InputTypes.of("stringUrl", HtmlInputType.URL), //
189190
InputTypes.of("email", HtmlInputType.EMAIL), //
190-
InputTypes.of("ranged", HtmlInputType.RANGE));
191+
InputTypes.of("ranged", HtmlInputType.RANGE),
192+
InputTypes.of("sized", HtmlInputType.RANGE));
191193

192194
InputPayloadMetadata metadata = PropertyUtils.getExposedProperties(InputTypeSample.class);
193195

@@ -225,6 +227,18 @@ void considersJsr303NotBlankAnnotation() {
225227
});
226228
}
227229

230+
@Test // #1920
231+
void exposesMinAndMaxFromJsr303AtSizeAnnotation() {
232+
233+
InputPayloadMetadata metadata = PropertyUtils.getExposedProperties(Jsr303SamplePayload.class);
234+
Optional<PropertyMetadata> property = metadata.stream().filter(it -> it.getName().equals("sized")).findFirst();
235+
236+
assertThat(property).hasValueSatisfying(it -> {
237+
assertThat(it.getMin()).isEqualTo(41);
238+
assertThat(it.getMax()).isEqualTo(4711);
239+
});
240+
}
241+
228242
@Data
229243
@AllArgsConstructor
230244
@JsonIgnoreProperties({ "ignoreThisProperty" })
@@ -271,6 +285,7 @@ static class Jsr303SamplePayload {
271285
@Pattern(regexp = "\\w") String pattern;
272286
@NotBlank @Pattern(regexp = "\\w") String nonBlankPattern;
273287
TypeAnnotated annotated;
288+
@Size(min = 41, max = 4711) int sized;
274289
}
275290

276291
@Pattern(regexp = "regex")
@@ -316,6 +331,8 @@ static class InputTypeSample {
316331
@URL String stringUrl;
317332

318333
@Range int ranged;
334+
335+
@Size int sized;
319336
}
320337

321338
@Value

0 commit comments

Comments
 (0)