From ab41df9d743c2a45144ce1362ced9b2461861508 Mon Sep 17 00:00:00 2001 From: mbiloshytskyi Date: Wed, 17 Apr 2024 15:21:39 -0700 Subject: [PATCH] 595: Fix resource serialisation --- .../java/io/openraven/magpie/data/Resource.java | 3 ++- .../magpie/data/utils/EntityTypeResolver.java | 12 ++++++++---- .../data/aws/utils/EntityTypeResolverTest.java | 13 +++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/magpie-data/src/main/java/io/openraven/magpie/data/Resource.java b/magpie-data/src/main/java/io/openraven/magpie/data/Resource.java index 1eae3611..211bb52f 100644 --- a/magpie-data/src/main/java/io/openraven/magpie/data/Resource.java +++ b/magpie-data/src/main/java/io/openraven/magpie/data/Resource.java @@ -23,9 +23,10 @@ import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; import io.openraven.magpie.data.utils.EntityTypeResolver; -@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.PROPERTY, property = "resourceType") +@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "resourceType") @JsonTypeIdResolver(EntityTypeResolver.class) public class Resource { + public String resourceType; public String getResourceType() { diff --git a/magpie-data/src/main/java/io/openraven/magpie/data/utils/EntityTypeResolver.java b/magpie-data/src/main/java/io/openraven/magpie/data/utils/EntityTypeResolver.java index bc042241..e4b689d7 100644 --- a/magpie-data/src/main/java/io/openraven/magpie/data/utils/EntityTypeResolver.java +++ b/magpie-data/src/main/java/io/openraven/magpie/data/utils/EntityTypeResolver.java @@ -38,6 +38,7 @@ import com.fasterxml.jackson.databind.DatabindContext; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase; +import io.openraven.magpie.data.Resource; import io.openraven.magpie.data.exception.MissingEntityTypeException; import org.reflections.Reflections; @@ -80,13 +81,16 @@ public void init(JavaType javaType) { } @Override - public String idFromValue(Object o) { - return idFromValueAndType(o, o.getClass()); + public String idFromValue(Object object) { + return idFromValueAndType(object, object.getClass()); } @Override - public String idFromValueAndType(Object o, Class aClass) { - return null; + public String idFromValueAndType(Object object, Class aClass) { + if (object instanceof Resource) { + return ((Resource) object).getResourceType(); + } + return aClass.getName(); } @Override diff --git a/magpie-data/src/test/java/io/openraven/magpie/data/aws/utils/EntityTypeResolverTest.java b/magpie-data/src/test/java/io/openraven/magpie/data/aws/utils/EntityTypeResolverTest.java index 267bdddc..e2fc97bf 100644 --- a/magpie-data/src/test/java/io/openraven/magpie/data/aws/utils/EntityTypeResolverTest.java +++ b/magpie-data/src/test/java/io/openraven/magpie/data/aws/utils/EntityTypeResolverTest.java @@ -24,6 +24,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import io.openraven.magpie.data.Resource; import io.openraven.magpie.data.aws.accounts.IamGroup; +import io.openraven.magpie.data.aws.rds.RDSInstance; import io.openraven.magpie.data.gcp.container.ContainerAnalysisNote; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -32,6 +33,18 @@ class EntityTypeResolverTest { public static final ObjectMapper MAPPER = new ObjectMapper().registerModule(new JavaTimeModule()); + + @Test + void testResourceSerialization() throws JsonProcessingException { + RDSInstance rdsInstance = new RDSInstance(); + String serialisedResource = MAPPER.writeValueAsString(rdsInstance); + String expectedJson = """ + {"resourceType":"AWS::RDS::DBInstance","documentId":null,"arn":null,"resourceName":null,"resourceId":null,"awsRegion":null,"awsAccountId":null,"createdIso":null,"updatedIso":null,"discoverySessionId":null,"maxSizeInBytes":null,"sizeInBytes":null,"configuration":null,"supplementaryConfiguration":null,"tags":null,"discoveryMeta":null}\ + """; + Assertions.assertEquals(expectedJson, serialisedResource); + } + + @Test public void testResolveAwsIamGroup() throws JsonProcessingException { String json = "{\n" +