Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays with plain integers #1765

Closed
Felix-Dyck opened this issue Jan 30, 2024 · 4 comments
Closed

Arrays with plain integers #1765

Felix-Dyck opened this issue Jan 30, 2024 · 4 comments
Labels
question Indicates that an issue, pull request, or discussion needs more information

Comments

@Felix-Dyck
Copy link

(android, kotlin and library: au.com.dius.pact" version "4.3.10")

Hello,
I have certain type of json format I want to verify:

[...]
"months": [
        4,
        5,
        6,
        7,
        8
    ],
[...]

The array can be of any size and I want to verify that any value inside is of type Integer.
So I started with something like:

this.newArray("months") {
    this.integerType(4)
    this.integerType(5)
    this.integerType(6)
    this.integerType(7)
    this.integerType(8)
  }

While the generated array looks correct, the matching rules look like this:

"[...].months[0]": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "integer"
                }
              ]
            },
            "[...].months[1]": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "integer"
                }
              ]
            },
            "[...].months[2]": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "integer"
                }
              ]
            },
            "[...].months[3]": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "integer"
                }
              ]
            },
            "[...].months[4]": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "integer"
                }
              ]
            },

So every entry that I have given is expected and is matched by their value. When I have a longer actual array than, my verification will fail. When my values change, my verification will fail.

I have also seen eachLike and minArrayLike which solve some of the issues. But then there are other issues:

this.minArrayLike("months", 1) { arr ->
                                    arr.integerType(4)
                                    arr.integerType(5)
                                    arr.integerType(6)
                                    arr.integerType(7)
                                    arr.integerType(8)
                                }

-> then integerType needs a field "names". But I don't want these integers to have key.
Anyways, there is also arr.integerType() which doesn't take a parameter. But still, when the array will be generated, it will look like this:

"months": [
    {

    }
  ],

-> an object is expected. That why the verification will fail when actually receiving integers.

Same goes for eachLike. Here are also objects expected.

Thanks for reading.

@rholshausen rholshausen added the question Indicates that an issue, pull request, or discussion needs more information label Feb 14, 2024
@rholshausen
Copy link
Contributor

Use minArrayLike("months", 1, PactDslJsonRootValue.integerType(10)) - replace 10 with whatever example value you want in your tests.

@Felix-Dyck
Copy link
Author

minArrayLike("months", 1, PactDslJsonRootValue.integerType(10))

Thanks for the reply!

Unfortunately, this doesn't seem to work:
"Type mismatch: inferred type is PactDslJsonRootValue but Consumer<LambdaDslObject!>! was expected"
is what I get.

@rholshausen
Copy link
Contributor

You need to use it on the outer object, not the inner one.

@rholshausen
Copy link
Contributor

Oh, sorry, for minArrayLike it is minArrayLike(String name, Integer size, PactDslJsonRootValue value, int numberExamples) so
minArrayLike("months", 1, PactDslJsonRootValue.integerType(10), 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Indicates that an issue, pull request, or discussion needs more information
Projects
None yet
Development

No branches or pull requests

2 participants