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

combined global models with endpoint entities #128

Merged
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 @@ -11,6 +11,7 @@
* Added Rubocop, Ruby-style linter - [@dblock](https://github.com/dblock).
* [#126](https://github.com/tim-vandecasteele/grape-swagger/pull/126): Rewritten demo in the `test` folder with CORS enabled - [@dblock](https://github.com/dblock).
* [#127](https://github.com/tim-vandecasteele/grape-swagger/pull/127): Fixed `undefined method 'reject' for nil:NilClass` error for an invalid route, now returning 404 Not Found - [@dblock](https://github.com/dblock).
* [#128](https://github.com/tim-vandecasteele/grape-swagger/pull/128): Combine global models and endpoint entities - [@dspaeth-faber](https://github.com/dspaeth-faber).
* Your Contribution Here

### 0.7.2 (February 6, 2014)
Expand Down
8 changes: 3 additions & 5 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,9 @@ def self.setup(options)

http_codes = parse_http_codes(route.route_http_codes, models)

models << if @@models.present?
@@models
elsif route.route_entity.present?
route.route_entity
end
models << @@models if @@models.present?

models << route.route_entity if route.route_entity.present?

models = models_with_included_presenters(models.flatten.compact)

Expand Down
34 changes: 34 additions & 0 deletions spec/api_global_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module Some
class Thing < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
end

class CombinedThing < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
end
end
end
end
Expand All @@ -20,6 +24,14 @@ class Thing < Grape::Entity
present thing, with: Entities::Some::Thing
end

desc 'This gets combined thing.',
params: Entities::Some::CombinedThing.documentation,
entity: Entities::Some::CombinedThing
get '/combined_thing' do
thing = OpenStruct.new text: 'thing'
present thing, with: Entities::Some::CombinedThing
end

add_swagger_documentation models: [Entities::Some::Thing]
end
end
Expand All @@ -39,4 +51,26 @@ def app
}
})
end

it 'uses global models and route endpoint specific entities together' do
get '/swagger_doc/combined_thing.json'
json = JSON.parse(last_response.body)

expect(json['models']).to include(
'Some::Thing' => {
'id' => 'Some::Thing',
'properties' => {
'text' => { 'type' => 'string', 'description' => 'Content of something.' }
}
})

expect(json['models']).to include(
'Some::CombinedThing' => {
'id' => 'Some::CombinedThing',
'properties' => {
'text' => { 'type' => 'string', 'description' => 'Content of something.' }
}
})

end
end