diff --git a/CHANGELOG.md b/CHANGELOG.md index 692a5c28..9b468709 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape-swagger/doc_methods/move_params.rb b/lib/grape-swagger/doc_methods/move_params.rb index e740311b..ecff9145 100644 --- a/lib/grape-swagger/doc_methods/move_params.rb +++ b/lib/grape-swagger/doc_methods/move_params.rb @@ -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 @@ -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 diff --git a/spec/swagger_v2/params_array_spec.rb b/spec/swagger_v2/params_array_spec.rb index 460bfefb..4f1f7fb2 100644 --- a/spec/swagger_v2/params_array_spec.rb +++ b/spec/swagger_v2/params_array_spec.rb @@ -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' => {