-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
support Array with index #1261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
support Array with index #1261
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -912,6 +912,31 @@ params do | |
| end | ||
| ``` | ||
|
|
||
| If type is Array, It support following kinds of parameter. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We never use |
||
| #### Array | ||
| ```sh | ||
| #without index | ||
| curl -X POST http://localhost:9292/api/v1/preference.json -d \ | ||
| &preferences[][key]=foo \ | ||
| &preferences[][value]=100 \ | ||
| &preferences[][key]=bar \ | ||
| &preferences[][value]=200 | ||
|
|
||
| #with index | ||
| curl -X POST http://localhost:9292/api/v1/preference.json -d \ | ||
| &preferences[0][key]=foo \ | ||
| &preferences[1][key]=bar \ | ||
| &preferences[0][value]=100 \ | ||
| &preferences[1][value]=200 | ||
| ``` | ||
|
|
||
| #### Hash | ||
| ```sh | ||
| curl -X POST http://localhost:9292/api/v1/preference.json -d \ | ||
| &preferences[key]=foo \ | ||
| &preferences[value]=100 \ | ||
| ```` | ||
|
|
||
| ### Dependent Parameters | ||
|
|
||
| Suppose some of your parameters are only relevant if another parameter is given; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require 'active_support/concern' | ||
| module Grape | ||
| module Util | ||
| module HashParameter | ||
| extend ActiveSupport::Concern | ||
|
|
||
| def deem_hash_array?(hash) | ||
| return false unless hash.is_a?(Hash) && hash.keys.any? { |key| integer_string?(key) } | ||
| true | ||
| end | ||
|
|
||
| def integer_string?(str) | ||
| Integer(str) | ||
| true | ||
| rescue ArgumentError, TypeError | ||
| false | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not in love with this. It brings an odd method, |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,6 +99,12 @@ def app | |
| expect(last_response.body).to eq({ array: [{ name: 'name', with_default: 'default' }, { name: 'name2', with_default: 'bar2' }] }.to_json) | ||
| end | ||
|
|
||
| it 'sets default values for grouped arrays with index' do | ||
| get('/array?array[0][name]=name&array[1][name]=name2&array[0][with_default]=bar1') | ||
| expect(last_response.status).to eq(200) | ||
| expect(last_response.body).to eq({ array: { 0 => { name: 'name', with_default: 'bar1' }, 1 => { name: 'name2', with_default: 'default' } } }.to_json) | ||
| end | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a little weak on specs. Add separate tests for simpler scenarios as well, one for when the key is an Integer, another a string, then a mixed one, etc. So we see what's broken when it is as opposed as to "it doesn't work". |
||
|
|
||
| context 'optional group with defaults' do | ||
| subject do | ||
| Class.new(Grape::API) do | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support with a capital S please.