Skip to content

Commit 43b9f29

Browse files
committed
#1507 - Fix potential NullPointerException in Jsr303AwarePropertyMetadata.
We now check whether we could really load `@Range`, which is only available in Hibernate Validator. An arrangement in which the Validation API JAR is on the classpath but not Hibernate Validator would let us end up with the relevant instance being `null` and need a check for before being handed into code that requires the type to not be `null`.
1 parent 107fcfa commit 43b9f29

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ protected String getAnnotatedInputType() {
539539
private static class Jsr303AwarePropertyMetadata extends DefaultPropertyMetadata {
540540

541541
private static final Optional<Class<? extends Annotation>> LENGTH_ANNOTATION;
542-
private static final Class<? extends Annotation> URL_ANNOTATION, RANGE_ANNOTATION;
542+
private static final @Nullable Class<? extends Annotation> URL_ANNOTATION, RANGE_ANNOTATION;
543543
private static final Map<Class<? extends Annotation>, String> TYPE_MAP;
544544

545545
static {
@@ -609,10 +609,13 @@ public Optional<String> getPattern() {
609609
@Override
610610
public Long getMin() {
611611

612-
Optional<Long> attribute = getAnnotationAttribute(RANGE_ANNOTATION, "min", Long.class);
612+
if (RANGE_ANNOTATION != null) {
613+
614+
Optional<Long> attribute = getAnnotationAttribute(RANGE_ANNOTATION, "min", Long.class);
613615

614-
if (attribute.isPresent()) {
615-
return attribute.get();
616+
if (attribute.isPresent()) {
617+
return attribute.get();
618+
}
616619
}
617620

618621
return getAnnotationAttribute(Min.class, "value", Long.class).orElse(null);
@@ -626,10 +629,13 @@ public Long getMin() {
626629
@Override
627630
public Long getMax() {
628631

629-
Optional<Long> attribute = getAnnotationAttribute(RANGE_ANNOTATION, "max", Long.class);
632+
if (RANGE_ANNOTATION != null) {
633+
634+
Optional<Long> attribute = getAnnotationAttribute(RANGE_ANNOTATION, "max", Long.class);
630635

631-
if (attribute.isPresent()) {
632-
return attribute.get();
636+
if (attribute.isPresent()) {
637+
return attribute.get();
638+
}
633639
}
634640

635641
return getAnnotationAttribute(Max.class, "value", Long.class).orElse(null);

0 commit comments

Comments
 (0)