s89_
s89_

Reputation: 1733

How can I specify decimal schema formats in RSpec/RSwag when decimal responses are returned as strings?

So my :decimal formatted DB field is being returned as a string, and making my tests fail.

I have a request spec, using RSwag, that I'm trying to get to pass with a format check, yet it's telling me the following:-

Rswag::Specs::UnexpectedResponse:
       Expected response body to match schema: The property '#/budget' of type string did not match the following type: number in schema 

My test:-

 path '/api/v1/jobs/{id}' do
    parameter name: 'id', in: :path, type: :string, description: 'Job id'

    get('show job') do
      tags 'Jobs'
      response(200, 'successful') do
        schema type: :object,
               properties: {
                 title: { type: :string },
                 description: { type: :string },
                 date: { type: :string, format: 'date-time', nullable: true  },
                 budget: { type: :number, format: 'double' },
                 awarded: { type: :boolean },
                 created_at: { type: :datetime},
                 updated_at: { type: :datetime}
               }
        let(:id) { @job.id }

        after do |example|
          example.metadata[:response][:content] = {
            'application/json' => {
              example: JSON.parse(response.body, symbolize_names: true)
            }
          }
        end
        run_test!
      end
    end

What is the best way around this? It's a little pointless going to the effort of using Swagger for my API docs if I have to make all the fields strings.

Upvotes: 1

Views: 604

Answers (1)

Trang Tung Nguyen
Trang Tung Nguyen

Reputation: 346

I set the type to :string and used decimal as the format, which worked well in my case.

For your scenario, you can use:

budget: { type: :string, format: 'decimal' },

Upvotes: 0

Related Questions