From 9ca20b1fcef33c140fd9da0062888e2f0e1bf3ac Mon Sep 17 00:00:00 2001 From: Ernest Bursa Date: Wed, 18 Feb 2015 10:56:07 +0100 Subject: [PATCH 1/3] add specs, support set of Entities (eg. one for input, other for response), add changelog --- CHANGELOG.md | 1 + lib/grape-swagger.rb | 4 ++-- spec/api_models_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8636ba1..abfa74ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### Features +* [#](https://github.com/tim-vandecasteele/grape-swagger/pull/): 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). diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 3d648b11..26bb1b5b 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -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) @@ -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 diff --git a/spec/api_models_spec.rb b/spec/api_models_spec.rb index 635697f5..6170b031 100644 --- a/spec/api_models_spec.rb +++ b/spec/api_models_spec.rb @@ -71,6 +71,26 @@ 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 @@ -118,6 +138,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 @@ -145,6 +173,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 @@ -251,4 +280,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 From 4e39c7e3802d583c07aaaf1fbcf0e859570c48fb Mon Sep 17 00:00:00 2001 From: Ernest Bursa Date: Wed, 18 Feb 2015 10:59:16 +0100 Subject: [PATCH 2/3] add PR number to commit --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abfa74ab..4b59e953 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ #### Features -* [#](https://github.com/tim-vandecasteele/grape-swagger/pull/): Support Array of entities for proper rendering of grape-entity input dependencies - [@swistaczek](https://github.com/swistaczek). +* [#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). From f828b4d02ab91d0dc6d8b3a18deca2cc82f11443 Mon Sep 17 00:00:00 2001 From: Ernest Bursa Date: Wed, 18 Feb 2015 11:43:18 +0100 Subject: [PATCH 3/3] fix rubocop builds --- .rubocop_todo.yml | 2 +- spec/api_models_spec.rb | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ecd8e780..4bb13f0e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -7,7 +7,7 @@ # Offense count: 8 Metrics/AbcSize: - Max: 331 + Max: 334 # Offense count: 1 # Configuration parameters: CountComments. diff --git a/spec/api_models_spec.rb b/spec/api_models_spec.rb index 6170b031..6e5b0cc3 100644 --- a/spec/api_models_spec.rb +++ b/spec/api_models_spec.rb @@ -80,10 +80,13 @@ class QueryInputElement < Grape::Entity 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 } + 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 @@ -139,8 +142,8 @@ def app end desc 'This tests diffrent entity for input and diffrent for output', - entity: [ Entities::QueryResult, Entities::QueryInput ], - params: Entities::QueryInput.documentation + 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