spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

Nested JSON Handling Issue in Generic Class #4669

Open MerzoukeMansouri opened 7 months ago

MerzoukeMansouri commented 7 months ago

I encountered an issue while working with Spring Data MongoDB latest version, where nested JSON objects within a generic class are not being handled correctly. Specifically, when attempting to interact with nested JSON structures using Spring Data MongoDB repositories, the behavior does not align with expectations.

Here a repository to reproduce.

Potential bug discovered

  1. When attempting to load a complex structure involving generic classes and nested JSON within a Spring Data MongoDB document, the document fails to load correctly. Despite following standard procedures for data retrieval, the loaded document exhibits inconsistencies, particularly regarding the handling of nested JSON structures. image

  2. It's observed that the order of attributes in the entity class has a peculiar effect on the loading process. Specifically, the first generic item within the entity seems to be handled correctly, while the subsequent ones are not processed as expected. This behavior is unexpected and indicates a potential issue with the loading mechanism. image

  3. Upon inspection, it becomes evident that the first generic class is loaded correctly, whereas the second one fails to be properly processed during the document loading procedure. This inconsistency in handling generic classes within the document poses a significant hurdle, leading to data integrity concerns and unpredictable behavior.

Set to reproduce demo repository

  1. Set up a Spring Boot project with version 3.2.3 of spring-boot-starter-parent.
  2. Define an entity class (MyEntity) with two attributes (attribute1 and attribute2) that are both of the same generic class (MyGenericClass), but with different nested JSON structures.tures.
  3. Define two different structures (Structure1 and Structure2) for the nested JSON within MyGenericClass.
  4. Implement the repository for MyEntity to handle CRUD operations.
  5. Save instances of MyEntity with different data for attribute1 and attribute2 using MyEntityRepository.save().
  6. Attempt to load the saved MyEntity instances from the database using MyEntityRepository.findAll() or other retrieval methods.

Expected Behavior: The saved MyEntity instances should be retrieved from the database without any errors or data loss. Both attribute1 and attribute2 should contain their respective nested JSON structures (Structure1 and Structure2).

Actual Behavior: Upon loading the MyEntity instances, potential issues may arise where the nested JSON structures within attribute1 and attribute2 are not correctly parsed or mapped, leading to unexpected behavior or data inconsistency.

christophstrobl commented 7 months ago

@MerzoukeMansouri thank you for getting in touch! Would you mind adding a (failing) junit test to the reproducer? Thank you!

MerzoukeMansouri commented 7 months ago

@MerzoukeMansouri thank you for getting in touch! Would you mind adding a (failing) junit test to the reproducer? Thank you!

Here you are, feel free to ask me any improvements.

Thanks.

DemoRepositoryTest.should_load_nested()

christophstrobl commented 4 months ago

thank you @MerzoukeMansouri - I do see the error now. Need to dig a bit deeper as it seems the _class type hint is not being recognized when reading back the type within the set.

christophstrobl commented 4 months ago

The issue seems to have been resolved in the meantime. The mapping does the expected from boot:3.2.4 (data-mongo:4.2.4) onwards.