From 981b3e1d5881a203cff7cdf5767dcd853a05757d Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 3 Feb 2014 20:11:47 -0500 Subject: [PATCH 1/2] more flexible Entity --- lib/grape-swagger.rb | 10 ++++++-- spec/api_models_spec.rb | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 581fd745..052a461b 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -140,7 +140,11 @@ def self.setup(options) :parameters => parse_header_params(route.route_headers) + parse_params(route.route_params, route.route_path, route.route_method) } - operations.merge!(:type => route.route_entity.to_s.split('::')[-1]) if route.route_entity + if route.route_entity + entity_parts = route.route_entity.to_s.split('::') + entity_parts.reject! {|p| p == "Entity" || p == "Entities"} + operations.merge!(:type => entity_parts.join("::")) + end operations.merge!(:responseMessages => http_codes) unless http_codes.empty? { @@ -251,7 +255,9 @@ def parse_entity_models(models) result = {} models.each do |model| - name = model.to_s.split('::')[-1] + entity_parts = model.to_s.split('::') + entity_parts.reject! {|p| p == "Entity" || p == "Entities"} + name = entity_parts.join("::") properties = {} model.documentation.each do |property_name, property_info| diff --git a/spec/api_models_spec.rb b/spec/api_models_spec.rb index 95425638..6ab580a0 100644 --- a/spec/api_models_spec.rb +++ b/spec/api_models_spec.rb @@ -9,6 +9,14 @@ class Something < Grape::Entity end end + module Entities + module Some + class Thing < Grape::Entity + expose :text, :documentation => { :type => "string", :desc => "Content of something." } + end + end + end + class ModelsApi < Grape::API format :json desc 'This gets something.', { @@ -18,6 +26,14 @@ class ModelsApi < Grape::API something = OpenStruct.new text: 'something' present something, with: Entities::Something end + + desc 'This gets thing.', { + entity: Entities::Some::Thing + } + get "/thing" do + thing = OpenStruct.new text: 'thing' + present thing, with: Entities::Some::Thing + end add_swagger_documentation end end @@ -35,6 +51,7 @@ def app; ModelsApi; end "operations" => [], "apis" => [ { "path" => "/swagger_doc/something.{format}" }, + { "path" => "/swagger_doc/thing.{format}" }, { "path" => "/swagger_doc/swagger_doc.{format}" } ] } @@ -76,4 +93,40 @@ def app; ModelsApi; end } end + it "should include nested type when specified" do + get '/swagger_doc/thing.json' + JSON.parse(last_response.body).should == { + "apiVersion" => "0.1", + "swaggerVersion" => "1.2", + "basePath" => "http://example.org", + "resourcePath" => "", + "apis" => [{ + "path" => "/thing.{format}", + "operations" => [{ + "produces" => [ + "application/json" + ], + "notes" => "", + "type" => "Some::Thing", + "summary" => "This gets thing.", + "nickname" => "GET-thing---format-", + "httpMethod" => "GET", + "parameters" => [] + }] + }], + "models" => { + "Some::Thing" => { + "id" => "Some::Thing", + "name" => "Some::Thing", + "properties" => { + "text" => { + "type" => "string", + "description" => "Content of something." + } + } + } + } + } + end + end From cc9ff0f2748232873d6684d915d1e87629eeb57b Mon Sep 17 00:00:00 2001 From: Jerome Gravel-Niquet Date: Mon, 3 Feb 2014 20:15:23 -0500 Subject: [PATCH 2/2] DRYing the entity name parsing --- lib/grape-swagger.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 052a461b..888a6b88 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -140,11 +140,7 @@ def self.setup(options) :parameters => parse_header_params(route.route_headers) + parse_params(route.route_params, route.route_path, route.route_method) } - if route.route_entity - entity_parts = route.route_entity.to_s.split('::') - entity_parts.reject! {|p| p == "Entity" || p == "Entities"} - operations.merge!(:type => entity_parts.join("::")) - end + operations.merge!(:type => parse_entity_name(route.route_entity)) if route.route_entity operations.merge!(:responseMessages => http_codes) unless http_codes.empty? { @@ -251,13 +247,17 @@ def parse_path(path, version) version ? parsed_path.gsub('{version}', version) : parsed_path end + def parse_entity_name(name) + entity_parts = name.to_s.split('::') + entity_parts.reject! {|p| p == "Entity" || p == "Entities"} + entity_parts.join("::") + end + def parse_entity_models(models) result = {} models.each do |model| - entity_parts = model.to_s.split('::') - entity_parts.reject! {|p| p == "Entity" || p == "Entities"} - name = entity_parts.join("::") + name = parse_entity_name(model) properties = {} model.documentation.each do |property_name, property_info|