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

Incorrect JSONPath is generated for numeric fields #127

Closed IschenkoArtem closed 9 years ago

IschenkoArtem commented 9 years ago

I'm using pact-jvm-consumer-junit 2.2.2. When matching JSON body that contains fields starting with digits responseMatchingRules being generated incorrectly.

Example:

"body" : {
        "2" : {
          "str" : "jildrdmxddnVzcQZfjCA"
        }
},
"responseMatchingRules" : {
        "$.body.2.str" : {
          "match" : "type"
        }
}

here is $.body.2.str is incorrect, because it's incorrect JS, thus on provider pact fails with meessage:

Path expression $.body.2.str is invalid, ignoring: [1.8] failure: string matching regex [$_\p{L}][$_\-\d\p{L}]*' expected but2' found

To make it work i have to change match rule manually to $.body['2'].str

bethesque commented 9 years ago

Interesting. The path $.body.2.str is not Javascript, it's JSON Path, and I haven't seen numeric keys specified in any different way. I suspect the code that looks up the path actually has the problem.

IschenkoArtem commented 9 years ago

Code that looks up the path uses JSONPath lib from gatling, which clearly doesn't want for field to start with number: https://github.com/gatling/jsonpath/blob/master/src/main/scala/io/gatling/jsonpath/Parser.scala#L52

So is gatling lib incorrect?

uglyog commented 9 years ago

You are correct, the field regular expression in gatling is expecting a field to start with a lower case character, underscore or dollar.

uglyog commented 9 years ago

I'll update the consumer DSLs to write ['2'] when it is a numeric field.

bethesque commented 9 years ago

Hm, not super keen on the fact that a quirk in a JVM library is going to force every other Pact implementation in different languages to support this quirk in behaviour that I'm not even convinced is valid JSONPath (it may well be, but I haven't seen any evidence for it).

bethesque commented 9 years ago

If we're going to do this, we need a pact-spec for it.

uglyog commented 9 years ago

['2'] is valid jsonpath. I refer you to http://goessner.net/articles/JsonPath/ on the part about the bracket–notation: $['store']['book'][0]['title']

bethesque commented 9 years ago

Yes, it's a valid jsonpath for an array lookup. But an object lookup?

bethesque commented 9 years ago

Ah, I see what you mean. if $['store'] is valid then $['2'] is valid. Ok, all good then.

uglyog commented 9 years ago

I've released version 2.2.4 with the fix to pact-jvm-consumer-junit for this.

IschenkoArtem commented 9 years ago

Very much appreciated. Will check that later.

IschenkoArtem commented 9 years ago

My example works now. Result is: $.body['2'].str. Thanks.