grape 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grape might be problematic. Click here for more details.

@@ -324,4 +324,28 @@ describe Grape::Middleware::Formatter do
324
324
  expect(bodies.body.first).to eq({ message: 'invalid' }.to_json)
325
325
  end
326
326
  end
327
+
328
+ context 'custom parser raises exception and rescue options are enabled for backtrace and original_exception' do
329
+ it 'adds the backtrace and original_exception to the error output' do
330
+ subject = Grape::Middleware::Formatter.new(
331
+ app,
332
+ rescue_options: { backtrace: true, original_exception: true },
333
+ parsers: { json: ->(_object, _env) { raise StandardError, 'fail' } }
334
+ )
335
+ io = StringIO.new('{invalid}')
336
+ error = catch(:error) {
337
+ subject.call(
338
+ 'PATH_INFO' => '/info',
339
+ 'REQUEST_METHOD' => 'POST',
340
+ 'CONTENT_TYPE' => 'application/json',
341
+ 'rack.input' => io,
342
+ 'CONTENT_LENGTH' => io.length
343
+ )
344
+ }
345
+
346
+ expect(error[:message]).to eq 'fail'
347
+ expect(error[:backtrace].size).to be >= 1
348
+ expect(error[:original_exception].class).to eq StandardError
349
+ end
350
+ end
327
351
  end
@@ -121,6 +121,42 @@ describe Grape::Validations::ParamsScope do
121
121
  end
122
122
  end
123
123
 
124
+ context 'param alias' do
125
+ it do
126
+ subject.params do
127
+ requires :foo, as: :bar
128
+ optional :super, as: :hiper
129
+ end
130
+ subject.get('/alias') { "#{declared(params)['bar']}-#{declared(params)['hiper']}" }
131
+ get '/alias', foo: 'any', super: 'any2'
132
+
133
+ expect(last_response.status).to eq(200)
134
+ expect(last_response.body).to eq('any-any2')
135
+ end
136
+
137
+ it do
138
+ subject.params do
139
+ requires :foo, as: :bar, type: String, coerce_with: ->(c) { c.strip }
140
+ end
141
+ subject.get('/alias-coerced') { "#{params['bar']}-#{params['foo']}" }
142
+ get '/alias-coerced', foo: ' there we go '
143
+
144
+ expect(last_response.status).to eq(200)
145
+ expect(last_response.body).to eq('there we go-')
146
+ end
147
+
148
+ it do
149
+ subject.params do
150
+ requires :foo, as: :bar, allow_blank: false
151
+ end
152
+ subject.get('/alias-not-blank') {}
153
+ get '/alias-not-blank', foo: ''
154
+
155
+ expect(last_response.status).to eq(400)
156
+ expect(last_response.body).to eq('foo is empty')
157
+ end
158
+ end
159
+
124
160
  context 'array without coerce type explicitly given' do
125
161
  it 'sets the type based on first element' do
126
162
  subject.params do
@@ -418,6 +454,46 @@ describe Grape::Validations::ParamsScope do
418
454
  end.to raise_error(Grape::Exceptions::UnknownParameter)
419
455
  end
420
456
 
457
+ it 'does not validate nested requires when given is false' do
458
+ subject.params do
459
+ requires :a, type: String, allow_blank: false, values: %w(x y z)
460
+ given a: ->(val) { val == 'x' } do
461
+ requires :inner1, type: Hash, allow_blank: false do
462
+ requires :foo, type: Integer, allow_blank: false
463
+ end
464
+ end
465
+ given a: ->(val) { val == 'y' } do
466
+ requires :inner2, type: Hash, allow_blank: false do
467
+ requires :bar, type: Integer, allow_blank: false
468
+ requires :baz, type: Array, allow_blank: false do
469
+ requires :baz_category, type: String, allow_blank: false
470
+ end
471
+ end
472
+ end
473
+ given a: ->(val) { val == 'z' } do
474
+ requires :inner3, type: Array, allow_blank: false do
475
+ requires :bar, type: Integer, allow_blank: false
476
+ requires :baz, type: Array, allow_blank: false do
477
+ requires :baz_category, type: String, allow_blank: false
478
+ end
479
+ end
480
+ end
481
+ end
482
+ subject.get('/varying') { declared(params).to_json }
483
+
484
+ get '/varying', a: 'x', inner1: { foo: 1 }
485
+ expect(last_response.status).to eq(200)
486
+
487
+ get '/varying', a: 'y', inner2: { bar: 2, baz: [{ baz_category: 'barstools' }] }
488
+ expect(last_response.status).to eq(200)
489
+
490
+ get '/varying', a: 'y', inner2: { bar: 2, baz: [{ unrelated: 'yep' }] }
491
+ expect(last_response.status).to eq(400)
492
+
493
+ get '/varying', a: 'z', inner3: [{ bar: 3, baz: [{ baz_category: 'barstools' }] }]
494
+ expect(last_response.status).to eq(200)
495
+ end
496
+
421
497
  it 'includes the parameter within #declared(params)' do
422
498
  get '/test', a: true, b: true
423
499
 
@@ -110,6 +110,13 @@ describe Grape::Validations::ValuesValidator do
110
110
  { type: params[:type] }
111
111
  end
112
112
 
113
+ params do
114
+ requires :number, type: Integer, values: ->(v) { v > 0 }
115
+ end
116
+ get '/lambda_int_val' do
117
+ { number: params[:number] }
118
+ end
119
+
113
120
  params do
114
121
  requires :type, values: -> { [] }
115
122
  end
@@ -357,6 +364,24 @@ describe Grape::Validations::ValuesValidator do
357
364
  expect(last_response.body).to eq({ error: 'type does not have a valid value' }.to_json)
358
365
  end
359
366
 
367
+ it 'does not allow non-numeric string value for int value using lambda' do
368
+ get('/lambda_int_val', number: 'foo')
369
+ expect(last_response.status).to eq 400
370
+ expect(last_response.body).to eq({ error: 'number is invalid, number does not have a valid value' }.to_json)
371
+ end
372
+
373
+ it 'does not allow nil for int value using lambda' do
374
+ get('/lambda_int_val', number: nil)
375
+ expect(last_response.status).to eq 400
376
+ expect(last_response.body).to eq({ error: 'number does not have a valid value' }.to_json)
377
+ end
378
+
379
+ it 'allows numeric string for int value using lambda' do
380
+ get('/lambda_int_val', number: '3')
381
+ expect(last_response.status).to eq 200
382
+ expect(last_response.body).to eq({ number: 3 }.to_json)
383
+ end
384
+
360
385
  it 'allows value using lambda' do
361
386
  get('/lambda_val', type: 'valid-type1')
362
387
  expect(last_response.status).to eq 200
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-03 00:00:00.000000000 Z
11
+ date: 2017-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -236,6 +236,7 @@ files:
236
236
  - lib/grape/validations/validator_factory.rb
237
237
  - lib/grape/validations/validators/all_or_none.rb
238
238
  - lib/grape/validations/validators/allow_blank.rb
239
+ - lib/grape/validations/validators/as.rb
239
240
  - lib/grape/validations/validators/at_least_one_of.rb
240
241
  - lib/grape/validations/validators/base.rb
241
242
  - lib/grape/validations/validators/coerce.rb
@@ -248,8 +249,10 @@ files:
248
249
  - lib/grape/validations/validators/regexp.rb
249
250
  - lib/grape/validations/validators/values.rb
250
251
  - lib/grape/version.rb
252
+ - pkg/grape-0.19.1.gem
251
253
  - spec/grape/api/custom_validations_spec.rb
252
254
  - spec/grape/api/deeply_included_options_spec.rb
255
+ - spec/grape/api/inherited_helpers_spec.rb
253
256
  - spec/grape/api/invalid_format_spec.rb
254
257
  - spec/grape/api/namespace_parameters_in_route_spec.rb
255
258
  - spec/grape/api/nested_helpers_spec.rb
@@ -363,13 +366,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
363
366
  version: '0'
364
367
  requirements: []
365
368
  rubyforge_project:
366
- rubygems_version: 2.6.12
369
+ rubygems_version: 2.6.10
367
370
  signing_key:
368
371
  specification_version: 4
369
372
  summary: A simple Ruby framework for building REST-like APIs.
370
373
  test_files:
371
374
  - spec/grape/api/custom_validations_spec.rb
372
375
  - spec/grape/api/deeply_included_options_spec.rb
376
+ - spec/grape/api/inherited_helpers_spec.rb
373
377
  - spec/grape/api/invalid_format_spec.rb
374
378
  - spec/grape/api/namespace_parameters_in_route_spec.rb
375
379
  - spec/grape/api/nested_helpers_spec.rb