[JAVA-741] Ability to disable validation for illegal characters in keys Created: 22/Jan/13  Updated: 19/Mar/13  Resolved: 22/Jan/13

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.10.1
Fix Version/s: 2.11.0

Type: Improvement Priority: Minor - P4
Reporter: Steve Briskin (Inactive) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

For use in internal 10gen applications, not part of public interface.
'.' and '$' are disallowed per MongoDB spec. I'd like a way to disable this check when working with legacy data.



 Comments   
Comment by auto [ 22/Jan/13 ]

Author:

{u'date': u'2013-01-22T21:10:30Z', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: Merge pull request #99 from stevebriskin/master

JAVA-741: don't validate keys for lazy objects
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/b5e846484ded3dcebeddddf2b4c6b02349f17c53

Comment by auto [ 22/Jan/13 ]

Author:

{u'date': u'2013-01-22T20:59:25Z', u'email': u'steve.briskin@10gen.com', u'name': u'Steve Briskin'}

Message: JAVA-741: disable key validation for lazy lists and objects
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/cf5dcb9185ccec7f16cacc4bb5149be91150c670

Comment by Jeffrey Yemin [ 22/Jan/13 ]

Merged https://github.com/mongodb/mongo-java-driver/pull/99.

Comment by Jeffrey Yemin [ 22/Jan/13 ]

Steve suggests another option which I like best: add an instanceof test for LazyDBObject. If true, don't validate.

Comment by Jeffrey Yemin [ 22/Jan/13 ]

If the system property is just too awful, another option is to add it to the public API: DBCollection.setFieldNameValidationEnabled(boolean enabled).

Comment by Jeffrey Yemin [ 22/Jan/13 ]

This could work

public class MyCollectionWrapper extends DBCollection {
 
   private final DBApiLayer.MyCollection wrapped;
 
   MyCollectionWrapper(DBApiLayer.MyCollection wrapped) {
       super(wrapped.getDB(), wrapped.getName());
       this.wrapped = wrapped;
   }
 
   public void insertNoCheck(DBObject obj) {
       wrapped.insert(Arrays.asList(obj), false, getWriteConcern(), getDBEncoderFactory().create());
   }
 
   // forward all abstract methods to wrapped

I don't like this much either.

Comment by Jeffrey Yemin [ 22/Jan/13 ]

That won't work since MyCollection is an inner class:

java: MyCollectionWrapper.java:25: an enclosing instance that contains com.mongodb.DBApiLayer.MyCollection is required

Comment by Scott Hernandez (Inactive) [ 22/Jan/13 ]

Don't think this is a good idea for public api at all.

I can explain if you need more than this, but here is a quick sample:

package com.mongodb;
 
public class Wrapper extends MyCollection {
   private final MyCollection w;
   public Wrapper(MyCollection wrapped) { w = wrapped;}
   public InsertWithoutCheck(DBObject obj) { w.insert(obj, false, getWriteConcern(), getDBEncoder()); }
}

Comment by Steve Briskin (Inactive) [ 22/Jan/13 ]

Aside from subclassing, wouldn't I need to pass a collection factory to Mongo & MongoClient so it returns the subclassed instance. That's a notable change to the public API.

Re. package naming, I'd much prefer MyCollection to be changed from package to protected.

Comment by Jeffrey Yemin [ 22/Jan/13 ]

Yes, it's possible, just very ugly. But give it a shot. Note that it will definitely break in 3.0.

Comment by Scott Hernandez (Inactive) [ 22/Jan/13 ]

Yes, you must subclass/wrap from the same package, but that is completely possible in your own class.

Comment by Jeffrey Yemin [ 22/Jan/13 ]

MyCollection is package private so it can't be subclassed, and the insert method with shouldApply parameter is protected so can't be called.

I don't see another way around this without an API change or an option. I don't want to support this going forward, but to get past this hurdle I recommend a system property to turn off the check, and a clear comment saying that this is not part of the public API and is subject to change without warning.

Comment by Scott Hernandez (Inactive) [ 22/Jan/13 ]

This can already be done by subclassing, or wrapping, the DBApiLayer.MyCollection impl and calling insert with shouldApply set to false.

For update/save you can simply use $ops to bypass this check, and leave it to the server.

Generated at Thu Feb 08 08:52:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.