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

Pact-PHP <=9.x.x - Verification failed when the query has nested keys #222

Open
mark-onenetwork opened this issue Sep 28, 2021 · 6 comments

Comments

@mark-onenetwork
Copy link

Hi All,

I encountered a weird issue: verification failed when the query has nested keys key_a[]=1&key_b[1]=2, key_a is an array, key_b is a hash.

Here is the output:

W, [2021-09-28T01:44:25.107664 #4532]  WARN -- : Verifying - actual interactions do not match expected interactions.
Incorrect requests:
        GET /api/products/my_endpoint?travel_date=2021-04-10&key_b[1]=2&key_a[]=1 (request query did not match)

Diff with interaction: "A get request to /api/products/my_endpoint" given "Resource 112 exists, and is available on 2021-05-21"
Diff
--------------------------------------
Key: - is expected
     + is actual
Matching keys and values are not shown

 {
   "query": {
-    "key_a": [
+    "key_a[]": [
       "1"
     ]

Description of differences
--------------------------------------
* Could not find key "key_a" (keys present are: key_b[1], key_a[]) at $.query
* Did not expect the key "key_a[]" to exist at $.query

I debug code and got some clues:

  1. When parsing the expected interaction, the query string: key_a[]=1&key_b[1]=2 is covert to key_a=1&key_b[1]=2 because of the logic here.
  2. However when trying to match the candidate interactions with actual request, the square brackets in the actual query key key_a[]=1 is not removed as in expected interaction key_a=1, which causes the inconsistency between expected interaction and actual interaction.

I'm wondering if it's a bug.

@bethesque
Copy link
Member

The query matching logic is a nightmare, because there are many different ways a query can be parsed. I'm afraid you'll need to try using a string query expectation in this case.

@mark-onenetwork
Copy link
Author

@bethesque Thanks for the quick response. Then it's a "know issue". Could you please explain a bit more on using string query expectation instead? Would be appreciated, I'm not Ruby expert.

@bethesque
Copy link
Member

Can you please copy your Pact expectation code into a comment.

@mark-onenetwork
Copy link
Author

$request = new ConsumerRequest();
$request
    ->setMethod('GET')
    ->setPath('/api/products/my_endpoint?key_a[]=1&key_b[1]=2')
    ->addHeader('Content-Type', 'application/json');

It's in PHP, probably no way to do that.

@bethesque
Copy link
Member

Ah, it's already a string. Is there no separate method to set the query? In the other languages, the path and the query are set separately, so that suggests to me that something in the php is splitting them out.

@mark-onenetwork
Copy link
Author

Yes, there is a separate method to set the query.

$request = new ConsumerRequest();
$request
    ->setMethod('GET')
    ->setPath('/api/products/my_endpoint')
    ->setQuery('key_a[]=1&key_b[1]=2')
    ->addHeader('Content-Type', 'application/json');

@YOU54F YOU54F changed the title Verification failed when the query has nested keys Pact-PHP <=9.x.x : Verification failed when the query has nested keys May 16, 2024
@YOU54F YOU54F changed the title Pact-PHP <=9.x.x : Verification failed when the query has nested keys Pact-PHP <=9.x.x - Verification failed when the query has nested keys May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants