Skip to content
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

Add Rails/ResponseParsedBody cop #863

Merged
merged 1 commit into from
Jan 27, 2023

Conversation

r7kamura
Copy link
Contributor

Since Rails 5, there is an often overlooked feature: response.parsed_body.

As a typical pattern, many Rails applications have JSON.parse(response.body) in their controlelr-specs and request-specs, but most of these could be replaced by response.parsed_body. So it would be nice for rubocop-rails to have a Cop that recommends response.parsed_body.

I think there are several advantages to using response.parsed_body.

  1. the fact that Rails provides this by default means that Rails would probably encourage the use of it
  2. it avoids direct references to JSON
  3. shorter code

What do you think?


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@r7kamura
Copy link
Contributor Author

I addressed the CI failure with the following Pull Request:

@r7kamura
Copy link
Contributor Author

Note: At first I created the following Pull Request in rubocop-rspec, but it seemed to be more suitable for rubocop-rails, so I re-created this Cop here.

Copy link
Contributor

@vlad-pisanov vlad-pisanov left a comment

Choose a reason for hiding this comment

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

Nice! We've been using our own version of this cop in our org for many years. We also correct Oj.load(response.body) (another popular JSON parser) -- but that might be overkill.

# response.parsed_body
class ResponseParsedBody < Base
extend AutoCorrector

Copy link
Contributor

Choose a reason for hiding this comment

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

Add

minimum_target_rails_version 5.0

@r7kamura r7kamura force-pushed the feature/response-parsed-body branch 2 times, most recently from 154caea to 0867a8e Compare November 15, 2022 21:13
#
# @safety
# This cop is unsafe because Content-Type may not be
# `application/json`.
Copy link
Member

Choose a reason for hiding this comment

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

Is Content-Type guaranteed for code before autocorrection? I'm wondering if autocorrection will change Content-Type. So, If Content-Type doesn't change, it looks like there is no change of safety.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's unsafe because the behavior changes a bit.

JSON.parse(body.response) attempts to parse the body as JSON regardless of Content-Type. e.g. if the controller spits out JSON but marks it as application/xml, this will still work. Whereas parsed_body will use the appropriate parser based on Content-Type, and will raise an exception if the response can't be parsed.

https://github.com/rails/rails/blob/282b769fb543404187abde76ca437aa697f6d6ba/actionpack/lib/action_dispatch/testing/test_response.rb#L21-L37

Copy link
Member

Choose a reason for hiding this comment

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

I get it. It seems to be an incompatibility behavior potential rather than a false positive potential. So it's unsafe for autocorrection.

config/default.yml Outdated Show resolved Hide resolved
@koic koic merged commit 94ccd43 into rubocop:master Jan 27, 2023
@r7kamura r7kamura deleted the feature/response-parsed-body branch January 27, 2023 18:34
koic added a commit to koic/rubocop-rails that referenced this pull request Apr 3, 2023
Fixes rubocop#940.

This PR marks `Rails/ResponseParsedBody` as unsafe because I was misleading below:
rubocop#863 (comment)

It's actually unsafe.
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.

None yet

3 participants