apipie-rails 0.7.0 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/build.yml +3 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +43 -0
- data/README.rst +68 -0
- data/apipie-rails.gemspec +1 -0
- data/app/controllers/apipie/apipies_controller.rb +1 -1
- 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/config/locales/ko.yml +31 -0
- data/gemfiles/Gemfile.rails70 +17 -0
- data/lib/apipie/application.rb +6 -4
- data/lib/apipie/configuration.rb +9 -3
- data/lib/apipie/dsl_definition.rb +13 -2
- data/lib/apipie/extractor/recorder.rb +1 -1
- data/lib/apipie/extractor.rb +2 -8
- data/lib/apipie/param_description.rb +8 -4
- data/lib/apipie/rspec/response_validation_helper.rb +2 -2
- data/lib/apipie/swagger_generator.rb +12 -2
- data/lib/apipie/validator.rb +9 -0
- data/lib/apipie/version.rb +1 -1
- data/spec/controllers/included_param_group_controller_spec.rb +13 -0
- data/spec/controllers/users_controller_spec.rb +23 -0
- data/spec/dummy/app/controllers/concerns_controller.rb +1 -1
- data/spec/dummy/app/controllers/{concerns/extending_concern.rb → extending_concern.rb} +0 -2
- 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/{concerns/sample_controller.rb → sample_controller.rb} +0 -2
- data/spec/dummy/app/helpers/random_param_group.rb +8 -0
- data/spec/dummy/config/application.rb +1 -1
- data/spec/lib/param_description_spec.rb +86 -0
- data/spec/spec_helper.rb +4 -32
- data/spec/support/custom_bool_validator.rb +17 -0
- metadata +25 -7
- data/Gemfile +0 -1
- data/spec/support/rails-42-ruby-26.rb +0 -15
@@ -0,0 +1,31 @@
|
|
1
|
+
ko:
|
2
|
+
apipie:
|
3
|
+
resources: 리소스
|
4
|
+
resource: 리소스
|
5
|
+
description: 설명
|
6
|
+
no_docs_found: 문서를 찾을 수 없습니다.
|
7
|
+
no_docs_found_descr: 해당 API에 대한 문서를 찾을 수 없습니다.
|
8
|
+
follow_instructions_html: 해당 컨트롤러의 설명을 %{href}를 따르세요.
|
9
|
+
follow_instructions_href: 자세한 설명
|
10
|
+
oops: 이런!!
|
11
|
+
resource_not_found_html: "%{resource} 리소스를 찾을 수 없습니다."
|
12
|
+
method_not_found_html: "%{resource} 리소스에 대한 %{method} 메소드를 찾을 수 없습니다."
|
13
|
+
goto_homepage_html: "%{href}로 시도해보세요."
|
14
|
+
goto_homepage_href: "%{app_name} API 문서 페이지"
|
15
|
+
required: 필수
|
16
|
+
optional: 옵션
|
17
|
+
nil_allowed: nil 허용
|
18
|
+
param_name: Param 이름
|
19
|
+
params: Params
|
20
|
+
examples: 예시
|
21
|
+
metadata: Metadata
|
22
|
+
errors: 에러
|
23
|
+
error_code: 코드
|
24
|
+
error_description: 설명
|
25
|
+
error_metadata: Metadata
|
26
|
+
supported_formats: 지원 포멧
|
27
|
+
enable_javascript_html: "%{comments_href}를 보기 위해서 JavaScript를 허용해주세요."
|
28
|
+
comments_powered_by_disqus: comments powered by %{disqus}
|
29
|
+
api_documentation: API 문서
|
30
|
+
headers: 헤더
|
31
|
+
header_name: 헤더 이름
|
@@ -0,0 +1,17 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
gemspec path: '..'
|
4
|
+
|
5
|
+
gem 'actionpack', '~> 7.0.2'
|
6
|
+
gem 'activesupport', '~> 7.0.2'
|
7
|
+
gem 'mime-types', '~> 3.0'
|
8
|
+
gem 'rails-controller-testing'
|
9
|
+
gem 'rspec-rails', '~> 5.0'
|
10
|
+
|
11
|
+
# net-smtp not included by default in Ruby 3.1
|
12
|
+
# Will be fixed by https://github.com/mikel/mail/pull/1439
|
13
|
+
if Gem.ruby_version >= Gem::Version.new("3.1.0")
|
14
|
+
gem 'net-smtp', require: false
|
15
|
+
end
|
16
|
+
|
17
|
+
gem 'test_engine', path: '../spec/dummy/components/test_engine', group: :test
|
data/lib/apipie/application.rb
CHANGED
@@ -40,8 +40,10 @@ module Apipie
|
|
40
40
|
flatten_routes = []
|
41
41
|
|
42
42
|
route_set.routes.each do |route|
|
43
|
-
#
|
44
|
-
|
43
|
+
# route is_a ActionDispatch::Journey::Route
|
44
|
+
# route.app is_a ActionDispatch::Routing::Mapper::Constraints
|
45
|
+
# route.app.app is_a TestEngine::Engine
|
46
|
+
route_app = route.app.app
|
45
47
|
if route_app.respond_to?(:routes) && route_app.routes.is_a?(ActionDispatch::Routing::RouteSet)
|
46
48
|
# recursively go though the mounted engines
|
47
49
|
flatten_routes.concat(rails_routes(route_app.routes, File.join(base_url, route.path.spec.to_s)))
|
@@ -456,10 +458,10 @@ module Apipie
|
|
456
458
|
# as this would break loading of the controllers.
|
457
459
|
def rails_mark_classes_for_reload
|
458
460
|
unless Rails.application.config.cache_classes
|
459
|
-
Rails
|
461
|
+
Rails.application.reloader.reload!
|
460
462
|
init_env
|
461
463
|
reload_examples
|
462
|
-
Rails
|
464
|
+
Rails.application.reloader.prepare!
|
463
465
|
end
|
464
466
|
end
|
465
467
|
|
data/lib/apipie/configuration.rb
CHANGED
@@ -5,13 +5,14 @@ module Apipie
|
|
5
5
|
:markup, :disqus_shortname,
|
6
6
|
:api_base_url, :doc_base_url, :required_by_default, :layout,
|
7
7
|
:default_version, :debug, :version_in_url, :namespaced_resources,
|
8
|
-
:validate, :validate_value, :validate_presence, :validate_key, :authenticate, :doc_path,
|
8
|
+
:validate, :validate_value, :validate_presence, :validate_key, :action_on_non_validated_keys, :authenticate, :doc_path,
|
9
9
|
:show_all_examples, :process_params, :update_checksum, :checksum_path,
|
10
10
|
:link_extension, :record, :languages, :translate, :locale, :default_locale,
|
11
|
-
:persist_show_in_doc, :authorize,
|
11
|
+
:persist_show_in_doc, :authorize, :ignore_allow_blank_false,
|
12
12
|
:swagger_include_warning_tags, :swagger_content_type_input, :swagger_json_input_uses_refs,
|
13
13
|
:swagger_suppress_warnings, :swagger_api_host, :swagger_generate_x_computed_id_field,
|
14
|
-
:swagger_allow_additional_properties_in_response, :swagger_responses_use_refs
|
14
|
+
:swagger_allow_additional_properties_in_response, :swagger_responses_use_refs,
|
15
|
+
:swagger_schemes, :swagger_security_definitions, :swagger_global_security
|
15
16
|
|
16
17
|
alias_method :validate?, :validate
|
17
18
|
alias_method :required_by_default?, :required_by_default
|
@@ -152,6 +153,7 @@ module Apipie
|
|
152
153
|
@validate_value = true
|
153
154
|
@validate_presence = true
|
154
155
|
@validate_key = false
|
156
|
+
@action_on_non_validated_keys = :raise
|
155
157
|
@required_by_default = false
|
156
158
|
@api_base_url = HashWithIndifferentAccess.new
|
157
159
|
@doc_base_url = "/apipie"
|
@@ -159,6 +161,7 @@ module Apipie
|
|
159
161
|
@disqus_shortname = nil
|
160
162
|
@default_version = "1.0"
|
161
163
|
@debug = false
|
164
|
+
@ignore_allow_blank_false = false
|
162
165
|
@version_in_url = true
|
163
166
|
@namespaced_resources = false
|
164
167
|
@doc_path = "doc"
|
@@ -181,6 +184,9 @@ module Apipie
|
|
181
184
|
@swagger_generate_x_computed_id_field = false
|
182
185
|
@swagger_allow_additional_properties_in_response = false
|
183
186
|
@swagger_responses_use_refs = true
|
187
|
+
@swagger_schemes = [:https]
|
188
|
+
@swagger_security_definitions = {}
|
189
|
+
@swagger_global_security = []
|
184
190
|
end
|
185
191
|
end
|
186
192
|
end
|
@@ -149,10 +149,10 @@ module Apipie
|
|
149
149
|
|
150
150
|
dsl_data = ResourceDescriptionDsl.eval_dsl(self, &block)
|
151
151
|
versions = dsl_data[:api_versions]
|
152
|
+
Apipie.set_controller_versions(self, versions)
|
152
153
|
@apipie_resource_descriptions = versions.map do |version|
|
153
154
|
Apipie.define_resource_description(self, version, dsl_data)
|
154
155
|
end
|
155
|
-
Apipie.set_controller_versions(self, versions)
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
@@ -262,7 +262,9 @@ module Apipie
|
|
262
262
|
if Apipie.configuration.validate_key?
|
263
263
|
params.reject{|k,_| %w[format controller action].include?(k.to_s) }.each_pair do |param, _|
|
264
264
|
# params allowed
|
265
|
-
|
265
|
+
if method_params.select {|_,p| p.name.to_s == param.to_s}.empty?
|
266
|
+
self.class._apipie_handle_validate_key_error params, param
|
267
|
+
end
|
266
268
|
end
|
267
269
|
end
|
268
270
|
|
@@ -290,6 +292,15 @@ module Apipie
|
|
290
292
|
end
|
291
293
|
end
|
292
294
|
|
295
|
+
def _apipie_handle_validate_key_error params, param
|
296
|
+
if Apipie.configuration.action_on_non_validated_keys == :raise
|
297
|
+
raise UnknownParam, param
|
298
|
+
elsif Apipie.configuration.action_on_non_validated_keys == :skip
|
299
|
+
params.delete(param)
|
300
|
+
Rails.logger.warn(UnknownParam.new(param).to_s)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
293
304
|
def _apipie_save_method_params(method, params)
|
294
305
|
@method_params ||= {}
|
295
306
|
@method_params[method] = params
|
@@ -150,7 +150,7 @@ module Apipie
|
|
150
150
|
end
|
151
151
|
|
152
152
|
module FunctionalTestRecording
|
153
|
-
def process(
|
153
|
+
def process(*) # action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
|
154
154
|
ret = super
|
155
155
|
if Apipie.configuration.record
|
156
156
|
Apipie::Extractor.call_recorder.analyze_functional_test(self)
|
data/lib/apipie/extractor.rb
CHANGED
@@ -17,14 +17,8 @@ class Apipie::Railtie
|
|
17
17
|
end
|
18
18
|
app.middleware.use ::Apipie::Extractor::Recorder::Middleware
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
prepend Apipie::Extractor::Recorder::FunctionalTestRecording
|
23
|
-
end
|
24
|
-
else
|
25
|
-
ActionController::TestCase.send(:prepend, Apipie::Extractor::Recorder::FunctionalTestRecording)
|
26
|
-
ActionController::TestCase::Behavior.send(:prepend, Apipie::Extractor::Recorder::FunctionalTestRecording)
|
27
|
-
end
|
20
|
+
ActionController::TestCase.send(:prepend, Apipie::Extractor::Recorder::FunctionalTestRecording)
|
21
|
+
ActionController::TestCase::Behavior.send(:prepend, Apipie::Extractor::Recorder::FunctionalTestRecording)
|
28
22
|
end
|
29
23
|
end
|
30
24
|
|
@@ -114,16 +114,20 @@ module Apipie
|
|
114
114
|
end
|
115
115
|
|
116
116
|
def validate(value)
|
117
|
-
return true if
|
118
|
-
return true if
|
117
|
+
return true if allow_nil && value.nil?
|
118
|
+
return true if allow_blank && value.blank?
|
119
119
|
value = normalized_value(value)
|
120
|
-
if (
|
121
|
-
error =
|
120
|
+
if (!allow_nil && value.nil?) || (blank_forbidden? && value.blank?) || !validator.valid?(value)
|
121
|
+
error = validator.error
|
122
122
|
error = ParamError.new(error) unless error.is_a? StandardError
|
123
123
|
raise error
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
|
+
def blank_forbidden?
|
128
|
+
!Apipie.configuration.ignore_allow_blank_false && !allow_blank && !validator.ignore_allow_blank?
|
129
|
+
end
|
130
|
+
|
127
131
|
def process_value(value)
|
128
132
|
value = normalized_value(value)
|
129
133
|
if @validator.respond_to?(:process_value)
|
@@ -161,8 +161,8 @@ ActionController::TestCase::Behavior.instance_eval do
|
|
161
161
|
# instrument the 'process' method in ActionController::TestCase to enable response validation
|
162
162
|
module Apipie::ResponseValidationHelpers
|
163
163
|
@is_response_validation_on = false
|
164
|
-
def process(
|
165
|
-
result = super
|
164
|
+
def process(*, **)
|
165
|
+
result = super
|
166
166
|
validate_response if @is_response_validation_on
|
167
167
|
|
168
168
|
result
|
@@ -73,7 +73,10 @@ module Apipie
|
|
73
73
|
consumes: [],
|
74
74
|
paths: {},
|
75
75
|
definitions: {},
|
76
|
+
schemes: Apipie.configuration.swagger_schemes,
|
76
77
|
tags: [],
|
78
|
+
securityDefinitions: Apipie.configuration.swagger_security_definitions,
|
79
|
+
security: Apipie.configuration.swagger_global_security
|
77
80
|
}
|
78
81
|
|
79
82
|
if Apipie.configuration.swagger_api_host
|
@@ -475,9 +478,16 @@ module Apipie
|
|
475
478
|
end
|
476
479
|
|
477
480
|
if swagger_def[:type] == "array"
|
478
|
-
|
479
|
-
|
481
|
+
array_of_validator_opts = param_desc.validator.param_description.options
|
482
|
+
items_type = array_of_validator_opts[:of].to_s || array_of_validator_opts[:array_of].to_s
|
483
|
+
if items_type == "Hash"
|
484
|
+
ref_name = "#{swagger_op_id_for_path(param_desc.method_description.apis.first.http_method, param_desc.method_description.apis.first.path)}_param_#{param_desc.name}"
|
485
|
+
swagger_def[:items] = {"$ref" => gen_referenced_block_from_params_array(ref_name, param_desc.validator.param_description.validator.params_ordered, allow_nulls)}
|
486
|
+
else
|
487
|
+
swagger_def[:items] = {type: "string"}
|
488
|
+
end
|
480
489
|
|
490
|
+
enum = param_desc.options.fetch(:in, [])
|
481
491
|
swagger_def[:items][:enum] = enum if enum.any?
|
482
492
|
end
|
483
493
|
|
data/lib/apipie/validator.rb
CHANGED
@@ -80,6 +80,10 @@ module Apipie
|
|
80
80
|
'string'
|
81
81
|
end
|
82
82
|
|
83
|
+
def ignore_allow_blank?
|
84
|
+
false
|
85
|
+
end
|
86
|
+
|
83
87
|
def merge_with(other_validator)
|
84
88
|
return self if self == other_validator
|
85
89
|
raise NotImplementedError, "Don't know how to merge #{self.inspect} with #{other_validator.inspect}"
|
@@ -333,6 +337,7 @@ module Apipie
|
|
333
337
|
@params_ordered ||= _apipie_dsl_data[:params].map do |args|
|
334
338
|
options = args.find { |arg| arg.is_a? Hash }
|
335
339
|
options[:parent] = self.param_description
|
340
|
+
options[:param_group] = @param_group
|
336
341
|
Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
|
337
342
|
end
|
338
343
|
end
|
@@ -477,6 +482,10 @@ module Apipie
|
|
477
482
|
string = %w(true false 1 0).map { |value| format_description_value(value) }.join(', ')
|
478
483
|
"Must be one of: #{string}."
|
479
484
|
end
|
485
|
+
|
486
|
+
def ignore_allow_blank?
|
487
|
+
true
|
488
|
+
end
|
480
489
|
end
|
481
490
|
|
482
491
|
class NestedValidator < BaseValidator
|
data/lib/apipie/version.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe IncludedParamGroupController do
|
5
|
+
|
6
|
+
let(:dsl_data) { ActionController::Base.send(:_apipie_dsl_data_init) }
|
7
|
+
|
8
|
+
it "should not error when there is a param_group that is deeply nested in response description" do
|
9
|
+
subject = Apipie.get_resource_description(IncludedParamGroupController, Apipie.configuration.default_version)
|
10
|
+
expect(subject._methods.keys).to include(:show)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -124,6 +124,29 @@ describe UsersController do
|
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
|
+
context "key validations are enabled and skip on non-validated keys" do
|
128
|
+
before do
|
129
|
+
Apipie.configuration.validate_value = false
|
130
|
+
Apipie.configuration.validate_presence = true
|
131
|
+
Apipie.configuration.validate_key = true
|
132
|
+
Apipie.configuration.action_on_non_validated_keys = :skip
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should reply to valid request" do
|
136
|
+
expect { get :show, :params => { :id => 5, :session => 'secret_hash' }}.not_to raise_error
|
137
|
+
assert_response :success
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should delete the param and not fail if an extra parameter is passed." do
|
141
|
+
expect { get :show, :params => { :id => 5 , :badparam => 'badfoo', :session => "secret_hash" }}.not_to raise_error
|
142
|
+
expect(controller.params.as_json).to eq({"session"=>"secret_hash", "id"=>"5", "controller"=>"users", "action"=>"show"})
|
143
|
+
end
|
144
|
+
|
145
|
+
after do
|
146
|
+
Apipie.configuration.action_on_non_validated_keys = :raise
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
127
150
|
context "presence and value validations are enabled" do
|
128
151
|
before do
|
129
152
|
Apipie.configuration.validate_value = true
|
@@ -3,6 +3,6 @@ class ConcernsController < ApplicationController
|
|
3
3
|
resource_description { resource_id 'concern_resources' }
|
4
4
|
|
5
5
|
apipie_concern_subst(:concern => 'user', :custom_subst => 'custom', 'string_subst' => 'string')
|
6
|
-
include
|
6
|
+
include SampleController
|
7
7
|
|
8
8
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class IncludedParamGroupController < ApplicationController
|
2
|
+
include RandomParamGroup
|
3
|
+
|
4
|
+
api :GET, '/included-param-group'
|
5
|
+
returns code:200 do
|
6
|
+
property :top_level, Array, of: Hash do
|
7
|
+
param_group :random_param_group
|
8
|
+
end
|
9
|
+
property :nested, Hash do
|
10
|
+
property :random_array, Array, of: Hash do
|
11
|
+
param_group :random_param_group
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
def show
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
@@ -3,7 +3,7 @@ class OverriddenConcernsController < ApplicationController
|
|
3
3
|
resource_description { resource_id 'overridden_concern_resources' }
|
4
4
|
|
5
5
|
apipie_concern_subst(:concern => 'user')
|
6
|
-
include
|
6
|
+
include SampleController
|
7
7
|
|
8
8
|
def_param_group :concern do
|
9
9
|
param :concern, String
|
@@ -11,7 +11,7 @@ class OverriddenConcernsController < ApplicationController
|
|
11
11
|
|
12
12
|
api :PUT, '/:resource_id/:id'
|
13
13
|
param :custom_parameter, String, "New parameter added by the overriding method"
|
14
|
-
param_group :concern,
|
14
|
+
param_group :concern, SampleController
|
15
15
|
def update
|
16
16
|
super
|
17
17
|
end
|
@@ -113,6 +113,92 @@ describe Apipie::ParamDescription do
|
|
113
113
|
|
114
114
|
end
|
115
115
|
|
116
|
+
describe 'validate' do
|
117
|
+
context 'when allow_blank is ignored, as it was before 0.7.0' do
|
118
|
+
before do
|
119
|
+
Apipie.configuration.ignore_allow_blank_false = true
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'when the parameter is a boolean' do
|
123
|
+
it "should not throw an exception when passed false" do
|
124
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, :boolean).validate(false) }.to_not raise_error
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should throw an exception when passed an empty value" do
|
128
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, :boolean).validate('') }.to raise_error(Apipie::ParamInvalid)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'when the parameter is a string' do
|
133
|
+
context 'when allow_blank is specified as true' do
|
134
|
+
it "should throw an exception when passed an empty value" do
|
135
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: true).validate('') }.to_not raise_error
|
136
|
+
end
|
137
|
+
end
|
138
|
+
context 'when allow_blank is specified as false' do
|
139
|
+
it "should throw an exception when passed an empty value" do
|
140
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: false).validate('') }.to_not raise_error
|
141
|
+
end
|
142
|
+
end
|
143
|
+
context 'when allow_blank is not specified' do
|
144
|
+
it "should throw an exception when passed an empty value" do
|
145
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, String).validate('') }.to_not raise_error
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
after do
|
151
|
+
Apipie.configuration.ignore_allow_blank_false = false
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'when the parameter is a boolean' do
|
156
|
+
it "should not throw an exception when passed false" do
|
157
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, :boolean).validate(false) }.to_not raise_error
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should still not throw an exception when passed false with explicit allow_blank: false" do
|
161
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, :boolean, allow_blank: false).validate(false) }.to_not raise_error
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should throw an exception when passed an empty value" do
|
165
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, :boolean).validate('') }.to raise_error(Apipie::ParamInvalid)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
context "when the parameter is a custom type with ignore_allow_blank? returning true" do
|
170
|
+
it "should not throw an exception when passed a blank but valid value" do
|
171
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, :custom_bool).validate(false) }.to_not raise_error
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should still not throw an exception when passed false with explicit allow_blank: false" do
|
175
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, :custom_bool, allow_blank: false).validate(false) }.to_not raise_error
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should throw an exception when passed an invalid but blank value" do
|
179
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, :custom_bool).validate("") }.to raise_error(Apipie::ParamInvalid)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
context 'when the parameter is a string' do
|
184
|
+
context 'when allow_blank is specified as true' do
|
185
|
+
it "should throw an exception when passed an empty value" do
|
186
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: true).validate('') }.to_not raise_error
|
187
|
+
end
|
188
|
+
end
|
189
|
+
context 'when allow_blank is specified as false' do
|
190
|
+
it "should throw an exception when passed an empty value" do
|
191
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, String, allow_blank: false).validate('') }.to raise_error(Apipie::ParamInvalid)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
context 'when allow_blank is not specified' do
|
195
|
+
it "should throw an exception when passed an empty value" do
|
196
|
+
expect { Apipie::ParamDescription.new(method_desc, :param, String).validate('') }.to raise_error(Apipie::ParamInvalid)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
116
202
|
describe "concern substitution" do
|
117
203
|
|
118
204
|
let(:concern_dsl_data) { dsl_data.merge(:from_concern => true) }
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler/setup'
|
3
3
|
|
4
|
+
require 'simplecov'
|
5
|
+
SimpleCov.minimum_coverage 89
|
6
|
+
SimpleCov.start
|
7
|
+
|
4
8
|
ENV["RAILS_ENV"] ||= 'test'
|
5
9
|
APIPIE_ROOT = File.expand_path('../..', __FILE__)
|
6
10
|
require File.expand_path("../dummy/config/environment", __FILE__)
|
@@ -10,37 +14,6 @@ require 'rspec/rails'
|
|
10
14
|
require 'apipie-rails'
|
11
15
|
require 'test_engine'
|
12
16
|
|
13
|
-
module Rails4Compatibility
|
14
|
-
module Testing
|
15
|
-
def process(*args)
|
16
|
-
compatible_request(*args) do |*new_args|
|
17
|
-
if Gem.ruby_version < Gem::Version.new('3.0.0')
|
18
|
-
super(*new_args)
|
19
|
-
else
|
20
|
-
super(new_args[0], **new_args[1])
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def compatible_request(method, action, hash = {})
|
26
|
-
if hash.is_a?(Hash)
|
27
|
-
if Gem::Version.new(Rails.version) < Gem::Version.new('5.0.0')
|
28
|
-
hash = hash.dup
|
29
|
-
hash.merge!(hash.delete(:params) || {})
|
30
|
-
elsif hash.key?(:params)
|
31
|
-
hash = { :params => hash }
|
32
|
-
end
|
33
|
-
end
|
34
|
-
if hash.empty?
|
35
|
-
yield method, action
|
36
|
-
else
|
37
|
-
yield method, action, hash
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
17
|
#
|
45
18
|
# Matcher to validate the properties (name, type and options) of a single field in the
|
46
19
|
# internal representation of a swagger schema
|
@@ -114,4 +87,3 @@ RSpec.configure do |config|
|
|
114
87
|
end
|
115
88
|
|
116
89
|
require 'action_controller/test_case.rb'
|
117
|
-
ActionController::TestCase::Behavior.send(:prepend, Rails4Compatibility::Testing)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CustomBoolValidator < Apipie::Validator::BaseValidator
|
2
|
+
def validate(value)
|
3
|
+
value.in?([true, false])
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.build(param_description, argument, options, block)
|
7
|
+
new(param_description) if argument == :custom_bool
|
8
|
+
end
|
9
|
+
|
10
|
+
def description
|
11
|
+
"Must be a boolean."
|
12
|
+
end
|
13
|
+
|
14
|
+
def ignore_allow_blank?
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|