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

Flexible Entity #83

Closed
wants to merge 2 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
10 changes: 8 additions & 2 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ 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
operations.merge!(:type => parse_entity_name(route.route_entity)) if route.route_entity
operations.merge!(:responseMessages => http_codes) unless http_codes.empty?

{
Expand Down Expand Up @@ -247,11 +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|
name = model.to_s.split('::')[-1]
name = parse_entity_name(model)
properties = {}

model.documentation.each do |property_name, property_info|
Expand Down
53 changes: 53 additions & 0 deletions spec/api_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.', {
Expand All @@ -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
Expand All @@ -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}" }
]
}
Expand Down Expand Up @@ -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