grape 1.7.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -1
- data/CONTRIBUTING.md +1 -1
- data/README.md +30 -25
- data/UPGRADING.md +35 -0
- data/grape.gemspec +3 -6
- data/lib/grape/api.rb +2 -2
- data/lib/grape/content_types.rb +2 -8
- data/lib/grape/dsl/desc.rb +1 -1
- data/lib/grape/dsl/inside_route.rb +11 -11
- data/lib/grape/dsl/request_response.rb +2 -1
- data/lib/grape/dsl/settings.rb +2 -6
- data/lib/grape/endpoint.rb +28 -18
- data/lib/grape/error_formatter/base.rb +1 -1
- data/lib/grape/exceptions/base.rb +2 -2
- data/lib/grape/exceptions/missing_group_type.rb +1 -6
- data/lib/grape/exceptions/unsupported_group_type.rb +1 -6
- data/lib/grape/exceptions/validation_errors.rb +1 -6
- data/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +3 -3
- data/lib/grape/extensions/hash.rb +4 -7
- data/lib/grape/extensions/hashie/mash.rb +3 -3
- data/lib/grape/formatter/serializable_hash.rb +7 -7
- data/lib/grape/http/headers.rb +12 -2
- data/lib/grape/middleware/auth/base.rb +1 -1
- data/lib/grape/middleware/auth/strategies.rb +1 -2
- data/lib/grape/middleware/error.rb +5 -5
- data/lib/grape/middleware/formatter.rb +6 -6
- data/lib/grape/middleware/versioner/header.rb +11 -19
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +8 -2
- data/lib/grape/router/route.rb +1 -3
- data/lib/grape/util/lazy_value.rb +3 -11
- data/lib/grape/util/strict_hash_configuration.rb +3 -4
- data/lib/grape/validations/multiple_attributes_iterator.rb +1 -1
- data/lib/grape/validations/params_scope.rb +8 -2
- data/lib/grape/validations/single_attribute_iterator.rb +3 -1
- data/lib/grape/validations/types/custom_type_coercer.rb +2 -16
- data/lib/grape/validations/validators/base.rb +9 -20
- data/lib/grape/validations/validators/default_validator.rb +2 -20
- data/lib/grape/validations/validators/multiple_params_base.rb +4 -8
- data/lib/grape/validations/validators/values_validator.rb +14 -5
- data/lib/grape/version.rb +1 -1
- data/lib/grape.rb +26 -5
- metadata +13 -253
- data/lib/grape/config.rb +0 -34
- data/lib/grape/extensions/deep_mergeable_hash.rb +0 -21
- data/lib/grape/extensions/deep_symbolize_hash.rb +0 -32
- data/spec/grape/api/custom_validations_spec.rb +0 -256
- data/spec/grape/api/deeply_included_options_spec.rb +0 -56
- data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -38
- data/spec/grape/api/documentation_spec.rb +0 -59
- data/spec/grape/api/inherited_helpers_spec.rb +0 -114
- data/spec/grape/api/instance_spec.rb +0 -103
- data/spec/grape/api/invalid_format_spec.rb +0 -45
- data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -38
- data/spec/grape/api/nested_helpers_spec.rb +0 -50
- data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -43
- data/spec/grape/api/parameters_modification_spec.rb +0 -41
- data/spec/grape/api/patch_method_helpers_spec.rb +0 -79
- data/spec/grape/api/recognize_path_spec.rb +0 -21
- data/spec/grape/api/required_parameters_in_route_spec.rb +0 -37
- data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -26
- data/spec/grape/api/routes_with_requirements_spec.rb +0 -59
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -41
- data/spec/grape/api/shared_helpers_spec.rb +0 -36
- data/spec/grape/api_remount_spec.rb +0 -473
- data/spec/grape/api_spec.rb +0 -4347
- data/spec/grape/config_spec.rb +0 -17
- data/spec/grape/dsl/callbacks_spec.rb +0 -45
- data/spec/grape/dsl/desc_spec.rb +0 -101
- data/spec/grape/dsl/headers_spec.rb +0 -62
- data/spec/grape/dsl/helpers_spec.rb +0 -100
- data/spec/grape/dsl/inside_route_spec.rb +0 -535
- data/spec/grape/dsl/logger_spec.rb +0 -24
- data/spec/grape/dsl/middleware_spec.rb +0 -60
- data/spec/grape/dsl/parameters_spec.rb +0 -180
- data/spec/grape/dsl/request_response_spec.rb +0 -206
- data/spec/grape/dsl/routing_spec.rb +0 -275
- data/spec/grape/dsl/settings_spec.rb +0 -261
- data/spec/grape/dsl/validations_spec.rb +0 -55
- data/spec/grape/endpoint/declared_spec.rb +0 -846
- data/spec/grape/endpoint_spec.rb +0 -1085
- data/spec/grape/entity_spec.rb +0 -336
- data/spec/grape/exceptions/base_spec.rb +0 -81
- data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -145
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +0 -358
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -15
- data/spec/grape/exceptions/invalid_response_spec.rb +0 -11
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -15
- data/spec/grape/exceptions/missing_group_type_spec.rb +0 -21
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -17
- data/spec/grape/exceptions/missing_option_spec.rb +0 -15
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -15
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -15
- data/spec/grape/exceptions/unsupported_group_type_spec.rb +0 -23
- data/spec/grape/exceptions/validation_errors_spec.rb +0 -92
- data/spec/grape/exceptions/validation_spec.rb +0 -19
- data/spec/grape/extensions/param_builders/hash_spec.rb +0 -83
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -105
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -79
- data/spec/grape/integration/global_namespace_function_spec.rb +0 -29
- data/spec/grape/integration/rack_sendfile_spec.rb +0 -48
- data/spec/grape/integration/rack_spec.rb +0 -51
- data/spec/grape/loading_spec.rb +0 -44
- data/spec/grape/middleware/auth/base_spec.rb +0 -31
- data/spec/grape/middleware/auth/dsl_spec.rb +0 -60
- data/spec/grape/middleware/auth/strategies_spec.rb +0 -120
- data/spec/grape/middleware/base_spec.rb +0 -221
- data/spec/grape/middleware/error_spec.rb +0 -85
- data/spec/grape/middleware/exception_spec.rb +0 -294
- data/spec/grape/middleware/formatter_spec.rb +0 -461
- data/spec/grape/middleware/globals_spec.rb +0 -30
- data/spec/grape/middleware/stack_spec.rb +0 -155
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -122
- data/spec/grape/middleware/versioner/header_spec.rb +0 -345
- data/spec/grape/middleware/versioner/param_spec.rb +0 -171
- data/spec/grape/middleware/versioner/path_spec.rb +0 -62
- data/spec/grape/middleware/versioner_spec.rb +0 -21
- data/spec/grape/named_api_spec.rb +0 -19
- data/spec/grape/parser_spec.rb +0 -86
- data/spec/grape/path_spec.rb +0 -252
- data/spec/grape/presenters/presenter_spec.rb +0 -71
- data/spec/grape/request_spec.rb +0 -136
- data/spec/grape/util/inheritable_setting_spec.rb +0 -242
- data/spec/grape/util/inheritable_values_spec.rb +0 -79
- data/spec/grape/util/reverse_stackable_values_spec.rb +0 -134
- data/spec/grape/util/stackable_values_spec.rb +0 -128
- data/spec/grape/util/strict_hash_configuration_spec.rb +0 -38
- data/spec/grape/validations/attributes_doc_spec.rb +0 -153
- data/spec/grape/validations/instance_behaivour_spec.rb +0 -43
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -40
- data/spec/grape/validations/params_scope_spec.rb +0 -1420
- data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -57
- data/spec/grape/validations/types/array_coercer_spec.rb +0 -33
- data/spec/grape/validations/types/primitive_coercer_spec.rb +0 -150
- data/spec/grape/validations/types/set_coercer_spec.rb +0 -32
- data/spec/grape/validations/types_spec.rb +0 -111
- data/spec/grape/validations/validators/all_or_none_spec.rb +0 -162
- data/spec/grape/validations/validators/allow_blank_spec.rb +0 -575
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -205
- data/spec/grape/validations/validators/coerce_spec.rb +0 -1261
- data/spec/grape/validations/validators/default_spec.rb +0 -463
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -233
- data/spec/grape/validations/validators/except_values_spec.rb +0 -192
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -214
- data/spec/grape/validations/validators/presence_spec.rb +0 -315
- data/spec/grape/validations/validators/regexp_spec.rb +0 -161
- data/spec/grape/validations/validators/same_as_spec.rb +0 -57
- data/spec/grape/validations/validators/values_spec.rb +0 -696
- data/spec/grape/validations/validators/zh-CN.yml +0 -10
- data/spec/grape/validations_spec.rb +0 -2029
- data/spec/integration/eager_load/eager_load_spec.rb +0 -15
- data/spec/integration/multi_json/json_spec.rb +0 -7
- data/spec/integration/multi_xml/xml_spec.rb +0 -7
- data/spec/shared/versioning_examples.rb +0 -215
- data/spec/spec_helper.rb +0 -52
- data/spec/support/basic_auth_encode_helpers.rb +0 -11
- data/spec/support/chunks.rb +0 -14
- data/spec/support/content_type_helpers.rb +0 -15
- data/spec/support/endpoint_faker.rb +0 -25
- data/spec/support/file_streamer.rb +0 -13
- data/spec/support/integer_helpers.rb +0 -13
- data/spec/support/versioned_helpers.rb +0 -55
@@ -1,153 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::Validations::ParamsScope::AttributesDoc do
|
4
|
-
shared_examples 'an optional doc attribute' do |attr|
|
5
|
-
it 'does not mention it' do
|
6
|
-
expected_opts.delete(attr)
|
7
|
-
validations.delete(attr)
|
8
|
-
|
9
|
-
expect(subject.first['nested[engine_age]']).not_to have_key(attr)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
let(:api) { Class.new(Grape::API::Instance) }
|
14
|
-
let(:scope) do
|
15
|
-
params = nil
|
16
|
-
api_instance = api
|
17
|
-
|
18
|
-
# just to get nested params
|
19
|
-
Grape::Validations::ParamsScope.new(type: Hash, api: api) do
|
20
|
-
params = Grape::Validations::ParamsScope.new(element: 'nested',
|
21
|
-
type: Hash,
|
22
|
-
api: api_instance,
|
23
|
-
parent: self)
|
24
|
-
end
|
25
|
-
|
26
|
-
params
|
27
|
-
end
|
28
|
-
|
29
|
-
let(:validations) do
|
30
|
-
{
|
31
|
-
presence: true,
|
32
|
-
desc: 'Age of...',
|
33
|
-
documentation: 'Age is...',
|
34
|
-
default: 1
|
35
|
-
}
|
36
|
-
end
|
37
|
-
|
38
|
-
let(:doc) { described_class.new(api, scope) }
|
39
|
-
|
40
|
-
describe '#extract_details' do
|
41
|
-
subject { doc.extract_details(validations) }
|
42
|
-
|
43
|
-
it 'cleans up doc attrs needed for documentation only' do
|
44
|
-
subject
|
45
|
-
|
46
|
-
expect(validations[:desc]).to be_nil
|
47
|
-
expect(validations[:documentation]).to be_nil
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'does not clean up doc attrs mandatory for validators' do
|
51
|
-
subject
|
52
|
-
|
53
|
-
expect(validations[:presence]).not_to be_nil
|
54
|
-
expect(validations[:default]).not_to be_nil
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'tells when attributes are required' do
|
58
|
-
subject
|
59
|
-
|
60
|
-
expect(doc.required).to be_truthy
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe '#document' do
|
65
|
-
subject do
|
66
|
-
doc.extract_details validations
|
67
|
-
doc.document attrs
|
68
|
-
end
|
69
|
-
|
70
|
-
let(:attrs) { %w[engine_age car_age] }
|
71
|
-
let(:valid_values) { [1, 3, 5, 8] }
|
72
|
-
|
73
|
-
let!(:expected_opts) do
|
74
|
-
{
|
75
|
-
required: true,
|
76
|
-
desc: validations[:desc],
|
77
|
-
documentation: validations[:documentation],
|
78
|
-
default: validations[:default],
|
79
|
-
type: 'Integer',
|
80
|
-
values: valid_values
|
81
|
-
}
|
82
|
-
end
|
83
|
-
|
84
|
-
before do
|
85
|
-
doc.type = Integer
|
86
|
-
doc.values = valid_values
|
87
|
-
end
|
88
|
-
|
89
|
-
context 'documentation is enabled' do
|
90
|
-
subject do
|
91
|
-
super()
|
92
|
-
api.namespace_stackable(:params)
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'documents attributes' do
|
96
|
-
expect(subject.first).to eq('nested[engine_age]' => expected_opts,
|
97
|
-
'nested[car_age]' => expected_opts)
|
98
|
-
end
|
99
|
-
|
100
|
-
it_behaves_like 'an optional doc attribute', :default
|
101
|
-
it_behaves_like 'an optional doc attribute', :documentation
|
102
|
-
it_behaves_like 'an optional doc attribute', :desc
|
103
|
-
it_behaves_like 'an optional doc attribute', :type do
|
104
|
-
before { doc.type = nil }
|
105
|
-
end
|
106
|
-
it_behaves_like 'an optional doc attribute', :values do
|
107
|
-
before { doc.values = nil }
|
108
|
-
end
|
109
|
-
|
110
|
-
context 'false as a default value' do
|
111
|
-
before { validations[:default] = false }
|
112
|
-
|
113
|
-
it 'is still documented' do
|
114
|
-
doc = subject.first['nested[engine_age]']
|
115
|
-
|
116
|
-
expect(doc).to have_key(:default)
|
117
|
-
expect(doc[:default]).to be(false)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
context 'nil as a default value' do
|
122
|
-
before { validations[:default] = nil }
|
123
|
-
|
124
|
-
it 'is still documented' do
|
125
|
-
doc = subject.first['nested[engine_age]']
|
126
|
-
|
127
|
-
expect(doc).to have_key(:default)
|
128
|
-
expect(doc[:default]).to be_nil
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
context 'the description key instead of desc' do
|
133
|
-
let!(:desc) { validations.delete(:desc) }
|
134
|
-
|
135
|
-
before { validations[:description] = desc }
|
136
|
-
|
137
|
-
it 'adds the given description' do
|
138
|
-
expect(subject.first['nested[engine_age]'][:desc]).to eq(desc)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
context 'documentation is disabled' do
|
144
|
-
before { api.namespace_inheritable :do_not_document, true }
|
145
|
-
|
146
|
-
it 'does not document attributes' do
|
147
|
-
subject
|
148
|
-
|
149
|
-
expect(api.namespace_stackable(:params)).to eq([])
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe 'Validator with instance variables' do
|
4
|
-
let(:validator_type) do
|
5
|
-
Class.new(Grape::Validations::Validators::Base) do
|
6
|
-
def validate_param!(_attr_name, _params)
|
7
|
-
if instance_variable_defined?(:@instance_variable) && @instance_variable
|
8
|
-
raise Grape::Exceptions::Validation.new(params: ['params'],
|
9
|
-
message: 'This should never happen')
|
10
|
-
end
|
11
|
-
@instance_variable = true
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
let(:app) do
|
16
|
-
Class.new(Grape::API) do
|
17
|
-
params do
|
18
|
-
optional :param_to_validate, instance_validator: true
|
19
|
-
optional :another_param_to_validate, instance_validator: true
|
20
|
-
end
|
21
|
-
get do
|
22
|
-
'noop'
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
before do
|
28
|
-
Grape::Validations.register_validator('instance_validator', validator_type)
|
29
|
-
end
|
30
|
-
|
31
|
-
after do
|
32
|
-
Grape::Validations.deregister_validator('instance_validator')
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'passes validation every time' do
|
36
|
-
expect(validator_type).to receive(:new).exactly(4).times.and_call_original
|
37
|
-
|
38
|
-
2.times do
|
39
|
-
get '/', param_to_validate: 'value', another_param_to_validate: 'value'
|
40
|
-
expect(last_response.status).to eq 200
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe Grape::Validations::MultipleAttributesIterator do
|
4
|
-
describe '#each' do
|
5
|
-
subject(:iterator) { described_class.new(validator, scope, params) }
|
6
|
-
|
7
|
-
let(:scope) { Grape::Validations::ParamsScope.new(api: Class.new(Grape::API)) }
|
8
|
-
let(:validator) { double(attrs: %i[first second third]) }
|
9
|
-
|
10
|
-
context 'when params is a hash' do
|
11
|
-
let(:params) do
|
12
|
-
{ first: 'string', second: 'string' }
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'yields the whole params hash and the skipped flag without the list of attrs' do
|
16
|
-
expect { |b| iterator.each(&b) }.to yield_with_args(params, false)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'when params is an array' do
|
21
|
-
let(:params) do
|
22
|
-
[{ first: 'string1', second: 'string1' }, { first: 'string2', second: 'string2' }]
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'yields each element of the array without the list of attrs' do
|
26
|
-
expect { |b| iterator.each(&b) }.to yield_successive_args([params[0], false], [params[1], false])
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'when params is empty optional placeholder' do
|
31
|
-
let(:params) do
|
32
|
-
[Grape::DSL::Parameters::EmptyOptionalValue, { first: 'string2', second: 'string2' }]
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'yields each element of the array without the list of attrs' do
|
36
|
-
expect { |b| iterator.each(&b) }.to yield_successive_args([Grape::DSL::Parameters::EmptyOptionalValue, true], [params[1], false])
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|