[java-oidc-common] branch main updated: Switched from TypeReference into MapType when reading JSON into a Map.

Henri Mikkonen henri.mikkonen at iki.fi
Wed Dec 1 17:41:39 UTC 2021


This is an automated email from the git hooks/post-receive script.

hjmikkon pushed a commit to branch main
in repository java-oidc-common.

View the commit online:
http://git.shibboleth.net/view/?p=java-oidc-common.git;a=commit;h=382649e5486f9cc838daa952174ea064e227cc2c

The following commit(s) were added to refs/heads/main by this push:
     new 382649e  Switched from TypeReference into MapType when reading JSON into a Map.
382649e is described below

commit 382649e5486f9cc838daa952174ea064e227cc2c
Author: Henri Mikkonen <henri.mikkonen at iki.fi>
AuthorDate: Wed Dec 1 19:38:52 2021 +0200

    Switched from TypeReference into MapType when reading JSON into a Map.
    
    TypeReference didn't work in the expected way when using generics.
---
 .../cache/impl/DefaultJSONMapParsingStrategy.java  | 26 ++++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DefaultJSONMapParsingStrategy.java b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DefaultJSONMapParsingStrategy.java
index 406cdf5..2f77587 100644
--- a/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DefaultJSONMapParsingStrategy.java
+++ b/oidc-common-metadata-impl/src/main/java/net/shibboleth/oidc/metadata/cache/impl/DefaultJSONMapParsingStrategy.java
@@ -30,8 +30,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.MapType;
 
 import net.shibboleth.utilities.java.support.logic.Constraint;
 
@@ -48,22 +48,34 @@ public class DefaultJSONMapParsingStrategy<V extends Object>
     
     /** JSON object mapper. */
     @Nonnull private final ObjectMapper objectMapper;
+
+    /** The map type used by object mapper for deserializing the JSON into desired format. */
+    @Nonnull private final MapType mapType;
     
     /**
      * 
      * Constructor.
      *
      * @param mapper the object mapper to use.
+     * @param valueClass the class for the map value items.
      */
-    public DefaultJSONMapParsingStrategy(@Nonnull final ObjectMapper mapper) {
+    public DefaultJSONMapParsingStrategy(@Nonnull final ObjectMapper mapper, @Nonnull final Class<V> valueClass) {
         objectMapper = Constraint.isNotNull(mapper, "Object mapper can not be null");
+        mapType = objectMapper.getTypeFactory().constructMapType(Map.class, String.class,
+                Constraint.isNotNull(valueClass,"Value class can not be null"));
     }
     
-    /** Constructor.*/
-    public DefaultJSONMapParsingStrategy() {
-        this(new ObjectMapper());
+    /**
+     * 
+     * Constructor.
+     *
+     * @param valueClass the class for the map value items.
+     */
+    public DefaultJSONMapParsingStrategy(@Nonnull final Class<V> valueClass) {
+        this(new ObjectMapper(), valueClass);
     }
 
+    /** {@inheritDoc} */
     @Override
     public List<Map<String, V>> apply(@Nullable final byte[] rawMetadata) {
         try {
@@ -72,7 +84,7 @@ public class DefaultJSONMapParsingStrategy<V extends Object>
             }
             final Map<String, V> parsed = 
                     objectMapper.readValue(new String(rawMetadata, StandardCharsets.UTF_8), 
-                            new TypeReference<Map<String,V>>(){});
+                            mapType);
             if (parsed != null) {
                 return List.of(parsed);
             }
@@ -82,5 +94,5 @@ public class DefaultJSONMapParsingStrategy<V extends Object>
             return Collections.emptyList();
         }
     }
-
+    
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list