spring-projects/spring-data-mongodb

NPE when projecting to an interface using String-based Aggregation Repository Query Methods

fdutton opened this issue · 5 comments

  1. AbstractMongoQuery.execute() calls ReturnedInterface.getTypeToRead()
  2. ReturnedInterface.getTypeToRead() returns null when the query's result is a closed projection
  3. AbstractMongoQuery.execute() calls StringBasedAggregation.execute()
  4. StringBasedAggregation.execute() calls StringBasedAggregation.isSimpleReturnType() and passes null as the targetType
  5. StringBasedAggregation.isSimpleReturnType() calls MongoSimpleTypes.HOLDER.isSimpleType()
  6. MongoSimpleTypes.HOLDER.isSimpleType() raises a NullPointerException by attempting to invoke type.isEnum() when type is null

This does not happen when projecting to a class.

If you would like us to spend some time helping you to diagnose the problem, please spend some time describing it and, ideally, providing a minimal yet complete sample that reproduces the problem.
You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

I already diagnosed the problem and attempted to describe what is happening step-by-step. I think the root cause is in ReturnedInterface.getTypeToRead as it returns null under certain conditions when it should return a Class that represents the interface used in the projection.

I already diagnosed the problem and attempted to describe what is happening step-by-step.

I appreciate that you understand the issue. In such a case, care to submit a pull request?

If not, then I'd like to understand the issue too and the way you understand things might be very different that other people understand them.

The best I can do at the moment is provide a failing unit-test. I attempted a fix but there are other issues preventing interface-based projections and I don't know enough about the inner workings of Spring Data Mongo. In my opinion, the root cause of this issue lies within spring-data-commons but a fix there would probably break some other implementation.

#4840

@fdutton thanks for the testcase.