google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Morphia successfully saves illegal keys to Mongo #395

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version are you using? (Morphia/Driver/MongoDB)

Morphia: 0.99
Driver: 2.3 (as resolved by maven)
MongoDB: 2.0.2

Please include a stack trace below:
No stack trace

MongoDB doesn't allow field keys to have dots within them, but Morphia 
successfully saves such illegal keys to mongo. I'd expect an attempt to save an 
illegal key would fail, especially when it's not possible to save an illegal 
key through the mongo shell.

Test Code:

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import org.bson.types.ObjectId;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.dao.BasicDAO;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

@Entity
class MyObj {
    @Id
    private ObjectId id;
    public Map<String, Boolean> myMap = new HashMap<String, Boolean>();

    public static void main(String[] args) throws UnknownHostException,
            MongoException {

        MyObj obj = new MyObj();
        obj.runTest();

    }

    public void runTest() throws UnknownHostException, MongoException {
        Morphia morphia = new Morphia();
        morphia.map(MyObj.class);

        MyObjDAO dao =
                new MyObjDAO(morphia.createDatastore(new Mongo(), "test"));

        this.myMap.put("test.test", true);

        dao.save(this);
    }

    public class MyObjDAO extends BasicDAO<MyObj, ObjectId> {

        public MyObjDAO(Datastore ds) {
            super(MyObj.class, ds);
        }

    }
}

Test Output:
{
  "className": "MyObj",
  "myMap": {
    "test.test": true
  }
}

Original issue reported on code.google.com by crbun...@gmail.com on 23 Mar 2012 at 3:25

GoogleCodeExporter commented 9 years ago
You need to upgrade the java driver for this behavior to be checked. It is not 
a feature nor responsibility in morphia.

Original comment by scotthernandez on 23 Mar 2012 at 3:30

GoogleCodeExporter commented 9 years ago
Thanks for the fast response, I didn't realise the Java driver was so far ahead 
of the version referenced in the Morphia POM, and have forced the use of the 
latest version, which as you said changed the behaviour to what I expected.

Original comment by crbun...@gmail.com on 23 Mar 2012 at 3:38