gitlab-grape-swagger 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.github/dependabot.yml +20 -0
- data/.github/workflows/ci.yml +45 -0
- data/.gitignore +44 -0
- data/.gitlab-ci.yml +19 -0
- data/.rspec +3 -0
- data/.rubocop.yml +136 -0
- data/.rubocop_todo.yml +60 -0
- data/.ruby-gemset +1 -0
- data/CHANGELOG.md +671 -0
- data/CONTRIBUTING.md +126 -0
- data/Dangerfile +3 -0
- data/Gemfile +45 -0
- data/Gemfile.lock +249 -0
- data/LICENSE.txt +20 -0
- data/README.md +1772 -0
- data/RELEASING.md +82 -0
- data/Rakefile +20 -0
- data/UPGRADING.md +201 -0
- data/example/api/endpoints.rb +131 -0
- data/example/api/entities.rb +18 -0
- data/example/config.ru +42 -0
- data/example/example_requests.postman_collection +146 -0
- data/example/splines.png +0 -0
- data/example/swagger-example.png +0 -0
- data/grape-swagger.gemspec +23 -0
- data/lib/grape-swagger/doc_methods/build_model_definition.rb +68 -0
- data/lib/grape-swagger/doc_methods/data_type.rb +110 -0
- data/lib/grape-swagger/doc_methods/extensions.rb +101 -0
- data/lib/grape-swagger/doc_methods/file_params.rb +17 -0
- data/lib/grape-swagger/doc_methods/format_data.rb +53 -0
- data/lib/grape-swagger/doc_methods/headers.rb +20 -0
- data/lib/grape-swagger/doc_methods/move_params.rb +209 -0
- data/lib/grape-swagger/doc_methods/operation_id.rb +32 -0
- data/lib/grape-swagger/doc_methods/optional_object.rb +30 -0
- data/lib/grape-swagger/doc_methods/parse_params.rb +190 -0
- data/lib/grape-swagger/doc_methods/path_string.rb +52 -0
- data/lib/grape-swagger/doc_methods/produces_consumes.rb +15 -0
- data/lib/grape-swagger/doc_methods/status_codes.rb +21 -0
- data/lib/grape-swagger/doc_methods/tag_name_description.rb +34 -0
- data/lib/grape-swagger/doc_methods/version.rb +20 -0
- data/lib/grape-swagger/doc_methods.rb +142 -0
- data/lib/grape-swagger/endpoint/params_parser.rb +76 -0
- data/lib/grape-swagger/endpoint.rb +476 -0
- data/lib/grape-swagger/errors.rb +17 -0
- data/lib/grape-swagger/instance.rb +7 -0
- data/lib/grape-swagger/model_parsers.rb +42 -0
- data/lib/grape-swagger/rake/oapi_tasks.rb +135 -0
- data/lib/grape-swagger/version.rb +5 -0
- data/lib/grape-swagger.rb +174 -0
- data/spec/issues/267_nested_namespaces.rb +55 -0
- data/spec/issues/403_versions_spec.rb +124 -0
- data/spec/issues/427_entity_as_string_spec.rb +39 -0
- data/spec/issues/430_entity_definitions_spec.rb +94 -0
- data/spec/issues/532_allow_custom_format_spec.rb +42 -0
- data/spec/issues/533_specify_status_code_spec.rb +78 -0
- data/spec/issues/537_enum_values_spec.rb +50 -0
- data/spec/issues/539_array_post_body_spec.rb +65 -0
- data/spec/issues/542_array_of_type_in_post_body_spec.rb +46 -0
- data/spec/issues/553_align_array_put_post_params_spec.rb +152 -0
- data/spec/issues/572_array_post_body_spec.rb +51 -0
- data/spec/issues/579_align_put_post_parameters_spec.rb +185 -0
- data/spec/issues/582_file_response_spec.rb +55 -0
- data/spec/issues/587_range_parameter_delimited_by_dash_spec.rb +26 -0
- data/spec/issues/605_root_route_documentation_spec.rb +23 -0
- data/spec/issues/650_params_array_spec.rb +65 -0
- data/spec/issues/677_consumes_produces_add_swagger_documentation_options_spec.rb +100 -0
- data/spec/issues/680_keep_204_error_schemas_spec.rb +55 -0
- data/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb +62 -0
- data/spec/issues/751_deeply_nested_objects_spec.rb +190 -0
- data/spec/issues/776_multiple_presents_spec.rb +59 -0
- data/spec/issues/784_extensions_on_params_spec.rb +42 -0
- data/spec/issues/809_utf8_routes_spec.rb +55 -0
- data/spec/issues/832_array_hash_float_decimal_spec.rb +114 -0
- data/spec/issues/847_route_param_options_spec.rb +37 -0
- data/spec/issues/873_wildcard_segments_path_parameters_spec.rb +28 -0
- data/spec/issues/878_optional_path_segments_spec.rb +29 -0
- data/spec/issues/881_handle_file_params_spec.rb +38 -0
- data/spec/issues/883_query_array_parameter_spec.rb +46 -0
- data/spec/issues/884_dont_document_non_schema_examples_spec.rb +49 -0
- data/spec/issues/887_prevent_duplicate_operation_ids_spec.rb +35 -0
- data/spec/lib/data_type_spec.rb +111 -0
- data/spec/lib/endpoint/params_parser_spec.rb +124 -0
- data/spec/lib/endpoint_spec.rb +153 -0
- data/spec/lib/extensions_spec.rb +185 -0
- data/spec/lib/format_data_spec.rb +115 -0
- data/spec/lib/model_parsers_spec.rb +104 -0
- data/spec/lib/move_params_spec.rb +444 -0
- data/spec/lib/oapi_tasks_spec.rb +163 -0
- data/spec/lib/operation_id_spec.rb +55 -0
- data/spec/lib/optional_object_spec.rb +47 -0
- data/spec/lib/parse_params_spec.rb +68 -0
- data/spec/lib/path_string_spec.rb +101 -0
- data/spec/lib/produces_consumes_spec.rb +116 -0
- data/spec/lib/tag_name_description_spec.rb +80 -0
- data/spec/lib/version_spec.rb +28 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/support/empty_model_parser.rb +23 -0
- data/spec/support/grape_version.rb +13 -0
- data/spec/support/mock_parser.rb +23 -0
- data/spec/support/model_parsers/entity_parser.rb +334 -0
- data/spec/support/model_parsers/mock_parser.rb +346 -0
- data/spec/support/model_parsers/representable_parser.rb +406 -0
- data/spec/support/namespace_tags.rb +93 -0
- data/spec/support/the_paths_definitions.rb +109 -0
- data/spec/swagger_v2/api_documentation_spec.rb +42 -0
- data/spec/swagger_v2/api_swagger_v2_additional_properties_spec.rb +83 -0
- data/spec/swagger_v2/api_swagger_v2_body_definitions_spec.rb +48 -0
- data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +36 -0
- data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +79 -0
- data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +145 -0
- data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +137 -0
- data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +56 -0
- data/spec/swagger_v2/api_swagger_v2_hash_and_array_spec.rb +64 -0
- data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +58 -0
- data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +57 -0
- data/spec/swagger_v2/api_swagger_v2_hide_param_spec.rb +109 -0
- data/spec/swagger_v2/api_swagger_v2_ignore_defaults_spec.rb +48 -0
- data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +153 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +355 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +217 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +247 -0
- data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +80 -0
- data/spec/swagger_v2/api_swagger_v2_response_spec.rb +147 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_examples_spec.rb +135 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_headers_spec.rb +216 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_models_spec.rb +53 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_root_spec.rb +153 -0
- data/spec/swagger_v2/api_swagger_v2_spec.rb +245 -0
- data/spec/swagger_v2/api_swagger_v2_status_codes_spec.rb +93 -0
- data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +90 -0
- data/spec/swagger_v2/boolean_params_spec.rb +38 -0
- data/spec/swagger_v2/default_api_spec.rb +175 -0
- data/spec/swagger_v2/deprecated_field_spec.rb +25 -0
- data/spec/swagger_v2/description_not_initialized_spec.rb +39 -0
- data/spec/swagger_v2/endpoint_versioned_path_spec.rb +130 -0
- data/spec/swagger_v2/errors_spec.rb +77 -0
- data/spec/swagger_v2/float_api_spec.rb +36 -0
- data/spec/swagger_v2/form_params_spec.rb +76 -0
- data/spec/swagger_v2/grape-swagger_spec.rb +17 -0
- data/spec/swagger_v2/guarded_endpoint_spec.rb +162 -0
- data/spec/swagger_v2/hide_api_spec.rb +147 -0
- data/spec/swagger_v2/host_spec.rb +43 -0
- data/spec/swagger_v2/inheritance_and_discriminator_spec.rb +57 -0
- data/spec/swagger_v2/mount_override_api_spec.rb +58 -0
- data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
- data/spec/swagger_v2/namespace_tags_prefix_spec.rb +122 -0
- data/spec/swagger_v2/namespace_tags_spec.rb +78 -0
- data/spec/swagger_v2/namespaced_api_spec.rb +121 -0
- data/spec/swagger_v2/nicknamed_api_spec.rb +25 -0
- data/spec/swagger_v2/operation_id_api_spec.rb +27 -0
- data/spec/swagger_v2/param_multi_type_spec.rb +82 -0
- data/spec/swagger_v2/param_type_spec.rb +95 -0
- data/spec/swagger_v2/param_values_spec.rb +180 -0
- data/spec/swagger_v2/params_array_collection_format_spec.rb +105 -0
- data/spec/swagger_v2/params_array_spec.rb +225 -0
- data/spec/swagger_v2/params_example_spec.rb +38 -0
- data/spec/swagger_v2/params_hash_spec.rb +77 -0
- data/spec/swagger_v2/params_nested_spec.rb +92 -0
- data/spec/swagger_v2/parent_less_namespace_spec.rb +32 -0
- data/spec/swagger_v2/reference_entity_spec.rb +129 -0
- data/spec/swagger_v2/security_requirement_spec.rb +46 -0
- data/spec/swagger_v2/simple_mounted_api_spec.rb +332 -0
- data/spec/version_spec.rb +10 -0
- metadata +225 -0
@@ -0,0 +1,180 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
# require 'grape_version'
|
5
|
+
|
6
|
+
describe 'Convert values to enum or Range' do
|
7
|
+
def app
|
8
|
+
Class.new(Grape::API) do
|
9
|
+
format :json
|
10
|
+
|
11
|
+
desc 'plain_array' do
|
12
|
+
consumes ['application/x-www-form-urlencoded']
|
13
|
+
end
|
14
|
+
params do
|
15
|
+
requires :letter, type: String, values: %w[a b c]
|
16
|
+
end
|
17
|
+
post :plain_array do
|
18
|
+
{ message: 'hi' }
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'array_in_proc' do
|
22
|
+
consumes ['application/x-www-form-urlencoded']
|
23
|
+
end
|
24
|
+
params do
|
25
|
+
requires :letter, type: String, values: proc { %w[d e f] }
|
26
|
+
end
|
27
|
+
post :array_in_proc do
|
28
|
+
{ message: 'hi' }
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'range_letter' do
|
32
|
+
consumes ['application/x-www-form-urlencoded']
|
33
|
+
end
|
34
|
+
params do
|
35
|
+
requires :letter, type: String, values: 'a'..'z'
|
36
|
+
end
|
37
|
+
post :range_letter do
|
38
|
+
{ message: 'hi' }
|
39
|
+
end
|
40
|
+
|
41
|
+
desc 'range_integer' do
|
42
|
+
consumes ['application/x-www-form-urlencoded']
|
43
|
+
end
|
44
|
+
params do
|
45
|
+
requires :integer, type: Integer, values: -5..5
|
46
|
+
end
|
47
|
+
post :range_integer do
|
48
|
+
{ message: 'hi' }
|
49
|
+
end
|
50
|
+
|
51
|
+
add_swagger_documentation
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def first_parameter_info(request)
|
56
|
+
get "/swagger_doc/#{request}"
|
57
|
+
expect(last_response.status).to eq 200
|
58
|
+
body = JSON.parse last_response.body
|
59
|
+
body['paths']["/#{request}"]['post']['parameters']
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'Plain array values' do
|
63
|
+
subject(:plain_array) { first_parameter_info('plain_array') }
|
64
|
+
it 'has values as array in enum' do
|
65
|
+
expect(plain_array).to eq [{
|
66
|
+
'in' => 'formData',
|
67
|
+
'name' => 'letter',
|
68
|
+
'type' => 'string',
|
69
|
+
'required' => true,
|
70
|
+
'enum' => %w[a b c]
|
71
|
+
}]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'Array in proc values' do
|
76
|
+
subject(:array_in_proc) { first_parameter_info('array_in_proc') }
|
77
|
+
|
78
|
+
it 'has proc returned values as array in enum' do
|
79
|
+
expect(array_in_proc).to eq [{
|
80
|
+
'in' => 'formData',
|
81
|
+
'name' => 'letter',
|
82
|
+
'type' => 'string',
|
83
|
+
'required' => true,
|
84
|
+
'enum' => %w[d e f]
|
85
|
+
}]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'Range values' do
|
90
|
+
subject(:range_letter) { first_parameter_info('range_letter') }
|
91
|
+
|
92
|
+
it 'has letter range values' do
|
93
|
+
expect(range_letter).to eq [{
|
94
|
+
'in' => 'formData',
|
95
|
+
'name' => 'letter',
|
96
|
+
'type' => 'string',
|
97
|
+
'required' => true
|
98
|
+
}]
|
99
|
+
end
|
100
|
+
|
101
|
+
subject(:range_integer) { first_parameter_info('range_integer') }
|
102
|
+
|
103
|
+
it 'has integer range values' do
|
104
|
+
expect(range_integer).to eq [{
|
105
|
+
'in' => 'formData',
|
106
|
+
'name' => 'integer',
|
107
|
+
'type' => 'integer',
|
108
|
+
'required' => true,
|
109
|
+
'format' => 'int32',
|
110
|
+
'minimum' => -5,
|
111
|
+
'maximum' => 5
|
112
|
+
}]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe 'Convert values to enum for float range and not arrays inside a proc', if: GrapeVersion.satisfy?('>= 0.11.0') do
|
118
|
+
def app
|
119
|
+
Class.new(Grape::API) do
|
120
|
+
format :json
|
121
|
+
|
122
|
+
desc 'non_array_in_proc' do
|
123
|
+
consumes ['application/x-www-form-urlencoded']
|
124
|
+
end
|
125
|
+
params do
|
126
|
+
requires :letter, type: String, values: proc { 'string' }
|
127
|
+
end
|
128
|
+
post :non_array_in_proc do
|
129
|
+
{ message: 'hi' }
|
130
|
+
end
|
131
|
+
|
132
|
+
desc 'range_float' do
|
133
|
+
consumes ['application/x-www-form-urlencoded']
|
134
|
+
end
|
135
|
+
params do
|
136
|
+
requires :float, type: Float, values: -5.0..5.0
|
137
|
+
end
|
138
|
+
post :range_float do
|
139
|
+
{ message: 'hi' }
|
140
|
+
end
|
141
|
+
|
142
|
+
add_swagger_documentation
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def first_parameter_info(request)
|
147
|
+
get "/swagger_doc/#{request}"
|
148
|
+
expect(last_response.status).to eq 200
|
149
|
+
body = JSON.parse last_response.body
|
150
|
+
body['paths']["/#{request}"]['post']['parameters']
|
151
|
+
end
|
152
|
+
|
153
|
+
context 'Non array in proc values' do
|
154
|
+
subject(:non_array_in_proc) { first_parameter_info('non_array_in_proc') }
|
155
|
+
|
156
|
+
it 'has proc returned value as string in enum' do
|
157
|
+
expect(non_array_in_proc).to eq [{
|
158
|
+
'in' => 'formData',
|
159
|
+
'name' => 'letter',
|
160
|
+
'type' => 'string',
|
161
|
+
'required' => true,
|
162
|
+
'enum' => %w[string]
|
163
|
+
}]
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'Range values' do
|
168
|
+
subject(:range_float) { first_parameter_info('range_float') }
|
169
|
+
|
170
|
+
it 'has float range values as string' do
|
171
|
+
expect(range_float).to eq [{
|
172
|
+
'in' => 'formData',
|
173
|
+
'name' => 'float',
|
174
|
+
'type' => 'number',
|
175
|
+
'required' => true,
|
176
|
+
'format' => 'float'
|
177
|
+
}]
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Group Array Params, using collection format' do
|
6
|
+
def app
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
format :json
|
9
|
+
|
10
|
+
params do
|
11
|
+
optional :array_of_strings, type: Array[String], desc: 'array in csv collection format'
|
12
|
+
end
|
13
|
+
|
14
|
+
get '/array_of_strings_without_collection_format' do
|
15
|
+
{ 'declared_params' => declared(params) }
|
16
|
+
end
|
17
|
+
|
18
|
+
params do
|
19
|
+
optional :array_of_strings, type: Array[String], desc: 'array in multi collection format', documentation: { collectionFormat: 'multi' }
|
20
|
+
end
|
21
|
+
|
22
|
+
get '/array_of_strings_multi_collection_format' do
|
23
|
+
{ 'declared_params' => declared(params) }
|
24
|
+
end
|
25
|
+
|
26
|
+
params do
|
27
|
+
optional :array_of_strings, type: Array[String], documentation: { collectionFormat: 'foo' }
|
28
|
+
end
|
29
|
+
|
30
|
+
get '/array_of_strings_invalid_collection_format' do
|
31
|
+
{ 'declared_params' => declared(params) }
|
32
|
+
end
|
33
|
+
|
34
|
+
params do
|
35
|
+
optional :array_of_strings, type: Array[String], desc: 'array in brackets collection format', documentation: { collectionFormat: 'brackets' }
|
36
|
+
end
|
37
|
+
|
38
|
+
get '/array_of_strings_brackets_collection_format' do
|
39
|
+
{ 'declared_params' => declared(params) }
|
40
|
+
end
|
41
|
+
|
42
|
+
add_swagger_documentation
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'documentation for array parameter in default csv collectionFormat' do
|
47
|
+
subject do
|
48
|
+
get '/swagger_doc/array_of_strings_without_collection_format'
|
49
|
+
JSON.parse(last_response.body)
|
50
|
+
end
|
51
|
+
|
52
|
+
specify do
|
53
|
+
expect(subject['paths']['/array_of_strings_without_collection_format']['get']['parameters']).to eql(
|
54
|
+
[
|
55
|
+
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'description' => 'array in csv collection format' }
|
56
|
+
]
|
57
|
+
)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'documentation for array parameters in multi collectionFormat set from documentation' do
|
62
|
+
subject do
|
63
|
+
get '/swagger_doc/array_of_strings_multi_collection_format'
|
64
|
+
JSON.parse(last_response.body)
|
65
|
+
end
|
66
|
+
|
67
|
+
specify do
|
68
|
+
expect(subject['paths']['/array_of_strings_multi_collection_format']['get']['parameters']).to eql(
|
69
|
+
[
|
70
|
+
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'multi', 'description' => 'array in multi collection format' }
|
71
|
+
]
|
72
|
+
)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'documentation for array parameters in brackets collectionFormat set from documentation' do
|
77
|
+
subject do
|
78
|
+
get '/swagger_doc/array_of_strings_brackets_collection_format'
|
79
|
+
JSON.parse(last_response.body)
|
80
|
+
end
|
81
|
+
|
82
|
+
specify do
|
83
|
+
expect(subject['paths']['/array_of_strings_brackets_collection_format']['get']['parameters']).to eql(
|
84
|
+
[
|
85
|
+
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'brackets', 'description' => 'array in brackets collection format' }
|
86
|
+
]
|
87
|
+
)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'documentation for array parameters with collectionFormat set to invalid option' do
|
92
|
+
subject do
|
93
|
+
get '/swagger_doc/array_of_strings_invalid_collection_format'
|
94
|
+
JSON.parse(last_response.body)
|
95
|
+
end
|
96
|
+
|
97
|
+
specify do
|
98
|
+
expect(subject['paths']['/array_of_strings_invalid_collection_format']['get']['parameters']).to eql(
|
99
|
+
[
|
100
|
+
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false }
|
101
|
+
]
|
102
|
+
)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,225 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Group Params as Array' do
|
6
|
+
include_context "#{MODEL_PARSER} swagger example"
|
7
|
+
|
8
|
+
[true, false].each do |array_use_braces|
|
9
|
+
context "when array_use_braces option is set to #{array_use_braces}" do
|
10
|
+
let(:braces) { array_use_braces ? '[]' : '' }
|
11
|
+
|
12
|
+
let(:app) do
|
13
|
+
Class.new(Grape::API) do
|
14
|
+
format :json
|
15
|
+
|
16
|
+
desc 'groups' do
|
17
|
+
consumes ['application/x-www-form-urlencoded']
|
18
|
+
end
|
19
|
+
params do
|
20
|
+
requires :required_group, type: Array do
|
21
|
+
requires :required_param_1
|
22
|
+
requires :required_param_2
|
23
|
+
end
|
24
|
+
end
|
25
|
+
post '/groups' do
|
26
|
+
{ 'declared_params' => declared(params) }
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'type_given' do
|
30
|
+
consumes ['application/x-www-form-urlencoded']
|
31
|
+
end
|
32
|
+
params do
|
33
|
+
requires :typed_group, type: Array do
|
34
|
+
requires :id, type: Integer, desc: 'integer given'
|
35
|
+
requires :name, type: String, desc: 'string given'
|
36
|
+
optional :email, type: String, desc: 'email given'
|
37
|
+
optional :others, type: Integer, values: [1, 2, 3]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
post '/type_given' do
|
41
|
+
{ 'declared_params' => declared(params) }
|
42
|
+
end
|
43
|
+
|
44
|
+
# as body parameters it would be interpreted a bit different,
|
45
|
+
# cause it could not be distinguished anymore, so this would be translated to one array,
|
46
|
+
# see also next example for the difference
|
47
|
+
desc 'array_of_type' do
|
48
|
+
consumes ['application/x-www-form-urlencoded']
|
49
|
+
end
|
50
|
+
|
51
|
+
params do
|
52
|
+
requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'nested array of strings' }
|
53
|
+
requires :array_of_integer, type: Array[Integer], documentation: { param_type: 'body', desc: 'nested array of integers' }
|
54
|
+
end
|
55
|
+
|
56
|
+
post '/array_of_type' do
|
57
|
+
{ 'declared_params' => declared(params) }
|
58
|
+
end
|
59
|
+
|
60
|
+
desc 'object_and_array' do
|
61
|
+
consumes ['application/x-www-form-urlencoded']
|
62
|
+
end
|
63
|
+
|
64
|
+
params do
|
65
|
+
requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'array of strings' }
|
66
|
+
requires :integer_value, type: Integer, documentation: { param_type: 'body', desc: 'integer value' }
|
67
|
+
end
|
68
|
+
|
69
|
+
post '/object_and_array' do
|
70
|
+
{ 'declared_params' => declared(params) }
|
71
|
+
end
|
72
|
+
|
73
|
+
desc 'array_of_type_in_form' do
|
74
|
+
consumes ['application/x-www-form-urlencoded']
|
75
|
+
end
|
76
|
+
|
77
|
+
params do
|
78
|
+
requires :array_of_string, type: Array[String]
|
79
|
+
requires :array_of_integer, type: Array[Integer]
|
80
|
+
end
|
81
|
+
|
82
|
+
post '/array_of_type_in_form' do
|
83
|
+
{ 'declared_params' => declared(params) }
|
84
|
+
end
|
85
|
+
|
86
|
+
desc 'array_of_entities' do
|
87
|
+
consumes ['application/x-www-form-urlencoded']
|
88
|
+
end
|
89
|
+
|
90
|
+
params do
|
91
|
+
requires :array_of_entities, type: Array[Entities::ApiError]
|
92
|
+
end
|
93
|
+
|
94
|
+
post '/array_of_entities' do
|
95
|
+
{ 'declared_params' => declared(params) }
|
96
|
+
end
|
97
|
+
|
98
|
+
add_swagger_documentation array_use_braces: array_use_braces
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'retrieves the documentation for grouped parameters' do
|
103
|
+
subject do
|
104
|
+
get '/swagger_doc/groups'
|
105
|
+
JSON.parse(last_response.body)
|
106
|
+
end
|
107
|
+
|
108
|
+
specify do
|
109
|
+
expect(subject['paths']['/groups']['post']['parameters']).to eql(
|
110
|
+
[
|
111
|
+
{ 'in' => 'formData', 'name' => "required_group#{braces}[required_param_1]", 'required' => true, 'type' => 'array', 'items' => { 'type' => 'string' } },
|
112
|
+
{ 'in' => 'formData', 'name' => "required_group#{braces}[required_param_2]", 'required' => true, 'type' => 'array', 'items' => { 'type' => 'string' } }
|
113
|
+
]
|
114
|
+
)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe 'retrieves the documentation for typed group parameters' do
|
119
|
+
subject do
|
120
|
+
get '/swagger_doc/type_given'
|
121
|
+
JSON.parse(last_response.body)
|
122
|
+
end
|
123
|
+
|
124
|
+
specify do
|
125
|
+
expect(subject['paths']['/type_given']['post']['parameters']).to eql(
|
126
|
+
[
|
127
|
+
{ 'in' => 'formData', 'name' => "typed_group#{braces}[id]", 'description' => 'integer given', 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true },
|
128
|
+
{ 'in' => 'formData', 'name' => "typed_group#{braces}[name]", 'description' => 'string given', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
|
129
|
+
{ 'in' => 'formData', 'name' => "typed_group#{braces}[email]", 'description' => 'email given', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false },
|
130
|
+
{ 'in' => 'formData', 'name' => "typed_group#{braces}[others]", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32', 'enum' => [1, 2, 3] }, 'required' => false }
|
131
|
+
]
|
132
|
+
)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe 'retrieves the documentation for parameters that are arrays of primitive types' do
|
137
|
+
subject do
|
138
|
+
get '/swagger_doc/array_of_type'
|
139
|
+
JSON.parse(last_response.body)
|
140
|
+
end
|
141
|
+
|
142
|
+
specify do
|
143
|
+
expect(subject['definitions']['postArrayOfType']['type']).to eql 'array'
|
144
|
+
expect(subject['definitions']['postArrayOfType']['items']).to eql(
|
145
|
+
'type' => 'object',
|
146
|
+
'properties' => {
|
147
|
+
'array_of_string' => {
|
148
|
+
'items' => { 'type' => 'string' }, 'type' => 'array', 'description' => 'nested array of strings'
|
149
|
+
},
|
150
|
+
'array_of_integer' => {
|
151
|
+
'items' => { 'type' => 'integer', 'format' => 'int32' }, 'type' => 'array', 'description' => 'nested array of integers'
|
152
|
+
}
|
153
|
+
},
|
154
|
+
'required' => %w[array_of_string array_of_integer]
|
155
|
+
)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
describe 'documentation for simple and array parameters' do
|
160
|
+
subject do
|
161
|
+
get '/swagger_doc/object_and_array'
|
162
|
+
JSON.parse(last_response.body)
|
163
|
+
end
|
164
|
+
|
165
|
+
specify do
|
166
|
+
expect(subject['definitions']['postObjectAndArray']['type']).to eql 'object'
|
167
|
+
expect(subject['definitions']['postObjectAndArray']['properties']).to eql(
|
168
|
+
'array_of_string' => {
|
169
|
+
'type' => 'array',
|
170
|
+
'description' => 'array of strings',
|
171
|
+
'items' => {
|
172
|
+
'type' => 'string'
|
173
|
+
}
|
174
|
+
},
|
175
|
+
'integer_value' => {
|
176
|
+
'type' => 'integer', 'format' => 'int32', 'description' => 'integer value'
|
177
|
+
}
|
178
|
+
)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
describe 'retrieves the documentation for typed group parameters' do
|
183
|
+
subject do
|
184
|
+
get '/swagger_doc/array_of_type_in_form'
|
185
|
+
JSON.parse(last_response.body)
|
186
|
+
end
|
187
|
+
|
188
|
+
specify do
|
189
|
+
expect(subject['paths']['/array_of_type_in_form']['post']['parameters']).to eql(
|
190
|
+
[
|
191
|
+
{ 'in' => 'formData', 'name' => "array_of_string#{braces}", 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
|
192
|
+
{ 'in' => 'formData', 'name' => "array_of_integer#{braces}", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true }
|
193
|
+
]
|
194
|
+
)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe 'documentation for entity array parameters' do
|
199
|
+
let(:parameters) do
|
200
|
+
[
|
201
|
+
{
|
202
|
+
'in' => 'formData',
|
203
|
+
'name' => "array_of_entities#{braces}",
|
204
|
+
'type' => 'array',
|
205
|
+
'items' => {
|
206
|
+
'$ref' => '#/definitions/ApiError'
|
207
|
+
},
|
208
|
+
'required' => true
|
209
|
+
}
|
210
|
+
]
|
211
|
+
end
|
212
|
+
|
213
|
+
subject do
|
214
|
+
get '/swagger_doc/array_of_entities'
|
215
|
+
JSON.parse(last_response.body)
|
216
|
+
end
|
217
|
+
|
218
|
+
specify do
|
219
|
+
expect(subject['definitions']['ApiError']).not_to be_blank
|
220
|
+
expect(subject['paths']['/array_of_entities']['post']['parameters']).to eql(parameters)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Param example' do
|
6
|
+
def app
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
format :json
|
9
|
+
|
10
|
+
params do
|
11
|
+
requires :id, type: Integer, documentation: { example: 123 }
|
12
|
+
optional :name, type: String, documentation: { example: 'Person' }
|
13
|
+
optional :obj, type: 'Object', documentation: { example: { 'foo' => 'bar' } }
|
14
|
+
end
|
15
|
+
|
16
|
+
post '/endpoint_with_examples' do
|
17
|
+
{ 'declared_params' => declared(params) }
|
18
|
+
end
|
19
|
+
|
20
|
+
add_swagger_documentation
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'documentation with parameter examples' do
|
25
|
+
subject do
|
26
|
+
get '/swagger_doc/endpoint_with_examples'
|
27
|
+
JSON.parse(last_response.body)
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:parameters) { subject['definitions']['postEndpointWithExamples']['properties'] }
|
31
|
+
|
32
|
+
specify do
|
33
|
+
expect(parameters).to eql(
|
34
|
+
{ 'id' => { 'type' => 'integer', 'format' => 'int32', 'example' => 123 }, 'name' => { 'type' => 'string', 'example' => 'Person' }, 'obj' => { 'type' => 'Object', 'example' => { 'foo' => 'bar' } } }
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Group Params as Hash' do
|
6
|
+
def app
|
7
|
+
Class.new(Grape::API) do
|
8
|
+
format :json
|
9
|
+
|
10
|
+
desc 'use groups' do
|
11
|
+
consumes ['application/x-www-form-urlencoded']
|
12
|
+
end
|
13
|
+
params do
|
14
|
+
requires :required_group, type: Hash do
|
15
|
+
requires :required_param_1
|
16
|
+
requires :required_param_2
|
17
|
+
end
|
18
|
+
end
|
19
|
+
post '/use_groups' do
|
20
|
+
{ 'declared_params' => declared(params) }
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'use given type' do
|
24
|
+
consumes ['application/x-www-form-urlencoded']
|
25
|
+
end
|
26
|
+
params do
|
27
|
+
requires :typed_group, type: Hash do
|
28
|
+
requires :id, type: Integer, desc: 'integer given'
|
29
|
+
requires :name, type: String, desc: 'string given'
|
30
|
+
optional :email, type: String, desc: 'email given'
|
31
|
+
optional :others, type: Integer, values: [1, 2, 3]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
post '/use_given_type' do
|
35
|
+
{ 'declared_params' => declared(params) }
|
36
|
+
end
|
37
|
+
|
38
|
+
add_swagger_documentation
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'grouped parameters' do
|
43
|
+
subject do
|
44
|
+
get '/swagger_doc/use_groups'
|
45
|
+
JSON.parse(last_response.body)
|
46
|
+
end
|
47
|
+
|
48
|
+
specify do
|
49
|
+
expect(subject['paths']['/use_groups']['post']).to include('parameters')
|
50
|
+
expect(subject['paths']['/use_groups']['post']['parameters']).to eql(
|
51
|
+
[
|
52
|
+
{ 'in' => 'formData', 'name' => 'required_group[required_param_1]', 'type' => 'string', 'required' => true },
|
53
|
+
{ 'in' => 'formData', 'name' => 'required_group[required_param_2]', 'type' => 'string', 'required' => true }
|
54
|
+
]
|
55
|
+
)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'grouped parameters with given type' do
|
60
|
+
subject do
|
61
|
+
get '/swagger_doc/use_given_type'
|
62
|
+
JSON.parse(last_response.body)
|
63
|
+
end
|
64
|
+
|
65
|
+
specify do
|
66
|
+
expect(subject['paths']['/use_given_type']['post']).to include('parameters')
|
67
|
+
expect(subject['paths']['/use_given_type']['post']['parameters']).to eql(
|
68
|
+
[
|
69
|
+
{ 'in' => 'formData', 'name' => 'typed_group[id]', 'description' => 'integer given', 'type' => 'integer', 'required' => true, 'format' => 'int32' },
|
70
|
+
{ 'in' => 'formData', 'name' => 'typed_group[name]', 'description' => 'string given', 'type' => 'string', 'required' => true },
|
71
|
+
{ 'in' => 'formData', 'name' => 'typed_group[email]', 'description' => 'email given', 'type' => 'string', 'required' => false },
|
72
|
+
{ 'in' => 'formData', 'name' => 'typed_group[others]', 'type' => 'integer', 'required' => false, 'format' => 'int32', 'enum' => [1, 2, 3] }
|
73
|
+
]
|
74
|
+
)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|