Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/openapi_contracts/validators/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def validate
if value.blank?
@errors << "Missing header #{header.name}" if header.required?
else
# Header values arrive as strings; deserialize to the schema's type
# (per `style: simple`) before validating, as Doc::Parameter does.
value = OpenapiParameters::Converter.convert(value, header.schema)
schemer = JSONSchemer.schema(header.schema)
unless schemer.valid?(value)
validation_errors = schemer.validate(value).to_a
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ paths:
responses:
'200':
description: Ok
headers:
x-rate-limit:
schema:
type: integer
content:
application/json:
schema:
Expand Down
22 changes: 22 additions & 0 deletions spec/openapi_contracts/validators/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@
end
end
end

context 'with a non-string header type (style: simple)' do
include_context 'when using GET /pets'

context 'when the value parses to the declared type' do
before { response_headers['x-rate-limit'] = '300' }

it 'deserializes the value and has no errors' do
expect(subject.call).to be_empty
end
end

context 'when the value does not parse to the declared type' do
before { response_headers['x-rate-limit'] = 'not-a-number' }

it 'returns the error' do
expect(subject.call).to eq [
'Header x-rate-limit validation error: value at root is not an integer (value: not-a-number)'
]
end
end
end
end
Loading