Manage Optional Fields In Consumer Contract In BDCT

Published date: April 15, 2024, Version: 1.0

PROBLEM STATEMENT:

I am a Consumer and I want to have some OPTIONAL fields in my Contract. So, when the Provider API returns these fields in the response, these optional fields should be entertained in the Consumer validation process in Pactflow and the Consumer validation should PASS / FAIL accordingly.

But, when these fields are not returned in the API response, Pactflow should not entertain these fields while validating the Consumer End in Contract validation. Pactflow should SKIP validation for these fields.

 

PROPOSED SOLUTION:

Before we dive deeper into the solution of this problem, we must educate ourselves that this is entirely controlled by the nature of the OAS Document provided by the Provider.

A consumer is only allowed to expect OPTIONAL fields in his contract if the OAS document allows for additional properties. It has a field by the name “additionalProperties“ which could be set to either of the following:

  1. additionalProperties: true → It ALLOWS the Consumer to have OPTIONAL fields in his contract

  2. additionalProperties: false → It DOES NOT allow the Consumer to have OPTIONAL fields in his contract

Scenario 1: 'additionalProperties' is set to “true“ in OAS document

Consumer Expectations:

Consumer wants Pactflow to consider the OPTIONAL FIELD while validating the Consumer End only if the Provider returns this field in the API response.

If API does not return this in the response, Consumer wants Pactflow to SKIP this field while performing validation.

There are 3 different sub cases under this scenario:

Case 1:

“additionalProperties“ Flag: true

Is “SocialSecurityNumber” Field Available In OAS Schema Section: NO

Contract Comparison Status: PASS

Reason: ‘socialSecurityNumber' is not available in the 'Components > Schema’ section in the OAS document, it will not participate in Consumer validation in Pactflow

Validation Result In Pactflow

Case 2:

“additionalProperties“ Flag: true

Is “SocialSecurityNumber” Field Available In OAS Schema Section: YES

Is Data Type Of This Field Same In Consumer Contract & OAS Document: YES

Contract Comparison Status: PASS

Reason: ‘socialSecurityNumber' is available in the 'Components > Schema’ section in the OAS document and its data type is also matching

Validation Result in Pactflow

Case 3:

“additionalProperties“ Flag: true

Is “SocialSecurityNumber” Field Available In OAS Schema Section: YES

Is Data Type Of This Field Same In Consumer Contract & OAS Document: NO

Contract Comparison Status: FAIL

Reason: ‘socialSecurityNumber' is available in the ‘Components > Schema’ section in the OAS document but its data type is not matching, in OAS it is ‘Boolean’ and in Consumer Contract it is 'String’.

Validation Result In Pactflow

Scenario 2: 'additionalProperties' is set to “false“ in OAS document

Screen shot below shows a Consumer Contract where highlighted field is OPTIONAL as per the Consumer’s expectations.

Now, the Consumer wants Pactflow to consider this field while validating the Consumer End in Contract validation only if the Provider returns this field in the API response.

If API does not return this in the response, Pactflow should SKIP this field while performing validation.

Validation Result: In this case, our Contract validation for Consumer in Pactflow should FAIL and the below screen shot proves the same and the highlighted part shows the failure reasons. Hence, the Consumer is not allowed to expect any OPTIONAL field in the contract when “additionalProperties“ is set to false in the OAS document