Registry Mirror
Skip to content

PUT method calling from_json although resource doesn't exist #207

Closed

Description

@kgish

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

Activity

  1. Asmod4n commented on Dec 29, 2014

    @Asmod4n
    Member

    PUT is there to create or update a resource, so its working as indented?

  2. kgish commented on Dec 29, 2014

    @kgish
    Author

    I thought that PUT only updated and did not create if no resource. What's the purpose of the id then?

  3. Asmod4n commented on Dec 29, 2014

    @Asmod4n
    Member

    To give that resource a id?
    E.g. to create /products/25

  4. seancribbs commented on Dec 29, 2014

    @seancribbs
    Member

    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.

  5. bethesque commented on Dec 29, 2014

    @bethesque
    Contributor

    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.

  6. seancribbs commented on Dec 30, 2014

    @seancribbs
    Member

    RFC 7231 Section 4.3.4 states (emphasis mine):

    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.

  7. seancribbs commented on Dec 30, 2014

    @seancribbs
    Member

    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).

    There's more than one way to spin this problem.

  8. kgish commented on Dec 30, 2014

    @kgish
    Author

    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.

  9. seancribbs commented on Dec 30, 2014

    @seancribbs
    Member

    Yeah, I think one of the inventors of the Erlang version called it "leaky in just the right way".

  10. bethesque commented on Jan 1, 2015

    @bethesque
    Contributor

    Closeable?

  11. shardnit commented on Aug 19, 2015

    @shardnit

    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.

  12. bethesque commented on Aug 19, 2015

    @bethesque
    Contributor

    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?

  13. shardnit commented on Aug 19, 2015

    @shardnit

    👍

  14. kgish commented on Aug 19, 2015

    @kgish
    Author

    Sounds good to me as well.

  15. seancribbs commented on Aug 19, 2015

    @seancribbs
    Member

    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.

  16. bethesque commented on Aug 19, 2015

    @bethesque
    Contributor

    Have updated doco with a note in the PUT example, and a link to this issue. Thanks for the pointer @shardnit.

  17. shardnit commented on Aug 20, 2015

    @shardnit

    Good that docs pointed to this issue. Its good to know the correct semantics of PUT, as @seancribbs raised, for handling non-existent resource.

  18. kgish commented on Aug 20, 2015

    @kgish
    Author

    Cool, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions