Skip to content

Commit 661a8de

Browse files
authored
Merge pull request #1313 from eshopguide/fix/registry-process-with-response-as-struct
Webhook Registry: Ensuring that the response body is always a Hash, even if ShopifyAPI.Context.response_as_struct is true.
2 parents 7c4ecb3 + d8a2e8c commit 661a8de

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
66

77
## 14.3.0
88
- [#1312](https://github.com/Shopify/shopify-api-ruby/pull/1312) Use same leeway for `exp` and `nbf` when parsing JWT
9+
- [#1313](https://github.com/Shopify/shopify-api-ruby/pull/1313) Fix: Webhook Registry now working with response_as_struct enabled
910
- [#1314](https://github.com/Shopify/shopify-api-ruby/pull/1314)
1011
- Add new session util method `SessionUtils::session_id_from_shopify_id_token`
1112
- `SessionUtils::current_session_id` now accepts shopify Id token in the format of `Bearer this_token` or just `this_token`

lib/shopify_api/clients/graphql/client.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ def initialize(session:, base_path:, api_version: nil)
2828
variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
2929
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
3030
tries: Integer,
31+
response_as_struct: T.nilable(T::Boolean),
3132
).returns(HttpResponse)
3233
end
33-
def query(query:, variables: nil, headers: nil, tries: 1)
34+
def query(query:, variables: nil, headers: nil, tries: 1, response_as_struct: Context.response_as_struct)
3435
body = { query: query, variables: variables }
3536
@http_client.request(
3637
HttpRequest.new(
@@ -42,7 +43,7 @@ def query(query:, variables: nil, headers: nil, tries: 1)
4243
body_type: "application/json",
4344
tries: tries,
4445
),
45-
response_as_struct: Context.response_as_struct || false,
46+
response_as_struct: response_as_struct || false,
4647
)
4748
end
4849
end

lib/shopify_api/clients/graphql/storefront.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ def initialize(shop, storefront_access_token = nil, private_token: nil, public_t
4545
variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
4646
headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
4747
tries: Integer,
48+
response_as_struct: T.nilable(T::Boolean),
4849
).returns(HttpResponse)
4950
end
50-
def query(query:, variables: nil, headers: {}, tries: 1)
51+
def query(query:, variables: nil, headers: {}, tries: 1, response_as_struct: Context.response_as_struct)
5152
T.must(headers).merge!({ @storefront_auth_header => @storefront_access_token })
52-
super(query: query, variables: variables, headers: headers, tries: tries)
53+
super(query: query, variables: variables, headers: headers, tries: tries,
54+
response_as_struct: response_as_struct)
5355
end
5456
end
5557
end

lib/shopify_api/webhooks/registry.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def unregister(topic:, session:)
138138
}
139139
MUTATION
140140

141-
delete_response = client.query(query: delete_mutation)
141+
delete_response = client.query(query: delete_mutation, response_as_struct: false)
142142
raise Errors::WebhookRegistrationError,
143143
"Failed to delete webhook from Shopify" unless delete_response.ok?
144144
result = T.cast(delete_response.body, T::Hash[String, T.untyped])
@@ -170,7 +170,7 @@ def get_webhook_id(topic:, client:)
170170
}
171171
QUERY
172172

173-
fetch_id_response = client.query(query: fetch_id_query)
173+
fetch_id_response = client.query(query: fetch_id_query, response_as_struct: false)
174174
raise Errors::WebhookRegistrationError,
175175
"Failed to fetch webhook from Shopify" unless fetch_id_response.ok?
176176
body = T.cast(fetch_id_response.body, T::Hash[String, T.untyped])
@@ -216,7 +216,7 @@ def process(request)
216216
).returns(T::Hash[Symbol, T.untyped])
217217
end
218218
def webhook_registration_needed?(client, registration)
219-
check_response = client.query(query: registration.build_check_query)
219+
check_response = client.query(query: registration.build_check_query, response_as_struct: false)
220220
raise Errors::WebhookRegistrationError,
221221
"Failed to check if webhook was already registered" unless check_response.ok?
222222
parsed_check_result = registration.parse_check_result(T.cast(check_response.body, T::Hash[String, T.untyped]))
@@ -233,7 +233,8 @@ def webhook_registration_needed?(client, registration)
233233
).returns(T::Hash[String, T.untyped])
234234
end
235235
def send_register_request(client, registration, webhook_id)
236-
register_response = client.query(query: registration.build_register_query(webhook_id: webhook_id))
236+
register_response = client.query(query: registration.build_register_query(webhook_id: webhook_id),
237+
response_as_struct: false)
237238

238239
raise Errors::WebhookRegistrationError, "Failed to register webhook with Shopify" unless register_response.ok?
239240

test/webhooks/registry_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ def test_process
5959
assert(handler_called)
6060
end
6161

62+
def test_process_with_response_as_struct
63+
modify_context(response_as_struct: true)
64+
65+
handler_called = false
66+
67+
handler = TestHelpers::FakeWebhookHandler.new(
68+
lambda do |topic, shop, body,|
69+
assert_equal(@topic, topic)
70+
assert_equal(@shop, shop)
71+
assert_equal({}, body)
72+
handler_called = true
73+
end,
74+
)
75+
76+
ShopifyAPI::Webhooks::Registry.add_registration(
77+
topic: @topic, path: "path", delivery_method: :http, handler: handler,
78+
)
79+
80+
ShopifyAPI::Webhooks::Registry.process(@webhook_request)
81+
82+
assert(handler_called)
83+
end
84+
6285
def test_process_new_handler
6386
handler_called = false
6487

0 commit comments

Comments
 (0)