suppress jackson error – through reference chain

By | July 12, 2018
Questions:

I have a class like this

@DynamoDBTable(tableName="x")
public class BaseEntity {

    private String tenantUid;
    private String sourceRecordId;
    private String uid;
    private String id;

    @DynamoDBHashKey(attributeName="tenantUid") 
    @DynamoDBIndexHashKey(globalSecondaryIndexName = "tenantUid-uid-index", attributeName = "tenantUid")
    public String getTenantUid() { return tenantUid; }
    public void setTenantUid(String tenantUid) { this.tenantUid = tenantUid; }

    @DynamoDBRangeKey(attributeName="sourceRecordId")  
    public String getSourceRecordId() { return sourceRecordId; }    
    public void setSourceRecordId(String sourceRecordId) { this.sourceRecordId = sourceRecordId; }

    @DynamoDBIndexRangeKey(globalSecondaryIndexName = "tenantUid-uid-index", attributeName = "uid")
    public String getUid() { return uid; }   
    public void setUid(String uid) { this.uid = uid; }

    @DynamoDBAttribute(attributeName="id") 
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }

}

public class EntityService {
    ........
    public String test (BaseEntity incomingEntity) throws JsonProcessingException {
        return objectMapper.writeValueAsString(incomingEntity);
    }

}

....
    @Test
    public void h_testTest() throws ClassNotFoundException, IOException {
        BaseEntity incomingEntity1 = new BaseEntity();
        String result = entityService.test(incomingEntity1);
        Assert.assertTrue(result.contains("sourceRecordId"));
    } 

If I debug through the test, it passes. However, if I run jacoco coverage tests on the whole project, it failed with the following error.

 com.fasterxml.jackson.databind.JsonMappingException: 15 (through reference chain: com.my.package.models.BaseEntity["sourceRecordId"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:199)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:683)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3559)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2927)
    at com.my.package.services.EntityService.test(EntityService.java:145)
    at com.my.package.services.EntityServiceTest.h_testTest(EntityServiceTest.java:221)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 15
    at com.my.package.models.BaseEntity.getsourceRecordId(BaseEntity.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:654)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675)
    ... 30 more

How do I fix this error?

Note that BaseEntity serves as a parent class to be extended by child classes.

Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *