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

Allow parsing Entity with no endpoint #12

Merged
merged 5 commits into from
Aug 2, 2016
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* [#11](https://github.com/ruby-grape/grape-swagger-entity/pull/11): Use an automated PR linter, [danger.systems](http://danger.systems) - [@Bugagazavr](https://github.com/Bugagazavr).
* [#12](https://github.com/ruby-grape/grape-swagger-entity/pull/12): Allow parsing Entity with no endpoint - [@lordnibbler](https://github.com/lordnibbler).
* Your contribution here.

#### Fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/entity/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def parse_grape_entity_params(params)
end

if model
name = endpoint.send(:expose_params_from_model, model)
name = endpoint.nil? ? model.to_s.demodulize : endpoint.send(:expose_params_from_model, model)
memo[entity_name] = entity_model_type(name, entity_options)
else
documented_type = entity_options[:type]
Expand Down
64 changes: 1 addition & 63 deletions spec/grape-swagger/entities/response_model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,69 +1,7 @@
require 'spec_helper'

describe 'responseModel' do
before :all do
module ThisApi
module Entities
class Kind < Grape::Entity
expose :title, documentation: { type: 'string', desc: 'Title of the kind.' }
end

class Relation < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end
class Tag < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end
class Error < Grape::Entity
expose :code, documentation: { type: 'string', desc: 'Error code' }
expose :message, documentation: { type: 'string', desc: 'Error message' }
end

class Something < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
expose :colors, documentation: { type: 'string', desc: 'Colors', is_array: true }
expose :kind, using: Kind, documentation: { type: 'ThisApi::Kind', desc: 'The kind of this something.' }
expose :kind2, using: Kind, documentation: { desc: 'Secondary kind.' }
expose :kind3, using: ThisApi::Entities::Kind, documentation: { desc: 'Tertiary kind.' }
expose :tags, using: ThisApi::Entities::Tag, documentation: { desc: 'Tags.', is_array: true }
expose :relation, using: ThisApi::Entities::Relation, documentation: { type: 'ThisApi::Relation', desc: 'A related model.' }
end
end

class ResponseModelApi < Grape::API
format :json
desc 'This returns something',
is_array: true,
http_codes: [{ code: 200, message: 'OK', model: Entities::Something }]
get '/something' do
something = OpenStruct.new text: 'something'
present something, with: Entities::Something
end

# something like an index action
desc 'This returns something or an error',
entity: Entities::Something,
http_codes: [
{ code: 200, message: 'OK', model: Entities::Something },
{ code: 403, message: 'Refused to return something', model: Entities::Error }
]
params do
optional :id, type: Integer
end
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

add_swagger_documentation
end
end
end
include_context 'this api'

def app
ThisApi::ResponseModelApi
Expand Down
20 changes: 20 additions & 0 deletions spec/grape-swagger/entity/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'
require_relative '../../../spec/support/shared_contexts/this_api'

describe GrapeSwagger::Entity::Parser do
include_context 'this api'

describe '#call' do
subject(:parsed_entity) { described_class.new(ThisApi::Entities::Something, endpoint).call }

context 'when no endpoint is passed' do
let(:endpoint) { nil }

it 'parses the model with the correct :using definition' do
expect(parsed_entity[:kind]['$ref']).to eq('#/definitions/Kind')
expect(parsed_entity[:kind2]['$ref']).to eq('#/definitions/Kind')
expect(parsed_entity[:kind3]['$ref']).to eq('#/definitions/Kind')
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
config.order = 'random'
config.seed = 40_834
end

Dir['spec/support/**/*.rb'].each { |file| require "./#{file}" }
65 changes: 65 additions & 0 deletions spec/support/shared_contexts/this_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
shared_context 'this api' do
before :all do
module ThisApi
module Entities
class Kind < Grape::Entity
expose :title, documentation: { type: 'string', desc: 'Title of the kind.' }
end

class Relation < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end
class Tag < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end
class Error < Grape::Entity
expose :code, documentation: { type: 'string', desc: 'Error code' }
expose :message, documentation: { type: 'string', desc: 'Error message' }
end

class Something < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
expose :colors, documentation: { type: 'string', desc: 'Colors', is_array: true }
expose :kind, using: Kind, documentation: { type: 'ThisApi::Kind', desc: 'The kind of this something.' }
expose :kind2, using: Kind, documentation: { desc: 'Secondary kind.' }
expose :kind3, using: ThisApi::Entities::Kind, documentation: { desc: 'Tertiary kind.' }
expose :tags, using: ThisApi::Entities::Tag, documentation: { desc: 'Tags.', is_array: true }
expose :relation, using: ThisApi::Entities::Relation, documentation: { type: 'ThisApi::Relation', desc: 'A related model.' }
end
end

class ResponseModelApi < Grape::API
format :json
desc 'This returns something',
is_array: true,
http_codes: [{ code: 200, message: 'OK', model: Entities::Something }]
get '/something' do
something = OpenStruct.new text: 'something'
present something, with: Entities::Something
end

# something like an index action
desc 'This returns something or an error',
entity: Entities::Something,
http_codes: [
{ code: 200, message: 'OK', model: Entities::Something },
{ code: 403, message: 'Refused to return something', model: Entities::Error }
]
params do
optional :id, type: Integer
end
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

add_swagger_documentation
end
end
end
end