spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

@Unwrapped for Map-like types

Open alexplavan opened this issue 3 years ago • 4 comments

Hello.

It would be nice to support @Unwrapped annotation for fields of Map-like type to include arbitrary fields in serialized BSON and deserialize them back.

For example:

@Data
@Document
public class TestEntity {
    private String id;
    @Unwrapped(onEmpty = USE_NULL)
    private Map<String, Integer> data;
}

This object

TestEntity testEntity = new TestEntity();
testEntity.setId("123");
testEntity.setData(Map.of("foo", 1, "bar", 2));

should be transformed to following json and vice versa:

{
  "id": "123",
  "foo": 1,
  "bar": 2
}

alexplavan avatar Feb 17 '23 17:02 alexplavan

Thanks @Stingray42 for raising the topic. Will take it to the team.

christophstrobl avatar Feb 22 '23 08:02 christophstrobl

Though the idea is interesting we do not plan to add this feature. Matching properties against the keys within the document to figure out the ones that need to go into the map does not fit well with how the mapping layer works and how it is intended to work.

christophstrobl avatar Feb 28 '23 09:02 christophstrobl

Sad, but understandable. Guess I will stick with BeforeSaveCallback/AfterConvertCallback pair that do the stuff.

alexplavan avatar Feb 28 '23 17:02 alexplavan

I would've liked this feature as well, surprised that it didn't work.

johanhaleby avatar Nov 21 '23 10:11 johanhaleby