Skip to content

Unexpected serialization of List<Object> fields [DATACOUCH-560] #868

New issue

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

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

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue May 28, 2020 · 0 comments
Closed
Assignees
Labels
in: mapping Mapping and conversion infrastructure type: bug A general bug

Comments

@spring-projects-issues
Copy link

spac-valentin opened DATACOUCH-560 and commented

I have an entity without a strict structure (user can add custom fields/json objects)

@Document
public class MyClass {
    @Id
    private String id;

    @Field
    private List<Map<String, Object>> fields; 
    @Field
    private List<Map<String, Object>> configuration;

MappingCouchbaseConverter converts this into a mix of CouchbaseDocuments & CouchbaseLists and sends it to couchbase Transcoder which encodes it and stores it into DB.

Issue: JacksonJsonSerializer used by JsonTranscoder serialises the above object into

{
   "configuration":{
      "empty":false
   },
   "_class":"org.myorg.MyEntity",
   "fields":{
      "empty":false
   }
}

Workaround:
In your CouchbaseConfiguration, override configureEnvironment and provide a custom object mapper to handle this scenario

 @Override
    protected void configureEnvironment(ClusterEnvironment.Builder builder) {
        Jackson2ObjectMapperBuilder jacksonBuilder = new Jackson2ObjectMapperBuilder();
        jacksonBuilder.serializationInclusion(JsonInclude.Include.NON_NULL);

        jacksonBuilder.modules(
                new CouchbaseJacksonModule(),
                new JsonValueModule() // Don't forget to add this module provided by couchbase client
        );
        
        builder.jsonSerializer(JacksonJsonSerializer.create(objectMapper()));
        super.configureEnvironment(builder);
    }

CouchbaseJacksonModule

public class CouchbaseJacksonModule extends SimpleModule {

    public CouchbaseJacksonModule() {
        super(new Version(1, 0, 0, null, "org.myorg", "CouchbaseWorkaround"));
        addSerializer(CouchbaseDocument.class, new CouchbaseDocumentSerializer());
        addSerializer(CouchbaseList.class, new CouchbaseListSerializer());
    }

    public static class CouchbaseDocumentSerializer extends JsonSerializer<CouchbaseDocument> {

        @Override
        public void serialize(CouchbaseDocument value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            Map<String, Object> valueContent = value.getContent();
            JsonSerializer<Object> serializer = serializers.findValueSerializer(valueContent.getClass());
            serializer.serialize(valueContent, gen, serializers);
        }
    }

    public static class CouchbaseListSerializer extends JsonSerializer<CouchbaseList> {

        @Override
        public void serialize(CouchbaseList value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            List<Object> objects = value.export();
            JsonSerializer<Object> serializer = serializers.findValueSerializer(objects.getClass());
            serializer.serialize(objects, gen, serializers);
        }
    }
}

I was expecting this to work OOTB


Affects: 4.0 GA (Neumann)

Referenced from: pull request runnerway@fed0e5f

1 votes, 2 watchers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants