Skip to content

Mark required elements inside group in mutually exclusive set as optional #207

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

Merged
merged 1 commit into from
Feb 9, 2015
Merged
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
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Metrics/AbcSize:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 400
Max: 410

# Offense count: 5
Metrics/CyclomaticComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [#208](https://github.com/tim-vandecasteele/grape-swagger/pull/208): Fixed `Float` parameters, exposed as Swagger `float` types - [@u2](https://github.com/u2).
* [#196](https://github.com/tim-vandecasteele/grape-swagger/pull/196): If `:type` is omitted, see if it's available in `:using` - [@jhollinger](https://github.com/jhollinger).
* [#200](https://github.com/tim-vandecasteele/grape-swagger/pull/200): Treat `type: Symbol` as string form parameter - [@ypresto](https://github.com/ypresto).
* [#207](https://github.com/tim-vandecasteele/grape-swagger/pull/207): Support grape `mutually_exclusive` - [@mintuhouse](https://github.com/mintuhouse).
* Your contribution here.

### 0.9.0 (December 19, 2014)
Expand Down
21 changes: 17 additions & 4 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ def combine_namespaces(app)
end
end

def get_non_nested_params(params)
# Duplicate the params as we are going to modify them
dup_params = params.each_with_object(Hash.new) do |(param, value), dparams|
dparams[param] = value.dup
end

dup_params.reject do |param, value|
is_nested_param = /^#{ Regexp.quote param }\[.+\]$/
0 < dup_params.count do |p, _|
match = p.match(is_nested_param)
dup_params[p][:required] = false if match && !value[:required]
match
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to combine each and reject, there's a bunch of duplicate processing here, notably is_nested_param.

end

def create_documentation_class
Class.new(Grape::API) do
class << self
Expand All @@ -65,10 +81,7 @@ def as_markdown(description)
def parse_params(params, path, method)
params ||= []

non_nested_parent_params = params.reject do |param, _|
is_nested_param = /^#{ Regexp.quote param }\[.+\]$/
params.keys.any? { |p| p.match is_nested_param }
end
non_nested_parent_params = get_non_nested_params(params)

non_nested_parent_params.map do |param, value|
items = {}
Expand Down
36 changes: 36 additions & 0 deletions spec/mutually_exclusive_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe 'Mutually exclusive group params' do
def app
Class.new(Grape::API) do
format :json

params do
requires :required_group, type: Hash do
group :param_group_1, type: Hash do
requires :param_1, type: String
end
optional :param_group_2, type: Array do
requires :param_2, type: String
end
mutually_exclusive :param_group_1, :param_group_2
end
end
post '/groups' do
{}
end

add_swagger_documentation
end
end

it 'marks the nested required params in a group as optional if the group is in a mutually_exclusive set' do
get '/swagger_doc/groups'

body = JSON.parse last_response.body
parameters = body['apis'].first['operations'].first['parameters']
expect(parameters).to eq [
{ 'paramType' => 'form', 'name' => 'required_group[param_group_1][param_1]', 'description' => nil, 'type' => 'string', 'required' => false, 'allowMultiple' => false },
{ 'paramType' => 'form', 'name' => 'required_group[param_group_2][param_2]', 'description' => nil, 'type' => 'string', 'required' => false, 'allowMultiple' => false }]
end
end