spring-projects / spring-projects/spring-data-rest

Link should not be created for null references [DATAREST-1415]

Open
#1,774 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

in: content-negotiation type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

ecattez opened DATAREST-1415 and commented

Hi !

I have a ManyToOne (OneToOne leads to the same issue) between Library and Book. In fact, in my case, I would like a book to be in one and only one library.

Library.java

@OneToMany
public Set<Book> getBooks() {
    return books;
}
Book.java

@ManyToOne
public Library getLibrary() {
    return library;
}

I allow the creation of a book, even if a library does not exist yet. So basically :

Book#getLibrary() is null

 

The problem is that LinkCollector / LinkCollectingAssociationHandler do not check the value. It results in the creation of a link to an unknown library that leads to a 404 Not Found :

 

{
    "name": "Discworld",
    "author": "Terry Pratchett",
    "_links": {
        "self": {
            "href": "http://localhost:8080/books/9783492285100"
        },
        "book": {
            "href": "http://localhost:8080/books/9783492285100"
        },
        "library": {
            "href": "http://localhost:8080/books/9783492285100/library"
        }
    }
}

 

The link association should not be created in such a case because it does not exist yet. The library link should not be present until the association of the two entities really happens


Affects: 3.1.9 (Lovelace SR9)

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.