Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
yadavdev opened this issue Jun 27, 2022 · 1 comment
Assignees
Labels
type: bug A general bug

Comments

@yadavdev
Copy link

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?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 27, 2022
@christophstrobl christophstrobl self-assigned this Aug 1, 2022
@christophstrobl christophstrobl added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 3, 2022
@christophstrobl
Copy link
Member

Sorry for long silence & thanks for reporting - we'll take care of it.

@mp911de mp911de changed the title 3.x MongoMappingConvertor replacing non @DocumentReference, @Reference nested objects with same _id MappingMongoConverter replaces non-@DocumentReference nested objects with same _id Aug 5, 2022
mp911de pushed a commit that referenced this issue Aug 5, 2022
…stance of same type.

This commit ensures to fully resolve non association values from the given source document instead of trying attempt a by id lookup in already resolved instances.

Closes: #4098
Original pull request: #4133.
mp911de added a commit that referenced this issue Aug 5, 2022
Encapsulate nested object lookup. Refine method signatures and tweak Javadoc.

See #4098
Original pull request: #4133.
@mp911de mp911de closed this as completed in 1e7dc7c Aug 5, 2022
mp911de added a commit that referenced this issue Aug 5, 2022
Encapsulate nested object lookup. Refine method signatures and tweak Javadoc.

See #4098
Original pull request: #4133.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: bug A general bug
Projects
None yet
4 participants