Pact-jvm: [Question] How can I specify a field to be String or null using PactDslJsonBody?

Created on 14 Oct 2016  路  2Comments  路  Source: pact-foundation/pact-jvm

As a part of a Pact, I want to specify a field to be String or null, for example.

PactDslJsonBody body = new PactDslJsonBody()
...
.stringType("nickname")
....;

But the Pact verification fails when 'nickname' is null :'( :(

I was using JSON Schema before and it allows me to do it. like this:

{
...
"nickname": [ "string", null ]
...
}

Is there any workaround to achieve this? this is getting problematic for us...

Thanks a lot!

question

Most helpful comment

Thanks!

It's a shame that it doesn't support that yet. In relation to your concern: I wouldn't be worried about it: if you define all fields on the payload optional, then a payload with all fields null is correct! In json schema you can specify additional rules like 'at least one of these fields must be not null'. In my opinion the pact definition 'has' to be very flexible to define these kind of rules, similar to json schema, otherwise ppl are not going to use it because doesn't suit their requirements.

Thanks for your response! And I will be happy to contribute on extending the pact definition if it's possible.

All 2 comments

There is currently no way to do this, but there is a specification change that adds support for OR: https://github.com/pact-foundation/pact-specification/tree/version-3#allow-arrays-of-matchers-to-be-defined-against-a-matcher-path

We do have some concern about this feature. Including this in the contract could result in the condition where all the tests use the null value and hence pass, and then an invalid value is used in production.

For related discussions: #311 and #261

Thanks!

It's a shame that it doesn't support that yet. In relation to your concern: I wouldn't be worried about it: if you define all fields on the payload optional, then a payload with all fields null is correct! In json schema you can specify additional rules like 'at least one of these fields must be not null'. In my opinion the pact definition 'has' to be very flexible to define these kind of rules, similar to json schema, otherwise ppl are not going to use it because doesn't suit their requirements.

Thanks for your response! And I will be happy to contribute on extending the pact definition if it's possible.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uglyog picture uglyog  路  4Comments

jordi9 picture jordi9  路  4Comments

ajhcpt picture ajhcpt  路  9Comments

DerKnecht picture DerKnecht  路  10Comments

thombergs picture thombergs  路  3Comments