Skip to content

Add support pact:matcher:combine OR in integration JSON - #601

Open
sergewar wants to merge 6 commits into
pact-foundation:masterfrom
sergewar:fix/or-integration-json
Open

sergewar wants to merge 6 commits into
pact-foundation:masterfrom
sergewar:fix/or-integration-json

Conversation

@sergewar

@sergewar sergewar commented Nov 9, 2026

Copy link
Copy Markdown

Add support pact:matcher:combine: "OR" in integration JSON

Split out of #599, following the review feedback there.

Problem

Integration JSON can only combine matchers with AND. The pact file format already supports rule lists combined with OR ("combine": "OR"), but language implementations had no way to create one through the FFI.

Solution

You can now set pact:matcher:combine next to an array of matchers. It takes AND (the default) or OR; any other value is an error.

"name": {
  "pact:matcher:type": [
    { "pact:matcher:type": "type" },
    { "pact:matcher:type": "null" }
  ],
  "pact:matcher:combine": "OR",
  "value": "Fred"
}

It's written to the pact file in the existing format, so the pact file format doesn't change and no RFC or major version bump is needed:

"$.name": { "combine": "OR", "matchers": [{ "match": "type" }, { "match": "null" }] }

V2 pact files still write only the first rule for a path, as before.

Changes

  • pact_ffi
    • New rule_list_from_integration_json: builds a RuleList using the logic from pact:matcher:combine, in the same style as Feat/composing matching rules #405.
    • New add_rule_list_to_category.
    • Bodies, headers, query parameters, paths, status codes and XML now use these two functions, instead of always adding rules with AND.
    • IntegrationJson.md documents OR and its limitations.
  • pact_matching
    • compare_json used to report a type mismatch straight away (object vs non-object, array vs non-array). When the path has an OR rule list, it now applies those rules first, so type OR null on an object accepts null.
    • compare_maps passes when at least one matcher in an OR list matches.

Limitations

  • Every alternative is applied to the top-level value. Matchers that compare against the example (like type) can only describe the shape of that example. For example, type OR null on an object accepts an object of the same shape or null, but not an array.
  • Each alternative must be a single matcher; a nested list of matchers is an error.
  • The experimental V2 matching engine (PACT_MATCHING_ENGINE=v2) doesn't support OR.

Alternatives with their own example values and nested rules (e.g. "an integer, or an object of this shape") need a pact file format change. That part goes in a separate PR for the RFC process.

Tests

  • Unit tests:
    • parsing pact:matcher:combine, including invalid values
    • JSON matching with OR on values, objects, arrays and scalars
    • header and query parameter matching
  • FFI tests:
    • the mock server with OR on a JSON body and on query parameters
    • the pact file written for V4, V3 and V2

Matchers in integration JSON can now be combined with OR using the
`pact:matcher:combine` attribute. The rules are written to the pact file as
an existing rule list combined with OR, so the pact file format is unchanged.
@YOU54F

YOU54F commented Nov 9, 2026

Copy link
Copy Markdown
Member

Thanks @sergewar, this is a really useful addition: it closes the gap in #249 and gives the language DSLs a way to express OR (and nullable values) without each one resolving it themselves, which unblocks pact-foundation/pact-ruby#420 and my "or null" query from #399.

Although I think this is a big ol' footgun for users (with great power comes great responsibility), it is in the spec, the other core (pact-jvm) so it needs to be in pact-reference and the client languages!

I ran it locally, along with the consumer libraries, and pulled together some follow-ups as stacked PRs on my fork. They're all optional, happy for any of them to be pulled into your change:

Fixes

  • YOU54F/pact-reference#28: the V2 matching engine's or action folded from OK, so an OR rule list could never fail there. It also reported errors from the alternatives that didn't match even when the OR passed. It now handles OR properly, including null-or-object (an object whose child rules are only checked when the value isn't null).
  • YOU54F/pact-reference#29: when a path already has rules combined with AND (e.g. the OR matcher's value is itself a matcher), the OR rules were appended to that list and silently became AND. Rules combined with different logic now replace the existing ones with a warning, the same as Pact-JVM's or. It also makes the combine value case-insensitive and documents nullable objects, wrapped matchers and the limitations.

I set claude on creating tests in the compatibility suite, as I am working in that area for plugin support, and broader support across the client libraries. Many of these are still running in CI, I'll pick up any failures probably tomorrow (or a couple of days 😂 due to contention limits

Tests

End-to-end in the consumer libraries, against a pact_ffi 0.5.13-or.1 prerelease built from the above (release), with docs and tests:

A few small things on this PR itself

  • An invalid pact:matcher:combine value (e.g. "XOR") logs an error, and then the behaviour depends on where it is used. In a body the value at that path is written as null (as process_object_with_invalid_matcher_combine_value asserts). In headers and query parameters the rules are dropped but the example value is kept. Either way pactffi_with_body / pactffi_with_header_v2 still return true, so a typo quietly changes the contract. Could it either return an error, or at least keep the example value in bodies too? Whichever you pick, the docs should say what happens; at the moment they only say the rules are ignored.
  • A pact:generator:type inside an entry of the pact:matcher:type array is ignored (matchers_from_integration_json returns no generator for the array form). This isn’t new, but it’s more likely to come up with OR. A note in IntegrationJson.md to put the generator on the outer object instead would help.
  • The out/ addition to .gitignore looks unrelated to this change, could you remove please

If you fancy taking a stab at an RFC for your other proposed change, feel free, I think I will be trying to raise one soon about indexed header and query values as it caused me pain when migrating the pact_broker (a ruby app) from the pact-ruby core to pact-rust, and some of the workarounds that ensued. It's a big ol' nut to crack, with lots of places to address, but feel embolded with the use of AI to help build up the compatability suite, and ability to test big changes across multiple clients, which has always been a huge time sink for me! An enjoyable one, but one none the less.

YOU54F and others added 5 commits November 13, 2026 16:34
…h's rules with different logic

add_rule_list_to_category used to append OR rules to an existing AND list (or the
reverse), silently changing how the existing rules are combined. Rules with a
different logic now replace the existing ones with a warning, as Pact-JVM's
`or` DSL does, and rules with the same logic are appended.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lues for pact:matcher:combine

* The combine value is not case-sensitive, and an invalid value writes null.
* Rules inside an object value still apply to its children (null-or-object).
* An OR matcher whose value is another matcher replaces that matcher's rules,
  with a warning, as Pact-JVM's or matcher does.
* Remove the V2 matching engine limitation, now that it supports OR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* The `or` action folded its results from OK, so it never failed. It now
  folds from the first child result.
* Errors from the branches of a successful `or` are no longer reported as
  mismatches.
* An object with rules combined with OR that include a null matcher now
  accepts a null value, and only checks the object's entries when the
  value is not null (new `json:not-null` action).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An invalid `pact:matcher:combine` value wrote the body value as `null`, while
headers and query parameters kept it. The rules are still ignored and the
error logged, but the example value (and any rules nested in it) is now kept
for bodies too. Also documents that a generator must be on the outer object
of an OR list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sergewar

sergewar commented Nov 13, 2026 •

Copy link
Copy Markdown
Author

@YOU54F Thank you for your review. I updated this PR with your changes and with your suggestions.

Regarding RFC, I'll look how to do this, but if you can provide an instruction - it will be amazing.

@YOU54F

YOU54F commented Nov 13, 2026

Copy link
Copy Markdown
Member

Thanks @sergewa, will take a look when I get a chance, will probably be some time next week

regarding the RFC process, the first RFC provided instructions on how to raise one. Any questions feel free to reach out here or on slack.pact.io 👍🏾

https://github.com/pact-foundation/roadmap/blob/master/rfc/0000-rfc-process.md

This branch has not been deployed

No deployments
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