Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,89 @@ Feature: V3 era Matching Rules
When the request is compared to the expected one
Then the comparison should NOT be OK
And the mismatches will contain a mismatch with error "$.four" -> "Expected 100 (Integer) to be the same type as 'a' (String)"

Scenario: Supports matching rules combined with OR (positive case)
Given an expected request configured with the following:
| body | matching rules |
| file: basic.json | or-regex-matcher-v3.json |
And the following requests are received:
| body | desc |
| JSON: { "one": "100", "two": "b" } | matches the first rule |
| JSON: { "one": "abc", "two": "b" } | matches the second rule |
When the requests are compared to the expected one
Then the comparison should be OK

Scenario: Supports matching rules combined with OR (negative case)
Given an expected request configured with the following:
| body | matching rules |
| file: basic.json | or-regex-matcher-v3.json |
And a request is received with the following:
| body |
| JSON: { "one": "A1", "two": "b" } |
When the request is compared to the expected one
Then the comparison should NOT be OK
And the mismatches will contain a mismatch with error "$.one" -> "Expected 'A1' to match '^\d+$'"
And the mismatches will contain a mismatch with error "$.one" -> "Expected 'A1' to match '^[a-z]+$'"

Scenario: Supports an integer or decimal matcher combined with OR (positive case)
Given an expected request configured with the following:
| body | matching rules |
| file: basic.json | or-number-matcher-v3.json |
And the following requests are received:
| body | desc |
| JSON: { "one": 100, "two": "b" } | integer |
| JSON: { "one": 100.25, "two": "b" } | decimal |
When the requests are compared to the expected one
Then the comparison should be OK

Scenario: Supports an integer or decimal matcher combined with OR (negative case)
Given an expected request configured with the following:
| body | matching rules |
| file: basic.json | or-number-matcher-v3.json |
And a request is received with the following:
| body |
| JSON: { "one": true, "two": "b" } |
When the request is compared to the expected one
Then the comparison should NOT be OK

Scenario: Supports a type matcher combined with a null matcher with OR (positive case)
Given an expected request configured with the following:
| body | matching rules |
| file: 3-level.json | or-null-matcher-v3.json |
And the following requests are received:
| body | desc |
| JSON: { "one": { "a": { "ids": [1], "status": "OK" } }, "two": [ { "ids": [1], "status": "BAD" } ] } | an object |
| JSON: { "one": null, "two": [ { "ids": [1], "status": "BAD" } ] } | null |
When the requests are compared to the expected one
Then the comparison should be OK

Scenario: Supports a type matcher combined with a null matcher with OR (negative case)
Given an expected request configured with the following:
| body | matching rules |
| file: 3-level.json | or-null-matcher-v3.json |
And a request is received with the following:
| body |
| JSON: { "one": "a string", "two": [ { "ids": [1], "status": "BAD" } ] } |
When the request is compared to the expected one
Then the comparison should NOT be OK

Scenario: Supports header and query matching rules combined with OR (positive case)
Given an expected request configured with the following:
| query | headers | matching rules |
| a=1234 | 'X-TEST: 1234' | or-matcher-header-query-v3.json |
And the following requests are received:
| query | headers | desc |
| a=100 | 'X-TEST: 100' | matches the first rule |
| a=ABC | 'X-TEST: ABC' | matches the second rule |
When the requests are compared to the expected one
Then the comparison should be OK

Scenario: Supports header and query matching rules combined with OR (negative case)
Given an expected request configured with the following:
| query | headers | matching rules |
| a=1234 | 'X-TEST: 1234' | or-matcher-header-query-v3.json |
And a request is received with the following:
| query | headers |
| a=abc | 'X-TEST: abc' |
When the request is compared to the expected one
Then the comparison should NOT be OK
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,33 @@ Feature: V4 era Matching Rules
When the request is compared to the expected one
Then the comparison should NOT be OK
And the mismatches will contain a mismatch with error "$.actions" -> "Variant at index 1 ({\"href\":\"http://api.x.io/orders/42/items\",\"method\":\"DELETE\",\"name\":\"delete-item\",\"title\":\"Delete Item\"}) was not found in the actual list"

Scenario: Supports a not empty matcher combined with a null matcher with OR (positive case)
Given an expected request configured with the following:
| body | matching rules |
| file: basic.json | or-notempty-matcher-v4.json |
And a request is received with the following:
| body |
| JSON: { "one": "a", "two": "b" } |
When the request is compared to the expected one
Then the comparison should be OK

Scenario: Supports a not empty matcher combined with a null matcher with OR (positive case, null value)
Given an expected request configured with the following:
| body | matching rules |
| file: basic.json | or-notempty-matcher-v4.json |
And a request is received with the following:
| body |
| JSON: { "one": null, "two": "b" } |
When the request is compared to the expected one
Then the comparison should be OK

Scenario: Supports a not empty matcher combined with a null matcher with OR (negative case)
Given an expected request configured with the following:
| body | matching rules |
| file: basic.json | or-notempty-matcher-v4.json |
And a request is received with the following:
| body |
| JSON: { "one": "", "two": "b" } |
When the request is compared to the expected one
Then the comparison should NOT be OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"header": {
"X-TEST": {
"combine": "OR",
"matchers": [
{
"match": "regex",
"regex": "^\\d+$"
},
{
"match": "regex",
"regex": "^[A-Z]+$"
}
]
}
},
"query": {
"a": {
"combine": "OR",
"matchers": [
{
"match": "regex",
"regex": "^\\d+$"
},
{
"match": "regex",
"regex": "^[A-Z]+$"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"body": {
"$.one": {
"combine": "OR",
"matchers": [
{
"match": "notEmpty"
},
{
"match": "null"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"body": {
"$.one": {
"combine": "OR",
"matchers": [
{
"match": "type"
},
{
"match": "null"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"body": {
"$.one": {
"combine": "OR",
"matchers": [
{
"match": "integer"
},
{
"match": "decimal"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"body": {
"$.one": {
"combine": "OR",
"matchers": [
{
"match": "regex",
"regex": "^\\d+$"
},
{
"match": "regex",
"regex": "^[a-z]+$"
}
]
}
}
}
97 changes: 95 additions & 2 deletions rust/pact_ffi/IntegrationJson.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ 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.
Expand Down Expand Up @@ -216,6 +217,98 @@ 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). The value is not case-sensitive. Any other
value is logged as an error: the matching rules are ignored, and the value at that path is written as `null`.

For example, a `name` 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"
}
}
```

The rules are written to the Pact file as a rule list combined with `OR`:

```json
"$.name": {
"combine": "OR",
"matchers": [{ "match": "type" }, { "match": "null" }]
}
```

`OR` can be used for bodies (JSON and XML), headers and query parameters.

#### Nullable objects

If the example `value` is an object or array, any matching rules defined inside it are still applied to its
children. This lets you describe an optional object (for example, an address that is either null or an object whose
`number` is an integer):

```json
{
"address": {
"pact:matcher:type": [
{ "pact:matcher:type": "type" },
{ "pact:matcher:type": "null" }
],
"pact:matcher:combine": "OR",
"value": {
"street": "Main St",
"number": { "pact:matcher:type": "integer", "value": 42 }
}
}
}
```

This writes an `OR` rule list for `$.address` and an `integer` rule for `$.address.number`. A request with
`"address": null` matches, and the rules for the children are only checked when the value is not null.

#### Wrapping another matcher

Each path in the Pact file has a single rule list, combined with either `AND` or `OR`. If the `value` of an `OR`
matcher is itself a matcher for the same path, its rules are replaced by the `OR` rules and a warning is logged. This
is the same as the `or` matcher in Pact-JVM. For example, this only writes `integer OR null` for `$.id`, not the
`regex` rule:

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

Put all the alternatives in the one `pact:matcher:type` array instead. Rules for the same path that use the same
logic are added to the list.

#### Limitations

* All the alternatives are applied to the top level `value`, so matchers that compare against the example (like
`type`) can only describe the shape of that example. For instance, `type OR null` on an object accepts an object of
the same shape or null, but not an array.
* Each matcher in the list must be a single matcher. A nested list of matchers is an error.
* 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`).
* 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.

## Supporting matching rule definitions

You can use the [matching rule definition expressions](https://docs.rs/pact_models/latest/pact_models/matchingrules/expressions/index.html)
Expand Down
Loading
Loading