You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
PUT method calling from_json although resource doesn't exist #207
For some reason PUT is still hitting the from_json method although the resource_exists? method returns a big bad false, any reason I still see the following?
[2014-12-29 22:02:49] INFO WEBrick 1.3.1
[2014-12-29 22:02:49] INFO ruby 2.1.5 (2014-11-13) [x86_64-linux]
[2014-12-29 22:02:49] INFO Webmachine::Adapters::WEBrick::Server#start: pid=1679 \
port=8080
ProductResource: resource_exists => false
ProductResource: from_json => PUT
localhost - - [29/Dec/2014:22:02:51 CET] "PUT /products/25 HTTP/1.1" 204 0
- -> /products/25
PUT semantics clearly state that one can create a non-existing resource. There are several ways to disallow that... just depends on what you are trying to do.
The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload. A successful PUT of a given representation would suggest that a subsequent GET on that same target resource will result in an equivalent representation being sent in a 200 (OK) response. However, there is no guarantee that such a state change will be observable, since the target resource might be acted upon by other user agents in parallel, or might be subject to dynamic processing by the origin server, before any subsequent GET is received. A successful response only implies that the user agent's intent was achieved at the time of its processing by the origin server.
If the target resource does not have a current representation and the PUT successfully creates one, then the origin server MUST inform the user agent by sending a 201 (Created) response. If the target resource does have a current representation and that representation is successfully modified in accordance with the state of the enclosed representation, then the origin server MUST send either a 200 (OK) or a 204 (No Content) response to indicate successful completion of the request.
So, we must know what the state of the resource was before the PUT was accepted to know whether to reply with 200, 204, or 201.
Of course, Webmachine's design is intentionally leaky. If you are ahead of the resource_exists? callback, you can call it directly from, say, allowed_methods and exclude PUT if you like (resulting in 405 response). Alternately, you could implement is_conflict? and check whether the internal resource state represents a creation, returning true (resulting in 409 response).
Perhaps, add a comment to the PUT example document. Hours after investigating, when resource_exists? not returning 404 for a PUT on non-available resource, I realized that I need to handle it in is_conflict? callback. In that sense, resource_available? callback returning true or false doesnt make any difference to PUT requests.
My head is a bit out of this space, but happy to update the docs. Something like "Note that the return value of resource_exists? does not affect the decision flow for PUT. If you wish to disallow PUT to a resource that does not already exist, then return true from is_conflict?
For PUT, resource_exists? only determines whether condition validation is done or not. If you would prefer only to accept the resource if it exists, the client should send If-Match: *, which will result in 412 Precondition Failed IIRC. The semantics of PUT are literally "put this thing here", even if that URI does not represent something already existing.
Activity
Asmod4n commentedon Dec 29, 2014
PUT is there to create or update a resource, so its working as indented?
kgish commentedon Dec 29, 2014
I thought that PUT only updated and did not create if no resource. What's the purpose of the
idthen?Asmod4n commentedon Dec 29, 2014
To give that resource a id?
E.g. to create /products/25
seancribbs commentedon Dec 29, 2014
PUT semantics clearly state that one can create a non-existing resource. There are several ways to disallow that... just depends on what you are trying to do.
bethesque commentedon Dec 29, 2014
Would there be a difference in the flow if
resource_exists?was not implemented for PUT? Just thinking of adding a comment to https://github.com/seancribbs/webmachine-ruby/blob/master/documentation/examples.md#put to clarify the behaviour.seancribbs commentedon Dec 30, 2014
RFC 7231 Section 4.3.4 states (emphasis mine):
So, we must know what the state of the resource was before the PUT was accepted to know whether to reply with 200, 204, or 201.
seancribbs commentedon Dec 30, 2014
Of course, Webmachine's design is intentionally leaky. If you are ahead of the
resource_exists?callback, you can call it directly from, say,allowed_methodsand exclude PUT if you like (resulting in 405 response). Alternately, you could implementis_conflict?and check whether the internal resource state represents a creation, returningtrue(resulting in 409 response).There's more than one way to spin this problem.
kgish commentedon Dec 30, 2014
Well "leaky" sounds a bit negative, I'd prefer to call it "flexible" ;) I think that
is_conflict?sounds like an option, so I'll look into it.seancribbs commentedon Dec 30, 2014
Yeah, I think one of the inventors of the Erlang version called it "leaky in just the right way".
bethesque commentedon Jan 1, 2015
Closeable?
shardnit commentedon Aug 19, 2015
Perhaps, add a comment to the PUT example document. Hours after investigating, when
resource_exists?not returning 404 for a PUT on non-available resource, I realized that I need to handle it inis_conflict?callback. In that sense,resource_available?callback returning true or false doesnt make any difference to PUT requests.bethesque commentedon Aug 19, 2015
My head is a bit out of this space, but happy to update the docs. Something like "Note that the return value of resource_exists? does not affect the decision flow for PUT. If you wish to disallow PUT to a resource that does not already exist, then return true from
is_conflict?shardnit commentedon Aug 19, 2015
👍
kgish commentedon Aug 19, 2015
Sounds good to me as well.
seancribbs commentedon Aug 19, 2015
For
PUT,resource_exists?only determines whether condition validation is done or not. If you would prefer only to accept the resource if it exists, the client should sendIf-Match: *, which will result in412 Precondition FailedIIRC. The semantics ofPUTare literally "put this thing here", even if that URI does not represent something already existing.bethesque commentedon Aug 19, 2015
Have updated doco with a note in the PUT example, and a link to this issue. Thanks for the pointer @shardnit.
shardnit commentedon Aug 20, 2015
Good that docs pointed to this issue. Its good to know the correct semantics of PUT, as @seancribbs raised, for handling non-existent resource.
kgish commentedon Aug 20, 2015
Cool, thanks.