Skip to content

Fix deprecation warning for :unprocessable_entity in scaffold-generated tests with Rack 3.2 #2860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2025

Conversation

taketo1113
Copy link
Contributor

@taketo1113 taketo1113 commented Aug 9, 2025

This Pull Request updates the scaffold and scaffold_controller templates to resolve the following warning that occurs when running the generated tests.

/path-to-app/vendor/bundle/ruby/3.4.0/gems/rspec-rails-8.0.1/lib/rspec/rails/matchers/have_http_status.rb:219: warning: Status code :unprocessable_entity is deprecated and will be removed in a future version of Rack. Please use :unprocessable_content instead.

Fix #2859

Details

It updates the scaffold and scaffold_controller templates for the invalid params cases in #create / #update within request specs and controller specs.

This change modifies lines like expect(response).to have_http_status(:unprocessable_entity) in the following kind of code:

# spec/requests/posts_spec.rb
  describe "POST /create" do
...
    context "with invalid parameters" do
...
      it "renders a response with 422 status (i.e. to display the 'new' template)" do
        post posts_url, params: { post: invalid_attributes }
        expect(response).to have_http_status(:unprocessable_entity)
      end
    end
  end

For rack 3.1 and higher, this change uses :unprocessable_content instead of :unprocessable_entity.
For rack 3.0 and below, it continues to use :unprocessable_entity.
This is because :unprocessable_content was introduced in rack 3.1:
rack/rack#2137

The symbol selection between :unprocessable_entity and :unprocessable_content is handled using Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).

The behavior of Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422) varies depending on the rack version:

Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422)
=> :unprocessable_content # rack 3.1 or higher
=> :unprocessable_entity # rack 3.0 and below

The code for Rack::Utils::SYMBOL_TO_STATUS_CODE can be found here:
https://github.com/rack/rack/blob/79d6820b73d9084a60ec1f9912e3ab00439bd5d3/lib/rack/utils.rb#L564-L566

…ed tests with Rack 3.2

For rack 3.1 and higher, this change uses `:unprocessable_content` instead of `:unprocessable_entity`.
For rack 3.0 and below, it continues to use `:unprocessable_entity`.
@taketo1113
Copy link
Contributor Author

taketo1113 commented Aug 9, 2025

The failing tests for Rails main / Rails ~> 8.0.0 in CI should pass once the following commit is merged:

741488e

# spec/rspec/rails/matchers/have_http_status_spec.rb
  context 'with deprecated rack status codes' do
    it 'supports the original names' do
+      allow(Rack::Utils).to receive(:warn).with(/unprocessable_entity is deprecated/, anything)
      expect(create_response(status: 422)).to have_http_status(:unprocessable_entity)
    end
  end

@JonRowe
Copy link
Member

JonRowe commented Aug 11, 2025

You can cherry pick that commit to make this pass

    RuntimeError:
       Warnings were generated: /home/runner/work/rspec-rails/rspec-rails/lib/rspec/rails/matchers/have_http_status.rb:219: warning: Status code :unprocessable_entity is deprecated and will be removed in a future version of Rack. Please use :unprocessable_content instead.
@taketo1113
Copy link
Contributor Author

@JonRowe Thanks for the comment.

I cherry-picked that commit and the tests passed.

@JonRowe JonRowe merged commit 75e4954 into rspec:main Aug 12, 2025
11 checks passed
@JonRowe
Copy link
Member

JonRowe commented Aug 12, 2025

Thanks!

JonRowe added a commit that referenced this pull request Aug 12, 2025
…ed tests with Rack 3.2 (#2860)

* Fix deprecation warning for :unprocessable_entity in scaffold-generated tests with Rack 3.2

For rack 3.1 and higher, this change uses `:unprocessable_content` instead of `:unprocessable_entity`.
For rack 3.0 and below, it continues to use `:unprocessable_entity`.

* Mute deliberate deprecation

    RuntimeError:
       Warnings were generated: /home/runner/work/rspec-rails/rspec-rails/lib/rspec/rails/matchers/have_http_status.rb:219: warning: Status code :unprocessable_entity is deprecated and will be removed in a future version of Rack. Please use :unprocessable_content instead.

---------

Co-authored-by: Phil Pirozhkov <hello@fili.pp.ru>
@JonRowe
Copy link
Member

JonRowe commented Aug 12, 2025

Released in 8.0.2

@taketo1113 taketo1113 deleted the fix-warning-rack-unprocessable_content branch August 12, 2025 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scaffold-generated tests produce deprecation warning for :unprocessable_entity in Rack 3.2
3 participants