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

Support for multiple entities #217

Closed
wants to merge 3 commits into from
Closed
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 @@ -7,7 +7,7 @@

# Offense count: 8
Metrics/AbcSize:
Max: 331
Max: 334

# Offense count: 1
# Configuration parameters: CountComments.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#217](https://github.com/tim-vandecasteele/grape-swagger/pull/217): Support Array of entities for proper rendering of grape-entity input dependencies - [@swistaczek](https://github.com/swistaczek).
* [#214](https://github.com/tim-vandecasteele/grape-swagger/pull/214): Allow anything that responds to `call` to be used in `:hidden` - [@zbelzer](https://github.com/zbelzer).
* [#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).
Expand Down
4 changes: 2 additions & 2 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def setup(options)

models |= @@models if @@models.present?

models |= [route.route_entity] if route.route_entity.present?
models |= Array(route.route_entity) if route.route_entity.present?

models = @@documentation_class.models_with_included_presenters(models.flatten.compact)

Expand All @@ -483,7 +483,7 @@ def setup(options)
operation.merge!(responseMessages: http_codes) unless http_codes.empty?

if route.route_entity
type = @@documentation_class.parse_entity_name(route.route_entity)
type = @@documentation_class.parse_entity_name(Array(route.route_entity).first)
operation.merge!('type' => type)
end

Expand Down
39 changes: 39 additions & 0 deletions spec/api_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ class FirstLevel < Grape::Entity
end
end

module Entities
class QueryInputElement < Grape::Entity
expose :key, documentation: {
type: String, desc: 'Name of parameter', required: true }
expose :value, documentation: {
type: String, desc: 'Value of parameter', required: true }
end

class QueryInput < Grape::Entity
expose :elements, using: Entities::QueryInputElement, documentation: {
type: 'QueryInputElement',
desc: 'Set of configuration',
param_type: 'body',
is_array: true,
required: true
}
end

class QueryResult < Grape::Entity
expose :elements_size, documentation: { type: Integer, desc: 'Return input elements size' }
end
end

def app
Class.new(Grape::API) do
format :json
Expand Down Expand Up @@ -118,6 +141,14 @@ def app
present first_level, with: Entities::FirstLevel
end

desc 'This tests diffrent entity for input and diffrent for output',
entity: [Entities::QueryResult, Entities::QueryInput],
params: Entities::QueryInput.documentation
get '/multiple_entities' do
result = OpenStruct.new(elements_size: params[:elements].size)
present result, with: Entities::QueryResult
end

add_swagger_documentation
end
end
Expand Down Expand Up @@ -145,6 +176,7 @@ def app
{ 'path' => '/enum_description_in_entity.{format}', 'description' => 'Operations about enum_description_in_entities' },
{ 'path' => '/aliasedthing.{format}', 'description' => 'Operations about aliasedthings' },
{ 'path' => '/nesting.{format}', 'description' => 'Operations about nestings' },
{ 'path' => '/multiple_entities.{format}', 'description' => 'Operations about multiple_entities' },
{ 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }
]
end
Expand Down Expand Up @@ -251,4 +283,11 @@ def app

expect(result['models']).to include('FirstLevel', 'SecondLevel', 'ThirdLevel', 'FourthLevel')
end

it 'includes all entities while using multiple entities' do
get '/swagger_doc/multiple_entities'
result = JSON.parse(last_response.body)

expect(result['models']).to include('QueryInput', 'QueryInputElement', 'QueryResult')
end
end