apipie-rails 0.3.6 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/build.yml +67 -0
- data/.github/workflows/rubocop-challenger.yml +28 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +37 -0
- data/.rubocop_todo.yml +1991 -0
- data/CHANGELOG.md +246 -2
- data/PROPOSAL_FOR_RESPONSE_DESCRIPTIONS.md +244 -0
- data/README.rst +646 -25
- data/Rakefile +0 -5
- data/apipie-rails.gemspec +14 -9
- data/app/controllers/apipie/apipies_controller.rb +51 -20
- data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +70 -41
- data/app/public/apipie/javascripts/bundled/bootstrap.js +1033 -479
- data/app/public/apipie/javascripts/bundled/jquery.js +5 -5
- data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +9 -12
- data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +9 -689
- data/app/views/apipie/apipies/_method_detail.erb +21 -0
- data/app/views/apipie/apipies/_params.html.erb +4 -2
- data/app/views/apipie/apipies/index.html.erb +5 -1
- data/app/views/apipie/apipies/resource.html.erb +3 -0
- data/app/views/layouts/apipie/apipie.html.erb +1 -1
- data/config/locales/en.yml +1 -0
- data/config/locales/fr.yml +31 -0
- data/config/locales/it.yml +31 -0
- data/config/locales/ja.yml +31 -0
- data/config/locales/ko.yml +31 -0
- data/config/locales/pt-BR.yml +1 -1
- data/gemfiles/Gemfile.rails50 +10 -0
- data/gemfiles/Gemfile.rails51 +10 -0
- data/gemfiles/Gemfile.rails52 +10 -0
- data/gemfiles/Gemfile.rails60 +17 -0
- data/gemfiles/Gemfile.rails61 +17 -0
- data/gemfiles/Gemfile.rails70 +17 -0
- data/lib/apipie/apipie_module.rb +22 -4
- data/lib/apipie/application.rb +54 -25
- data/lib/apipie/configuration.rb +26 -4
- data/lib/apipie/core_ext/route.rb +9 -0
- data/lib/apipie/dsl_definition.rb +168 -16
- data/lib/apipie/error_description.rb +9 -2
- data/lib/apipie/errors.rb +34 -0
- data/lib/apipie/extractor/collector.rb +4 -0
- data/lib/apipie/extractor/recorder.rb +14 -12
- data/lib/apipie/extractor/writer.rb +86 -58
- data/lib/apipie/extractor.rb +5 -5
- data/lib/apipie/generator/generator.rb +2 -0
- data/lib/apipie/generator/swagger/swagger.rb +2 -0
- data/lib/apipie/generator/swagger/type.rb +16 -0
- data/lib/apipie/generator/swagger/type_extractor.rb +70 -0
- data/lib/apipie/generator/swagger/warning.rb +77 -0
- data/lib/apipie/generator/swagger/warning_writer.rb +48 -0
- data/lib/apipie/markup.rb +14 -11
- data/lib/apipie/method_description/api.rb +12 -0
- data/lib/apipie/method_description/apis_service.rb +82 -0
- data/lib/apipie/method_description.rb +51 -49
- data/lib/apipie/param_description.rb +63 -5
- data/lib/apipie/resource_description.rb +11 -4
- data/lib/apipie/response_description.rb +131 -0
- data/lib/apipie/response_description_adapter.rb +200 -0
- data/lib/apipie/routes_formatter.rb +1 -1
- data/lib/apipie/rspec/response_validation_helper.rb +194 -0
- data/lib/apipie/static_dispatcher.rb +5 -2
- data/lib/apipie/swagger_generator.rb +717 -0
- data/lib/apipie/tag_list_description.rb +11 -0
- data/lib/apipie/validator.rb +83 -9
- data/lib/apipie/version.rb +1 -1
- data/lib/apipie-rails.rb +15 -4
- data/lib/generators/apipie/install/install_generator.rb +1 -1
- data/lib/generators/apipie/views_generator.rb +1 -1
- data/lib/tasks/apipie.rake +115 -15
- data/rel-eng/gem_release.ipynb +398 -0
- data/spec/controllers/apipies_controller_spec.rb +79 -14
- data/spec/controllers/concerns_controller_spec.rb +2 -2
- data/spec/controllers/extended_controller_spec.rb +14 -0
- data/spec/controllers/included_param_group_controller_spec.rb +13 -0
- data/spec/controllers/memes_controller_spec.rb +10 -0
- data/spec/controllers/users_controller_spec.rb +139 -76
- data/spec/dummy/Rakefile +1 -1
- data/spec/dummy/app/controllers/application_controller.rb +5 -1
- data/spec/dummy/app/controllers/concerns_controller.rb +1 -1
- data/spec/dummy/app/controllers/extended_controller.rb +14 -0
- data/spec/dummy/app/controllers/extending_concern.rb +10 -0
- data/spec/dummy/app/controllers/included_param_group_controller.rb +19 -0
- data/spec/dummy/app/controllers/overridden_concerns_controller.rb +2 -2
- data/spec/dummy/app/controllers/pets_controller.rb +408 -0
- data/spec/dummy/app/controllers/pets_using_auto_views_controller.rb +73 -0
- data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +95 -0
- data/spec/dummy/app/controllers/{concerns/sample_controller.rb → sample_controller.rb} +5 -7
- data/spec/dummy/app/controllers/tagged_cats_controller.rb +32 -0
- data/spec/dummy/app/controllers/tagged_dogs_controller.rb +15 -0
- data/spec/dummy/app/controllers/twitter_example_controller.rb +5 -0
- data/spec/dummy/app/controllers/users_controller.rb +26 -12
- data/spec/dummy/app/helpers/random_param_group.rb +8 -0
- data/spec/dummy/components/test_engine/Gemfile +6 -0
- data/spec/dummy/components/test_engine/app/controllers/test_engine/application_controller.rb +4 -0
- data/spec/dummy/components/test_engine/app/controllers/test_engine/memes_controller.rb +37 -0
- data/spec/dummy/components/test_engine/config/routes.rb +3 -0
- data/spec/dummy/components/test_engine/db/.gitkeep +0 -0
- data/spec/dummy/components/test_engine/lib/test_engine.rb +7 -0
- data/spec/dummy/components/test_engine/test_engine.gemspec +11 -0
- data/spec/dummy/config/application.rb +6 -4
- data/spec/dummy/config/boot.rb +2 -2
- data/spec/dummy/config/environment.rb +1 -1
- data/spec/dummy/config/environments/development.rb +3 -3
- data/spec/dummy/config/environments/production.rb +3 -3
- data/spec/dummy/config/environments/test.rb +3 -5
- data/spec/dummy/config/initializers/apipie.rb +5 -3
- data/spec/dummy/config/routes.rb +25 -1
- data/spec/dummy/config.ru +1 -1
- data/spec/dummy/script/rails +2 -2
- data/spec/lib/application_spec.rb +1 -1
- data/spec/lib/extractor/writer_spec.rb +37 -7
- data/spec/lib/file_handler_spec.rb +25 -0
- data/spec/lib/generator/swagger/type_extractor_spec.rb +61 -0
- data/spec/lib/generator/swagger/warning_spec.rb +51 -0
- data/spec/lib/generator/swagger/warning_writer_spec.rb +59 -0
- data/spec/lib/method_description/apis_service_spec.rb +60 -0
- data/spec/lib/method_description_spec.rb +34 -0
- data/spec/lib/param_description_spec.rb +90 -4
- data/spec/lib/rake_spec.rb +2 -4
- data/spec/lib/swagger/openapi_2_0_schema.json +1607 -0
- data/spec/lib/swagger/rake_swagger_spec.rb +154 -0
- data/spec/lib/swagger/response_validation_spec.rb +104 -0
- data/spec/lib/swagger/swagger_dsl_spec.rb +658 -0
- data/spec/lib/validator_spec.rb +59 -1
- data/spec/lib/validators/array_validator_spec.rb +28 -8
- data/spec/spec_helper.rb +49 -3
- data/spec/support/custom_bool_validator.rb +17 -0
- metadata +104 -99
- data/.travis.yml +0 -12
- data/Gemfile +0 -7
- data/Gemfile.rails32 +0 -6
- data/Gemfile.rails40 +0 -5
- data/Gemfile.rails41 +0 -5
- data/Gemfile.rails42 +0 -5
- data/lib/apipie/client/generator.rb +0 -135
@@ -7,20 +7,22 @@ module Apipie
|
|
7
7
|
# path - relative path (/api/articles)
|
8
8
|
# methods - array of keys to Apipie.method_descriptions (array of Apipie::MethodDescription)
|
9
9
|
# name - human readable alias of resource (Articles)
|
10
|
-
# id -
|
10
|
+
# id - resource name
|
11
11
|
# formats - acceptable request/response format types
|
12
12
|
# headers - array of headers
|
13
|
+
# deprecated - boolean indicating if resource is deprecated
|
13
14
|
class ResourceDescription
|
14
15
|
|
15
16
|
attr_reader :controller, :_short_description, :_full_description, :_methods, :_id,
|
16
|
-
:_path, :_name, :_params_args, :
|
17
|
-
:_headers
|
17
|
+
:_path, :_name, :_params_args, :_returns_args, :_tag_list_arg, :_errors_args,
|
18
|
+
:_formats, :_parent, :_metadata, :_headers, :_deprecated
|
18
19
|
|
19
20
|
def initialize(controller, resource_name, dsl_data = nil, version = nil, &block)
|
20
21
|
|
21
22
|
@_methods = ActiveSupport::OrderedHash.new
|
22
23
|
@_params_args = []
|
23
24
|
@_errors_args = []
|
25
|
+
@_returns_args = []
|
24
26
|
|
25
27
|
@controller = controller
|
26
28
|
@_id = resource_name
|
@@ -39,9 +41,12 @@ module Apipie
|
|
39
41
|
@_formats = dsl_data[:formats]
|
40
42
|
@_errors_args = dsl_data[:errors]
|
41
43
|
@_params_args = dsl_data[:params]
|
44
|
+
@_returns_args = dsl_data[:returns]
|
45
|
+
@_tag_list_arg = dsl_data[:tag_list]
|
42
46
|
@_metadata = dsl_data[:meta]
|
43
47
|
@_api_base_url = dsl_data[:api_base_url]
|
44
48
|
@_headers = dsl_data[:headers]
|
49
|
+
@_deprecated = dsl_data[:deprecated] || false
|
45
50
|
|
46
51
|
if dsl_data[:app_info]
|
47
52
|
Apipie.configuration.app_info[_version] = dsl_data[:app_info]
|
@@ -101,6 +106,7 @@ module Apipie
|
|
101
106
|
|
102
107
|
{
|
103
108
|
:doc_url => doc_url,
|
109
|
+
:id => _id,
|
104
110
|
:api_url => api_url,
|
105
111
|
:name => @_name,
|
106
112
|
:short_description => Apipie.app.translate(@_short_description, lang),
|
@@ -109,7 +115,8 @@ module Apipie
|
|
109
115
|
:formats => @_formats,
|
110
116
|
:metadata => @_metadata,
|
111
117
|
:methods => methods,
|
112
|
-
:headers => _headers
|
118
|
+
:headers => _headers,
|
119
|
+
:deprecated => @_deprecated
|
113
120
|
}
|
114
121
|
end
|
115
122
|
|
@@ -0,0 +1,131 @@
|
|
1
|
+
module Apipie
|
2
|
+
|
3
|
+
class ResponseDescription
|
4
|
+
class ResponseObject
|
5
|
+
include Apipie::DSL::Base
|
6
|
+
include Apipie::DSL::Param
|
7
|
+
|
8
|
+
attr_accessor :additional_properties, :typename
|
9
|
+
|
10
|
+
def initialize(method_description, scope, block, typename)
|
11
|
+
@method_description = method_description
|
12
|
+
@scope = scope
|
13
|
+
@param_group = {scope: scope}
|
14
|
+
@additional_properties = false
|
15
|
+
@typename = typename
|
16
|
+
|
17
|
+
self.instance_exec(&block) if block
|
18
|
+
|
19
|
+
prepare_hash_params
|
20
|
+
end
|
21
|
+
|
22
|
+
# this routine overrides Param#_default_param_group_scope and is called if Param#param_group is
|
23
|
+
# invoked during the instance_exec call in ResponseObject#initialize
|
24
|
+
def _default_param_group_scope
|
25
|
+
@scope
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
"response #{@code} for #{@method_description.method}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def params_ordered
|
33
|
+
@params_ordered ||= _apipie_dsl_data[:params].map do |args|
|
34
|
+
options = args.find { |arg| arg.is_a? Hash }
|
35
|
+
options[:param_group] = @param_group
|
36
|
+
Apipie::ParamDescription.from_dsl_data(@method_description, args) unless options[:only_in] == :request
|
37
|
+
end.compact
|
38
|
+
end
|
39
|
+
|
40
|
+
def prepare_hash_params
|
41
|
+
@hash_params = params_ordered.reduce({}) do |h, param|
|
42
|
+
h.update(param.name.to_sym => param)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
class ResponseDescription
|
51
|
+
include Apipie::DSL::Base
|
52
|
+
include Apipie::DSL::Param
|
53
|
+
|
54
|
+
attr_reader :code, :description, :scope, :type_ref, :hash_validator, :is_array_of
|
55
|
+
|
56
|
+
def self.from_dsl_data(method_description, code, args)
|
57
|
+
options, scope, block, adapter = args
|
58
|
+
|
59
|
+
Apipie::ResponseDescription.new(method_description,
|
60
|
+
code,
|
61
|
+
options,
|
62
|
+
scope,
|
63
|
+
block,
|
64
|
+
adapter)
|
65
|
+
end
|
66
|
+
|
67
|
+
def is_array?
|
68
|
+
@is_array_of != false
|
69
|
+
end
|
70
|
+
|
71
|
+
def typename
|
72
|
+
@response_object.typename
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
def initialize(method_description, code, options, scope, block, adapter)
|
77
|
+
|
78
|
+
@type_ref = options[:param_group]
|
79
|
+
@is_array_of = options[:array_of] || false
|
80
|
+
raise ReturnsMultipleDefinitionError, options if @is_array_of && @type_ref
|
81
|
+
|
82
|
+
@type_ref ||= @is_array_of
|
83
|
+
|
84
|
+
@method_description = method_description
|
85
|
+
|
86
|
+
if code.is_a? Symbol
|
87
|
+
@code = Rack::Utils::SYMBOL_TO_STATUS_CODE[code]
|
88
|
+
else
|
89
|
+
@code = code
|
90
|
+
end
|
91
|
+
|
92
|
+
@description = options[:desc]
|
93
|
+
if @description.nil?
|
94
|
+
@description = Rack::Utils::HTTP_STATUS_CODES[@code]
|
95
|
+
raise "Cannot infer description from status code #{@code}" if @description.nil?
|
96
|
+
end
|
97
|
+
@scope = scope
|
98
|
+
|
99
|
+
if adapter
|
100
|
+
@response_object = adapter
|
101
|
+
else
|
102
|
+
@response_object = ResponseObject.new(method_description, scope, block, @type_ref)
|
103
|
+
end
|
104
|
+
|
105
|
+
@response_object.additional_properties ||= options[:additional_properties]
|
106
|
+
end
|
107
|
+
|
108
|
+
def param_description
|
109
|
+
nil
|
110
|
+
end
|
111
|
+
|
112
|
+
def params_ordered
|
113
|
+
@response_object.params_ordered
|
114
|
+
end
|
115
|
+
|
116
|
+
def additional_properties
|
117
|
+
!!@response_object.additional_properties
|
118
|
+
end
|
119
|
+
alias :allow_additional_properties :additional_properties
|
120
|
+
|
121
|
+
def to_json(lang=nil)
|
122
|
+
{
|
123
|
+
:code => code,
|
124
|
+
:description => description,
|
125
|
+
:is_array => is_array?,
|
126
|
+
:returns_object => params_ordered.map{ |param| param.to_json(lang).tap{|h| h.delete(:validations) }}.flatten,
|
127
|
+
:additional_properties => additional_properties,
|
128
|
+
}
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
module Apipie
|
2
|
+
|
3
|
+
def self.prop(name, expected_type, options={}, sub_properties=[])
|
4
|
+
Apipie::ResponseDescriptionAdapter::PropDesc.new(name, expected_type, options, sub_properties)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.additional_properties(yesno)
|
8
|
+
Apipie::ResponseDescriptionAdapter::AdditionalPropertiesModifier.new(yesno)
|
9
|
+
end
|
10
|
+
|
11
|
+
class ResponseDescriptionAdapter
|
12
|
+
class Modifier
|
13
|
+
def apply(adapter)
|
14
|
+
raise "Modifer subclass must implement 'apply' method"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class AdditionalPropertiesModifier < Modifier
|
19
|
+
def initialize(additional_properties_allowed)
|
20
|
+
@additional_properties_allowed = additional_properties_allowed
|
21
|
+
end
|
22
|
+
|
23
|
+
def apply(adapter)
|
24
|
+
adapter.additional_properties = @additional_properties_allowed
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
class ResponseDescriptionAdapter
|
31
|
+
|
32
|
+
#
|
33
|
+
# A ResponseDescriptionAdapter::PropDesc object pretends to be an Apipie::Param in a ResponseDescription
|
34
|
+
#
|
35
|
+
# To successfully masquerade as such, it needs to:
|
36
|
+
# respond_to?('name') and/or ['name'] returning the name of the parameter
|
37
|
+
# respond_to?('required') and/or ['required'] returning boolean
|
38
|
+
# respond_to?('additional_properties') and/or ['additional_properties'] returning boolean
|
39
|
+
# respond_to?('validator') and/or ['validator'] returning 'nil' (so type is 'string'), or an object that:
|
40
|
+
# 1) describes a type. currently type is inferred as follows:
|
41
|
+
# if validator.is_a? Apipie::Validator::EnumValidator --> respond_to? 'values' (returns array). Type is enum or boolean
|
42
|
+
# else: use v.expected_type(). This is expected to be the swagger type, or:
|
43
|
+
# numeric ==> swagger type is 'number'
|
44
|
+
# hash ==> swagger type is 'object' and validator should respond_to? 'params_ordered'
|
45
|
+
# array ==> swagger type is array and validator (FUTURE) should indicate type of element
|
46
|
+
|
47
|
+
class PropDesc
|
48
|
+
|
49
|
+
def to_s
|
50
|
+
"PropDesc -- name: #{@name} type: #{@expected_type} required: #{@required} options: #{@options} subprop count: #{@sub_properties.length} additional properties: #{@additional_properties}"
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# a ResponseDescriptionAdapter::PropDesc::Validator pretends to be an Apipie::Validator
|
55
|
+
#
|
56
|
+
class Validator
|
57
|
+
attr_reader :expected_type
|
58
|
+
|
59
|
+
def [](key)
|
60
|
+
return self.send(key) if self.respond_to?(key.to_s)
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize(expected_type, enum_values=nil, sub_properties=nil)
|
64
|
+
@expected_type = expected_type
|
65
|
+
@enum_values = enum_values
|
66
|
+
@is_enum = !!enum_values
|
67
|
+
@sub_properties = sub_properties
|
68
|
+
end
|
69
|
+
|
70
|
+
def is_enum?
|
71
|
+
!!@is_enum
|
72
|
+
end
|
73
|
+
|
74
|
+
def values
|
75
|
+
@enum_values
|
76
|
+
end
|
77
|
+
|
78
|
+
def params_ordered
|
79
|
+
raise "Only validators with expected_type 'object' can have sub-properties" unless @expected_type == 'object'
|
80
|
+
@sub_properties
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
#======================================================================
|
85
|
+
|
86
|
+
|
87
|
+
def initialize(name, expected_type, options={}, sub_properties=[])
|
88
|
+
@name = name
|
89
|
+
@required = true
|
90
|
+
@required = false if options[:required] == false
|
91
|
+
@expected_type = expected_type
|
92
|
+
@additional_properties = false
|
93
|
+
|
94
|
+
options[:desc] ||= options[:description]
|
95
|
+
@description = options[:desc]
|
96
|
+
@options = options
|
97
|
+
@is_array = options[:is_array] || false
|
98
|
+
@sub_properties = []
|
99
|
+
for prop in sub_properties do
|
100
|
+
add_sub_property(prop)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def [](key)
|
105
|
+
return self.send(key) if self.respond_to?(key.to_s)
|
106
|
+
end
|
107
|
+
|
108
|
+
def add_sub_property(prop_desc)
|
109
|
+
raise "Only properties with expected_type 'object' can have sub-properties" unless @expected_type == 'object'
|
110
|
+
if prop_desc.is_a? PropDesc
|
111
|
+
@sub_properties << prop_desc
|
112
|
+
elsif prop_desc.is_a? Modifier
|
113
|
+
prop_desc.apply(self)
|
114
|
+
else
|
115
|
+
raise "Unrecognized prop_desc type (#{prop_desc.class})"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def to_json(lang)
|
120
|
+
{
|
121
|
+
name: name,
|
122
|
+
required: required,
|
123
|
+
validator: validator,
|
124
|
+
description: description,
|
125
|
+
additional_properties: additional_properties,
|
126
|
+
is_array: is_array?,
|
127
|
+
options: options
|
128
|
+
}
|
129
|
+
end
|
130
|
+
attr_reader :name, :required, :expected_type, :options, :description
|
131
|
+
attr_accessor :additional_properties
|
132
|
+
|
133
|
+
alias_method :desc, :description
|
134
|
+
|
135
|
+
def is_array?
|
136
|
+
@is_array
|
137
|
+
end
|
138
|
+
|
139
|
+
def validator
|
140
|
+
Validator.new(@expected_type, options[:values], @sub_properties)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
#======================================================================
|
146
|
+
|
147
|
+
class ResponseDescriptionAdapter
|
148
|
+
|
149
|
+
def self.from_self_describing_class(cls)
|
150
|
+
adapter = ResponseDescriptionAdapter.new(cls.to_s)
|
151
|
+
props = cls.describe_own_properties
|
152
|
+
adapter.add_property_descriptions(props)
|
153
|
+
adapter
|
154
|
+
end
|
155
|
+
|
156
|
+
def initialize(typename)
|
157
|
+
@property_descs = []
|
158
|
+
@additional_properties = false
|
159
|
+
@typename = typename
|
160
|
+
end
|
161
|
+
|
162
|
+
attr_accessor :additional_properties, :typename
|
163
|
+
|
164
|
+
def allow_additional_properties
|
165
|
+
additional_properties
|
166
|
+
end
|
167
|
+
|
168
|
+
def to_json
|
169
|
+
params_ordered.to_json
|
170
|
+
end
|
171
|
+
|
172
|
+
def add(prop_desc)
|
173
|
+
if prop_desc.is_a? PropDesc
|
174
|
+
@property_descs << prop_desc
|
175
|
+
elsif prop_desc.is_a? Modifier
|
176
|
+
prop_desc.apply(self)
|
177
|
+
else
|
178
|
+
raise "Unrecognized prop_desc type (#{prop_desc.class})"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def add_property_descriptions(prop_descs)
|
183
|
+
for prop_desc in prop_descs
|
184
|
+
add(prop_desc)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def property(name, expected_type, options)
|
189
|
+
@property_descs << PropDesc.new(name, expected_type, options)
|
190
|
+
end
|
191
|
+
|
192
|
+
def params_ordered
|
193
|
+
@property_descs
|
194
|
+
end
|
195
|
+
|
196
|
+
def is_array?
|
197
|
+
false
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
@@ -0,0 +1,194 @@
|
|
1
|
+
#----------------------------------------------------------------------------------------------
|
2
|
+
# response_validation_helper.rb:
|
3
|
+
#
|
4
|
+
# this is an rspec utility to allow validation of responses against the swagger schema generated
|
5
|
+
# from the Apipie 'returns' definition for the call.
|
6
|
+
#
|
7
|
+
#
|
8
|
+
# to use this file in a controller rspec you should
|
9
|
+
# require 'apipie/rspec/response_validation_helper' in the spec file
|
10
|
+
#
|
11
|
+
#
|
12
|
+
# this utility provides two mechanisms: matcher-based validation and auto-validation
|
13
|
+
#
|
14
|
+
# matcher-based: an rspec matcher allowing 'expect(response).to match_declared_responses'
|
15
|
+
# auto-validation: all responses returned from 'get', 'post', etc. are automatically tested
|
16
|
+
#
|
17
|
+
# ===================================
|
18
|
+
# Matcher-based validation - example
|
19
|
+
# ===================================
|
20
|
+
# Assume the file 'my_controller_spec.rb':
|
21
|
+
#
|
22
|
+
# require 'apipie/rspec/response_validation_helper'
|
23
|
+
#
|
24
|
+
# RSpec.describe MyController, :type => :controller, :show_in_doc => true do
|
25
|
+
#
|
26
|
+
# describe "GET stuff with response validation" do
|
27
|
+
# render_views # this makes sure the 'get' operation will actually
|
28
|
+
# # return the rendered view even though this is a Controller spec
|
29
|
+
#
|
30
|
+
# it "does something" do
|
31
|
+
# response = get :index, {format: :json}
|
32
|
+
#
|
33
|
+
# # the following expectation will fail if the returned object
|
34
|
+
# # does not match the 'returns' declaration in the Controller,
|
35
|
+
# # or if there is no 'returns' declaration for the returned
|
36
|
+
# # HTTP status code
|
37
|
+
# expect(response).to match_declared_responses
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
#
|
42
|
+
# ===================================
|
43
|
+
# Auto-validation
|
44
|
+
# ===================================
|
45
|
+
# To use auto-validation, at the beginning of the block in which you want to turn on validation:
|
46
|
+
# -) turn on view rendering (by stating 'render_views')
|
47
|
+
# -) turn on response validation by stating 'auto_validate_rendered_views'
|
48
|
+
#
|
49
|
+
# For example, assume the file 'my_controller_spec.rb':
|
50
|
+
#
|
51
|
+
# require 'apipie/rspec/response_validation_helper'
|
52
|
+
#
|
53
|
+
# RSpec.describe MyController, :type => :controller, :show_in_doc => true do
|
54
|
+
#
|
55
|
+
# describe "GET stuff with response validation" do
|
56
|
+
# render_views
|
57
|
+
# auto_validate_rendered_views
|
58
|
+
#
|
59
|
+
# it "does something" do
|
60
|
+
# get :index, {format: :json}
|
61
|
+
# end
|
62
|
+
# it "does something else" do
|
63
|
+
# get :another_index, {format: :json}
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# describe "GET stuff without response validation" do
|
68
|
+
# it "does something" do
|
69
|
+
# get :index, {format: :json}
|
70
|
+
# end
|
71
|
+
# it "does something else" do
|
72
|
+
# get :another_index, {format: :json}
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
#
|
77
|
+
# Once this is done, responses from http operations ('get', 'post', 'delete', etc.)
|
78
|
+
# will fail the test if the response structure does not match the 'returns' declaration
|
79
|
+
# on the method (for the actual HTTP status code), or if there is no 'returns' declaration
|
80
|
+
# for the HTTP status code.
|
81
|
+
#----------------------------------------------------------------------------------------------
|
82
|
+
|
83
|
+
|
84
|
+
#----------------------------------------------------------------------------------------------
|
85
|
+
# Response validation: core logic (used by auto-validation and manual-validation mechanisms)
|
86
|
+
#----------------------------------------------------------------------------------------------
|
87
|
+
class ActionController::Base
|
88
|
+
module Apipie::ControllerValidationHelpers
|
89
|
+
# this method is injected into ActionController::Base in order to
|
90
|
+
# get access to the names of the current controller, current action, as well as to the response
|
91
|
+
def schema_validation_errors_for_response
|
92
|
+
unprocessed_schema = Apipie::json_schema_for_method_response(controller_name, action_name, response.code, true)
|
93
|
+
|
94
|
+
if unprocessed_schema.nil?
|
95
|
+
err = "no schema defined for #{controller_name}##{action_name}[#{response.code}]"
|
96
|
+
return [nil, [err], RuntimeError.new(err)]
|
97
|
+
end
|
98
|
+
|
99
|
+
schema = JSON.parse(JSON(unprocessed_schema))
|
100
|
+
|
101
|
+
error_list = JSON::Validator.fully_validate(schema, response.body, :strict => false, :version => :draft4, :json => true)
|
102
|
+
|
103
|
+
error_object = Apipie::ResponseDoesNotMatchSwaggerSchema.new(controller_name, action_name, response.code, error_list, schema, response.body)
|
104
|
+
|
105
|
+
[schema, error_list, error_object]
|
106
|
+
rescue Apipie::NoDocumentedMethod
|
107
|
+
[nil, [], nil]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
include Apipie::ControllerValidationHelpers
|
112
|
+
end
|
113
|
+
|
114
|
+
module Apipie
|
115
|
+
def self.print_validation_errors(validation_errors, schema, response, error_object=nil)
|
116
|
+
Rails.logger.warn(validation_errors.to_s)
|
117
|
+
if Rails.env.test?
|
118
|
+
puts "schema validation errors:"
|
119
|
+
validation_errors.each { |e| puts "--> #{e.to_s}" }
|
120
|
+
puts "schema: #{schema.nil? ? '<none>' : JSON(schema)}"
|
121
|
+
puts "response: #{response.body}"
|
122
|
+
raise error_object if error_object
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
#---------------------------------
|
128
|
+
# Manual-validation (RSpec matcher)
|
129
|
+
#---------------------------------
|
130
|
+
RSpec::Matchers.define :match_declared_responses do
|
131
|
+
match do |actual|
|
132
|
+
(schema, validation_errors) = subject.send(:schema_validation_errors_for_response)
|
133
|
+
valid = (validation_errors == [])
|
134
|
+
Apipie::print_validation_errors(validation_errors, schema, response) unless valid
|
135
|
+
|
136
|
+
valid
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
#---------------------------------
|
142
|
+
# Auto-validation logic
|
143
|
+
#---------------------------------
|
144
|
+
module RSpec::Rails::ViewRendering
|
145
|
+
# Augment the RSpec DSL
|
146
|
+
module ClassMethods
|
147
|
+
def auto_validate_rendered_views
|
148
|
+
before do
|
149
|
+
@is_response_validation_on = true
|
150
|
+
end
|
151
|
+
|
152
|
+
after do
|
153
|
+
@is_response_validation_on = false
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
ActionController::TestCase::Behavior.instance_eval do
|
161
|
+
# instrument the 'process' method in ActionController::TestCase to enable response validation
|
162
|
+
module Apipie::ResponseValidationHelpers
|
163
|
+
@is_response_validation_on = false
|
164
|
+
def process(*, **)
|
165
|
+
result = super
|
166
|
+
validate_response if @is_response_validation_on
|
167
|
+
|
168
|
+
result
|
169
|
+
end
|
170
|
+
|
171
|
+
def validate_response
|
172
|
+
controller.send(:validate_response_and_abort_with_info_if_errors)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
prepend Apipie::ResponseValidationHelpers
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
class ActionController::Base
|
181
|
+
module Apipie::ControllerValidationHelpers
|
182
|
+
def validate_response_and_abort_with_info_if_errors
|
183
|
+
|
184
|
+
(schema, validation_errors, error_object) = schema_validation_errors_for_response
|
185
|
+
|
186
|
+
valid = (validation_errors == [])
|
187
|
+
if !valid
|
188
|
+
Apipie::print_validation_errors(validation_errors, schema, response, error_object)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
|
@@ -8,9 +8,12 @@ module Apipie
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def match?(path)
|
11
|
-
|
11
|
+
# Replace all null bytes
|
12
|
+
path = ::Rack::Utils.unescape(path || '')
|
13
|
+
.encode(Encoding::UTF_8, invalid: :replace, replace: '')
|
14
|
+
.gsub(/\x0/, '')
|
12
15
|
|
13
|
-
full_path = path.empty? ? @root : File.join(@root,
|
16
|
+
full_path = path.empty? ? @root : File.join(@root, path)
|
14
17
|
paths = "#{full_path}#{ext}"
|
15
18
|
|
16
19
|
matches = Dir[paths]
|