Does swagger-mock-validator validate the pact response / request body schema against the swagger definition?

Issue #70 resolved
Jon O created an issue

Hi all,

Apologies for creating an issue to ask you this but I was not sure what was the best way to ask this question?

After watching Ben on the video "Verifying Microservice Integrations with Contract Testing - Atlassian Summit 2016" (great video!), I did some research on pact and came across swagger-mock-validator. I have been playing with it using some simple pact files with the intention of using it to validate that an API's generated swagger matches a pact file to ensure the request and responses are as expected. However, I have noticed that I can change the names of properties within the response body in the pact file to properties which do not match the swagger definition and the validator does not generate any errors? Is this intentional or have I over looked something (highly likely)? Does the validator not validate the response and request body property names?

Again, sorry for raising this as an issue. If there is a more appropriate way to discuss this with you, feel free to point me in the right direction and close this "issue".

Great work with the validator - Being able to validate pact files against a swagger definition is really useful.

Thanks,

Jon

Comments (3)

  1. Ben Sayers

    Hi Jon,

    Raising an issue to ask a question is fine by me!

    Yes the swagger mock validator does validate response bodies. You can see the full list of what is validated here: https://bitbucket.org/atlassian/swagger-mock-validator/src/43adb8eaea49d35c4b7fa9a88ed0b2d05d7edd76/RULES.md?fileviewer=file-view-default

    I suspect the reason you are not seeing the failure you are looking for relates to the default behaviour of json schema. By default properties are not required and additional properties (properties not defined by the schema) are allowed. So when you misspelled a property the validation did not fail as the property was optional so it being missing is ok, and additional properties are allowed so this extra misspelled property is also ok.

    If you set additional properties to false your misspelled property would be detected as properties not defined by the schema would not be permitted. Keep in mind you would need to do this on every object within the response body, including nested objects. I would recommend following Postel's Law and only setting additional properties to false in your response bodies but leaving it missing (defaulting to true) for request bodies.

    You might be thinking that another way to resolve this is to make the property required. However, for response bodies we disable the required property checking. This is because we want consumers to only mock out properties in the response body they actually care about and doing required property checking would potentially force them to mock out more. We also do not want to allow providers to break consumer builds by adding a new required property.

    Hopefully that helps.

    Regards, Ben

  2. Jon O reporter

    Hi Ben,

    Thanks for the quick response, it helped a lot - Adding additionalProperties: false to the schema object in the swagger.json file did the trick perfectly.

    Thanks for all the additional details in your response too - really appreciate you taking the time to provide such a comprehensive response.

    Jon

  3. Log in to comment