Bug 4969: NPE in JSONCodecFactory by attempt to find codec for a leafref
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONCodecFactory.java
index 9df9b347dc16c59a19931c650814420d59a8c221..1d6ffc827724a1670da028bdc5a34cc9810ccc43 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.data.codec.gson;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -80,7 +81,7 @@ public final class JSONCodecFactory {
 
     private JSONCodecFactory(final SchemaContext context) {
         this.schemaContext = Preconditions.checkNotNull(context);
-        iidCodec = new JSONStringInstanceIdentifierCodec(context);
+        iidCodec = new JSONStringInstanceIdentifierCodec(context, this);
     }
 
     /**
@@ -111,6 +112,7 @@ public final class JSONCodecFactory {
         // FIXME: Verify if this does indeed support leafref of leafref
         final TypeDefinition<?> referencedType =
                 SchemaContextUtil.getBaseTypeForLeafRef(type, getSchemaContext(), schema);
+        Verify.verifyNotNull(referencedType, "Unable to find base type for leafref node '%s'.", schema.getPath());
         return createCodec(schema, referencedType);
     }
 
@@ -120,7 +122,7 @@ public final class JSONCodecFactory {
             return (JSONCodec<Object>) iidCodec;
         }
         if (type instanceof EmptyTypeDefinition) {
-            return (JSONCodec<Object>) JSONEmptyCodec.INSTANCE;
+            return JSONEmptyCodec.INSTANCE;
         }
 
         final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);