Skip to content

Commit c6f976a

Browse files
Sergey Verevkindblock
Sergey Verevkin
authored andcommitted
Added ability to load nested models recursively.
1 parent e951ce3 commit c6f976a

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

.rubocop_todo.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2014-11-10 15:12:48 -0500 using RuboCop version 0.27.0.
2+
# on 2014-11-20 15:23:00 +0200 using RuboCop version 0.27.0.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
66
# versions of RuboCop, may require this file to be generated again.
77

88
# Offense count: 8
99
Metrics/AbcSize:
10-
Max: 327
10+
Max: 330
1111

1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 387
15+
Max: 390
1616

1717
# Offense count: 4
1818
Metrics/CyclomaticComplexity:
1919
Max: 92
2020

21-
# Offense count: 196
21+
# Offense count: 203
2222
# Configuration parameters: AllowURI, URISchemes.
2323
Metrics/LineLength:
2424
Max: 254
2525

26-
# Offense count: 12
26+
# Offense count: 13
2727
# Configuration parameters: CountComments.
2828
Metrics/MethodLength:
29-
Max: 352
29+
Max: 355
3030

3131
# Offense count: 4
3232
Metrics/PerceivedComplexity:
@@ -36,7 +36,7 @@ Metrics/PerceivedComplexity:
3636
Style/ClassVars:
3737
Enabled: false
3838

39-
# Offense count: 62
39+
# Offense count: 68
4040
Style/Documentation:
4141
Enabled: false
4242

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [#169](https://github.com/tim-vandecasteele/grape-swagger/pull/169): Test against multiple versions of Grape - [@dblock](https://github.com/dblock).
77
* [#166](https://github.com/tim-vandecasteele/grape-swagger/pull/166): Ensure compatibility with Grape 0.8.0 or newer - [@dblock](https://github.com/dblock).
88
* [#174](https://github.com/tim-vandecasteele/grape-swagger/pull/172): Fix problem with using prefix name somewhere in api paths - [@grzesiek](https://github.com/grzesiek).
9+
* [#176](https://github.com/tim-vandecasteele/grape-swagger/pull/176): Added ability to load nested models recursively - [@sergey-verevkin](https://github.com/sergey-verevkin).
910

1011
* Your contribution here.
1112

lib/grape-swagger.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,15 @@ def models_with_included_presenters(models)
420420

421421
models.each do |model|
422422
# get model references from exposures with a documentation
423-
additional_models = model.exposures.map do |_, config|
423+
nested_models = model.exposures.map do |_, config|
424424
config[:using] if config.key?(:documentation)
425425
end.compact
426426

427+
# get all nested models recursively
428+
additional_models = nested_models.map do |nested_model|
429+
models_with_included_presenters([nested_model])
430+
end.flatten
431+
427432
all_models += additional_models
428433
end
429434

spec/api_models_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ class AliasedThing < Grape::Entity
5151
expose :something, as: :post, using: Entities::Something, documentation: { type: 'Something', desc: 'Reference to something.' }
5252
end
5353
end
54+
55+
module Entities
56+
class FourthLevel < Grape::Entity
57+
expose :text, documentation: { type: 'string' }
58+
end
59+
60+
class ThirdLevel < Grape::Entity
61+
expose :parts, using: Entities::FourthLevel, documentation: { type: 'FourthLevel' }
62+
end
63+
64+
class SecondLevel < Grape::Entity
65+
expose :parts, using: Entities::ThirdLevel, documentation: { type: 'ThirdLevel' }
66+
end
67+
68+
class FirstLevel < Grape::Entity
69+
expose :parts, using: Entities::SecondLevel, documentation: { type: 'SecondLevel' }
70+
end
71+
end
5472
end
5573

5674
def app
@@ -91,6 +109,16 @@ def app
91109
present something, with: Entities::AliasedThing
92110
end
93111

112+
desc 'This gets all nested entities.', entity: Entities::FirstLevel
113+
get '/nesting' do
114+
fourth_level = OpenStruct.new text: 'something'
115+
third_level = OpenStruct.new parts: [fourth_level]
116+
second_level = OpenStruct.new parts: [third_level]
117+
first_level = OpenStruct.new parts: [second_level]
118+
119+
present first_level, with: Entities::FirstLevel
120+
end
121+
94122
add_swagger_documentation
95123
end
96124
end
@@ -117,6 +145,7 @@ def app
117145
{ 'path' => '/somethingelse.{format}', 'description' => 'Operations about somethingelses' },
118146
{ 'path' => '/enum_description_in_entity.{format}', 'description' => 'Operations about enum_description_in_entities' },
119147
{ 'path' => '/aliasedthing.{format}', 'description' => 'Operations about aliasedthings' },
148+
{ 'path' => '/nesting.{format}', 'description' => 'Operations about nestings' },
120149
{ 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }
121150
]
122151
end
@@ -226,4 +255,11 @@ def app
226255
}
227256
)
228257
end
258+
259+
it 'includes all entities with four levels of nesting' do
260+
get '/swagger_doc/nesting.json'
261+
result = JSON.parse(last_response.body)
262+
263+
expect(result['models']).to include('FirstLevel', 'SecondLevel', 'ThirdLevel', 'FourthLevel')
264+
end
229265
end

0 commit comments

Comments
 (0)