Skip to content

Commit

Permalink
Merge pull request #596 from openraven/595-bug-unable-to-serialize-re…
Browse files Browse the repository at this point in the history
…source-after-jackson-upgrade

595: Fix resource serialization
  • Loading branch information
belosh59 authored Apr 18, 2024
2 parents 4f64e15 + ab41df9 commit b75dc0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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" +
Expand Down

0 comments on commit b75dc0d

Please sign in to comment.