Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ target/
# Generated Release Artifacts
release_artifacts

out/

node_modules/
*.log
.idea/
Expand Down
161 changes: 145 additions & 16 deletions rust/pact_ffi/IntegrationJson.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ To make setting up matching rules and generators easier for language implementat
JSON format can be used with embedded rules in it. This JSON can be used for most interaction values (i.e. headers,
query parameters, etc.)

For each value were matching rules or generators are required, a JSON object can be passed in with the following
For each value were matching rules or generators are required, a JSON object can be passed in with the following
attributes:

| Attribute | Description | Values |
Expand All @@ -18,7 +18,7 @@ documented at [V4 matching rules](https://github.com/pact-foundation/pact-specif
generators at [V4 generators](https://github.com/pact-foundation/pact-specification/tree/version-4#generators).

For example: Assuming we want to use a regex matcher on the request path. The `pactffi_with_request` function allows us
to pass the JSON value in as the path value. So instead of calling
to pass the JSON value in as the path value. So instead of calling

```js
pactffi_with_request(handle, "PUT", "/path/to/100")
Expand All @@ -40,8 +40,8 @@ For paths, the value used must be a string value (or a primitive value that will
Example JSON:
```json
{
"value": "/path/to/100",
"pact:matcher:type": "regex",
"value": "/path/to/100",
"pact:matcher:type": "regex",
"regex": "\\/path\\/to\\/\\d+"
}
```
Expand All @@ -58,18 +58,18 @@ array should be used for the value).
Single parameter JSON:
```json
{
"value": "100",
"pact:matcher:type": "regex",
"value": "100",
"pact:matcher:type": "regex",
"regex": "\\d+"
}
```

Multiple parameter JSON (this example doesn't make much sense, because query parameters are always string values, so
Multiple parameter JSON (this example doesn't make much sense, because query parameters are always string values, so
will always have the same type. But it will check the minimum length):
```json
{
"value": ["1"],
"pact:matcher:type": "type",
"value": ["1"],
"pact:matcher:type": "type",
"min": 1
}
```
Expand All @@ -86,8 +86,8 @@ array should be used for the value).
Single header value JSON:
```json
{
"value": "100",
"pact:matcher:type": "regex",
"value": "100",
"pact:matcher:type": "regex",
"regex": "\\d+"
}
```
Expand Down Expand Up @@ -122,7 +122,7 @@ For example, add a type matcher to an `id` attribute and a regex matcher to the
},
"created": {
"value": "maybe",
"pact:matcher:type": "regex",
"pact:matcher:type": "regex",
"regex": "(yes|no|maybe)"
}
}
Expand Down Expand Up @@ -160,11 +160,12 @@ Here the `interests` attribute would be expanded to

## Supporting multiple matching rules

Matching rules can be combined. These rules will be evaluated with an AND (i.e. all the rules must match successfully
for the result to be successful). The main reason to do this is to combine the `EachKey` and `EachValue` matching rules
Matching rules can be combined. By default, these rules will be evaluated with an AND (i.e. all the rules must match
successfully for the result to be successful). See [Combining matching rules with OR](#combining-matching-rules-with-or)
to require only one of the rules to match. The main reason to do this is to combine the `EachKey` and `EachValue` matching rules
on a map structure, but other rules make sense to combine (like the `include` matcher).

To provide multiple matchers, you need to provide an array format.
To provide multiple matchers, you need to provide an array format.

For example, assume you have an API that returns results for a document store where the documents are keyed based on some index:
```json
Expand Down Expand Up @@ -216,9 +217,137 @@ So the resulting intermediate JSON would be something like:
}
```

### Combining matching rules with OR

The `pact:matcher:combine` attribute sets how the matching rules are combined. It can be either `AND` (the default,
all the rules must match) or `OR` (at least one of the rules must match). Any other value is an error, and the
matching rules will be ignored.

The top level `value` is the example used for the consumer test.

#### Alternatives with their own example values

Each matcher in an `OR` list can have its own `value`. This makes it a complete alternative: the value is the
example for that alternative, and can contain matchers of its own. The alternatives can be any shape, so a value
can be an object of one shape or another, an object or an array, an array or a string, and so on.

For example, a `contact` that can either be an object with an email, an object with a phone number, a non-empty
array of contacts, or null:

```json
{
"contact": {
"pact:matcher:type": [
{
"pact:matcher:type": "type",
"value": {
"email": { "pact:matcher:type": "regex", "regex": "^[^@ ]+@[^@ ]+$", "value": "fred@example.com" }
}
},
{
"pact:matcher:type": "type",
"value": {
"phone": { "pact:matcher:type": "regex", "regex": "^\\d+$", "value": "0123456789" }
}
},
{ "pact:matcher:type": "type", "min": 1, "value": [{ "email": "fred@example.com" }] },
{ "pact:matcher:type": "null", "value": null }
],
"pact:matcher:combine": "OR",
"value": { "email": "fred@example.com" }
}
}
```

A value matches an alternative if it matches the alternative's example using the alternative's matchers, in the
same way as any other value and matchers.

In the Pact file, the alternatives are stored as the matchers of a rule list combined with `OR`. Each one has a
`variant` attribute with its example value and its matching rules, with paths relative to the example value:

```json
"$.contact": {
"combine": "OR",
"matchers": [
{
"match": "type",
"variant": {
"value": { "email": "fred@example.com" },
"rules": {
"$": { "combine": "AND", "matchers": [{ "match": "type" }] },
"$.email": { "combine": "AND", "matchers": [{ "match": "regex", "regex": "^[^@ ]+@[^@ ]+$" }] }
}
}
},
...
]
}
```

Implementations that don't support the `variant` attribute will ignore it, and use the matchers as a simple `OR`.

#### Alternatives without example values

If a matcher in an `OR` list doesn't have a `value`, it is applied to the top level example value. This is enough
when the alternatives can be checked without an example, like a field that is either a string or null:

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

Matchers that compare against the example (like `type`) can only describe the shape of the top level example, so
use alternatives with their own example values for anything else. Also, the `regex` and `include` matchers convert
non-string values to their JSON text before matching, so as an alternative they can accept numbers, booleans,
objects or arrays whose JSON text matches (for example, `^\w+$` matches the number `5`).

An alternative can also be a list of matchers combined with `AND` or `OR`, as long as it has its own `value`:

```json
{
"id": {
"pact:matcher:type": [
{
"pact:matcher:type": [
{ "pact:matcher:type": "regex", "regex": "^\\d+$" },
{ "pact:matcher:type": "include", "value": "1" }
],
"value": "100"
},
{ "pact:matcher:type": "null", "value": null }
],
"pact:matcher:combine": "OR",
"value": "100"
}
}
```

#### Headers, query parameters, paths and XML

Alternatives also work for headers, query parameters, paths, status codes, and XML attributes and text. These
values are not JSON, so only the matchers for the alternative value itself are applied, and they are compared
against the alternative's example value (i.e. an `equality` alternative compares against its own `value`).

#### Limitations

* Use `OR` with care. Only the top level example value is used for the consumer test, so the other alternatives are
never exercised against your consumer code.
* XML elements can not have alternatives with their own example values, only simple `OR` lists. This is because the
matching rules for XML are keyed by the element names, so an alternative element would need the same name.
* Other Pact implementations may not support the `variant` attribute, and will use the matchers as a simple `OR`.
* The experimental V2 matching engine (`PACT_MATCHING_ENGINE=v2`) does not support `OR`.

## Supporting matching rule definitions

You can use the [matching rule definition expressions](https://docs.rs/pact_models/latest/pact_models/matchingrules/expressions/index.html)
You can use the [matching rule definition expressions](https://docs.rs/pact_models/latest/pact_models/matchingrules/expressions/index.html)
in the `pact:matcher:type` field.

For example, with the previous document result JSON, you could then use the following for the `relatesTo` field:
Expand Down
Loading
Loading