Skip to content

Conversation

karouf
Copy link

@karouf karouf commented Apr 10, 2025

Since v1.66.0, with the introduction of generators, we started to see issues where instead of setting the JSON body of the expected request as a string, Pact started returning the same data but encoded as query parameters.

The consumer Pact is generated using v3 of the Pact spec with the JVM SDK, and specifically stringType from PactDslJsonBody. It produces a Pact file that contains something like

"generators": {
  "body": {
    "$.user.name": {
      "size": 20,
      "type": "RandomString"
    }
  }
}

You can find a reproduction case in a dedicated repository.
Just run bundle exec rake pact:verify:foobar to have a successful verification with v1.65.3.
And run FAIL=1 bundle exec rake pact:verify:foobar to demonstrate the issue with v1.66.0.

After investigating a bit, we narrowed it down to that commit.
In order to demonstrate the issue, I added this test.
My proposed fix is done in Pact::Provider::Request::Replayable because I'm not super familiar with the code base and I wasn't sure if Pact::Provider::Generators.apply_generators should ever return anything else than a string or not. But if it's not the case, the fix could probably go in Pact::Provider::Generators as I suspect there is the same issue with the way path and maybe headers are generated as well.

I'd be happy to fix it another way if you find it better and cover other cases if needed. Just let me know 😄

@YOU54F
Copy link
Member

YOU54F commented May 28, 2025

👋🏾

Sorry I have missed this on my radar, thanks for the great reproduction.

I will make some time either this week or next week to review.

@YOU54F YOU54F self-requested a review May 30, 2025 12:06
@YOU54F
Copy link
Member

YOU54F commented May 30, 2025

firstly, apologies for causing you impact with the change, I knew this would potentially break someones workflow, but I wanted to ensure we could use generators in pact-ruby and didn't want to lose great work done by a contributor.

I've taken a quick look but not ran any of the code locally and added a few suggestions, as I really appreciate you considering where the fix should go, and the wider impact already present with this bug.

My proposed fix is done in Pact::Provider::Request::Replayable because I'm not super familiar with the code base and I wasn't sure if Pact::Provider::Generators.apply_generators should ever return anything else than a string or not. But if it's not the case, the fix could probably go in Pact::Provider::Generators as I suspect there is the same issue with the way path and maybe headers are generated as well.

I think this should go into Pact::Provider::Generators.apply_generator in pact-support, I believe it should only return stringified json back.

That change would live in pact-support, the generator code is here

https://github.com/pact-foundation/pact-support/blob/master/lib/pact/generators.rb

with each individual generator here

https://github.com/pact-foundation/pact-support/tree/master/lib/pact/generator

if you are developing locally, you can use the X_PACT_DEVELOPMENT env var, to reference a locally cloned dependency, or one that has been pushed to github.

gem "pact-support", path: '../pact-support'

If the changes go into pact-support, they will be leveraged by the other pact ruby projects, to ensure the fix is available to all

@karouf
Copy link
Author

karouf commented Jun 5, 2025

Alright thanks for the feedback. I'll have a look at that next week.

@YOU54F
Copy link
Member

YOU54F commented Sep 11, 2025

Hey @karouf,

Do we still want to take a look at this?

I am currently prepping a v2 rewrite but the v1 code will remain in place for a while to support transitioning

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

Successfully merging this pull request may close these issues.

2 participants