Bug 2606: Fixed serialization of null augmentations. 50/14250/1
authorTony Tkacik <ttkacik@cisco.com>
Mon, 19 Jan 2015 14:24:12 +0000 (15:24 +0100)
committerTony Tkacik <ttkacik@cisco.com>
Mon, 19 Jan 2015 14:46:59 +0000 (15:46 +0100)
Binding DTO contract allowed to use addAugmentation(T,null)
to remove augmentation, but this was not supported
by binding-data-codec.

Added explicit ommision of null augmentations.

Change-Id: Ib16f3a18e6b09ab970dcaef83e3fca4f490c9ec3
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
code-generator/binding-data-codec/src/main/java/org/opendaylight/yangtools/binding/data/codec/util/AugmentableDispatchSerializer.java

index 38b4e8b211e00cb181e246623928dff4b2de32b6..b0860eb335108a993a7f9c21d7ff21e25a768af3 100644 (file)
@@ -8,14 +8,12 @@
 package org.opendaylight.yangtools.binding.data.codec.util;
 
 import com.google.common.base.Preconditions;
-
 import java.io.IOException;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Proxy;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Map.Entry;
-
 import org.opendaylight.yangtools.yang.binding.Augmentable;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
@@ -49,14 +47,14 @@ public class AugmentableDispatchSerializer implements DataObjectSerializerImplem
             } else {
                 augmentations = BindingReflections.getAugmentations((Augmentable<?>) obj);
             }
-            for (Entry<Class<? extends Augmentation<?>>, Augmentation<?>> aug : augmentations.entrySet()) {
+            for (final Entry<Class<? extends Augmentation<?>>, Augmentation<?>> aug : augmentations.entrySet()) {
                 emitAugmentation(aug.getKey(), aug.getValue(), stream, reg);
             }
         }
     }
 
     private Map<Class<? extends Augmentation<?>>, Augmentation<?>> getFromProxy(final DataObject obj) {
-        InvocationHandler proxy = Proxy.getInvocationHandler(obj);
+        final InvocationHandler proxy = Proxy.getInvocationHandler(obj);
         if (proxy instanceof AugmentationReader) {
             return ((AugmentationReader) proxy).getAugmentations(obj);
         }
@@ -66,9 +64,17 @@ public class AugmentableDispatchSerializer implements DataObjectSerializerImplem
     @SuppressWarnings("rawtypes")
     private void emitAugmentation(final Class type, final Augmentation<?> value, final BindingStreamEventWriter stream,
             final DataObjectSerializerRegistry registry) throws IOException {
+        /*
+         * Binding Specification allowed to insert augmentation with null for
+         * value, which effectively could be used to remove augmentation
+         * from builder / DTO.
+         */
+        if(value == null) {
+            return;
+        }
         Preconditions.checkArgument(value instanceof DataObject);
         @SuppressWarnings("unchecked")
-        DataObjectSerializer serializer = registry.getSerializer(type);
+        final DataObjectSerializer serializer = registry.getSerializer(type);
         if (serializer != null) {
             serializer.serialize((DataObject) value, stream);
         } else {