MorphiaOrg / morphia

MongoDB object-document mapper in Java based on https://github.com/mongodb/mongo-java-driver
Apache License 2.0
1.65k stars 456 forks source link

Using the unset operator on @Reference fields results in NPE #1809

Closed ptwohig closed 2 years ago

ptwohig commented 2 years ago

Describe the bug NPE when trying to use unset in combination with modify() when the field is annotated with @Reference.

To Reproduce Steps to reproduce the behavior:

  1. Define an @Entity annotated class Foo that has an @Id annotated field.
  2. Define an @Entity annotated class Bar that has an @Id annotated field and a @Reference annotated field of type Foo
  3. Instantiate Bar with with a null reference to Foo and save it to the database. Allow Morphia to assign ID and leave all references to Foo as null.
  4. Using Query.modify().execute(new ModifyOptions()) attempt to unset the blank field using unset operator

Expected behavior The operation succeeds, or Morphia throws a meaningful exception.

Please complete the following information:

Additional context

Stack trace:

java.lang.NullPointerException
    at dev.morphia.mapping.codec.references.ReferenceCodec.collectIdValues(ReferenceCodec.java:249)
    at dev.morphia.mapping.codec.references.ReferenceCodec.encode(ReferenceCodec.java:215)
    at dev.morphia.mapping.codec.references.ReferenceCodec.lambda$encode$0(ReferenceCodec.java:205)
    at dev.morphia.aggregation.experimental.codecs.ExpressionHelper.document(ExpressionHelper.java:35)
    at dev.morphia.mapping.codec.references.ReferenceCodec.encode(ReferenceCodec.java:203)
    at dev.morphia.query.OperationTarget.encode(OperationTarget.java:70)
    at dev.morphia.query.Operations.toDocument(Operations.java:84)
    at dev.morphia.query.UpdateBase.toDocument(UpdateBase.java:76)
    at dev.morphia.query.Modify.execute(Modify.java:47)
ptwohig commented 2 years ago

https://github.com/ptwohig/morphia-issue-1809-reproducer

evanchooly commented 2 years ago

This actually seems to work correctly in 2.2.6 with your reproducer. Can you upgrade and give that a try?

ptwohig commented 2 years ago

@evanchooly Looks like it passes for me. Now with 2.2.6 I'm getting a different issue in production code. I"ll check on my end and make a new reproducer test if it seems to be a problem with Morphia.

evanchooly commented 2 years ago

closing this since this issue seems ok. please open a new one if you find something else.