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

All positional operator $[] throws JsonParseException in 1.5.0 #386

Open
hisener opened this issue Jul 14, 2021 · 4 comments
Open

All positional operator $[] throws JsonParseException in 1.5.0 #386

hisener opened this issue Jul 14, 2021 · 4 comments

Comments

@hisener
Copy link

hisener commented Jul 14, 2021

After upgrading Jongo 1.5.0, we started seeing JsonParseException in a couple of places where we use all positional operator $[]. Our usages are the same as the example in MongoDB doc.

BsonQueryFactory#addRequiredQuotesAndParameters seems like the culprit as it applies the following transformations, and therefore BasicDBObject.parse fails:

{ $set: { array.$[].id: 10 } } -> {"$set":{[  array.$]".id": 10 } }

My test case in BsonQueryFactoryTest:

@Test
public void allPositionalOperator() {
    assertThatCode(() -> factory.createQuery("{ $set: { array.$[].id: 10 } }")).doesNotThrowAnyException();
}

Failure:

java.lang.AssertionError: 
Expecting code not to raise a throwable but caught
  "java.lang.IllegalArgumentException: Cannot parse query: { $set: { array.$[].id: 10 } }
	at org.jongo.query.BsonQueryFactory.createQuery(BsonQueryFactory.java:106)
	at org.jongo.query.BsonQueryFactoryTest.lambda$allPositionalOperator$0(BsonQueryFactoryTest.java:390)
...
Caused by: org.bson.json.JsonParseException: JSON reader was expecting a name but found '['.
	at org.bson.json.JsonReader.readBsonType(JsonReader.java:146)
	at com.mongodb.DBObjectCodec.readDocument(DBObjectCodec.java:397)
	at com.mongodb.DBObjectCodec.readValue(DBObjectCodec.java:322)
	at com.mongodb.DBObjectCodec.readDocument(DBObjectCodec.java:399)
	at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:167)
	at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:67)
	at com.mongodb.BasicDBObject.parse(BasicDBObject.java:83)
	at com.mongodb.BasicDBObject.parse(BasicDBObject.java:70)
	at org.jongo.query.BsonQueryFactory.createQuery(BsonQueryFactory.java:99)

Also, Jongo 1.4.1 returns the expected query:

{"$set": {"array.$[].id": 10}}
@github-actions github-actions bot added the stale label Sep 13, 2021
@bguerout bguerout added bug-report and removed stale labels Sep 13, 2021
@github-actions github-actions bot added the stale label Nov 13, 2021
@bguerout bguerout removed the stale label Nov 15, 2021
@github-actions github-actions bot added the stale label Jan 15, 2022
@bguerout bguerout removed the stale label Jan 17, 2022
@github-actions github-actions bot added the stale label Mar 19, 2022
@bguerout bguerout removed the stale label Mar 19, 2022
Repository owner deleted a comment from github-actions bot Mar 19, 2022
Repository owner deleted a comment from github-actions bot Mar 19, 2022
Repository owner deleted a comment from github-actions bot Mar 19, 2022
Repository owner deleted a comment from github-actions bot Mar 19, 2022
@Dayde
Copy link
Contributor

Dayde commented May 1, 2022

You can add quotes around your key, the following test runs as expected

@Test
public void allPositionalOperator() {
    assertThatCode(() -> factory.createQuery("{ $set: { 'array.$[].id': 10 } }")).doesNotThrowAnyException();
}

I’ve been trying to modify the query builder to accept the query without the quotes but it is quite tricky to do so without introducing a lot of complexity to the algorithm. Considering the doc you mentioned had those quotes I’m not sure we want to fix it.

@hisener
Copy link
Author

hisener commented May 2, 2022

Thanks @Dayde. It's still surprising this works in Jongo 1.4.1, though. 🙂

@Dayde
Copy link
Contributor

Dayde commented May 9, 2022

Not really to me as the query factory was rewritten from scratch while updating to mongo legacy driver 4.x

Dayde added a commit to Dayde/jongo that referenced this issue May 9, 2022
@github-actions
Copy link

github-actions bot commented Jul 9, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants