Skip to content

Commit e8acc11

Browse files
woshidandblock
authored andcommitted
Raise validation error when optional Hash type parameter is received String type value and exactly_one_of be used (#1822)
* adds test for exactly_one_of validator with nested and optional parmeters * add check if the parameter responds to any? method in MultipleParamsBase#scope_requires_params to handle nested invalid parameter
1 parent b14b196 commit e8acc11

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

.rubocop_todo.yml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2017-12-10 01:15:22 +0900 using RuboCop version 0.51.0.
3+
# on 2018-11-22 00:04:22 +0900 using RuboCop version 0.51.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -27,29 +27,30 @@ Lint/RescueWithoutErrorClass:
2727
Exclude:
2828
- 'lib/grape/validations/validators/coerce.rb'
2929

30-
# Offense count: 49
30+
# Offense count: 47
3131
Metrics/AbcSize:
3232
Max: 44
3333

34-
# Offense count: 282
34+
# Offense count: 4
3535
# Configuration parameters: CountComments, ExcludedMethods.
3636
Metrics/BlockLength:
37-
Max: 3125
37+
Max: 182
3838

3939
# Offense count: 9
4040
# Configuration parameters: CountComments.
4141
Metrics/ClassLength:
4242
Max: 288
4343

44-
# Offense count: 32
44+
# Offense count: 31
4545
Metrics/CyclomaticComplexity:
4646
Max: 14
4747

48-
# Offense count: 1159
48+
# Offense count: 1227
4949
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
5050
# URISchemes: http, https
5151
Metrics/LineLength:
5252
Max: 215
53+
5354
# Offense count: 57
5455
# Configuration parameters: CountComments.
5556
Metrics/MethodLength:
@@ -58,9 +59,7 @@ Metrics/MethodLength:
5859
# Offense count: 11
5960
# Configuration parameters: CountComments.
6061
Metrics/ModuleLength:
61-
Max: 212
62-
Exclude:
63-
- 'spec/grape/dsl/routing_spec.rb'
62+
Max: 220
6463

6564
# Offense count: 21
6665
Metrics/PerceivedComplexity:
@@ -91,7 +90,7 @@ Performance/HashEachMethods:
9190
- 'lib/grape/api/instance.rb'
9291
- 'lib/grape/middleware/versioner/header.rb'
9392

94-
# Offense count: 1
93+
# Offense count: 2
9594
# Cop supports --auto-correct.
9695
# Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods.
9796
# SupportedStyles: line_count_based, semantic, braces_for_chaining

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).
1818
* [#1810](https://github.com/ruby-grape/grape/pull/1810): Fix support in `given` for aliased params - [@darren987469](https://github.com/darren987469).
1919
* [#1811](https://github.com/ruby-grape/grape/pull/1811): Support nested dependent parameters - [@darren987469](https://github.com/darren987469), [@andreacfm](https://github.com/andreacfm).
20+
* [#1822](https://github.com/ruby-grape/grape/pull/1822): Raise validation error when optional hash type parameter is received string type value and exactly_one_of be used - [@woshidan](https://github.com/woshidan).
2021

2122
### 1.1.0 (8/4/2018)
2223

lib/grape/validations/validators/multiple_params_base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def validate!(params)
1111
private
1212

1313
def scope_requires_params
14-
@scope.required? || scoped_params.any?(&:any?)
14+
@scope.required? || scoped_params.any? { |param| param.respond_to?(:any?) && param.any? }
1515
end
1616

1717
def keys_in_common(resource_params)

spec/grape/validations/params_scope_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,4 +1045,40 @@ def initialize(value)
10451045
end
10461046
end
10471047
end
1048+
1049+
context 'with exactly_one_of validation for optional parameters within an Hash param' do
1050+
before do
1051+
subject.params do
1052+
optional :memo, type: Hash do
1053+
optional :text, type: String
1054+
optional :custom_body, type: Hash, coerce_with: JSON
1055+
exactly_one_of :text, :custom_body
1056+
end
1057+
end
1058+
subject.get('test')
1059+
end
1060+
1061+
context 'when correct data is provided' do
1062+
it 'returns a successful response' do
1063+
get 'test', memo: {}
1064+
expect(last_response.status).to eq(200)
1065+
1066+
get 'test', memo: { text: 'HOGEHOGE' }
1067+
expect(last_response.status).to eq(200)
1068+
1069+
get 'test', memo: { custom_body: '{ "xxx": "yyy" }' }
1070+
expect(last_response.status).to eq(200)
1071+
end
1072+
end
1073+
1074+
context 'when invalid data is provided' do
1075+
it 'returns a failure response' do
1076+
get 'test', memo: { text: 'HOGEHOGE', custom_body: '{ "xxx": "yyy" }' }
1077+
expect(last_response.status).to eq(400)
1078+
1079+
get 'test', memo: '{ "custom_body": "HOGE" }'
1080+
expect(last_response.status).to eq(400)
1081+
end
1082+
end
1083+
end
10481084
end

0 commit comments

Comments
 (0)