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

Unable to augment collection association using POST [DATAREST-523] #897

Closed
spring-projects-issues opened this issue Apr 20, 2015 · 5 comments
Closed
Assignees

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Apr 20, 2015

Endre Czirbesz opened DATAREST-523 and commented

It seems that POSTing an item to an associated collection does not work as documented.

According to the Spring Data REST documentation, POST method is supported for collections, so

curl -i -X POST -H 'Content-type: text/uri-list' -d 'http://localhost:8080/artifacts/1' http://localhost:8080/collectors/1/artifacts

should add an artifact to the (yet empty) collection. Unfortunately, it does not:

HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
Allow: GET, DELETE, PATCH, PUT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 17 Apr 2015 22:54:59 GMT
{"timestamp":1429311299117,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/collectors/1/artifacts"}

My entities:

@Data
@Entity
public class Collector {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToMany(mappedBy = "owner")
    private List<Artifact> artifacts;
}
@Data
@Entity
@Accessors(chain = true, fluent = true)
public class Artifact {
    @Id
    @GeneratedValue
    private Long id;
    private String title;
    @ManyToOne
    @JoinColumn(name = "collector_id")
    private Collector owner;
}

The complete code is on Github.


Affects: 2.2.2 (Evans SR2), 2.3 GA (Fowler)

Reference URL: https://github.com/endrec/associated-collection-test

Referenced from: commits f2d8a99, 08bb441

Backported to: 2.3.1 (Fowler SR1)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented May 19, 2015

Endre Czirbesz commented

Same result in Fowler

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented May 19, 2015

Oliver Drotbohm commented

You should be able to see this working (creating the collection) by using PUT. The documentation is currently misleading cause we're not supporting POST for association resources at all. I'll use this ticket to add this functionality

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented May 19, 2015

Oliver Drotbohm commented

That's fixed for Gosling and the maintenance branch for Fowler. You should actually be able to get this working out of the box by using PATCH instead of POST. We chose this as the method to use as it resembles the "partial update" semantics more than POST does. Although you're interacting with a collection, you don't use the resource to create new resources but rather to augment the existing one.

That said, I took the chance to revamp the internals of the checks for supported HTTP methods. POST is now allowed on collection properties and basically resembles what PATCH does

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 11, 2015

Endre Czirbesz commented

Still not work as (I think) it is intended.

curl -i -X POST -H 'Content-type: text/uri-list' -d 'http://localhost:8080/artifacts/1' http://localhost:8080/collectors/1/artifacts

Returns HTTP/1.1 204 No Content, which looks promising, but on the other hand, no association is created:

2015-06-11 11:38:08.339 DEBUG 29673 --- [nio-8080-exec-1] o.s.d.r.w.RepositoryRestHandlerMapping   : Looking up handler method for path /collectors/1/artifacts
2015-06-11 11:38:08.364 DEBUG 29673 --- [nio-8080-exec-1] o.s.d.r.w.RepositoryRestHandlerMapping   : Returning handler method [public org.springframework.http.ResponseEntity<? extends org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.createPropertyReference(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.http.HttpMethod,org.springframework.hateoas.Resources<java.lang.Object>,java.io.Serializable,java.lang.String) throws java.lang.Exception]
2015-06-11 11:38:08.562 DEBUG 29673 --- [nio-8080-exec-1] stomAnnotationTransactionAttributeSource : Adding transactional method 'findOne' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2015-06-11 11:38:08.587 DEBUG 29673 --- [nio-8080-exec-1] ataPostProcessor$ThreadBoundTargetSource : Initializing lazy target object
2015-06-11 11:38:08.594 DEBUG 29673 --- [nio-8080-exec-1] org.hibernate.SQL                        : select collector0_.id as id1_1_0_, collector0_.name as name2_1_0_ from collector collector0_ where collector0_.id=?
2015-06-11 11:38:08.595 TRACE 29673 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2015-06-11 11:38:08.619 TRACE 29673 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([name2_1_0_] : [VARCHAR]) - [Ann]
2015-06-11 11:38:08.630 TRACE 29673 --- [nio-8080-exec-1] org.hibernate.type.CollectionType        : Created collection wrapper: [example.Collector.artifacts#1]
2015-06-11 11:38:08.634 DEBUG 29673 --- [nio-8080-exec-1] stomAnnotationTransactionAttributeSource : Adding transactional method 'findOne' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2015-06-11 11:38:08.634 DEBUG 29673 --- [nio-8080-exec-1] ataPostProcessor$ThreadBoundTargetSource : Initializing lazy target object
2015-06-11 11:38:08.634 DEBUG 29673 --- [nio-8080-exec-1] org.hibernate.SQL                        : select artifact0_.id as id1_0_0_, artifact0_.collector_id as collecto3_0_0_, artifact0_.title as title2_0_0_, collector1_.id as id1_1_1_, collector1_.name as name2_1_1_ from artifact artifact0_ left outer join collector collector1_ on artifact0_.collector_id=collector1_.id where artifact0_.id=?
2015-06-11 11:38:08.648 TRACE 29673 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2015-06-11 11:38:08.649 TRACE 29673 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([id1_1_1_] : [BIGINT]) - [null]
2015-06-11 11:38:08.650 TRACE 29673 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([collecto3_0_0_] : [BIGINT]) - [null]
2015-06-11 11:38:08.650 TRACE 29673 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicExtractor   : extracted value ([title2_0_0_] : [VARCHAR]) - [Ugly sculpture]

It is pretty much the same for PUT, or PATCH requests

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 11, 2015

Endre Czirbesz commented

Ignore the previous comment, I was on the wrong side of the relationship. It works fine form the owning side.

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

Successfully merging a pull request may close this issue.

None yet
2 participants