Skip to content

Document with Map decorated with @NonNull and @DocumentReference fails to deserialize #5065

@bamapookie

Description

@bamapookie

Given the following class:

package org.example.mongoemptymaptest.model;

import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.DocumentReference;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Data
@Builder
@Document(collection = "parent_documents")
public class ParentDocument {

    @Id
    @Builder.Default
    UUID id = UUID.randomUUID();

    @NonNull
    @Builder.Default
    @DBRef
    Map<String, ChildDocument> dbRefChildren = new HashMap<>();

    @NonNull
    @Builder.Default
    @DBRef(lazy = true)
    Map<String, ChildDocument> lazyDbRefChildren = new HashMap<>();

    @NonNull
    @Builder.Default
    @DocumentReference
    Map<String, ChildDocument> documentReferenceChildren = new HashMap<>();

    @NonNull
    @Builder.Default
    @DocumentReference(lazy = true)
    Map<String, ChildDocument> lazyDocumentReferenceChildren = new HashMap<>();
}

The document will fail to deserialize if documentReferenceChildren is an empty map, and will fail to properly resolve the map for lazyDocumentReferenceChildren if it is an empty map. In that case, getLazyDocumentReferenceChildren().values() method will return null.

Unit tests illustrating the bug: https://github.com/bamapookie/MongoEmptyMapTest

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions