Skip to content

Commit

Permalink
fix: Use JSON Content-Type in mutating requests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
agis committed Jan 17, 2023
1 parent c348c5e commit e0f9f67
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/tmp/

.byebug_history
.ruby-version
*.gem

.idea

6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/clerk/resources/singular_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/clerk/sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/sdk_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e0f9f67

Please sign in to comment.