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

Id equal to model name when root is used in Entity #251

Closed
wants to merge 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#### Fixes

* [#251](https://github.com/tim-vandecasteele/grape-swagger/pull/251): Fixed model id equal to model name when root existing in entities - [@aitortomas](https://github.com/aitortomas).
* [#232](https://github.com/tim-vandecasteele/grape-swagger/pull/232): Fixed missing raw array params - [@u2](https://github.com/u2).
* [#234](https://github.com/tim-vandecasteele/grape-swagger/pull/234): Fixed range :values with float - [@azhi](https://github.com/azhi).
* [#225](https://github.com/tim-vandecasteele/grape-swagger/pull/225): Fixed `param_type` to have it read from parameter's documentation hash - [@zsxking](https://github.com/zsxking).
Expand Down
4 changes: 2 additions & 2 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def parse_entity_name(model)
def parse_entity_models(models)
result = {}
models.each do |model|
name = parse_entity_name(model)
name = (model.instance_variable_get(:@root) || parse_entity_name(model))
properties = {}
required = []

Expand Down Expand Up @@ -383,7 +383,7 @@ def parse_entity_models(models)
end

result[name] = {
id: model.instance_variable_get(:@root) || name,
id: name,
properties: properties
}
result[name].merge!(required: required) unless required.empty?
Expand Down
20 changes: 20 additions & 0 deletions spec/api_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class QueryResult < Grape::Entity
end
end

module Entities
class ThingWithRoot < Grape::Entity
root 'things', 'thing'
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
end
end

def app
Class.new(Grape::API) do
format :json
Expand Down Expand Up @@ -149,6 +156,12 @@ def app
present result, with: Entities::QueryResult
end

desc 'This gets thing_with_root.', entity: Entities::ThingWithRoot
get '/thing_with_root' do
thing = OpenStruct.new text: 'thing'
present thing, with: Entities::ThingWithRoot
end

add_swagger_documentation
end
end
Expand Down Expand Up @@ -177,6 +190,7 @@ def app
{ 'path' => '/aliasedthing.{format}', 'description' => 'Operations about aliasedthings' },
{ 'path' => '/nesting.{format}', 'description' => 'Operations about nestings' },
{ 'path' => '/multiple_entities.{format}', 'description' => 'Operations about multiple_entities' },
{ 'path' => '/thing_with_root.{format}', 'description' => 'Operations about thing_with_roots' },
{ 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }
]
end
Expand Down Expand Up @@ -290,4 +304,10 @@ def app

expect(result['models']).to include('QueryInput', 'QueryInputElement', 'QueryResult')
end

it 'includes an id equal to the model name' do
get '/swagger_doc/thing_with_root'
result = JSON.parse(last_response.body)
expect(result['models']['thing']['id']).to eq('thing')
end
end