Skip to content
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

QueryMapper does not support $function while converting _id #4812

Open
gbaso opened this issue Oct 16, 2024 · 3 comments
Open

QueryMapper does not support $function while converting _id #4812

gbaso opened this issue Oct 16, 2024 · 3 comments
Assignees
Labels
status: pending-design-work Needs design work before any code can be developed type: bug A general bug

Comments

@gbaso
Copy link

gbaso commented Oct 16, 2024

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 16, 2024
@christophstrobl
Copy link
Member

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 christophstrobl added the status: waiting-for-feedback We need additional information before we can continue label Oct 16, 2024
@gbaso
Copy link
Author

gbaso commented Oct 16, 2024

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

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 16, 2024
@christophstrobl
Copy link
Member

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 christophstrobl added type: bug A general bug status: pending-design-work Needs design work before any code can be developed and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pending-design-work Needs design work before any code can be developed type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants