From 43b9f29ee8a34530a01b7d0b5ca0a53e0d01988d Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Sat, 3 Apr 2021 17:04:55 +0200 Subject: [PATCH] #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`. --- .../hateoas/mediatype/PropertyUtils.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/mediatype/PropertyUtils.java b/src/main/java/org/springframework/hateoas/mediatype/PropertyUtils.java index 85aedfef..e3b1676f 100644 --- a/src/main/java/org/springframework/hateoas/mediatype/PropertyUtils.java +++ b/src/main/java/org/springframework/hateoas/mediatype/PropertyUtils.java @@ -539,7 +539,7 @@ public class PropertyUtils { private static class Jsr303AwarePropertyMetadata extends DefaultPropertyMetadata { private static final Optional> LENGTH_ANNOTATION; - private static final Class URL_ANNOTATION, RANGE_ANNOTATION; + private static final @Nullable Class URL_ANNOTATION, RANGE_ANNOTATION; private static final Map, String> TYPE_MAP; static { @@ -609,10 +609,13 @@ public class PropertyUtils { @Override public Long getMin() { - Optional attribute = getAnnotationAttribute(RANGE_ANNOTATION, "min", Long.class); + if (RANGE_ANNOTATION != null) { - if (attribute.isPresent()) { - return attribute.get(); + Optional attribute = getAnnotationAttribute(RANGE_ANNOTATION, "min", Long.class); + + if (attribute.isPresent()) { + return attribute.get(); + } } return getAnnotationAttribute(Min.class, "value", Long.class).orElse(null); @@ -626,10 +629,13 @@ public class PropertyUtils { @Override public Long getMax() { - Optional attribute = getAnnotationAttribute(RANGE_ANNOTATION, "max", Long.class); + if (RANGE_ANNOTATION != null) { - if (attribute.isPresent()) { - return attribute.get(); + Optional attribute = getAnnotationAttribute(RANGE_ANNOTATION, "max", Long.class); + + if (attribute.isPresent()) { + return attribute.get(); + } } return getAnnotationAttribute(Max.class, "value", Long.class).orElse(null);