Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

595: Fix resource serialization #596

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With value PROPERTY we were getting two resourceType in serialized string:

@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();
Copy link
Contributor Author

@belosh59 belosh59 Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously under Jackson version 2.13.5 by returning null here and include = JsonTypeInfo.As.PROPERTY we were getting following.:
(Pay attention into two resourceType that is how PROPERTY include works)

{"resourceType":"null",
"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}

Now Jackson 2.15.4 doesn't allow to set null to resolving id and we were getting mentioned expection:

com.fasterxml.jackson.databind.JsonMappingException: Can not write a field name, expecting a value (through reference chain: io.openraven.magpie.data.aws.rds.RDSInstance["resourceType"])

With above fix we are getting now.

{"resourceType":"AWS::RDS::DBInstance",
"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}

When we set include = JsonTypeInfo.As.EXISTING_PROPERTY we are getting not getting resourceType duplicates:

{"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}

}

@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
Loading