Skip to content
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

Deserialize Unexploded Query Parameters #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/openapi_parser/schemas/parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,27 @@ class Parameter < Base
# @return [Object] coerced or original params
# @param [OpenAPIParser::SchemaValidator::Options] options
def validate_params(params, options)
::OpenAPIParser::SchemaValidator.validate(params, schema, options)
::OpenAPIParser::SchemaValidator.validate(deserialize(params), schema, options)
end

# Parameters can be serialized in several different ways
# See [documentation](https://swagger.io/docs/specification/serialization/) for details
# @return [Object] deserialized version of parameters
def deserialize(params)
# binding.pry
return params unless params.kind_of?(String)
if explode == false
delimiter = case style
when 'spaceDelimited'
' '
when 'pipeDelimited'
'|'
else
','
end
params = params.split(delimiter)
end
params
end
end
end
28 changes: 28 additions & 0 deletions spec/data/normal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@ paths:
type: array
items:
type: integer
- name: form_unexploded_array
in: query
description: array that looks like 'apple,banana,coconut'
style: form
explode: false
schema:
type: array
items:
type: string
- name: space_unexploded_array
in: query
description: array that looks like '5 10 15'
style: spaceDelimited
explode: false
schema:
type: array
items:
type:
integer
- name: pipe_unexploded_array
in: query
description: array that looks like 'first|second|last'
style: pipeDelimited
explode: false
schema:
type: array
items:
type: string
- name: nested_array
in: query
description: nested_array
Expand Down
51 changes: 51 additions & 0 deletions spec/openapi_parser/schema_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,57 @@
end
end

context 'with style=form exploded=false array' do
let(:params) do
{
'form_unexploded_array' => 'apple,banana,coconut'
}
end

it do
expect(subject['form_unexploded_array'][0]).to eq 'apple'
expect(params['form_unexploded_array'][0]).to eq 'apple'
expect(subject['form_unexploded_array'][1]).to eq 'banana'
expect(params['form_unexploded_array'][1]).to eq 'banana'
expect(subject['form_unexploded_array'][2]).to eq 'coconut'
expect(params['form_unexploded_array'][2]).to eq 'coconut'
end
end

context 'with style=spaceDelimited exploded=false array' do
let(:params) do
{
'space_unexploded_array' => '5 10 15'
}
end

it do
expect(subject['space_unexploded_array'][0]).to eq 5
expect(params['space_unexploded_array'][0]).to eq 5
expect(subject['space_unexploded_array'][1]).to eq 10
expect(params['space_unexploded_array'][1]).to eq 10
expect(subject['space_unexploded_array'][2]).to eq 15
expect(params['space_unexploded_array'][2]).to eq 15
end
end

context 'with style=pipeDelimited exploded=false array' do
let(:params) do
{
'pipe_unexploded_array' => 'first|second|last'
}
end

it do
expect(subject['pipe_unexploded_array'][0]).to eq 'first'
expect(params['pipe_unexploded_array'][0]).to eq 'first'
expect(subject['pipe_unexploded_array'][1]).to eq 'second'
expect(params['pipe_unexploded_array'][1]).to eq 'second'
expect(subject['pipe_unexploded_array'][2]).to eq 'last'
expect(params['pipe_unexploded_array'][2]).to eq 'last'
end
end

context 'nested_array' do
let(:params) do
{ 'nested_array' => nested_array }
Expand Down