Repository navigation
Conversation
…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>
This was referenced Oct 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_v2with anindex> 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 byHashMapiteration 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.3warning.Root cause
MatchingRuleCategory::to_v3_json(rust/pact_models/src/matchingrules/mod.rs, ~line 1122):first_field()drops the index, so$.X-Request[0]and$.X-Request[1]both map toX-Request, and the secondinsertoverwrites the first.self.rulesis aHashMap<DocPath, RuleList>, so the survivor is random.Reproduction
V4 pact, one request header with two values, each with its own regex:
first-1^first-\d+$second-1^second-\d+$Via pact_ffi:
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.headerholds one rule underX-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
Related
build_query_stringiterates aHashMap. Same family (multi-value data held inHashMaps 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::QUERYgoes through the same branch, and generators (Generators::to_json) had the samefirst_field()collapse. Both are verified as affected, and both are covered by the fix.Fix (draft)
Branch
fix/pact-models-indexed-matcherson YOU54F/pact-reference, commit 18137be:to_v3_jsonandGenerators::to_jsonwrite 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]).$-prefixed[Root, Field, Index]keys map back to indexed paths. Every other key keeps its meaning.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.headerormatchingRules.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 arefirst-1, second-1, with rules^first-\d+$/^second-\d+$:first-7, second-8first-7, first-8$['X-Request'][0]+$['X-Request'][1](fix as committed)Expected header 'X-Request' to have value 'first-1' but was 'first-7')X-Request+$['X-Request'][1](alternative)[1]X-Request= first rule (today, when the first rule wins)X-Request= second rule (today, when the second rule wins)pact-jvm doesn't support per-value header or query matchers in any layout.
MatchingContext.resolveMatcherscompares 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)
X-Request[1](and parse it back inmatchers_from_json), for V3 and V4. Index 0 / no index stays as the bare name, so existing pact files are unchanged.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_modelsthat builds a header category with[0]and[1]rules and round-trips it throughto_v3_json/matchers_from_jsonwould cover both options.🤖 Generated with Claude Code