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

Ensure correct Faraday JSON parsing/response body for invalid response header #110

Merged
merged 1 commit into from
Jan 6, 2024
Merged

Conversation

taylorthurlow
Copy link
Contributor

@taylorthurlow taylorthurlow commented Jan 5, 2024

See my investigation here:
#100 (comment)

In the Faraday 1 to Faraday 2 process, the JSON response middleware was rewritten from scratch, which caused a change in how permissive the middleware was to response Content-Type headers which did not properly indicate a JSON response.

In Faraday 1.x, the JSON middleware would attempt a JSON parse of the body, regardless of the presence or value of a Content-Type header. This meant that Graphlient would have raised an exception (or re-raised one caught from Faraday) if the response body was not valid JSON.

In Faraday 2.x, the new JSON middleware will only attempt to parse the response body if it determines that the Content-Type response header indicates a JSON body. By default it uses a regex on the header value (/\bjson$/ at the time of writing). Crucially, if the header is missing, or does not match the regex, instead of raising an exception, Faraday::Response#body returns a String, rather than the previously-assumed Hash or Array.

This change restores the parsing logic which is tolerant to incorrect or missing Content-Type header values. Response bodies which are not valid JSON will raise an exception. Any unforeseen situation where #body returns a type that is not String, Hash, or Array, will raise a separate exception detailing that this is unexpected behavior and a bug should be filed.

Resolves #100


I can't say that I have actually replicated any graphlient-on-Faraday-1.x functionality here, as far as what exceptions are raised on failed JSON parsing, but at the very least, the production application I am using this library in is now functioning as it did before the transition.

I also added new specs.

@ashkan18
Copy link
Owner

ashkan18 commented Jan 5, 2024

Perfect! can you fix rubocop problem and add a CHANGELOG entry per this and we are :shipit:
I can cut a new version once it's in the main branch.

…e header

See my investigation here:
#100 (comment)

In the Faraday 1 to Faraday 2 process, the ensure JSON response
middleware was rewritten from scratch, which caused a change in how
permissive the middleware was to response Content-Type headers which did
not properly indicate a JSON response.

In Faraday 1.x, the JSON middleware would attempt a JSON parse of the
body, regardless of the presence or value of a Content-Type header. This
meant that Graphlient would have raised an exception (or re-raised one
caught from Faraday) if the response body was not valid JSON.

In Faraday 2.x, the new JSON middleware will only attempt to parse the
response body if it determines that the Content-Type response header
indicates a JSON body. By default it uses a regex on the header value
(/\bjson$/ at the time of writing). Crucially, if the header is missing,
or does not match the regex, instead of raising an exception,
`Faraday::Response#body` returns a String, rather than the
previously-assumed Hash or Array.

This change restores the parsing logic which is tolerant to incorrect or
missing Content-Type header values. Response bodies which are not valid
JSON will raise an exception. Any unforseen situation where `#body`
returns a type that is not String, Hash, or Array, will raise a separate
exception detailing that this is unexpected behavior and a bug should be
filed.

Resolves #100
@taylorthurlow
Copy link
Contributor Author

@ashkan18 Done 👍

Copy link
Owner

@ashkan18 ashkan18 left a comment

Choose a reason for hiding this comment

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

Awesome! 🙌 Thanks 💜

@ashkan18 ashkan18 merged commit 423afee into ashkan18:master Jan 6, 2024
9 checks passed
@ashkan18
Copy link
Owner

ashkan18 commented Jan 6, 2024

@taylorthurlow just created a new version 0.8.0

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.

cannot stub request anymore
2 participants