Skip to content
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

Example for ManyToOne/OneToOne relationship update #55

Open
rainmore opened this issue Feb 4, 2019 · 4 comments
Open

Example for ManyToOne/OneToOne relationship update #55

rainmore opened this issue Feb 4, 2019 · 4 comments

Comments

@rainmore
Copy link
Contributor

rainmore commented Feb 4, 2019

Could we provide an example for ManyToOne/OneToOne relationship update?

The API update does not update the relationship as expected.

@Mithradatha
Copy link

In order to update a resource and its associations that have their own repositories (that is, those that are not embedded directly in the target resource, but contain a link instead), you must use the PATCH method.

class Book
@Required author: Author;

GET: /api/books => { _links: { self: ..., author: { href: '/api/books/1/author' } } }

POST: /api/books => { author: '/api/authors/2' }

PUT: /api/books/1 => { author: '/api/authors/3' } => FAILURE

PATCH: /api/books/1 => { author: '/api/authors/3' } => SUCCESS

See DATAREST-1001: Unable to update associated resource using PUT request on the item resource

@sante85
Copy link
Collaborator

sante85 commented Feb 12, 2019

ok, provide a pull request

@Mithradatha
Copy link

@sante85 I was responding to "The API update does not update the relationship as expected.". Your library is acting exactly as it should given the [arguable] bug with Spring Data Rest.

As soon as I get chance, I can create a pull request with additional documentation.

@rainmore
Copy link
Contributor Author

rainmore commented Mar 7, 2019

I've tried to update the ManyToOne relationship and I found the usage is that books.updateRelation('author', author);. The relationship update is not in RestService but in Resource.

However, in Spring Data Rest 3.1.5.RELEASE, the PATCH doesn't work for the link update. The working request method is PUT.

curl -i -X PUT -H "Content-Type:text/uri-list" -d "http://localhost:8080/authors/1" http://localhost:8080/books/1/author/ returns HTTP/1.1 204 No Content

curl -i -X PATCH -H "Content-Type:text/uri-list" -d "http://localhost:8080/authors/1" http://localhost:8080/books/1/author/ returns HTTP/1.1 405 Method Not Allowed

Could anyone explain why we need to use PATCH instead of PUT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants