pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 479 forks source link

Value matching does not work in arrays #1532

Open SunriseOYR opened 2 years ago

SunriseOYR commented 2 years ago

versions

pact-jvm-consumer-junit:4.0.10
pact-jvm-consumer-java8:4.0.10
pact-jvm-provider-gradle:4.0.10

DslPart

DslPart body = newJsonArrayMinLike(1,(dto)->{
            dto.eachLike((item)->{
                item.stringValue("marketCode", "RU");
            }).build();
        }).build();

json

{
  "provider": {
    "name": "Rule"
  },
  "consumer": {
    "name": "RUThirdOrder"
  },
  "interactions": [
    {
      "description": "RU Third Order",
      "request": {
        "method": "GET",
        "path": "/orderDatas",
        "query": {
          "pact_jvm_orderIds": [
            "RCFFR000009302"
          ]
        }
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": [
          [
            {
              "marketCode": "RU"
            }
          ]
        ],
        "matchingRules": {
          "body": {
            "$": {
              "matchers": [
                {
                  "match": "type",
                  "min": 1
                }
              ],
              "combine": "AND"
            },
            "$[*]": {
              "matchers": [
                {
                  "match": "type",
                  "min": 0
                }
              ],
              "combine": "AND"
            }
          }
        }
      },
      "providerStates": [
        {
          "name": ""
        }
      ]
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "3.0.0"
    },
    "pact-jvm": {
      "version": "4.0.10"
    }
  }
}

But now, as long as marketCode is a string, validation will be successful, I tested stringType and stringMatcher and they work fine, while stringValue and numberValue don't work

mefellows commented 2 years ago

Well with the example you have provided that is expected behaviour. Could you please share an example of what you expect that's not working?

Are you saying that you want to match exact values?

SunriseOYR commented 2 years ago

Well with the example you have provided that is expected behaviour. Could you please share an example of what you expect that's not working?

Are you saying that you want to match exact values?

Yes, I want to match exact values, The match is only successful when marketCode = RU

kevinmavely commented 1 year ago

Any update on this, I found the same issue and I am using version 4.4.3. The "stringValue" field is not being checked on the value, just on the type.

Does eachLike support checking the value when defined in the example?

   return new PactDslJsonBody()
        .eachLike("fruits")
        .stringType("id", "example-id")
        .stringType("name", "example-name")
        .stringType("description", "test description")
        .stringValue("type", "gala")
        .stringType("color", "test-color")
        .stringType("grade", "test-grade")
        .closeArray();
  }
rholshausen commented 1 year ago

@kevinmavely eachLike sets up type matching on all the fields

sergtk commented 6 months ago

I had also issue with newJsonArrayMinLike. This is when min=0. Probably they are related: https://github.com/pact-foundation/pact-specification/issues/108

rholshausen commented 6 months ago

As per the previous comment, the eachLike set of functions setup type matching on all fields. If you want to override it with a particular value, use equalTo instead of stringValue.

sergtk commented 6 months ago

I just want to make test failed, when array contain invalid element, but tests still passes.

If I call newJsonArrayMinLike(1, ...), everything works as expected. But in some cases newJsonArrayMinLike(0, ...) is needed, and this result in passing tests on invalid response bodies.

(Not sure if this this is the same issue as the thread started with)