api/server/httputils: add ReadJSON() utility and fix handling of invalid JSON#43463
Conversation
1248903 to
a7b55f2
Compare
a7b55f2 to
2d95e3b
Compare
ee3c291 to
34c20ce
Compare
samuelkarp
left a comment
There was a problem hiding this comment.
This is a good improvement to consistency and readability, LGTM!
| return errdefs.InvalidParameter(errors.Wrap(err, "invalid JSON")) | ||
| } | ||
|
|
||
| if dec.More() { |
There was a problem hiding this comment.
Does this method (officially/intentionally) still work correctly after the body is closed?
(The implementation in it appears to do a peek() but I'm not sure if that's on an internal buffer?)
There was a problem hiding this comment.
I changed the r.Body.Close() to a defer r.Body.Close(), just in case 👍
| return w.Config, hc, w.NetworkingConfig, nil | ||
| } | ||
|
|
||
| func loadJSON(src io.Reader, out interface{}) error { |
There was a problem hiding this comment.
Maybe we could have a very small comment that explains why this is separate with a link back to the "original" implementation? 😇
There was a problem hiding this comment.
Added a comment:
// loadJSON is similar to api/server/httputils.ReadJSON()|
(Two minor comments - neither are blockers IMO) |
…ogging Signed-off-by: Sebastiaan van Stijn <[email protected]>
Implement a ReadJSON() utility to help reduce some code-duplication,
and to make sure we handle JSON requests consistently (e.g. always
check for the content-type).
Differences compared to current handling:
- prevent possible panic if request.Body is nil ("should never happen")
- always require Content-Type to be "application/json"
- be stricter about additional content after JSON (previously ignored)
- but, allow the body to be empty (an empty body is not invalid);
update TestContainerInvalidJSON accordingly, which was testing the
wrong expectation.
- close body after reading (some code did this)
We should consider to add a "max body size" on this function, similar to
https://github.com/moby/moby/blob/7b9275c0da707b030e62c96b679a976f31f929d3/api/server/middleware/debug.go#L27-L40
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Implement similar logic as is used in httputils.ReadJSON(). Before this patch, endpoints using the ContainerDecoder would incorrectly return a 500 (internal server error) status. Signed-off-by: Sebastiaan van Stijn <[email protected]>
34c20ce to
b6d58d7
Compare
|
Windows 2022 failure is unrelated (known flaky test); #43012 |
Implement a
httputils.ReadJSON()utility to help reduce some code-duplication, and to make sure we handle JSON requests consistently (e.g. always check for the content-type).Differences compared to current handling:
We should consider to add a "max body size" on this function, similar to
moby/api/server/middleware/debug.go
Lines 27 to 40 in 7b9275c
In addition to the changes above, two commits are included in this PR that:
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)