Skip to content

Commit

Permalink
issue 539: corrects exposing of array in post body
Browse files Browse the repository at this point in the history
- updates README
- adds changelog entry
  • Loading branch information
LeFnord committed Nov 28, 2016
1 parent cc30fc8 commit e6f6c8d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#### Fixes

* [#540](https://github.com/ruby-grape/grape-swagger/pull/540): Corrects exposing of array in post body - [@LeFnord](https://github.com/LeFnord).
* [#509](https://github.com/ruby-grape/grape-swagger/pull/509), [#529](https://github.com/ruby-grape/grape-swagger/pull/529): Making parent-less routes working - [@mur-wtag](https://github.com/mur-wtag).
* Your contribution here.

Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ module API
class Client < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
expose :addresses, using: Entities::Address,
documentation: { type: 'API::Address', desc: 'Addresses.', param_type: 'body', is_array: true }
documentation: { type: 'Entities::Address', desc: 'Addresses.', param_type: 'body', is_array: true }
end
class Address < Grape::Entity
Expand All @@ -967,13 +967,15 @@ module API
class Clients < Grape::API
version 'v1'
desc 'Clients index', params: Entities::Client.documentation
desc 'Clients index',
params: Entities::Client.documentation,
success: Entities::Client
get '/clients' do
...
end
end
add_swagger_documentation models: [Entities::Client, Entities::Address]
add_swagger_documentation
end
```
Expand All @@ -988,7 +990,7 @@ module API
class Client < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
expose :address, using: Entities::Address,
documentation: { type: 'API::Address', desc: 'Addresses.', param_type: 'body', is_array: false }
documentation: { type: 'Entities::Address', desc: 'Addresses.', param_type: 'body', is_array: false }
end
class Address < Grape::Entity
Expand All @@ -999,13 +1001,15 @@ module API
class Clients < Grape::API
version 'v1'
desc 'Clients index', params: Entities::Client.documentation
desc 'Clients index',
params: Entities::Client.documentation,
success: Entities::Client
get '/clients' do
...
end
end
add_swagger_documentation models: [Entities::Client, Entities::Address]
add_swagger_documentation
end
```
Expand Down
10 changes: 10 additions & 0 deletions lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def to_definition(params, route, definitions)
@definitions = definitions
unify!(params)

return correct_array_param(params) if should_correct_array?(params)

params_to_move = movable_params(params)
params << parent_definition_of_params(params_to_move, route)

Expand All @@ -20,6 +22,14 @@ def to_definition(params, route, definitions)

private

def should_correct_array?(param)
param.length == 1 && param.first[:in] == 'body' && param.first[:type] == 'array'
end

def correct_array_param(param)
param.first[:schema] = { type: param.first.delete(:type), items: param.first.delete(:items) }
end

def parent_definition_of_params(params, route)
definition_name = GrapeSwagger::DocMethods::OperationId.manipulate(parse_model(route.path))
referenced_definition = build_definition(definition_name, params, route.request_method.downcase)
Expand Down
2 changes: 2 additions & 0 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def document_array_param(value_type, definitions)
collection_format = value_type[:documentation][:collectionFormat]
end

param_type ||= value_type[:param_type]

array_items = {}
if definitions[value_type[:data_type]]
array_items['$ref'] = "#/definitions/#{@parsed_param[:type]}"
Expand Down
63 changes: 63 additions & 0 deletions spec/issues/539_array_post_body.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'spec_helper'
require 'grape-entity'
require 'grape-swagger-entity'

describe '#427 nested entity given as string' do
let(:app) do
Class.new(Grape::API) do
namespace :issue_427 do
class Element < Grape::Entity
expose :id
expose :description
expose :role
end

class ArrayOfElements < Grape::Entity
expose :elements,
documentation: {
type: ::Element, is_array: true, param_type: 'body', required: true
}
end

desc 'create account',
params: ArrayOfElements.documentation
post do
present params
end
end

add_swagger_documentation format: :json
end
end

subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

let(:parameters) { subject['paths']['/issue_427']['post']['parameters'] }
let(:definitions) { subject['definitions'] }

specify do
expect(parameters).to eql(
[
{
'in' => 'body', 'name' => 'elements', 'required' => true, 'schema' => {
'type' => 'array', 'items' => { '$ref' => '#/definitions/Element' }
}
}
]
)

expect(definitions).to eql(
'Element' => {
'type' => 'object',
'properties' => {
'id' => { 'type' => 'string' },
'description' => { 'type' => 'string' },
'role' => { 'type' => 'string' }
}
}
)
end
end

0 comments on commit e6f6c8d

Please sign in to comment.