I have the following rswag test:
response '200', 'response with array of objects' do
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
}
run_test!
end
My API responds with JSON:
[
"invalid_key": 1,
"invalid_key": 2
]
In other words, an invalid response according to the schema above.
However, the test does not fail. This is unexpected.
On the other hand, if my API responds with an array of null values: "[null]"
I get the response I expect:
Failure/Error: test.run
Rswag::Specs::UnexpectedResponse:
Expected response body to match schema: The property '#/0' of type null did not match the following type: object in schema 19e6c168-1da4-58a6-93fc-a33d9d91233a
So my test is checking that there is an array of objects, but not checking that the properties in the nested objects are what is expected. How do I make it check the properties as well?
I was missing a required
clause in my schema:
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
required: ['expected_id'] # <- this was missing
}
This raises a test error if the response my endpoint generates does not include the expected_id
key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With