Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook Registry: Ensuring that the response body is always a Hash, even if ShopifyAPI.Context.response_as_struct is true. #1313

Merged

Conversation

DaveEshopGuide
Copy link
Contributor

@DaveEshopGuide DaveEshopGuide commented Apr 17, 2024

Fixes #1311
ensuring that the response body is always a Hash, even if ShopifyAPI.Context.response_as_struct is true.

Description

Had to add a Utility (ShopifyAPI::Utils::OstructHashUtils) to handle the conversion since a simple .to_h and even JSON.parse(response.body.to_json) did not work as expected (nested Keys and Array handling failed).

How has this been tested?

Wrote new Specs to test the registry process with ShopifyAPI::Context.response_as_struct = true

Checklist:

  • My commit message follow the pattern described in here
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the project documentation.
  • I have added a changelog line.

…h, even if ShopifyAPI.Context.response_as_struct is true.

Had to add a Utility (ShopifyAPI::Utils::OstructHashUtils) to handle the conversion since a simple .to_h and even JSON.parse(response.body.to_json) did not work as expected (nested Keys and Array handling failed).
@DaveEshopGuide DaveEshopGuide requested a review from a team as a code owner April 17, 2024 14:52
@DaveEshopGuide
Copy link
Contributor Author

I have signed the CLA!

Copy link
Contributor

@lizkenyon lizkenyon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there 👋

Thank you for flagging this issue, and taking the time to dig into this!

I was thinking instead of converting the openstruct object back to a hash. (After we have converter a hash to an openstruct), instead in the cases of the webhook registry we stop this conversion from happening in the first place.

We could do this by adding a default param to the client.query that allows us to override the context.response_as_struct value for the cases of the webhook registration.

#client.rb
        sig do
          params(
            query: String,
            variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
            headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
            tries: Integer,
+            response_as_struct: T::Boolean,
          ).returns(HttpResponse)
        end
-        def query(query:, variables: nil, headers: nil, tries: 1)
+        def query(query:, variables: nil, headers: nil, tries: 1, response_as_struct: Context.response_as_struct)
#registry.rb
        def webhook_registration_needed?(client, registration)
-          check_response = client.query(query: registration.build_check_query)
+          check_response = client.query(query: registration.build_check_query, response_as_struct: false)

If you would like to make these changes you are welcome to! Otherwise I am happy to put up a PR myself with these changes shortly. Thanks again!

@@ -59,6 +59,29 @@ def test_process
assert(handler_called)
end

def test_process_with_response_as_struct
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could keep this test! Thank you for adding it. This would help us catch any regressions in the future.

@DaveEshopGuide
Copy link
Contributor Author

Hi there 👋

Thank you for flagging this issue, and taking the time to dig into this!

I was thinking instead of converting the openstruct object back to a hash. (After we have converter a hash to an openstruct), instead in the cases of the webhook registry we stop this conversion from happening in the first place.

We could do this by adding a default param to the client.query that allows us to override the context.response_as_struct value for the cases of the webhook registration.

#client.rb
        sig do
          params(
            query: String,
            variables: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
            headers: T.nilable(T::Hash[T.any(Symbol, String), T.untyped]),
            tries: Integer,
+            response_as_struct: T::Boolean,
          ).returns(HttpResponse)
        end
-        def query(query:, variables: nil, headers: nil, tries: 1)
+        def query(query:, variables: nil, headers: nil, tries: 1, response_as_struct: Context.response_as_struct)
#registry.rb
        def webhook_registration_needed?(client, registration)
-          check_response = client.query(query: registration.build_check_query)
+          check_response = client.query(query: registration.build_check_query, response_as_struct: false)

If you would like to make these changes you are welcome to! Otherwise I am happy to put up a PR myself with these changes shortly. Thanks again!

@lizkenyon Thanks for the feedback. Yes, that seems much more reasonable. I wasn't quite happy with introducing a new Util for this anyways. I'll see what I can do.

best, Dave

DaveEshopGuide and others added 2 commits April 19, 2024 09:56
Added response_as_struct as param to client query to be able to override the default behavior and passed in false for all calls from the webhook registry.
@DaveEshopGuide
Copy link
Contributor Author

@lizkenyon Feedback applied ;)

@lizkenyon
Copy link
Contributor

@DaveEshopGuide Thank you for this fix!

Could you add a post to the CHANGELOG.md and then this should be good to go!

Copy link
Contributor

@lizkenyon lizkenyon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for your contribution! 🌟 After a rebase/merge let get this PR merged in!

@Iben1993
Copy link

Iben1993 commented May 7, 2024

Good

@lizkenyon
Copy link
Contributor

Hey @DaveEshopGuide Could you rebase/ resolve the merge conflicts for this PR?

@lizkenyon lizkenyon merged commit 661a8de into Shopify:main May 13, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError in ShopifyApp::WebhooksManagerJob when ShopifyAPI::Context.response_as_struct is set to true
4 participants