Skip to content

Commit

Permalink
Allow references for responses fixes westfieldlabs#84
Browse files Browse the repository at this point in the history
  • Loading branch information
trekdemo committed Oct 22, 2019
1 parent 275e799 commit cb62017
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/apivore/swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ def each_response(&block)
raise "No responses found in swagger for path '#{path}', " \
"method #{verb}: #{method_data.inspect}"
end

method_data.responses.each do |response_code, response_data|
schema_location = nil
if response_data.schema
schema_location = Fragment.new ['#', 'paths', path, verb, 'responses', response_code, 'schema']
end
schema_location =
case
when response_data.schema
Fragment.new(['#', 'paths', path, verb, 'responses', response_code, 'schema'])
when (ref = response_data['$ref'])
Fragment.new(ref.split('/'))
end

block.call(path, verb, response_code, schema_location)
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/apivore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
["/services/{id}.json", "get", "200", ['#', 'paths', '/services/{id}.json', 'get', 'responses', '200', 'schema']],
["/services/{id}.json", "put", "204", nil],
["/services/{id}.json", "delete", "204", nil],
["/services/{id}.json", "patch", "204", nil]
["/services/{id}.json", "patch", "204", nil],
["/services/{id}/with_response_definition.json", "get", "200", ['#', 'responses', 'sample_response']]
)
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/data/01_sample2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@
}
}
}
},
"/services/{id}/with_response_definition.json": {
"get": {
"description": "Returns a service.",
"operationId": "Services#show",
"responses": {
"200": {
"$ref": "#/responses/sample_response"
}
}
}
}
},
"definitions": {
Expand All @@ -110,5 +121,13 @@
}
}
}
},
"responses": {
"sample_response": {
"description": "Sample response",
"schema": {
"$ref": "#/definitions/service"
}
}
}
}
2 changes: 2 additions & 0 deletions spec/fixtures/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def call(env)
respond_with 204
when "PATCH /api/services/1.json"
respond_with 204
when "GET /api/services/1/with_response_definition.json"
respond_with 200
else
if test_swagger_files.include?(path)
respond_with 200, File.read(File.expand_path("../../data#{path}", __FILE__))
Expand Down
6 changes: 6 additions & 0 deletions spec/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
:patch, "/services/{id}.json", 204, {'id' => 1}
)
end

it do
expect(subject).to validate(
:get, "/services/{id}/with_response_definition.json", 200, {'id' => 1}
)
end
end

context 'and' do
Expand Down

0 comments on commit cb62017

Please sign in to comment.