You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2020/07/09 18:27:12 UTC
[GitHub] [couchdb] iilyak opened a new pull request #2991: Simplify using `req_body` for JSON requests
iilyak opened a new pull request #2991:
URL: https://github.com/apache/couchdb/pull/2991
## Overview
Currently the EPI plugins have no easy way to modify body of the document in
before request. There are complicated approaches via overriding compression header.
In any case you are obligated to serialize your data structure into JSON.
This is due to the fact that `chttp:json_body/1` expects compressed body.
We can rely on the fact that `MochiReq:recv_body/1` returns binary to allow
passing of already parsed JSON terms (objects and lists).
If we do so we can modify request's body as easy as:
```
Req#httpd{req_body = {[{<<"hello">>, <<"couch">>}]}}
```
## Testing recommendations
`make check`
## Related Issues or Pull Requests
<!-- If your changes affects multiple components in different
repositories please put links to those issues or pull requests here. -->
## Checklist
- [x] Code is written and works correctly
- [ ] Changes are covered by tests
- [ ] Any new configurable parameters are documented in `rel/overlay/etc/default.ini`
- [ ] A PR for documentation changes has been made in https://github.com/apache/couchdb-documentation
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [couchdb] iilyak merged pull request #2991: Simplify using `req_body` for JSON requests
Posted by GitBox <gi...@apache.org>.
iilyak merged pull request #2991:
URL: https://github.com/apache/couchdb/pull/2991
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [couchdb] nickva commented on a change in pull request #2991: Simplify using `req_body` for JSON requests
Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2991:
URL: https://github.com/apache/couchdb/pull/2991#discussion_r452495868
##########
File path: src/chttpd/src/chttpd.erl
##########
@@ -681,8 +681,10 @@ json_body(Httpd) ->
case body(Httpd) of
undefined ->
throw({bad_request, "Missing request body"});
+ Body when is_binary(Body) ->
+ ?JSON_DECODE(maybe_decompress(Httpd, Body));
Body ->
Review comment:
Ah good point we do match on that in json_body_obj.
In this function, if we support arbitrary ejson values there would still be an ambiguity between a terminal string value say `<<"a">>` and the encoded form `<<"\"a\"">>` as they'd both be binaries. We have that case in the `_config` handler to set values. I think that just means in that case if we want to override req_body, we'd have to supply the encoded form.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [couchdb] nickva commented on a change in pull request #2991: Simplify using `req_body` for JSON requests
Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2991:
URL: https://github.com/apache/couchdb/pull/2991#discussion_r452943669
##########
File path: src/chttpd/src/chttpd.erl
##########
@@ -681,8 +681,10 @@ json_body(Httpd) ->
case body(Httpd) of
undefined ->
throw({bad_request, "Missing request body"});
+ Body when is_binary(Body) ->
+ ?JSON_DECODE(maybe_decompress(Httpd, Body));
Body ->
Review comment:
That's better solution and removes the ugly hack with post_body
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [couchdb] iilyak commented on a change in pull request #2991: Simplify using `req_body` for JSON requests
Posted by GitBox <gi...@apache.org>.
iilyak commented on a change in pull request #2991:
URL: https://github.com/apache/couchdb/pull/2991#discussion_r452432218
##########
File path: src/chttpd/src/chttpd.erl
##########
@@ -681,8 +681,10 @@ json_body(Httpd) ->
case body(Httpd) of
undefined ->
throw({bad_request, "Missing request body"});
+ Body when is_binary(Body) ->
+ ?JSON_DECODE(maybe_decompress(Httpd, Body));
Body ->
Review comment:
the `{Props}` wouldn't work for the case when JSON term is a list of items.
We have `{Props}` match in `json_body_obj/1` bellow.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [couchdb] iilyak commented on a change in pull request #2991: Simplify using `req_body` for JSON requests
Posted by GitBox <gi...@apache.org>.
iilyak commented on a change in pull request #2991:
URL: https://github.com/apache/couchdb/pull/2991#discussion_r452726313
##########
File path: src/chttpd/src/chttpd.erl
##########
@@ -681,8 +681,10 @@ json_body(Httpd) ->
case body(Httpd) of
undefined ->
throw({bad_request, "Missing request body"});
+ Body when is_binary(Body) ->
+ ?JSON_DECODE(maybe_decompress(Httpd, Body));
Body ->
Review comment:
Good point. I think the easiest way to support this corner case would be to bypass call to body.
```
body(#httpd{req_body=undefined}) ->
case body(Httpd) of
...
end;
body(#httpd{req_body=ReqBody}) ->
ReqBody.
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [couchdb] nickva commented on a change in pull request #2991: Simplify using `req_body` for JSON requests
Posted by GitBox <gi...@apache.org>.
nickva commented on a change in pull request #2991:
URL: https://github.com/apache/couchdb/pull/2991#discussion_r452426181
##########
File path: src/chttpd/src/chttpd.erl
##########
@@ -681,8 +681,10 @@ json_body(Httpd) ->
case body(Httpd) of
undefined ->
throw({bad_request, "Missing request body"});
+ Body when is_binary(Body) ->
+ ?JSON_DECODE(maybe_decompress(Httpd, Body));
Body ->
Review comment:
Let's explicitly match on `{Props} when is_list(Props)` (or similar ejson match) to assert that the only other option is ejson here.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org