spring-projects / spring-projects/spring-data-mongodb
Inconsistent type alias placement in documents with subclasses [DATAMONGO-1526]
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
Tomasz Grabarczyk opened DATAMONGO-1526 and commented
The type alias _class is placed differently when saving a document and differently when creating a query, if the document contains a class that contains a subclass. This causes an incorrect query mismatch
Example: I have the following classes:
@Document
public class TestDocument {
@Id private String id;
private ClassA classA;
public TestDocument(ClassA classA) {
this.classA = classA;
}
}
class ClassA {
private ClassB classB;
public ClassA(ClassB classB) {
this.classB = classB;
}
}
public abstract class ClassB { }
class ClassBImpl extends ClassB {
private String field;
public ClassBImpl(String field) {
this.field = field;
}
}
Now I run the following code:
ClassA classA = new ClassA(new ClassBImpl("value"));
mongoTemplate.insert(new TestDocument(classA));
Query query = Query.query(Criteria.where("classA").is(classA));
TestDocument result = mongoTemplate.findOne(query, TestDocument.class);
System.out.println(result); // prints: null
I would expect that this query should return the document I inserted at the beginning, but it doesn't. The reason for that is that the ClassA is converted differently when saving document, and differently when creating the query:
Document saved to the database:
"_id" : ObjectId("5820521ff9854f15481ce52b"),
"_class" : "TestDocument",
"classA" : {
"classB" : {
"_class" : "ClassBImpl",
"field" : "value"
}
}
The query:
{ "classA" : { "classB" : { "field" : "value"}}}
(the query does not have the _class field)
Affects: 1.10 M1 (Ingalls), 1.9.4 (Hopper SR4)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.