question on array matching in pact-jvm

1,756 views
Skip to first unread message

Natasha Badillo

unread,
Jul 23, 2015, 12:55:19 PM7/23/15
to Pact
Hello! I've just started working with Pact, and I apologize if there's something obvious that I've missed. But I've run into what could possibly be a bug, or maybe just something that's not supported. Here's what I'm trying to do:

I have a provider that returns a JSON array in the following format:

[ {
 
"clearedDate" : "07/22/2015",
 
"status" : "C",
 
"amount" : 15.0
}, {
 
"clearedDate" : "07/22/2015",
 
"status" : "C",
 
"amount" : 15.0
}, {

 
"clearedDate" : "07/22/2015",
 
"status" : "C",
 
"amount" : 15.0
} ]


So I'm writing a consumer pact for this endpoint. Now, since the array is the entire http body, it doesn't have a label/name - which means I can't use the arrayLike(String name) method on a PactDslJsonBody. The closest I've managed to get is this:

DslPart body = new PactDslJsonArray()
   
.eachLike()
       
.date("clearedDate", "mm/dd/yyyy")
       
.stringType("status")
       
.realType("amount")
       
.closeObject()
   
.closeArray();


However, this returns a pact file looking like this:

"response" : {
 
"status" : 200,
 
"headers" : {
 
"Content-Type" : "application/json"
 
},
 
"body" : [ [ {
   
"amount" : 6.709851646E9,
   
"status" : "MNDJHNtmnzGNiBIIQyOF",
   
"clearedDate" : "22/23/2015"
 
} ] ],
 
"matchingRules" : {
   
"$.body[0][*].status" : {
     
"match" : "type"
   
},
   
"$.headers.Content-Type" : {
     
"regex" : "application/json"
   
},
   
"$.body[0][*].clearedDate" : {
     
"date" : "mm/dd/yyyy"
   
},
   
"$.body[0][*].amount" : {
     
"match" : "real"
   
},
   
"$.body[0]" : {
     
"min" : 0
   
}
 
}
}


Obviously, the extra [ ] around the body are a problem. But the matchers also don't look right -- should that [0] be there before the wildcard [*]? From digging around in the code, it also looks like there's another matcher that should have been added and isn't there - "$.body" : { "match" : "type" }

I realized I could get the format of the body correct if I just dropped the final closeArray() in my code, but that results in this pact file:

"response" : {
 
"status" : 200,
 
"headers" : {
   
"Content-Type" : "application/json"
 
},
 
"body" : [ {
   
"amount" : 9.329956477E9,
   
"status" : "qzDcbvVfZAiXnYNTuabk",
   
"clearedDate" : "38/23/2015"
 
} ],
 
"matchingRules" : {
   
"$.body[*].clearedDate" : {
     
"date" : "mm/dd/yyyy"
   
},
   
"$.headers.Content-Type" : {
     
"regex" : "application/json"
   
},
   
"$.body[*].status" : {
     
"match" : "type"
   
},
   
"$.body[*].amount" : {
     
"match" : "real"
   
}
 
}
}


Now the body is right, but I'm missing the matchers "$.body" : { "match" : "type" } and "$.body" : { "min" : 0 }. The "min" is especially necessary, as the provider test will fail without it. I have tried both ways with minArrayLike() as well, with the same results.

So in conclusion, I'm stumped. Is this a bug? Or am I doing something wrong or unsupported? I very much appreciate any guidance you can offer. Thanks!

Ronald Holshausen

unread,
Jul 23, 2015, 10:56:25 PM7/23/15
to Natasha Badillo, Pact
Hi Natasha,

The eachLike matcher is assuming there is an object at the root of the body. Can you raise an issue for this at https://github.com/DiUS/pact-jvm to allow it to work when the root of the body is an array?

In the mean time if you are able to could change your JSON to have an object at the root, which is probably better practice, you can get it to work. E.g., something like

{
  "values": [

    {
      "clearedDate": "07/22/2015",
      "status": "C",
      "amount": 15.0
    },
    {
      "clearedDate": "07/22/2015",
      "status": "C",
      "amount": 15.0
    },
    {
      "clearedDate": "07/22/2015",
      "status": "C",
      "amount": 15.0
    }
  ]
}


DslPart body = new PactDslJsonBody()
    .eachLike("values")

        .date("clearedDate", "mm/dd/yyyy")
        .stringType("status")
        .realType("amount")
        .closeObject()
    .closeArray()
.closeObject();

Regards,

Ronald

--
You received this message because you are subscribed to the Google Groups "Pact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pact-support...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ronald Holshausen

DiUS Computing Pty Ltd

Level 10, 99 Queens Street
Melbourne, VIC 3000

Phone: +61 3 9008 5400
Mobile: +61 413 162 439

http://www.diuscomputing.com.au

Natasha Badillo

unread,
Jul 27, 2015, 2:18:42 PM7/27/15
to Pact, rhols...@dius.com.au
Thanks for your quick response! I was afraid that this was the case. I've raise Issue #145 at Github: https://github.com/DiUS/pact-jvm/issues/145

Unfortunately the code for the provider is maintained by another team, and I don't really have the freedom to make changes like that. And since we don't yet have proper contract tests in place, I couldn't really be sure what other consumers might break if we change that endpoint. Sort of a chicken or the egg situation ... ;)
Reply all
Reply to author
Forward
0 new messages