Use :unprocessable_content
for scaffolds with Rack 3.1 or higher
#603
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.
Motivation / Background
In Rack v3.1.0, the symbol for HTTP status code 422 was changed from
:unprocessable_entity
to:unprocessable_content
.Rails supports both
:unprocessable_entity
and:unprocessable_content
, but for scaffolds generated when using Rack 3.1 or higher, it now uses:unprocessable_content
.unprocessable_{entity,content}
rails#53383Detail
This Pull Request updates controllers generated by scaffolds so that when using Rack v3.1 or higher, the HTTP status code returned in JSON format responses is changed from
:unprocessable_entity
to:unprocessable_content
, aligning with the behavior of Rails scaffolds.When using Rack v3.1 or higher, the generated scaffold code is updated as follows:
When using Rack v3.0 or lower, the generated code continues to use
:unprocessable_entity
as before.Additional information
Since the jbuilder gem does not depend on actionpack (specifically action_dispatch) in its gemspec,
the choice between
:unprocessable_entity
and:unprocessable_content
is determined by Rack (not byActionDispatch::Constants::UNPROCESSABLE_CONTENT
):The behavior of
Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422)
depends on the Rack version:The code for
Rack::Utils::SYMBOL_TO_STATUS_CODE
can be found here:https://github.com/rack/rack/blob/v3.2.1/lib/rack/utils.rb#L564-L566
https://github.com/rack/rack/blob/2.0.0/lib/rack/utils.rb#L581-L583