Skip to content

NullPointerException when POST is invoked for the field with custom logic in getter #3328

@tory-kk

Description

@tory-kk

Hello,
I am getting NullPointerException when invoking resource creation for JSON:API for the following scenario:

  • there is an entity (see TestEntity in gist below) with the field stringValue
  • there is a custom logic in getStringValue method that relies on another field (relatedValue) which is another entity
  • in order to get computed value from the getter in the response, the method is annotated with @ComputedAttribute. Otherwise, getter logic is ignored and a raw value from the field is returned.

There is a DemoApplicationTests that covers GET/POST/PATCH requests and currently testCreate fails with the exception:

java.lang.reflect.InvocationTargetException: null
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:569) ~[na:na]
	at com.yahoo.elide.core.type.MethodType.invoke(MethodType.java:83) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.core.dictionary.EntityDictionary.getValue(EntityDictionary.java:1758) ~[elide-core-7.1.4.jar:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:569) ~[na:na]
	at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:281) ~[spring-core-6.1.14.jar:6.1.14]
	at org.springframework.cloud.context.scope.GenericScope$LockedScopedProxyFactoryBean.invoke(GenericScope.java:482) ~[spring-cloud-context-4.1.4.jar:4.1.4]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.1.14.jar:6.1.14]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:768) ~[spring-aop-6.1.14.jar:6.1.14]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:720) ~[spring-aop-6.1.14.jar:6.1.14]
	at com.yahoo.elide.core.dictionary.EntityDictionary$$SpringCGLIB$$0.getValue(<generated>) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.core.PersistentResource.getValue(PersistentResource.java:487) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.core.PersistentResource.getValueUnchecked(PersistentResource.java:1752) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.core.PersistentResource.updateAttribute(PersistentResource.java:602) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.parser.state.CollectionTerminalState.createObject(CollectionTerminalState.java:232) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.parser.state.CollectionTerminalState.handlePost(CollectionTerminalState.java:141) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.parser.state.StateContext.handlePost(StateContext.java:116) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.parser.PostVisitor.visitQuery(PostVisitor.java:31) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.parser.PostVisitor.visitQuery(PostVisitor.java:18) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.generated.parsers.CoreParser$QueryContext.accept(CoreParser.java:582) ~[elide-core-7.1.4.jar:na]
	at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visitChildren(AbstractParseTreeVisitor.java:46) ~[antlr4-runtime-4.13.0.jar:4.13.0]
	at com.yahoo.elide.generated.parsers.CoreBaseVisitor.visitStart(CoreBaseVisitor.java:21) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.parser.BaseVisitor.visitStart(BaseVisitor.java:47) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.parser.BaseVisitor.visitStart(BaseVisitor.java:33) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.generated.parsers.CoreParser$StartContext.accept(CoreParser.java:118) ~[elide-core-7.1.4.jar:na]
	at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18) ~[antlr4-runtime-4.13.0.jar:4.13.0]
	at com.yahoo.elide.jsonapi.JsonApi.visit(JsonApi.java:251) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.JsonApi.lambda$post$1(JsonApi.java:137) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.JsonApi.handleRequest(JsonApi.java:276) ~[elide-core-7.1.4.jar:na]
	at com.yahoo.elide.jsonapi.JsonApi.post(JsonApi.java:129) ~[elide-core-7.1.4.jar:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:an]
	...
	at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na]
Caused by: java.lang.NullPointerException: Cannot invoke "com.example.demo.entity.RelatedEntity.getStringValue()" because "this.relatedValue" is null
	at com.example.demo.entity.TestEntity.getStringValue(TestEntity.java:47) ~[classes/:na]
	... 54 common frames omitted

Please advise if I have a misconfiguration or how the issue could be fixed.

Gist with a demo application: https://gist.github.com/tory-kk/5db5dfd5b073446b1bfa680ddd0cf63f

Elide version: 7.1.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions