spring-projects / spring-projects/spring-data-mongodb

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

Open
#5,065 4 comments 0 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Oct 6, 2025.

in: mapping type: bug
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.