Skip to content

Conversation

taketo1113
Copy link
Contributor

Background

In Rack 3.1, several HTTP status symbol names were changed.
For example, the HTTP status 422 was renamed from :unprocessable_entity to :unprocessable_content.

To align with these changes in Rack, the scaffold and scaffold_controller generators in the rspec-rails gem have also been updated to use :unprocessable_content instead of :unprocessable_entity, as shown below:

  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)
+        expect(response).to have_http_status(:unprocessable_content)
      end
    end
  end

Details

This Pull Request adds a new cop that detects the use of obsolete HTTP status symbols in existing test code and enforces consistency by using the current HTTP status names.
The cop checks for symbols defined in Rack::Utils::OBSOLETE_SYMBOL_MAPPINGS, which currently include :payload_too_large and :unprocessable_entity.
https://github.com/rack/rack/blob/v3.1.0/lib/rack/utils.rb#L573-L576

Additional Information

The rubocop-rails gem has also introduced a similar cop named Rails/HttpStatusNameConsistency,
which enforces consistency by using the current HTTP status names in app/controllers/*.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

@taketo1113 taketo1113 requested a review from a team as a code owner October 16, 2025 14:05
@pirj
Copy link
Member

pirj commented Oct 16, 2025

Thanks for the contribution. In your PR to rspec-rails there’s this hinting that there will be a warning.

Do you feel that the cop is needed when there’s a deprecation warning?
For autocorrecting?
I’m on the fence if this a goal or a non-goal for us.

@taketo1113 taketo1113 force-pushed the add-http-status-name-consistency-cop branch from b3ad323 to 83c0f50 Compare October 16, 2025 15:54
@taketo1113
Copy link
Contributor Author

@pirj Thank you for the comment.

I think the cop is needed for autocorrecting.
While newly generated files from the rspec-rails gem do not produce any warnings, it would be useful for fixing existing obsolete HTTP status symbols.

(I fixed the CI error by confirming the configuration and documentation.)

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

class HttpStatusNameConsistency < ::RuboCop::Cop::Base
extend AutoCorrector

requires_gem 'rack', '>= 3.1.0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, today I learned.

Copy link
Contributor Author

@taketo1113 taketo1113 Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's so nice.

Copy link
Member

@ydah ydah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!❤️

class HttpStatusNameConsistency < ::RuboCop::Cop::Base
extend AutoCorrector

requires_gem 'rack', '>= 3.1.0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's so nice.

@ydah ydah merged commit 2df4a5b into rubocop:master Oct 17, 2025
27 checks passed
@taketo1113 taketo1113 deleted the add-http-status-name-consistency-cop branch October 17, 2025 02:12
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.

3 participants