spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

QueryMapper does not support $function while converting _id

Open gbaso opened this issue 1 year ago • 3 comments

Suppose you have a document where _id is defined with a $function, like the following:

{
  "_id": {
    "$function": {
      "body": "some js code that takes no arguments"
      "args": []
      "lang": "js"
    }
  }
}

When invoking QueryMapper#getMappedObject (e.g. when building an aggregation pipeline), given that documentField.isIdField() is true, QueryMapper tries to convert every field of the subdocument to an ObjectId. Strings (that are not valid ObjectIds) are left untouched, but an empty array is converted to null, resulting in:

{
  "_id": {
    "$function": {
      "body": "some js code that takes no arguments"
      "args": null
      "lang": "js"
    }
  }
}

Sending this to mongodb will result in an exception since args is required and must be an array.

gbaso avatar Oct 16 '24 09:10 gbaso

Thank you for getting in touch. If you'd like us to spend some time investigating, please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

Since MongoDB already deprecated Server-side JavaScript it might also make sense to think about an alternative approach.

christophstrobl avatar Oct 16 '24 09:10 christophstrobl

I have created spring-data-mongodb-4812 with a reproducible sample.

gbaso avatar Oct 16 '24 16:10 gbaso

Thanks for the reproducer, I do see what happens here now. Since you already have a ready to use Document parsed from the String you may omit the field mapping and replace context -> context.getMappedObject(project) with just context -> project.

Nevertheless I think we need to look closer into id conversion, which has been source of a couple of issues lately.

christophstrobl avatar Oct 23 '24 13:10 christophstrobl