Skip to content

feat(pact_models): check pact files against their spec version on write - #613

Draft
YOU54F wants to merge 1 commit into
masterfrom
feat/ffi-enforce-spec-version
Draft

YOU54F wants to merge 1 commit into
masterfrom
feat/ffi-enforce-spec-version

Conversation

@YOU54F

@YOU54F YOU54F commented Nov 27, 2026

Copy link
Copy Markdown
Member

Problem

pact_models will happily write rules a spec version doesn't define:
eachKey / eachValue / arrayContains / statusCode / notEmpty /
semver / plugin matchers and MockServerURL / ProviderState
generators into V3 pacts, and integer etc. into V2 ones. V2 pacts also
got "generators": null. Verifiers on the older spec silently skip the
rules, so the contract is weaker than the consumer thinks it is, and
schema validation (pactflow/pact-schemas) rejects the files.

Change

  1. Lossless downgrades for V2. Equality rules are dropped (V2 already
    matches by equality), unless they sit under a parent rule. include is
    written as an escaped regex ((?s).*<escaped>.*). Generators aren't
    written at all.
  2. Check on write. spec_version_check::spec_violations walks the
    serialised JSON. write_pact reports each rule or generator the target
    version doesn't define, e.g.
    matcher 'eachKey' at /interactions/0/response/matchingRules/body/$.k/matchers/0 requires a V4 pact
  3. Warn now, strict opt-in. It warns by default. With
    PACT_STRICT_SPEC_VERSION=true the write fails and lists every
    violation. Unknown match/generator types are treated as plugins, so they
    need V4.

I went with warn-by-default because pact-js users write
fromProviderState into V3 pacts today, and failing those writes in a
minor would break people. I'd like strict to be the default in the next
major.

Testing

  • pact_models: new unit tests for the checker and the V2 downgrades. Full
    suite passes (641 + doc tests).
  • pact_ffi: suite passes.
  • End to end: built libpact_ffi (release) and swapped it into
    pact-js-core 20.2.0's prebuild folder, then ran the pact-msw-adapter
    golden harness (86 cases across V2/V3/V4, validated against
    pact-schemas):
    • default: 86/86 pass. Pact output is unchanged apart from the V2
      downgrades, and the warnings are logged.
    • PACT_STRICT_SPEC_VERSION=true: exactly the 5 known leak cases fail,
      each with the violations listed (eachKey, eachValue, arrayContains,
      notEmpty, semver, statusCode, a plugin matcher, MockServerURL and
      ProviderState into V3; integer into V2).

Follow ups

  • An FFI setter (e.g. pactffi_with_strict_spec_version) so pact-js etc.
    can opt in without an env var.
  • The check could run earlier, at mock server start, so the failure shows
    up in the test rather than at write time. I kept it at write because
    FFI interactions keep changing until then.

Related: pactflow/pact-schemas#174 fixes the v4 schema gaps the same harness turned up.

The model is version agnostic, so it is easy to build an interaction with
V4-only matchers (eachKey, arrayContains, statusCode, plugins...) and write
it as a V3 pact, or V3 matchers into a V2 one. Older verifiers ignore the
rules they don't know, and anything validating against the spec rejects
the file.

Downgrade where nothing is lost (V2):
- equality rules are dropped, unless they sit under a parent rule where
  dropping them would change what is matched
- include becomes an escaped regex
- generators are no longer written (was "generators": null)

Everything else is checked in write_pact against the JSON that is about to
land on disk. By default it logs a warning per violation, naming the rule,
its JSON pointer and the spec version it needs. Set
PACT_STRICT_SPEC_VERSION=true to fail the write instead.

Warn-by-default because pact-js users write fromProviderState generators
into V3 pacts today. Plan is to make strict the default in the next major.
YOU54F added a commit to pactflow/pact-msw-adapter that referenced this pull request Nov 27, 2026
The FFI writes equality and include into v2 pacts as-is today, so both
cases are tracked in ffiSpecLeaks. With the v2 downgrade in
pact-foundation/pact-reference#613:

- equalityInclude flips: top-level equality is dropped and include
  becomes a regex. The test will then ask for it to come out of
  ffiSpecLeaks, and toV2MatchingRules needs to do the same downgrade
- equalityUnderParent stays a leak: equality under eachLike has no
  lossless v2 form, so the FFI keeps it and warns

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.

1 participant