From 88ed602b66a3bfe2525baa4ded2e57c754152181 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 23 Sep 2024 11:31:28 +0100 Subject: [PATCH] Don't register the request/response json middleware for Faraday 1.10+ (#288) Faraday 1.10 ships with an out-of-the-box JSON middleware, so we want to to default to that one rather than the faraday_middleware one. Co-authored-by: Olle Jonsson --- lib/faraday_middleware.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/faraday_middleware.rb b/lib/faraday_middleware.rb index 4cf0a80e..dd4d68b1 100644 --- a/lib/faraday_middleware.rb +++ b/lib/faraday_middleware.rb @@ -27,13 +27,11 @@ module FaradayMiddleware Faraday::Request.register_middleware \ oauth: -> { OAuth }, oauth2: -> { OAuth2 }, - json: -> { EncodeJson }, method_override: -> { MethodOverride } Faraday::Response.register_middleware \ mashify: -> { Mashify }, rashify: -> { Rashify }, - json: -> { ParseJson }, json_fix: -> { ParseJson::MimeTypeFix }, xml: -> { ParseXml }, marshal: -> { ParseMarshal }, @@ -46,6 +44,13 @@ module FaradayMiddleware Faraday::Middleware.register_middleware \ instrumentation: -> { Instrumentation }, gzip: -> { Gzip } + + # The request/reponse JSON middleware is included in Faraday since + # version 1.10.0, so we only register the middleware if it's not already. + if Gem::Version.new(Faraday::VERSION) < Gem::Version.new('1.10.0') + Faraday::Request.register_middleware(json: -> { EncodeJson }) + Faraday::Response.register_middleware(json: -> { ParseJson }) + end end end