From e0f9f67382598fda85317c85c9de0ec34844871e Mon Sep 17 00:00:00 2001 From: Agis Anastasopoulos Date: Tue, 17 Jan 2023 13:27:28 +0200 Subject: [PATCH] fix: Use JSON Content-Type in mutating requests The Backend API supports both form-urlencoded and application/json content types in requests. However, we're slowly fading out support for form-urlencoded and transitioning to application/json. Most important, the Ruby SDK generated payloads that the Backend API couldn't properly unmarshal, particularly in the case of payloads like the following: { backup_codes: ["abcdef", "zxcqwe"] } in such cases, the resulting payload would be: backup_codes[]: "abcdef; zxcqwe" which the Backend API didn't know how to properly parse. Fixes AUTH-129 --- .gitignore | 2 ++ Gemfile.lock | 6 +++--- lib/clerk/resources/singular_resource.rb | 2 +- lib/clerk/sdk.rb | 6 ++++-- test/sdk_test.rb | 24 ++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 05d574b..7a10690 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ /tmp/ .byebug_history +.ruby-version *.gem .idea + diff --git a/Gemfile.lock b/Gemfile.lock index 9a4f6ab..894bca3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - clerk-sdk-ruby (2.8.0) + clerk-sdk-ruby (2.9.0.beta2) concurrent-ruby (~> 1.1) faraday (~> 1.4.1) jwt (~> 2.5) @@ -24,8 +24,8 @@ GEM faraday-excon (1.1.0) faraday-net_http (1.0.1) faraday-net_http_persistent (1.2.0) - jwt (2.5.0) - minitest (5.16.3) + jwt (2.6.0) + minitest (5.17.0) multipart-post (2.2.3) rake (13.0.6) ruby2_keywords (0.0.5) diff --git a/lib/clerk/resources/singular_resource.rb b/lib/clerk/resources/singular_resource.rb index 4a85026..6bea2f5 100644 --- a/lib/clerk/resources/singular_resource.rb +++ b/lib/clerk/resources/singular_resource.rb @@ -7,7 +7,7 @@ def initialize(client, resource_path) end def update(changes = {}) - @client.request(:patch, @resource_path, body:changes) + @client.request(:patch, @resource_path, body: changes) end end end diff --git a/lib/clerk/sdk.rb b/lib/clerk/sdk.rb index 8b7b7fd..c8aad6c 100644 --- a/lib/clerk/sdk.rb +++ b/lib/clerk/sdk.rb @@ -74,12 +74,14 @@ def request(method, path, query: [], body: nil, timeout: nil) end when :post @conn.post(path, body) do |req| - req.body = body + req.body = body.to_json + req.headers[:content_type] = "application/json" req.options.timeout = timeout if timeout end when :patch @conn.patch(path, body) do |req| - req.body = body + req.body = body.to_json + req.headers[:content_type] = "application/json" req.options.timeout = timeout if timeout end when :delete diff --git a/test/sdk_test.rb b/test/sdk_test.rb index 344a93c..01c8d98 100644 --- a/test/sdk_test.rb +++ b/test/sdk_test.rb @@ -11,6 +11,30 @@ def test_sdk_init_without_config assert sdk end + def test_application_json_encoding + unparsed_payload = { a: "b", c: ["d"], d: { e: 3 } } + json_payload = '{"a":"b","c":["d"],"d":{"e":3}}' + + conn = Faraday.new do |faraday| + faraday.adapter :test do |stub| + stub.patch("/users/user_1", json_payload, { "Content-Type" => "application/json" } ) do |env| + parsed = JSON.parse(env.request_body) + parsed["a"] = "b" + parsed["c"] = ["d"] + parsed["d"] = { "e" => 3 } + + json_ok("user_1_updated") + end + end + end + + sdk = ::Clerk::SDK.new(connection: conn) + user = sdk.users.update("user_1", unparsed_payload) + + assert_equal "Mary", user["first_name"] + assert_equal "user_1", user["id"] + end + def test_no_api_key_raises_on_api_call sdk = ::Clerk::SDK.new assert_raises ArgumentError do