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

Nested exposures #22

Merged
merged 3 commits into from
Mar 2, 2017
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
24 changes: 12 additions & 12 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-02-02 15:42:44 +0100 using RuboCop version 0.47.1.
# on 2017-02-26 22:17:25 +0200 using RuboCop version 0.47.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Offense count: 2
Metrics/AbcSize:
Max: 45
Max: 35

# Offense count: 8
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 27
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 126

# Offense count: 1
Metrics/CyclomaticComplexity:
Max: 14

# Offense count: 11
# Offense count: 14
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 120

# Offense count: 2
# Offense count: 3
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 30
Max: 27

# Offense count: 1
# Offense count: 2
Metrics/PerceivedComplexity:
Max: 16
Max: 15

# Offense count: 2
Style/Documentation:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#22](https://github.com/ruby-grape/grape-swagger-entity/pull/22): Nested exposures - [@Bugagazavr](https://github.com/Bugagazavr).
* Your contribution here.

#### Fixes
Expand Down
43 changes: 39 additions & 4 deletions lib/grape-swagger/entity/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ def call

private

def parse_grape_entity_params(params)
def parse_grape_entity_params(params, parent_model = nil)
return unless params

params.each_with_object({}) do |(entity_name, entity_options), memo|
next if entity_options.fetch(:documentation, {}).fetch(:in, nil).to_s == 'header'

entity_name = entity_options[:as] if entity_options[:as]
documentation = entity_options[:documentation]
model = model_from(entity_options)
entity_model = model_from(entity_options)

if model
name = endpoint.nil? ? model.to_s.demodulize : endpoint.send(:expose_params_from_model, model)
if entity_model
name = endpoint.nil? ? entity_model.to_s.demodulize : endpoint.send(:expose_params_from_model, entity_model)
memo[entity_name] = entity_model_type(name, entity_options)
elsif entity_options[:nesting]
memo[entity_name] = parse_nested(entity_name, entity_options, parent_model)
else
memo[entity_name] = data_type_from(entity_options)
next unless documentation
Expand Down Expand Up @@ -70,6 +72,39 @@ def model_from(entity_options)
model
end

def parse_nested(entity_name, entity_options, parent_model = nil)
nested_entity = if parent_model.nil?
model.root_exposures.find_by(entity_name)
else
parent_model.nested_exposures.find_by(entity_name)
end

params = nested_entity.nested_exposures.each_with_object({}) do |value, memo|
memo[value.attribute] = value.send(:options)
end

properties = parse_grape_entity_params(params, nested_entity)
is_a_collection = entity_options[:documentation].is_a?(Hash) &&
entity_options[:documentation][:type].to_s.casecmp('array').zero?

if is_a_collection
{
type: :array,
items: {
type: :object,
properties: properties
},
description: entity_options[:desc] || ''
}
else
{
type: :object,
properties: properties,
description: entity_options[:desc] || ''
}
end
end

def could_it_be_a_model?(value)
return false if value.nil?
direct_model_type?(value[:type]) || ambiguous_model_type?(value[:type])
Expand Down
59 changes: 58 additions & 1 deletion spec/grape-swagger/entities/response_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ class Tag < Grape::Entity
expose :name, documentation: { type: 'string', desc: 'Name' }
end

class Nested < Grape::Entity
expose :nested, documentation: { type: Hash, desc: 'Nested entity' } do
expose :some1, documentation: { type: 'String', desc: 'Nested some 1' }
expose :some2, documentation: { type: 'String', desc: 'Nested some 2' }
end
expose :deep_nested, documentation: { type: 'Object', desc: 'Deep nested entity' } do
expose :level_1, documentation: { type: 'Object', desc: 'More deepest nested entity' } do
expose :level_2, documentation: { type: 'String', desc: 'Level 2' }
end
end

expose :nested_array, documentation: { type: 'Array', desc: 'Nested array' } do
expose :id, documentation: { type: 'Integer', desc: 'Collection element id' }
expose :name, documentation: { type: 'String', desc: 'Collection element name' }
end
end

class SomeEntity < Grape::Entity
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
expose :kind, using: Kind, documentation: { type: 'TheseApi::Kind', desc: 'The kind of this something.' }
Expand All @@ -104,6 +121,7 @@ class SomeEntity < Grape::Entity
expose :tags, using: TheseApi::Entities::Tag, documentation: { desc: 'Tags.', is_array: true }
expose :relation, using: TheseApi::Entities::Relation, documentation: { type: 'TheseApi::Relation', desc: 'A related model.' }
expose :values, using: TheseApi::Entities::Values, documentation: { desc: 'Tertiary kind.' }
expose :nested, using: TheseApi::Entities::Nested, documentation: { desc: 'Nested object.' }
end
end

Expand Down Expand Up @@ -151,6 +169,44 @@ def app
expect(subject['Relation']).to eql(
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } }
)
expect(subject['Nested']).to eq(
'properties' => {
'nested' => {
'type' => 'object',
'properties' => {
'some1' => { 'type' => 'string', 'description' => 'Nested some 1' },
'some2' => { 'type' => 'string', 'description' => 'Nested some 2' }
},
'description' => 'Nested entity'
},
'deep_nested' => {
'type' => 'object',
'properties' => {
'level_1' => {
'type' => 'object',
'properties' => {
'level_2' => { 'type' => 'string', 'description' => 'Level 2' }
},
'description' => 'More deepest nested entity'
}
},
'description' => 'Deep nested entity'
},
'nested_array' => {
'type' => 'array',
'items' => {
'type' => 'object',
'properties' => {
'id' => { 'type' => 'integer', 'format' => 'int32', 'description' => 'Collection element id' },
'name' => { 'type' => 'string', 'description' => 'Collection element name' }
}
},
'description' => 'Nested array'
}
},
'type' => 'object'
)

expect(subject['SomeEntity']).to eql(
'type' => 'object',
'properties' => {
Expand All @@ -160,7 +216,8 @@ def app
'kind3' => { '$ref' => '#/definitions/Kind', 'description' => 'Tertiary kind.' },
'tags' => { 'type' => 'array', 'items' => { '$ref' => '#/definitions/Tag' }, 'description' => 'Tags.' },
'relation' => { '$ref' => '#/definitions/Relation', 'description' => 'A related model.' },
'values' => { '$ref' => '#/definitions/Values', 'description' => 'Tertiary kind.' }
'values' => { '$ref' => '#/definitions/Values', 'description' => 'Tertiary kind.' },
'nested' => { '$ref' => '#/definitions/Nested', 'description' => 'Nested object.' }
},
'description' => 'This returns something'
)
Expand Down