From c39d31fab72d1bb50a90d1c0403465ee2845a0ab Mon Sep 17 00:00:00 2001 From: Pierre Lecerf Date: Tue, 17 Nov 2026 21:47:22 +0100 Subject: [PATCH] fix: MetricData backcompat deserialization --- .../core/source/data/KubernetesData.java | 7 +++ .../wombat/core/source/data/LLMData.java | 7 +++ .../core/source/data/MetricDataTest.java | 54 +++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/core/src/main/java/tech/illuin/wombat/core/source/data/KubernetesData.java b/core/src/main/java/tech/illuin/wombat/core/source/data/KubernetesData.java index 4c11f22..ea848c5 100644 --- a/core/src/main/java/tech/illuin/wombat/core/source/data/KubernetesData.java +++ b/core/src/main/java/tech/illuin/wombat/core/source/data/KubernetesData.java @@ -1,9 +1,16 @@ package tech.illuin.wombat.core.source.data; +import com.fasterxml.jackson.annotation.JsonTypeInfo; import tech.illuin.wombat.core.asset.type.ServiceFamily; import java.util.Objects; +@JsonTypeInfo( + use = JsonTypeInfo.Id.NAME, + include = JsonTypeInfo.As.PROPERTY, + property = "family", + defaultImpl = KubernetesData.class +) public record KubernetesData( String serviceId, String cluster, diff --git a/core/src/main/java/tech/illuin/wombat/core/source/data/LLMData.java b/core/src/main/java/tech/illuin/wombat/core/source/data/LLMData.java index 1138853..1f805df 100644 --- a/core/src/main/java/tech/illuin/wombat/core/source/data/LLMData.java +++ b/core/src/main/java/tech/illuin/wombat/core/source/data/LLMData.java @@ -1,11 +1,18 @@ package tech.illuin.wombat.core.source.data; +import com.fasterxml.jackson.annotation.JsonTypeInfo; import tech.illuin.wombat.core.activity.commons.Country; import tech.illuin.wombat.core.asset.profile.LLMProvider; import tech.illuin.wombat.core.asset.type.ServiceFamily; import java.util.Objects; +@JsonTypeInfo( + use = JsonTypeInfo.Id.NAME, + include = JsonTypeInfo.As.PROPERTY, + property = "family", + defaultImpl = LLMData.class +) public record LLMData( String serviceId, LLMProvider provider, diff --git a/core/src/test/java/tech/illuin/wombat/core/source/data/MetricDataTest.java b/core/src/test/java/tech/illuin/wombat/core/source/data/MetricDataTest.java index 3612b45..55db14b 100644 --- a/core/src/test/java/tech/illuin/wombat/core/source/data/MetricDataTest.java +++ b/core/src/test/java/tech/illuin/wombat/core/source/data/MetricDataTest.java @@ -3,6 +3,9 @@ import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; +import com.fasterxml.jackson.databind.exc.ValueInstantiationException; +import com.fasterxml.jackson.databind.node.ObjectNode; import org.junit.jupiter.api.Test; import tech.illuin.wombat.core.activity.commons.Country; import tech.illuin.wombat.core.asset.profile.LLMProvider; @@ -11,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; class MetricDataTest { @@ -45,4 +49,54 @@ void familyNames_matchTheServiceFamilyOfEachVariant() assertEquals(sample.serviceFamily().name(), type.name()); } } + + @Test + void metricData_deserializesDirectlyToItsConcreteType() throws Exception + { + assertEquals(KUBERNETES, MAPPER.readValue(MAPPER.writeValueAsString(KUBERNETES), KubernetesData.class)); + assertEquals(LLM, MAPPER.readValue(MAPPER.writeValueAsString(LLM), LLMData.class)); + } + + @Test + void metricData_deserializesLegacyPayloadWithoutFamilyToConcreteType() throws Exception + { + for (MetricData data : List.of(KUBERNETES, LLM)) + { + ObjectNode tree = MAPPER.valueToTree(data); + tree.remove("family"); + + assertEquals(data, MAPPER.readValue(tree.toString(), data.getClass())); + } + } + + @Test + void metricData_rejectsPolymorphicDeserializationWithoutFamily() + { + for (MetricData data : List.of(KUBERNETES, LLM)) + { + ObjectNode tree = MAPPER.valueToTree(data); + tree.remove("family"); + String untaggedJson = tree.toString(); + + assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(untaggedJson, MetricData.class)); + } + } + + @Test + void metricData_rejectsUnknownOrMismatchedFamily() throws Exception + { + String unknownFamily = """ + {"family":"UNKNOWN","serviceId":"api"} + """; + + assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(unknownFamily, MetricData.class)); + assertThrows(ValueInstantiationException.class, () -> MAPPER.readValue(unknownFamily, KubernetesData.class)); + assertThrows(ValueInstantiationException.class, () -> MAPPER.readValue(unknownFamily, LLMData.class)); + + String llmJson = MAPPER.writeValueAsString(LLM); + String k8sJson = MAPPER.writeValueAsString(KUBERNETES); + + assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(llmJson, KubernetesData.class)); + assertThrows(InvalidTypeIdException.class, () -> MAPPER.readValue(k8sJson, LLMData.class)); + } }