Skip to content

Commit 16b7cee

Browse files
elasti-roniNecas
authored andcommitted
response auto-validation of does not fail for methods that do not have apipie documentation
1 parent aadda72 commit 16b7cee

File tree

6 files changed

+26
-1
lines changed

6 files changed

+26
-1
lines changed

lib/apipie/application.rb

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def reload_examples
257257

258258
def json_schema_for_method_response(version, controller_name, method_name, return_code, allow_nulls)
259259
method = @resource_descriptions[version][controller_name].method_description(method_name)
260+
raise NoDocumentedMethod.new(controller_name, method_name) if method.nil?
260261
@swagger_generator.json_schema_for_method_response(method, return_code, allow_nulls)
261262
end
262263

lib/apipie/errors.rb

+11
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,15 @@ def to_s
7272
"Response does not match swagger schema (#{@controller_name}##{@method_name} #{@response_code}): #{@error_messages}\nSchema: #{JSON(@schema)}\nReturned object: #{@returned_object}"
7373
end
7474
end
75+
76+
class NoDocumentedMethod < Error
77+
def initialize(controller_name, method_name)
78+
@method_name = method_name
79+
@controller_name = controller_name
80+
end
81+
82+
def to_s
83+
"There is no documented method #{@controller_name}##{@method_name}"
84+
end
85+
end
7586
end

lib/apipie/rspec/response_validation_helper.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ def schema_validation_errors_for_response
103103
error_object = Apipie::ResponseDoesNotMatchSwaggerSchema.new(controller_name, action_name, response.code, error_list, schema, response.body)
104104

105105
[schema, error_list, error_object]
106+
rescue Apipie::NoDocumentedMethod
107+
[nil, [], nil]
106108
end
107-
108109
end
109110

110111
include Apipie::ControllerValidationHelpers

spec/dummy/app/controllers/pets_controller.rb

+6
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ def return_and_validate_type_mismatch
244244
render :json => result
245245
end
246246

247+
#-----------------------------------------------------------
248+
# A method with no documentation
249+
#-----------------------------------------------------------
250+
def undocumented_method
251+
render :json => {:result => "ok"}
252+
end
247253

248254
#-----------------------------------------------------------
249255
# A method which has a response with a missing field

spec/dummy/config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
get "/pets/returns_response_with_valid_array" => "pets#returns_response_with_valid_array"
4343
get "/pets/returns_response_with_invalid_array" => "pets#returns_response_with_invalid_array"
44+
get "/pets/undocumented_method" => "pets#undocumented_method"
4445
end
4546

4647
apipie

spec/lib/swagger/response_validation_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
it "raises exception when a response field has the wrong type and auto validation is turned on" do
8080
expect { get :return_and_validate_type_mismatch, {format: :json} }.to raise_error(Apipie::ResponseDoesNotMatchSwaggerSchema)
8181
end
82+
83+
it "does not raise an exception when calling an undocumented method" do
84+
expect { get :undocumented_method, {format: :json} }.not_to raise_error
85+
end
86+
8287
end
8388

8489

0 commit comments

Comments
 (0)