Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/workos/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_request(path:, auth: false, params: {}, request_options: nil)
def post_request(path:, auth: false, body: {}, params: {}, request_options: nil)
req = build_request(Net::HTTP::Post, append_query(path, params),
auth: auth, request_options: request_options)
req.body = body.nil? ? "" : body.compact.to_json
req.body = body.nil? ? "" : body.to_json
req["Content-Type"] = "application/json"
inject_idempotency_key(req, request_options)
req
Expand All @@ -75,7 +75,7 @@ def post_request(path:, auth: false, body: {}, params: {}, request_options: nil)
def put_request(path:, auth: false, body: {}, params: {}, request_options: nil)
req = build_request(Net::HTTP::Put, append_query(path, params),
auth: auth, request_options: request_options)
req.body = body.nil? ? "" : body.compact.to_json
req.body = body.nil? ? "" : body.to_json
req["Content-Type"] = "application/json"
inject_idempotency_key(req, request_options)
req
Expand All @@ -84,7 +84,7 @@ def put_request(path:, auth: false, body: {}, params: {}, request_options: nil)
def patch_request(path:, auth: false, body: {}, params: {}, request_options: nil)
req = build_request(Net::HTTP::Patch, append_query(path, params),
auth: auth, request_options: request_options)
req.body = body.nil? ? "" : body.compact.to_json
req.body = body.nil? ? "" : body.to_json
req["Content-Type"] = "application/json"
inject_idempotency_key(req, request_options)
req
Expand All @@ -94,7 +94,7 @@ def delete_request(path:, auth: false, body: nil, params: {}, request_options: n
req = build_request(Net::HTTP::Delete, append_query(path, params),
auth: auth, request_options: request_options)
if body
req.body = body.compact.to_json
req.body = body.to_json
req["Content-Type"] = "application/json"
end
req
Expand Down
27 changes: 27 additions & 0 deletions test/workos/test_base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@ def test_post_request_reads_idempotency_key_from_request_options
assert_equal "idem_123", request["Idempotency-Key"]
end

# The body is serialized verbatim (no `.compact`), so an explicit `nil` is
# sent as JSON `null` — this is what lets generated methods clear a nullable
# field. Omission is handled upstream in the generated method, not here.
def test_put_request_serializes_explicit_nil_as_json_null
request = @client.put_request(path: "/widgets/w_1", auth: true, body: {"external_id" => nil})

assert_equal({"external_id" => nil}, JSON.parse(request.body))
end

def test_post_request_serializes_explicit_nil_as_json_null
request = @client.post_request(path: "/widgets", auth: true, body: {"external_id" => nil})

assert_equal({"external_id" => nil}, JSON.parse(request.body))
end
Comment thread
greptile-apps[bot] marked this conversation as resolved.

def test_patch_request_serializes_explicit_nil_as_json_null
request = @client.patch_request(path: "/widgets/w_1", auth: true, body: {"external_id" => nil})

assert_equal({"external_id" => nil}, JSON.parse(request.body))
end

def test_delete_request_serializes_explicit_nil_as_json_null
request = @client.delete_request(path: "/widgets/w_1", auth: true, body: {"external_id" => nil})

assert_equal({"external_id" => nil}, JSON.parse(request.body))
end

def test_retry_path_generates_idempotency_key_for_mutating_requests
stub_request(:post, "https://api.workos.com/widgets")
.to_return({status: 500, body: '{"message":"retry"}'}, {status: 200, body: "{}"})
Expand Down
Loading