Skip to content

MappingMongoConverter replaces non-@DocumentReference nested objects with same _id #4098

Closed
@yadavdev

Description

@yadavdev

A change was introduced as part of #3602
Commit hash: 6ed274b

There seems to be a cache which was introduced while implementing above

if (hasIdentifier(bson)) {
	S existing = findContextualEntity(context, entity, bson);
	if (existing != null) {
		return existing;
	}
}

This however has an unintended effect for self referencing objects which don't use @DocumentReference, @Reference or @DbRef. Deserialization of these objects don't match the original saved object.

@Document
public class Form {
        @Id
	private String id;
	private Form subForm;
}

Sample Json:


{
	"_id" : "dummyFormId", 
	"name" : "form name 1",
	"child" : {
		"_id" : "dummyFormId",
		"name" : "form name 2"
}

After being inserted correctly the object on fetching returns an infinitely nested object:

{
	"_id" : "dummyFormId", 
	"name" : "form name 1",
	"child" : {
		{
	           "_id" : "dummyFormId", 
	           "name" : "form name 1",
	          "child" : {
		      ....
	}
}

Sample test code:

    @Test
    public void testNestedForms() {
        MongoTemplate mongoTemplate = getMongoTemplate();
        
        Form form = new Form();
        form.setId("dummyId");
        form.setName("formName1");

        Form subForm = new Form();
        subForm.setId("dummyId");
        subForm.setName("formName2");
        form.setSubForm(subForm);

        mongoTemplate.save(form);
        //fails
        Assert.assertEquals(form.getSubForm().getName(), formFromMongo.getSubForm().getName());
    }

    @Document
    public static class Form {
        @Id
        private String id;
        private String name;
        private Form subForm;
    }

Shouldn't this behaviour be limited to properties marked with reference annotations?

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions