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

False positive on Rails/ResponseParsedBody #940

Closed
jcoyne opened this issue Feb 27, 2023 · 1 comment · Fixed by #971
Closed

False positive on Rails/ResponseParsedBody #940

jcoyne opened this issue Feb 27, 2023 · 1 comment · Fixed by #971
Labels
bug Something isn't working

Comments

@jcoyne
Copy link
Contributor

jcoyne commented Feb 27, 2023

I'm seeing this error:

spec/controllers/spotlight/catalog_controller_spec.rb:308:16: C: [Correctable] Rails/ResponseParsedBody: Prefer response.parsed_body to JSON.parse(response.body).
        data = JSON.parse(response.body).with_indifferent_access
               ^^^^^^^^^^^^^^^^^^^^^^^^^

However I can not autocorrect this as parsed_body only works in the case that there is a parser found for the corresponding mime type. https://api.rubyonrails.org/classes/ActionDispatch/TestResponse.html#method-i-parsed_body

My response doesn't have one:

response.content_type
"application/solr+json; charset=utf-8"

So this cop depends on the assumption that the request has a mime-type of application/json, which isn't going to be true for all cases.

@vlad-pisanov
Copy link
Contributor

Autocorrect is marked unsafe for this very reason: https://docs.rubocop.org/rubocop-rails/cops_rails.html#safety-27

This cop’s autocorrection is unsafe because Content-Type may not be application/json

A potential workaround is to register a JSON encoder for your custom content-type for your test suite:

ActionDispatch::RequestEncoder.register_encoder :jsonapi, response_parser: ->(body) { JSON.parse(body) }

koic added a commit to koic/rubocop-rails that referenced this issue 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.
@koic koic added the bug Something isn't working label Apr 4, 2023
@koic koic closed this as completed in #971 Apr 4, 2023
koic added a commit that referenced this issue Apr 4, 2023
…unsafe

[Fix #940] Mark `Rails/ResponseParsedBody` as unsafe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants