Skip to content

Commit 9ad4a01

Browse files
MaximeRVYaka-momo
authored andcommitted
Add required with multiple present (ruby-grape#815)
* Add required with multiple present * Change Readme * Fix rubocop to 1.6.1 * Fix Changelog * Up rubocop
1 parent 0c93c0c commit 9ad4a01

File tree

10 files changed

+42
-4
lines changed

10 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#815](https://github.com/ruby-grape/grape-swagger/pull/815): Add required for multiple presents - [@MaximeRDY](https://github.com/MaximeRDY).
67

78
#### Fixes
89

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ You can also specify if the response is an array, with the `is_array` key:
13021302
desc 'Multiple response with array',
13031303
success: [
13041304
{ model: Entities::EnumValues, as: :gender },
1305-
{ model: Entities::Something, as: :somethings, is_array: true }
1305+
{ model: Entities::Something, as: :somethings, is_array: true, required: true }
13061306
]
13071307
end
13081308

@@ -1327,7 +1327,8 @@ The result will look like following:
13271327
"$ref":"#/definitions/Something"
13281328
}
13291329
}
1330-
}
1330+
},
1331+
"required": ["somethings"]
13311332
}
13321333
}
13331334
}

lib/grape-swagger/errors.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
module GrapeSwagger
44
module Errors
55
class UnregisteredParser < StandardError; end
6+
67
class SwaggerSpec < StandardError; end
8+
79
class SwaggerSpecDeprecated < SwaggerSpec
810
class << self
911
def tell!(what)

lib/grape-swagger/swagger_2/endpoint.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,18 @@ def tag_object(route, path)
270270
def build_memo_schema(memo, route, value, response_model, options)
271271
if memo[value[:code]][:schema] && value[:as]
272272
memo[value[:code]][:schema][:properties].merge!(build_reference(route, value, response_model, options))
273+
274+
if value[:required]
275+
memo[value[:code]][:schema][:required] ||= []
276+
memo[value[:code]][:schema][:required] << value[:as].to_s
277+
end
278+
273279
elsif value[:as]
274280
memo[value[:code]][:schema] = {
275281
type: :object,
276282
properties: build_reference(route, value, response_model, options)
277283
}
284+
memo[value[:code]][:schema][:required] = [value[:as].to_s] if value[:required]
278285
else
279286
memo[value[:code]][:schema] = build_reference(route, value, response_model, options)
280287
end
@@ -413,6 +420,7 @@ def success_code_from_entity(route, entity)
413420
default_code[:headers] = entity[:headers] if entity[:headers]
414421
default_code[:as] = entity[:as] if entity[:as]
415422
default_code[:is_array] = entity[:is_array] if entity[:is_array]
423+
default_code[:required] = entity[:required] if entity[:required]
416424
else
417425
default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
418426
default_code[:model] = entity if entity

spec/issues/776_multiple_presents_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
desc 'Get multiple presents',
1212
success: [
1313
{ model: Entities::EnumValues, as: :gender },
14-
{ model: Entities::Something, as: :somethings, is_array: true }
14+
{ model: Entities::Something, as: :somethings, is_array: true, required: true }
1515
]
1616

1717
get do
@@ -50,7 +50,10 @@
5050
'$ref' => '#/definitions/EnumValues'
5151
}
5252
},
53-
'type' => 'object'
53+
'type' => 'object',
54+
'required' => [
55+
'somethings'
56+
]
5457
})
5558
end
5659
end

spec/support/empty_model_parser.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# frozen_string_literal: true
22

3+
# rubocop:disable Lint/EmptyClass
34
class EmptyClass
45
end
6+
# rubocop:enable Lint/EmptyClass
57

68
module GrapeSwagger
79
class EmptyModelParser

spec/support/model_parsers/mock_parser.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,37 @@ def documentation
4040
end
4141

4242
class UseNestedWithAddress < OpenStruct; end
43+
4344
class TypedDefinition < OpenStruct; end
45+
4446
class UseItemResponseAsType < OpenStruct; end
47+
4548
class OtherItem < OpenStruct; end
49+
4650
class EnumValues < OpenStruct; end
51+
4752
class AliasedThing < OpenStruct; end
53+
4854
class FourthLevel < OpenStruct; end
55+
4956
class ThirdLevel < OpenStruct; end
57+
5058
class SecondLevel < OpenStruct; end
59+
5160
class FirstLevel < OpenStruct; end
61+
5262
class QueryInputElement < OpenStruct; end
63+
5364
class QueryInput < OpenStruct; end
65+
5466
class ApiError < OpenStruct; end
67+
5568
class SecondApiError < OpenStruct; end
69+
5670
class RecursiveModel < OpenStruct; end
71+
5772
class DocumentedHashAndArrayModel < OpenStruct; end
73+
5874
module NestedModule
5975
class ApiResponse < OpenStruct; end
6076
end

spec/support/namespace_tags.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
RSpec.shared_context 'namespace example' do
44
before :all do
55
module TheApi
6+
# rubocop:disable Lint/EmptyClass
67
class CustomType; end
8+
# rubocop:enable Lint/EmptyClass
79

810
class NamespaceApi < Grape::API
911
namespace :hudson do

spec/swagger_v2/inheritance_and_discriminator_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Cat < Pet
3333
}
3434
end
3535
end
36+
3637
class NameApi < Grape::API
3738
add_swagger_documentation models: [Entities::Pet, Entities::Cat]
3839
end

spec/swagger_v2/simple_mounted_api_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
describe 'a simple mounted api' do
66
before :all do
7+
# rubocop:disable Lint/EmptyClass
78
class CustomType; end
9+
# rubocop:enable Lint/EmptyClass
810

911
class SimpleMountedApi < Grape::API
1012
desc 'Document root'

0 commit comments

Comments
 (0)