Use :unprocessable_content in generated Devise config for Rack 3.1+
#5797
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.
Background
In Rack v3.1.0, the symbol for HTTP status code 422 was changed from
:unprocessable_entityto:unprocessable_content.As a result, when using rack 3.2 with the following configuration in
config/initializers/devise.rb, a warning is shown on login failure:Warning message:
/path-to-app/vendor/bundle/ruby/3.4.0/gems/devise-4.9.4/lib/devise/failure_app.rb:80: warning: Status code :unprocessable_entity is deprecated and will be removed in a future version of Rack. Please use :unprocessable_content instead.This warning can be resolved by updating the config as follows:
Note that this warning continues to occur even after applying the following Rails patch:
unprocessable_{entity,content}rails/rails#53383Details
This Pull Request fixes the root cause of the warning by adjusting the generated config during
$ rails generate devise:installdepending on the rack version.With this change, newly generated Devise configs will no longer produce warnings.
( Fix #5791 )
For rack 3.1 and higher, this change uses
:unprocessable_contentinstead of:unprocessable_entity.For rack 3.0 and below, it continues to use
:unprocessable_entity.Additional information
DeviseController: Error Status
Controllers under
app/controllers/devisehave also been updated to return the status defined inconfig.responder.error_statuswhen handling errors.If
config.responder.error_statusremains at its default value (:ok), the status falls back to the rack version::unprocessable_contentfor rack 3.1+:unprocessable_entityfor rack 3.0 and belowSymbol selection between
:unprocessable_entityand:unprocessable_contentThe symbol selection between
:unprocessable_entityand:unprocessable_contentis determined by Rack:The behavior of
Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422)depends on the Rack version:The code for
Rack::Utils::SYMBOL_TO_STATUS_CODEcan 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