Skip to content

NPE in QueryMapper when use Query with null as value #3633

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

Closed
EGD opened this issue Apr 20, 2021 · 3 comments
Closed

NPE in QueryMapper when use Query with null as value #3633

EGD opened this issue Apr 20, 2021 · 3 comments
Assignees
Labels
type: bug A general bug

Comments

@EGD
Copy link

EGD commented Apr 20, 2021

Spring Data MongoDB version: 3.1.5

used Query annotation:

@Query("parentId: null")
AnyEntity findRootScope();

used entity class:

    @Id
    public String id;
    public String name;
    @Field(targetType = FieldType.OBJECT_ID)
    public String parentId;
}

when calling this method i get NullPointerException:

java.lang.NullPointerException
at org.springframework.data.mongodb.core.convert.QueryMapper.applyFieldTargetTypeHintToValue(QueryMapper.java:692)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedValue(QueryMapper.java:335)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObjectForField(QueryMapper.java:240)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:143)
at org.springframework.data.mongodb.core.QueryOperations$QueryContext.getMappedQuery(QueryOperations.java:255)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2542)
at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:220)
at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.oneValue(ExecutableFindOperationSupport.java:132)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.lambda$getExecution$4(AbstractMongoQuery.java:159)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.doExecute(AbstractMongoQuery.java:132)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:107)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy44.findRootScope(Unknown Source)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 20, 2021
@christophstrobl christophstrobl self-assigned this Apr 20, 2021
@christophstrobl
Copy link
Member

Thanks for reporting - well look into it.
Meanwhile, have you tried { parentId : { $exists: false } } or is an equality filter required here?

@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 20, 2021
@EGD
Copy link
Author

EGD commented Apr 20, 2021

Thanks, in my particular case, replacing to $exists works.

@rogeriomgatto
Copy link

Another workaround is to use new BsonNull() instead of null. This also works for setting null values in update dsl, which triggers the same bug.

@mp911de mp911de added this to the 3.1.10 (2020.0.10) milestone Jun 9, 2021
mp911de pushed a commit that referenced this issue Jun 9, 2021
mp911de added a commit that referenced this issue Jun 9, 2021
Add nullability annotation. Return early on null value conversion.

See #3633
Original pull request: #3643.
@mp911de mp911de closed this as completed in 149a703 Jun 9, 2021
mp911de pushed a commit that referenced this issue Jun 9, 2021
mp911de added a commit that referenced this issue Jun 9, 2021
Add nullability annotation. Return early on null value conversion.

See #3633
Original pull request: #3643.
mp911de pushed a commit that referenced this issue Jun 9, 2021
mp911de added a commit that referenced this issue Jun 9, 2021
Add nullability annotation. Return early on null value conversion.

See #3633
Original pull request: #3643.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
5 participants