Hi Team, Im a QE thats started implementing Pactfl...
# pactflow
j
Hi Team, Im a QE thats started implementing Pactflow with the pact-cypress-adapter However we’re running into issues with oneOf, allOf, ect… Pactflow expects the response to look like this:
Copy code
"response": {
        "status": 200,
        "headers": {
          "content-type": "application/json",
          "access-control-allow-origin": "<http://exampleapi.com|exampleapi.com>"
        },
        "oneOf": {
          "body": {
            "id": "string",
            "email": "string",
            "group": "string",
            "isOnboarded": true,
            "organizationId": 0,
            "createdAt": "string",
            "updatedAt": "string",
            "contactType": "string",
            "devices": ["string"],
            "notificationPreferences": { "email": true, "sms": true },
            "phone": "string",
            "isDeactivated": true,
            "deactivatedAt": "string",
            "entityName": "string"
          }
        }
      }
However its impossible to create a response like this with cypress. Cypress’s intercept can only return a body, not a oneOf. How can we resolve this?
m
What makes you think Pactflow expects the body to have the top level property
oneOf
?
I’m guessing you’re using our BDCT feature with an OAS.
oneOf
is a logical keyword in JSON schema, and not something that should appear in an actual body.
j
since manually changing the contract to the above format, makes the contract work as expected
m
are you doing BDCT (i.e. publishing an OAS) or standard CDC (verifying with Pact)?
j
yes, BDCT and publishing an OAS
heres what the provider looks like:
Copy code
"responses": {
          "200": {
            "description": "The found contact if they exist",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    { "$ref": "#/components/schemas/IndividualContact" },
                    { "$ref": "#/components/schemas/AgentContact" },
                    { "$ref": "#/components/schemas/EntityContact" },
                    { "$ref": "#/components/schemas/LenderContact" },
                    { "$ref": "#/components/schemas/InternalContact" }
                  ]
                }
              }
            }
          },
well the response ^
m
strange
j
I dereferenced the oneOf schema (based on suggestions in that page) and unfortunately that didn’t do the trick.
pactflow is still saying that the properties are all additional and its not matching the oneOf exactly. not sure what to make of it:
dereferenced OAS
m
I don’t see
entityName
in either of those schemas
Is that just an artifact of the screenshot?
j
no, your right. It was in the consumer request body but the provider did not specify it in the schema. However, I just removed it from the consumer and still get an abundance of issues:
I also made all the properties required based on what I read on that page, still same issue. Heres the updated provider:
m
Any chance you could please share the OAS/pact file? if not here, please send to support@pactflow.io (along with your account information) so the team can review
j
Ill send an email with that data. Thanks for the help so far though!
🙏 1
m
You’re welcome.
c
Hi Jacob, I replied to your email but thought I would here as well, just for posterity. After dereferencing and inlining the oneOf and anyOf keywords, I notice all the errors were related to the oneOf used for the 200 response to path
/v2/contacts/{contactId}
. Digging into this the issue here is that the 2 objects provided are too similar for our tool to match the response from your pact file to one of these objects. This is because one has properties that are the subset of the other, and no unique properties of its own. In this case we recommend combining the objects into one schema rather than using the oneOf keyword, and making the required list contain the fields that your consumers are using.
1
e
@Candy Goodison could you help me understand
inlining the oneOf
keywords please? I'm observing issues very similar to what Jacob posted here - this is after i dereferenced all
$ref
, and in our schema we also have a bunch of
oneOf
responses.
json-schema-merge-allof
seems to not affect
oneOf
keywords at all 🤔
I think we have 3 issues with trying to validate Pacts against OAS, after merging/dereferencing all $refs: •
oneOf
keywords inside the schema with Properties, each of the
oneOf
schemas also having Properties. Errors of the kind
should NOT have additional properties
- not sure how to work around it if there's no option to allow additional properties? Something along these lines? • if i set
--additionalPropertiesInResponse true
in swagger-mock-validator ^ goes away, but two more issues occur: •
Response body is incompatible with the response body schema in the spec file: should match exactly one schema in oneOf
and
Request body is incompatible with the request body schema in the spec file: should match exactly one schema in oneOf
What's strange looking to me about the last two errors are these two comparisons:
Copy code
Comparing Mock
value: { day: 5, interval: 2, month: 4, type: 'YEARLY' }
With OAS
value: [ 'daysOfWeek', 'interval', 'type' ]
and
Copy code
Comparing Mock
value: { daysOfMonth: [Array], interval: 1, type: 'MONTHLY' }
With OAS
value: [ [Object], [Object], [Object], [Object] ]
In the first case it compares mock response with a non-matching
oneOf
schema (while there is a matching one, but it doesn't error). And in the second case it compares request with a literal
[Object]
? Any help/pointers are appreciated 🙏
c
Hi @Eugene Malihins I'm not 100% sure why you are having issues with the properties in your oneOf section, however it's not recommended to turn on additional properties as it can cause other unexpected issues with the oneOf, allOf and anyOf keywords. Write up about it can be found here: https://bitbucket.org/atlassian/swagger-mock-validator/src/master/FAQ.md In your OAS spec are you including a 'required' section for the objects? The tool uses the required fields to know which specific fields to compare with the values in the pact file. It's hard for me to tell exactly what is causing these errors, Any chance you could please share the OAS/pact file? if not here, please send to support@pactflow.io (along with your account information) and I can have a bit more of a look for you 🙂
1
e
Hi @Candy Goodison, thanks for your response! I've figured out most of the oneOf issues by now - biggest issue was that properties contained in oneOf objects weren't actually present in the parent object itself. Required section existed already for both the parent object (requiring child object with oneOf) and inside the oneOf array. I am now hitting one more issue with oneOf, and i think it might come down to the either msw responding with incorrect object, or pact-msw-adapter capturing incorrect response. I'll dig a bit more and will send an email to support@ if i cannot figure it out. Thanks!
🙌 2
m
Great! Do let us know how you go Eugene and best of luck!