Skip to content

fix(models): keep matching rules and generators for each header and query value - #15

Closed
YOU54F wants to merge 1 commit into
masterfrom
fix/pact-models-indexed-matchers
Closed

YOU54F wants to merge 1 commit into
masterfrom
fix/pact-models-indexed-matchers

Conversation

@YOU54F

@YOU54F YOU54F commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Draft on the fork for review before anything goes upstream. It fixes the issue drafted below (not posted yet). Not stacked on pact-foundation#584: this branch is based on upstream master.

Because this is a pact file format extension, it needs a pact-specification discussion before it goes upstream, and there's an open question about the layout (see pact-jvm compatibility).

Summary

pactffi_with_header_v2 with an index > 0 stores a matching rule per header value. In memory the rules are keyed $.X-Request / $.X-Request[0] and $.X-Request[1], and the mock server matches each value against its own rule correctly.

When the pact file is written (V3 and V4), both rules are written under the bare header name X-Request. Only one survives, and which one is decided by HashMap iteration order, so the pact file changes from run to run.

This happens on the released pact_ffi 0.5.x as well as on pact-foundation#568 and pact-foundation#584, so it predates the RFC 9110 work. pact-foundation#584 makes it more visible: on replay the single surviving rule is applied to every value, so the provider side goes through the scalar fallback and logs the RFC 9110 §5.3 warning.

Root cause

MatchingRuleCategory::to_v3_json (rust/pact_models/src/matchingrules/mod.rs, ~line 1122):

Category::HEADER | Category::QUERY => {
  let name = category.first_field().map(|v| v.to_string())
    .unwrap_or_else(|| category.to_string());
  map.insert(name, rulelist.to_v3_json());
}

first_field() drops the index, so $.X-Request[0] and $.X-Request[1] both map to X-Request, and the second insert overwrites the first. self.rules is a HashMap<DocPath, RuleList>, so the survivor is random.

Reproduction

V4 pact, one request header with two values, each with its own regex:

index value matcher
0 first-1 regex ^first-\d+$
1 second-1 regex ^second-\d+$

Via pact_ffi:

pactffi_with_header_v2(i, InteractionPart_Request, "X-Request", 0,
  "{\"value\":\"first-1\",\"pact:matcher:type\":\"regex\",\"regex\":\"^first-\\\\d+$\"}");
pactffi_with_header_v2(i, InteractionPart_Request, "X-Request", 1,
  "{\"value\":\"second-1\",\"pact:matcher:type\":\"regex\",\"regex\":\"^second-\\\\d+$\"}");

Send a request with both values (mock server matches fine), then pactffi_write_pact_file.

Expected: the pact file keeps both rules, e.g. X-Request[0] → ^first-\d+$, X-Request[1] → ^second-\d+$ (or an equivalent per-value form).

Actual: matchingRules.header holds one rule under X-Request. Over 2×8 runs per lib (released, pr568, pr584), the regex written was a random mix of ^first-\d+$ and ^second-\d+$. In V3 the header value is also joined as "first-1, second-1".

Impact

  • Provider verification checks every header value against whichever rule survived, so it fails for values the consumer test accepted (or passes values it shouldn't).
  • The pact file isn't deterministic, so the same consumer test can publish a different contract on each run, which changes the content hash in the broker.
  • Consumer libraries that now expose per-index header matchers (pact-go, pact-js, pact-php, pact-net, pact-ruby in the downstream PRs for fix(matching): RFC 9110 repeated-header matching (review PR for #568) pact-foundation/pact-reference#584) can't promise those matchers reach the provider.

Related

  • fix: query param ordering respected when creating v2 pacts pact-foundation/pact-reference#504 (closed): V2 query strings lose the cross-key order of repeated values because build_query_string iterates a HashMap. Same family (multi-value data held in HashMaps loses information on write), but a different defect: that one is value ordering and was parked because preserving insertion order means changing the map type (a breaking change). This one is rule loss from a key collision, and is fixable without changing any types.
  • Category::QUERY goes through the same branch, and generators (Generators::to_json) had the same first_field() collapse. Both are verified as affected, and both are covered by the fix.

Fix (draft)

Branch fix/pact-models-indexed-matchers on YOU54F/pact-reference, commit 18137be:

  • to_v3_json and Generators::to_json write indexed header and query keys with their full path, e.g. $['X-Request'][1]. A lone [0] is still written as the bare name, so existing pact files don't change. When other indices exist, [0] keeps its index ($['X-Request'][0]).
  • When reading, $-prefixed [Root, Field, Index] keys map back to indexed paths. Every other key keeps its meaning.
  • Tests: round-trip tests in pact_models for matching rules and for generators, plus an FFI test (matchers_for_each_header_and_query_value_survive_writing_the_pact_file) that writes a pact through pact_ffi and checks that both rules are in the file.

Spec / format note

Neither the V3 nor the V4 spec defines indexed keys under matchingRules.header or matchingRules.query. This is a file-format extension, so it should be discussed in pact-specification before it's merged. Readers that don't know the format won't break: they won't recognise the indexed key as a header name, so they ignore the rule. They then fall back to equality for that value, as below.

pact-jvm compatibility (tested locally)

pact-jvm master (c0d2181fc), RequestMatching.requestMismatches, run with both the V1 and the V2 matching engine against V3 and V4 pacts. Headers and query parameters gave identical results. The expected values are first-1, second-1, with rules ^first-\d+$ / ^second-\d+$:

rule layout in the pact file actual first-7, second-8 actual first-7, first-8 what pact-jvm does
$['X-Request'][0] + $['X-Request'][1] (fix as committed) mismatch mismatch ignores both keys and falls back to equality (Expected header 'X-Request' to have value 'first-1' but was 'first-7')
bare X-Request + $['X-Request'][1] (alternative) mismatch match applies the bare rule to every value and ignores [1]
bare X-Request = first rule (today, when the first rule wins) mismatch match same as above
bare X-Request = second rule (today, when the second rule wins) mismatch mismatch applies the second rule to every value

pact-jvm doesn't support per-value header or query matchers in any layout. MatchingContext.resolveMatchers compares the rule key literally with the header name. Writing the first value's rule under the bare name (the alternative layout) is more backward compatible: pact-jvm and older pact_matching then behave as they do today when the first rule wins, and the run-to-run randomness goes away. That layout needs pact_matching to prefer [index] over the bare name when both exist, and the FFI to stop re-keying index 0 to [0] (or the reader to treat a bare name as [0] when indexed siblings exist).

Proposed fix (original)

  1. Keep the index when serialising header and query rules, e.g. write X-Request[1] (and parse it back in matchers_from_json), for V3 and V4. Index 0 / no index stays as the bare name, so existing pact files are unchanged.
  2. If that format change needs a spec discussion first, at least make the output deterministic (e.g. iterate sorted, or merge the rule lists with combine: OR) so the pact file doesn't change between runs, and log a warning that per-index rules were merged.

A regression test in pact_models that builds a header category with [0] and [1] rules and round-trips it through to_v3_json / matchers_from_json would cover both options.

🤖 Generated with Claude Code

…uery value

Matching rules and generators added for a specific value of a repeated
header or query parameter (e.g. via pactffi_with_header_v2 with index > 0)
are keyed as $.name[n]. When writing V3/V4 pact JSON, to_v3_json and the
generator serialisation reduced the path to first_field(), so every index
collapsed onto the bare name and only one (random) rule survived.

Indexed rules are now written with their full path, e.g. $['X-Request'][1].
A lone [0] is still written as the bare name, so existing pact files are
unchanged. Reading maps $-prefixed [Root, Field, Index] keys back to
indexed paths; all other keys keep their existing meaning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant