Skip to content

Commit 67ea152

Browse files
authored
Fix declared_hash_attr when passed_params is not an Hash (#2552)
* Fix declared_hash_attr where passed_params could not respond to `try` * Add CHANGELOG.md Fix rubocop
1 parent 2b35fed commit 67ea152

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [#2546](https://github.com/ruby-grape/grape/pull/2546): Fix middleware with keywords - [@ericproulx](https://github.com/ericproulx).
1919
* [#2547](https://github.com/ruby-grape/grape/pull/2547): Remove jsonapi related code - [@ericproulx](https://github.com/ericproulx).
2020
* [#2548](https://github.com/ruby-grape/grape/pull/2548): Formatting from header acts like versioning from header - [@ericproulx](https://github.com/ericproulx).
21+
* [#2552](https://github.com/ruby-grape/grape/pull/2552): Fix declared params optional array - [@ericproulx](https://github.com/ericproulx).
2122
* Your contribution here.
2223

2324
### 2.3.0 (2025-02-08)

lib/grape/dsl/inside_route.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def declared_hash_attr(passed_params, options, declared_param, params_nested_pat
7979
else
8080
# If it is not a Hash then it does not have children.
8181
# Find its value or set it to nil.
82-
return unless options[:include_missing] || passed_params.key?(declared_param)
82+
return unless options[:include_missing] || passed_params.try(:key?, declared_param)
8383

8484
rename_path = params_nested_path + [declared_param.to_s]
8585
renamed_param_name = renamed_params[rename_path]

spec/grape/endpoint/declared_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -856,4 +856,26 @@
856856
end
857857
end
858858
end
859+
860+
describe 'optional_array' do
861+
subject { last_response }
862+
863+
let(:app) do
864+
Class.new(Grape::API) do
865+
params do
866+
requires :z, type: Array do
867+
optional :a, type: Integer
868+
end
869+
end
870+
871+
post do
872+
declared(params, include_missing: false)
873+
end
874+
end
875+
end
876+
877+
before { post '/', { z: [] } }
878+
879+
it { is_expected.to be_successful }
880+
end
859881
end

0 commit comments

Comments
 (0)