Skip to content

Reference lookup query fails for empty @DocumentReference List and Map #3805

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
Izimbergenov opened this issue Sep 8, 2021 · 1 comment
Closed
Labels
in: references DBRef and @DocumentReference type: bug A general bug

Comments

@Izimbergenov
Copy link

Izimbergenov commented Sep 8, 2021

Hello,

I have a problem when using @DocumentReference with an empty list. Specifically when we initialize our list as new ArrayList<>(). Below are the test codes: the first one failed, the second one success

static class Book
{
	@MongoId(targetType = FieldType.OBJECT_ID)
	String id;

	String name;
}

static class Library
{
	@MongoId(targetType = FieldType.OBJECT_ID)
	String id;

	@DocumentReference
	private List<Book> books = new ArrayList<>();
}

Failed test code(throws NoSuchElementException when template.findById):

@Test
public void Given_Library_Without_Books_When_Get_By_Id_Then_Success() throws Exception
{
	Library library = new Library();
	library = template.save(library);

	Library existingLibrary = template.findById(library.id, Library.class);
	assertThat(existingLibrary).isNotNull();
}

Success test code:

@Test
public void Given_Library_With_Books_When_Get_By_Id_Then_Success() throws Exception
{
	Book book = new Book();
	book.name = "Harry Potter";
	book = template.save(book);

	Library library = new Library();
	library.books = Arrays.asList(book);
	library = template.save(library);

	Library existingLibrary = template.findById(library.id, Library.class);
	assertThat(existingLibrary).isNotNull();
}

Spring Data MongoDB version: 3.3.0-SNAPSHOT

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 8, 2021
@christophstrobl
Copy link
Member

Thanks for reporting - we'll look into it.

@mp911de mp911de closed this as completed in 4e960a9 Sep 8, 2021
mp911de added a commit that referenced this issue Sep 8, 2021

Verified

This commit was signed with the committer’s verified signature.
mp911de Mark Paluch
Extract query that yields no hits into constant. Guard Map-typed reference properties against empty $or.

See #3805
Original pull request: #3807.
@mp911de mp911de changed the title @DocumentReference empty list problem Reference lookup query fails for empty @DocumentReference List and Map Sep 8, 2021
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 8, 2021
@mp911de mp911de added this to the 3.3 M3 (2021.1.0) milestone Sep 8, 2021
@mp911de mp911de added the in: references DBRef and @DocumentReference label Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: references DBRef and @DocumentReference type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants