From 127cf5a02f645b6504c9303d7357046debed4461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gergo=CC=8B=20Sulymosi?= Date: Tue, 12 Jul 2016 13:32:51 +0200 Subject: [PATCH] Allow references for responses fixes #84 --- lib/apivore/swagger.rb | 13 +++++++++---- spec/apivore_spec.rb | 3 ++- spec/data/01_sample2.0.json | 19 +++++++++++++++++++ spec/fixtures/application.rb | 2 ++ spec/request_spec.rb | 6 ++++++ 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/apivore/swagger.rb b/lib/apivore/swagger.rb index 78fed66..5dbcc9f 100644 --- a/lib/apivore/swagger.rb +++ b/lib/apivore/swagger.rb @@ -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 diff --git a/spec/apivore_spec.rb b/spec/apivore_spec.rb index 95cb649..1189975 100644 --- a/spec/apivore_spec.rb +++ b/spec/apivore_spec.rb @@ -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 diff --git a/spec/data/01_sample2.0.json b/spec/data/01_sample2.0.json index c64c5e9..2a2c611 100644 --- a/spec/data/01_sample2.0.json +++ b/spec/data/01_sample2.0.json @@ -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": { @@ -110,5 +121,13 @@ } } } + }, + "responses": { + "sample_response": { + "description": "Sample response", + "schema": { + "$ref": "#/definitions/service" + } + } } } diff --git a/spec/fixtures/application.rb b/spec/fixtures/application.rb index c206d86..245b895 100644 --- a/spec/fixtures/application.rb +++ b/spec/fixtures/application.rb @@ -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__)) diff --git a/spec/request_spec.rb b/spec/request_spec.rb index 4b58a55..44b710a 100644 --- a/spec/request_spec.rb +++ b/spec/request_spec.rb @@ -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