Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rswag schema does not fail for invalid properties of object in array

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?

like image 385
iftheshoefritz Avatar asked Jan 21 '19 12:01

iftheshoefritz


1 Answers

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.

like image 154
iftheshoefritz Avatar answered Oct 08 '22 16:10

iftheshoefritz