Emulate gson behaviour in clamp for serialization 62/143362/2
authorrameshiyer27 <ramesh.murugan.iyer@est.tech>
Sun, 22 Feb 2026 18:07:30 +0000 (18:07 +0000)
committerrameshiyer27 <ramesh.murugan.iyer@est.tech>
Mon, 23 Feb 2026 10:07:16 +0000 (10:07 +0000)
standardCoder is configured to skip null fields during serialization to
emulate the same behavior from gson.

Note: De-serialization still includes null values same as gson.

Issue-ID: POLICY-5548
Signed-off-by: rameshiyer27 <ramesh.murugan.iyer@est.tech>
Change-Id: I5fc0f9b904d96ea0d68f2302556ddb40d3de8525

models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/AutomationCompositionDeployTest.java
models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageUtils.java
models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/PropertiesUpdateTest.java
policy-common/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java
policy-common/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java

index f735c61..8f29740 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2023-2024 Nordix Foundation.
+ * Copyright (C) 2023-2024,2026 OpenInfra Foundation Europe. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,11 +23,12 @@ package org.onap.policy.clamp.models.acm.messages.kafka.participant;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageUtils.assertSerializable;
+import static org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageUtils.assertSerializeBehaviour;
 import static org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageUtils.removeVariableFields;
 
 import java.time.Instant;
+import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 import org.junit.jupiter.api.Test;
 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
@@ -63,7 +64,10 @@ class AutomationCompositionDeployTest {
         property.setType("testType");
         var standardCoder = new StandardCoder();
         var json = standardCoder.encode(property);
-        var propertiesMap = Map.of("Prop1", (Object) json);
+        var propertiesMap = new HashMap<String, Object>();
+        propertiesMap.put("Prop1", json);
+        propertiesMap.put("testProperty", "dummy");
+        propertiesMap.put("nullProperty", null);
         acElement.setProperties(propertiesMap);
 
         var participantDeploy = new ParticipantDeploy();
@@ -75,5 +79,6 @@ class AutomationCompositionDeployTest {
 
         assertEquals(removeVariableFields(orig.toString()), removeVariableFields(other.toString()));
         assertSerializable(orig, AutomationCompositionDeploy.class);
+        assertSerializeBehaviour(orig);
     }
 }
index 5bb7a67..52e47ef 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2021,2023-2024 Nordix Foundation.
+ * Copyright (C) 2021,2023-2024,2026 OpenInfra Foundation Europe. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
 package org.onap.policy.clamp.models.acm.messages.kafka.participant;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
@@ -52,4 +54,16 @@ public class ParticipantMessageUtils {
 
         assertEquals(removeVariableFields(object.toString()), removeVariableFields(other.toString()));
     }
+
+    /**
+     * Check if the null fields in properties are omitted in serialization.
+     * @param object the Object
+     * @throws CoderException if object is not serializable
+     */
+    public static <T> void assertSerializeBehaviour(Object object) throws CoderException {
+        var standardCoder = new StandardCoder();
+        var json = standardCoder.encode(object);
+        assertFalse(json.contains("nullProperty"));
+        assertTrue(json.contains("testProperty"));
+    }
 }
index 7ad2cfb..43f5fe9 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
+ * Copyright (C) 2025-2026 OpenInfra Foundation Europe. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,8 +22,10 @@ package org.onap.policy.clamp.models.acm.messages.kafka.participant;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageUtils.assertSerializable;
+import static org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageUtils.assertSerializeBehaviour;
 import static org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantMessageUtils.removeVariableFields;
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.UUID;
 import org.junit.jupiter.api.Test;
@@ -37,13 +39,17 @@ class PropertiesUpdateTest {
 
     @Test
     void testCopyConstructor() throws CoderException {
-        var acElement = new AcElementDeploy();
-        acElement.setId(UUID.randomUUID());
+        var acElementDeploy = new AcElementDeploy();
+        acElementDeploy.setId(UUID.randomUUID());
         var id = new ToscaConceptIdentifier("id", "1.2.3");
-        acElement.setDefinition(id);
+        acElementDeploy.setDefinition(id);
+        var properties = new HashMap<String, Object>();
+        properties.put("testProperty", "dummy");
+        properties.put("nullProperty", null);
+        acElementDeploy.setProperties(properties);
         var participantDeploy = new ParticipantDeploy();
         participantDeploy.setParticipantId(CommonTestData.getParticipantId());
-        participantDeploy.setAcElementList(List.of(acElement));
+        participantDeploy.setAcElementList(List.of(acElementDeploy));
         var orig = new PropertiesUpdate();
         orig.setParticipantUpdatesList(List.of(participantDeploy));
         orig.setCompositionId(UUID.randomUUID());
@@ -54,5 +60,6 @@ class PropertiesUpdateTest {
         var other = new PropertiesUpdate(orig);
         assertEquals(removeVariableFields(orig.toString()), removeVariableFields(other.toString()));
         assertSerializable(orig, PropertiesUpdate.class);
+        assertSerializeBehaviour(orig);
     }
 }
index ec8a8f5..63d8819 100644 (file)
@@ -22,6 +22,7 @@
 package org.onap.policy.common.utils.coder;
 
 
+import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -59,7 +60,8 @@ public class StandardCoder implements Coder {
         mapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);
         // Don't write self references as null, just ignore them
         mapper.configure(SerializationFeature.WRITE_SELF_REFERENCES_AS_NULL, false);
-        
+        // Ignore null fields during serialization
+        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
         // Register modules for Java 8 time support (JSR310)
         mapper.findAndRegisterModules();
 
index 31c8eed..0b477fa 100644 (file)
@@ -21,6 +21,7 @@
 
 package org.onap.policy.common.utils.coder;
 
+import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -72,6 +73,8 @@ public class YamlJsonTranslator {
         this.mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
         // Configure to handle circular references
         this.mapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);
+        // Ignore null fields during serialization
+        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
     }
 
     /**