Open
Conversation
be246e8 to
38f442b
Compare
83bbb60 to
f421bc9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce body parsing to Hanami Action, which runs according to the action's configured format. Now Hanami Action can be used completely standalone and provide all the functionality you would expect when it comes to parsing and handling params.
By default, it includes default body parsers for JSON and multipart form requests.
The overall approach to body parsing is the same as Hanami Router, which this new feature will supersede for full Hanami apps:
{_: parsed_body}.request.params.Because parsing is now handled at the action level, this means a more "correct" overall behaviour, however, because body parsing will only occur if the request matches on of the action's accepted formats.
Failed body parsing will raise a new
Hanami::Action::BodyParsingError, which users can handle in their own action classes if they wish to provide a custom response to failed parsing.This also includes compatibility with router-handled parsing: if
env["router.parsed_body"]is already set, body parsing will be skipped, and the"router.parsed_body"and"router.params"keys are used.Other details
formats.register(:custom, "application/custom", parser: ->(body, env) { ... })formats.body_parsers["application/custom"] = parserbodybeing given, we also passenvin case future parsers need to access headers or other Rack APIs that require then env (which we actually do inside the multipart form parser).Hanami::Action::BodyParser. This is mostly about mutating the rack env, but because body parsing is such a distinct concern, keeping this separate allows the code to be understood most easily (i.e. we don't makeaction.rbso big)Mimepublic, so they could be re-used here.Action#call, since body parsing occurs early, its error is caught and then "suspended" until therequestandresponseobjects are built, and at that point re-raised, which ensures custom exception handlers can have access to therequestandresponseobjects they expect.Overall, this feels like a really solid improvement, but there's one gotcha: multipart form bodies won't be parsed unless you add
formats.accept :html. Same deal for JSON bodies andformats.accept :json. This is because we connect body parsing with format acceptance. This is IMO the most "correct" behaviour, but it will mean that we will probably need to encourage Hanami app authors to specify a default format to accept across their actions. For Hanami 2.3, our stopgap measure we just to parse multipart forms and JSON bodies at all times (if such bodies were provided). This required zero boilerplate, but it is possibly "overstepping" a little, adding behaviour where you didn't expect it.Fixes #484