From b58877fc3b9129c16d7c23f1031d93e7c07aaf88 Mon Sep 17 00:00:00 2001 From: "NAMCK\\euz1e4r" Date: Thu, 24 May 2018 16:13:45 -0700 Subject: [PATCH] #1394 Filter out null auth holder entity extension values. --- .../model/AuthenticationHolderEntity.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/openid-connect-common/src/main/java/org/mitre/oauth2/model/AuthenticationHolderEntity.java b/openid-connect-common/src/main/java/org/mitre/oauth2/model/AuthenticationHolderEntity.java index 28accd47e..7561637c6 100644 --- a/openid-connect-common/src/main/java/org/mitre/oauth2/model/AuthenticationHolderEntity.java +++ b/openid-connect-common/src/main/java/org/mitre/oauth2/model/AuthenticationHolderEntity.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import javax.persistence.Basic; @@ -255,7 +256,7 @@ public class AuthenticationHolderEntity { @MapKeyColumn(name="extension") @Convert(converter=SerializableStringConverter.class) public Map getExtensions() { - return extensions; + return filterNullValues(extensions); } /** @@ -322,6 +323,41 @@ public class AuthenticationHolderEntity { this.requestParameters = requestParameters; } - + /** + * Filter out null values from a map.
+ * A local utility method.
+ * Default visibility allows individual unit test.
+ * Creates a new map only if needed. + * If the input map is clean, it is simply returned unchanged.
+ * Motivated by observing that a null value causes a failure when + * fetched from the database by JPA. + * @param input + * @return a map guaranteed to contain null values, + * either the original map or a replacement with null values omitted. + */ + static Map filterNullValues(Map input) { + if (input == null) { + return null; // a bit of bullet proofing + } + // Test for null value. + boolean hasNull = false; + for (Entry entry : input.entrySet()) { + if (entry.getValue() == null) { + hasNull = true; // found one + break; + } + } + if (hasNull == false) { + return input; // no change needed; nothing to do + } + // construct a new map, omitting null values + Map output = new HashMap<>(input.size()); + for (Entry entry : input.entrySet()) { + if (entry.getValue() != null) { + output.put(entry.getKey(), entry.getValue()); + } + } + return output; + } }