Bug 2964 - ClassCastException when querying -
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONCodecFactory.java
index c5840cff986b359c5f4271a29c8a5fa6829e0eb6..fcfbf112d695d5e67e595d67de73a51457070d43 100644 (file)
@@ -75,12 +75,10 @@ public final class JSONCodecFactory {
 
     private final SchemaContext schemaContext;
     private final JSONCodec<?> iidCodec;
-    private final JSONCodec<?> idrefCodec;
 
     private JSONCodecFactory(final SchemaContext context) {
         this.schemaContext = Preconditions.checkNotNull(context);
         iidCodec = new JSONStringInstanceIdentifierCodec(context);
-        idrefCodec = new JSONStringIdentityrefCodec(context);
     }
 
     /**
@@ -101,10 +99,15 @@ public final class JSONCodecFactory {
         return superType;
     }
 
+    @SuppressWarnings("unchecked")
     private JSONCodec<Object> createCodec(DataSchemaNode key, TypeDefinition<?> type) {
         TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
         if (baseType instanceof LeafrefTypeDefinition) {
             return createReferencedTypeCodec(key, (LeafrefTypeDefinition) baseType);
+        } else if (baseType instanceof IdentityrefType) {
+            final JSONCodec<?> jsonStringIdentityrefCodec = new JSONStringIdentityrefCodec(schemaContext,
+                    key.getQName().getModule());
+            return (JSONCodec<Object>) jsonStringIdentityrefCodec;
         }
         return createFromSimpleType(type);
     }
@@ -114,7 +117,7 @@ public final class JSONCodecFactory {
         // FIXME: Verify if this does indeed support leafref of leafref
         TypeDefinition<?> referencedType =
                 SchemaContextUtil.getBaseTypeForLeafRef(type, getSchemaContext(), schema);
-        return createFromSimpleType(referencedType);
+        return createCodec(schema, referencedType);
     }
 
     @SuppressWarnings("unchecked")
@@ -123,9 +126,6 @@ public final class JSONCodecFactory {
         if (baseType instanceof InstanceIdentifierType) {
             return (JSONCodec<Object>) iidCodec;
         }
-        if (baseType instanceof IdentityrefType) {
-            return (JSONCodec<Object>) idrefCodec;
-        }
 
         final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
         if (codec == null) {