Skip to content

Add OR rule support - #599

Open
sergewar wants to merge 1 commit into
pact-foundation:masterfrom
sergewar:fix/add-or-rule
Open

sergewar wants to merge 1 commit into
pact-foundation:masterfrom
sergewar:fix/add-or-rule

Conversation

@sergewar

@sergewar sergewar commented Nov 2, 2026 •

Copy link
Copy Markdown

Add support OR combined matchers with per-alternative values and rules

Closes #249

Problem

"combine": "OR" already exists in the pact file format, but it only works for a list of plain rules on one path, such as regex OR number. You can't write "this field is either an integer or an object with this shape", because each alternative needs its own example value and its own nested rules.

Solution

Pact file format (V3/V4)

A matcher inside an OR list can now carry a variant attribute. It holds the alternative's example value and the matching rules that apply under that path:

​json "$.a": { "combine": "OR", "matchers": [ { "match": "integer", "variant": { "value": 1, "rules": {} } }, { "match": "type", "variant": { "value": { "d": ["x"] }, "rules": { "$.d": { "matchers": [{ "match": "type", "min": 1 }] }, "$.d[*]": { "combine": "OR", "matchers": [{ "match": "type" }, { "match": "number" }] } } } } ] } ​

  • Matching passes if any alternative matches. An alternative matches when its root rule passes and every rule under it passes for the actual value.
  • Alternatives can nest, including an OR inside an alternative.
  • Plain rules and variants can be mixed in one OR list.
  • V2 pact files write only the inner rule; variants are dropped, since the V2 format can't represent them.

Integration JSON (FFI)

pact:matcher:combine: "OR" is accepted on a pact:matcher:type array. Any matcher in that array with its own value becomes a variant, and its value is processed like a normal body, so nested matchers produce the alternative's rules:

​json "a": { "pact:matcher:type": [ { "pact:matcher:type": "integer", "value": 1 }, { "pact:matcher:type": "type", "value": { "d": { "pact:matcher:type": "type", "min": 1, "value": [ ... ] } } } ], "pact:matcher:combine": "OR", "value": 1 } ​

Bodies (JSON and XML text/attributes), headers and query parameters are supported.

Changes

  • pact_models
    • Adds MatchingRule::Variant(Box<MatchingRule>, Box<MatchingRuleVariant>) and MatchingRuleVariant { value, rules }, with JSON read/write, hash and equality.
    • RuleList::to_v2_json writes only the inner rule.
  • pact_matching
    • compare_json matches OR lists that contain variants by trying each alternative with its own rules. Mismatch paths are rebased onto the original path.
    • OR is honoured on type mismatches and in compare_maps.
    • Scalar, string and status-code matching handle Variant through matches_variant_with.
    • The list and map matchers (used for query parameters and headers) apply only a variant's length restrictions to the collection itself.
  • pact_ffi
    • rule_list_from_integration_json is the single entry point for building a RuleList from integration JSON. It follows the style of Feat/composing matching rules #405.
    • Adds add_rule_list_to_category.
    • matchers_from_integration_json now contains the logic of the removed matchers_from_json and is not deprecated.
    • Headers, query parameters and XML use the new functions. XML elements accept only plain rules.
    • IntegrationJson.md documents OR, including its limitations.

Limitations

  • XML elements can't be OR alternatives with values; text and attributes can.
  • An alternative that is itself a combined matcher must have its own value.
  • The experimental V2 matching engine (PACT_MATCHING_ENGINE=v2) doesn't support OR variants. All V2 engine changes from this branch have been reverted.

Tests

  • Unit tests for:
    • the Variant round trip (V3/V4), V2 output, hash/equality and invalid input
    • JSON matching: objects, arrays, array items, mixed plain and variant rules, nested OR, root $ and NoUnexpectedKeys
    • header and query-parameter matching
    • integration JSON parsing
  • FFI mock-server tests:
    • JSON, XML and query parameters
    • pact file output for V4/V3/V2
    • nested matchers inside an alternative, e.g. integer() OR { d: eachLike(string() OR number()) }

@rholshausen

Copy link
Copy Markdown
Contributor

This PR changes the Pact file format, which is more that the original issue asked for. Changing the Pact file format requires changes for all language implementations, so has much larger implications.

I think this PR needs to be split, with just the Integration JSON changes the original issue asked for in one. The other changes require an RFC process to be adopted.

@YOU54F @mefellows what do you think?

@mefellows

Copy link
Copy Markdown
Member

Yes, agree I think we should split it so that we can have support for OR in the existing frameworks now without requiring a major version bump. Most already support 4.0 format today, so could benefit from it right away.

@sergewar

sergewar commented Nov 9, 2026

Copy link
Copy Markdown
Author

Created a new one PR #601

@YOU54F

YOU54F commented Nov 9, 2026

Copy link
Copy Markdown
Member

Yes to OR sooner, rather than later.

RFC's take ages and I am near retirement (😂), seriously though, I would be up for an RFC raising as mentioned in your follow up PR.

I would suggest closing this one down for now, or splitting it the change, building on top of your existing PR and maybe marking as a draft, so any viewers/reviewers are aware!

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.

Combined Matchers - AND (implemented) or OR (to be implemented)

4 participants