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

Projection expressions with single arguments e.g. {"$size": "$source"} #1150

Closed
wants to merge 3 commits into from
Closed

Projection expressions with single arguments e.g. {"$size": "$source"} #1150

wants to merge 3 commits into from

Conversation

kothar
Copy link

@kothar kothar commented Feb 16, 2017

After #667 was merged, expressions could be composed such as

Projection.expression("$size", Projection.projection("field"))

Which would be translated to:

{"$size": ["$field"]}

Though $size only accepts a single argument, an array containing a single string is also accepted.

After @e95cdc3271408000bc57cf8f94c9468de55f1c43 (Location of change) a check was added to unwrap single-argument expressions, but it assumes that the type of the argument is a DBObject. This fails in the above case, where the argument is a projection reference string.

This PR adds a helper method for $size projection expressions, and a corresponding test to make sure the argument is unwrapped correctly from the arguments list.

The fix for the ClassCastException moves the unwrapping check to the caller of toExpressionArgs so that the string value can be handled without a cast to DBObject causing an exception.

This fixes the case where a single argument expression such as {$size: “$source”} generates an arg array
containing a string rather than a DBObject. The current behaviour would try to unwrap the “$source” and cast it to
a DBObject, which caused a ClassCastException.

By moving the check for single arguments to the toDBObject method, it’s possible to preserve the existing behaviour
where DBObjects are unwrapped and used as the argument while also correctly handling string arguments.
@kothar kothar changed the title Projection expressions Projection expressions with single arguments e.g. {"$size": "$source"} Feb 16, 2017
@evanchooly evanchooly added this to the 1.5.0 milestone Jan 19, 2019
@evanchooly
Copy link
Member

rebased and merged.
Thank you for the patch and I apologize for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants