Skip to content

Commit

Permalink
Allow customizing entity name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Levente Bagi authored and dblock committed Jul 14, 2014
1 parent 99b1430 commit 982f71a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Next Release

* [#110](https://github.com/tim-vandecasteele/grape-swagger/pull/110) - Added `responseModel` support - [@bagilevi](https://github.com/bagilevi).
* [#110](https://github.com/tim-vandecasteele/grape-swagger/pull/110), [#111](https://github.com/tim-vandecasteele/grape-swagger/pull/111) - Added `responseModel` support - [@bagilevi](https://github.com/bagilevi).
* [#105](https://github.com/tim-vandecasteele/grape-swagger/pull/105): Fixed compatibility with Swagger-UI - [@CraigCottingham](https://github.com/CraigCottingham).
* [#87](https://github.com/tim-vandecasteele/grape-swagger/pull/87): Fixed mapping of `default` to `defaultValue` - [@m-o-e](https://github.com/m-o-e).
* Rewritten .gemspec and removed Jeweler - [@dblock](https://github.com/dblock).
Expand Down
20 changes: 13 additions & 7 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def self.setup(options)
ops.each do |path, op_routes|
operations = op_routes.map do |route|
notes = as_markdown(route.route_notes)

http_codes = parse_http_codes(route.route_http_codes, models)

models << if @@models.present?
Expand Down Expand Up @@ -320,10 +321,15 @@ 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('::')
def parse_entity_name(model)
if model.respond_to?(:entity_name)
model.entity_name
else
name = model.to_s
entity_parts = name.split('::')
entity_parts.reject! { |p| p == 'Entity' || p == 'Entities' }
entity_parts.join('::')
end
end

def parse_entity_models(models)
Expand Down Expand Up @@ -378,12 +384,12 @@ def parse_http_codes(codes, models)
codes ||= {}
codes.map do |k, v, m|
models << m if m
code = {
http_code_hash = {
code: k,
message: v
}
code[:responseModel] = parse_entity_name(m) if m
code
http_code_hash[:responseModel] = parse_entity_name(m) if m
http_code_hash
end
end

Expand Down
100 changes: 55 additions & 45 deletions spec/response_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,76 @@

describe 'responseModel' do
before :all do
module Entities
class Something < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
end
module MyAPI
module Entities
class BaseEntity < Grape::Entity
def self.entity_name
name.sub(/^MyAPI::Entities::/, '')
end
end

class Error < Grape::Entity
expose :code, documentation: { type: 'string', desc: 'Error code' }
expose :message, documentation: { type: 'string', desc: 'Error message' }
class Something < BaseEntity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
end

class Error < BaseEntity
expose :code, documentation: { type: 'string', desc: 'Error code' }
expose :message, documentation: { type: 'string', desc: 'Error message' }
end
end
end

class ResponseModelApi < Grape::API
format :json
desc 'This returns something or an error',
entity: Entities::Something,
http_codes: [
[200, 'OK', Entities::Something],
[403, 'Refused to return something', Entities::Error]
]
class ResponseModelApi < Grape::API
format :json
desc 'This returns something or an error',
entity: Entities::Something,
http_codes: [
[200, 'OK', Entities::Something],
[403, 'Refused to return something', Entities::Error]
]

get '/something/:id' do
if params[:id] == 1
something = OpenStruct.new text: 'something'
present something, with: Entities::Something
else
error = OpenStruct.new code: 'some_error', message: 'Some error'
present error, with: Entities::Error
get '/something/:id' do
if params[:id] == 1
something = OpenStruct.new text: 'something'
present something, with: Entities::Something
else
error = OpenStruct.new code: 'some_error', message: 'Some error'
present error, with: Entities::Error
end
end
end

add_swagger_documentation
add_swagger_documentation
end
end
end

def app
ResponseModelApi
MyAPI::ResponseModelApi
end

it 'should document specified models' do
get '/swagger_doc/something'
parsed_response = JSON.parse(last_response.body)
parsed_response['apis'][0]['operations'][0]['responseMessages'].should eq([
{
'code' => 200,
'message' => 'OK',
'responseModel' => 'Something'
},
{
'code' => 403,
'message' => 'Refused to return something',
'responseModel' => 'Error'
}
])
parsed_response['apis'][0]['operations'][0]['responseMessages'].should eq(
[
{
'code' => 200,
'message' => 'OK',
'responseModel' => 'Something'
},
{
'code' => 403,
'message' => 'Refused to return something',
'responseModel' => 'Error'
}
]
)
parsed_response['models'].keys.should include 'Error'
parsed_response['models']['Error'].should eq(
'id' => 'Error',
'properties' => {
'code' => { 'type' => 'string', 'description' => 'Error code' },
'message' => { 'type' => 'string', 'description' => 'Error message' }
}
)
parsed_response['models']['Error'].should == {
'id' => 'Error',
'properties' => {
'code' => { 'type' => 'string', 'description' => 'Error code' },
'message' => { 'type' => 'string', 'description' => 'Error message' }
}
}
end
end

0 comments on commit 982f71a

Please sign in to comment.