From f00a94eb8b97ad771b34925586f33f38bab05c54 Mon Sep 17 00:00:00 2001 From: rameshiyer27 Date: Sun, 22 Feb 2026 18:07:30 +0000 Subject: [PATCH] Emulate gson behaviour in clamp for serialization 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 Change-Id: I5fc0f9b904d96ea0d68f2302556ddb40d3de8525 --- .../participant/AutomationCompositionDeployTest.java | 11 ++++++++--- .../kafka/participant/ParticipantMessageUtils.java | 16 +++++++++++++++- .../kafka/participant/PropertiesUpdateTest.java | 17 ++++++++++++----- .../onap/policy/common/utils/coder/StandardCoder.java | 4 +++- .../policy/common/utils/coder/YamlJsonTranslator.java | 3 +++ 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/AutomationCompositionDeployTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/AutomationCompositionDeployTest.java index f735c615f..8f29740ca 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/AutomationCompositionDeployTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/AutomationCompositionDeployTest.java @@ -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(); + 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); } } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageUtils.java b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageUtils.java index 5bb7a67d8..52e47efe5 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageUtils.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/ParticipantMessageUtils.java @@ -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 void assertSerializeBehaviour(Object object) throws CoderException { + var standardCoder = new StandardCoder(); + var json = standardCoder.encode(object); + assertFalse(json.contains("nullProperty")); + assertTrue(json.contains("testProperty")); + } } diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/PropertiesUpdateTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/PropertiesUpdateTest.java index 7ad2cfb85..43f5fe936 100644 --- a/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/PropertiesUpdateTest.java +++ b/models/src/test/java/org/onap/policy/clamp/models/acm/messages/kafka/participant/PropertiesUpdateTest.java @@ -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(); + 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); } } diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java b/policy-common/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java index ec8a8f5f6..63d881998 100644 --- a/policy-common/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java +++ b/policy-common/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java @@ -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(); diff --git a/policy-common/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java b/policy-common/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java index 31c8eed37..0b477fa86 100644 --- a/policy-common/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java +++ b/policy-common/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java @@ -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); } /** -- 2.16.6