Skip to content

Commit

Permalink
minor correction, refactoring (#478)
Browse files Browse the repository at this point in the history
- adds changelog entry
  • Loading branch information
peter scholz authored Jul 21, 2016
1 parent 9dd244b commit eb0599f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Fixes

* [#476](https://github.com/ruby-grape/grape-swagger/pull/476): Fixes for handling the parameter type when body parameters are defined inside desc block - [@anakinj](https://github.com/anakinj).
* [#478](https://github.com/ruby-grape/grape-swagger/pull/478): Refactors building of properties, corrects documentation of array items - [@LeFnord](https://github.com/LeFnord).
* Your contribution here.

### 0.22.0 (July 12, 2016)
Expand Down
68 changes: 33 additions & 35 deletions lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,39 @@ def move_params_to_new(definition, params)
add_properties_to_definition(definition, nested_properties, []) unless nested_params.blank?
end

def build_properties(params)
properties = {}
required = []

prepare_nested_types(params) if should_expose_as_array?(params)

params.each do |param|
name = param[:name].to_sym

properties[name] = if should_expose_as_array?([param])
document_as_array(param)
else
document_as_property(param)
end

required << name if deletable?(param) && param[:required]
end

[properties, required]
end

def document_as_array(param)
{}.tap do |property|
property[:type] = 'array'
property[:description] = param.delete(:description) unless param[:description].nil?
property[:items] = document_as_property(param)[:items]
end
end

def document_as_property(param)
property_keys.each_with_object({}) { |x, memo| memo[x] = param[x] if param[x].present? }
end

def build_nested_properties(params, properties = {})
property = params.bsearch { |x| x[:name].include?('[') }[:name].split('[').first

Expand Down Expand Up @@ -94,41 +127,6 @@ def add_to_required(definition, value)
definition[:required].push(*value)
end

def build_properties(params)
properties = {}
required = []

prepare_nested_types(params) if should_expose_as_array?(params)

params.each do |param|
name = param[:name].to_sym
properties[name] = {}

if should_expose_as_array?([param])
prepare_nested_types([param])

properties[name][:type] = 'array'
properties[name][:items] = {}
properties[name][:items].tap do |x|
property_keys.each do |attribute|
x[attribute] = param[attribute] unless param[attribute].nil?
end
end
else

properties[name].tap do |x|
property_keys.each do |attribute|
x[attribute] = param[attribute] unless param[attribute].nil?
end
end
end

required << name if deletable?(param) && param[:required]
end

[properties, required]
end

def build_body_parameter(reference, name)
{}.tap do |x|
x[:name] = name
Expand Down
6 changes: 4 additions & 2 deletions spec/swagger_v2/params_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ def app
expect(subject['definitions']['postObjectAndArray']['type']).to eql 'object'
expect(subject['definitions']['postObjectAndArray']['properties']).to eql(
'array_of_string' => {
'type' => 'array', 'items' => {
'type' => 'string', 'description' => 'array of strings'
'type' => 'array',
'description' => 'array of strings',
'items' => {
'type' => 'string'
}
},
'integer_value' => {
Expand Down

0 comments on commit eb0599f

Please sign in to comment.