grape-swagger 0.11.0 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -1
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +14 -22
- data/.travis.yml +7 -4
- data/CHANGELOG.md +53 -26
- data/Gemfile +1 -1
- data/README.md +414 -327
- data/RELEASING.md +3 -4
- data/example/api/endpoints.rb +132 -0
- data/example/api/entities.rb +18 -0
- data/example/config.ru +36 -2
- data/example/example_requests.postman_collection +146 -0
- data/example/swagger-example.png +0 -0
- data/grape-swagger.gemspec +9 -6
- data/lib/grape-swagger.rb +69 -99
- data/lib/grape-swagger/doc_methods.rb +69 -544
- data/lib/grape-swagger/doc_methods/data_type.rb +77 -0
- data/lib/grape-swagger/doc_methods/extensions.rb +75 -0
- data/lib/grape-swagger/doc_methods/move_params.rb +153 -0
- data/lib/grape-swagger/doc_methods/operation_id.rb +27 -0
- data/lib/grape-swagger/doc_methods/optional_object.rb +15 -0
- data/lib/grape-swagger/doc_methods/parse_params.rb +113 -0
- data/lib/grape-swagger/doc_methods/path_string.rb +29 -0
- data/lib/grape-swagger/doc_methods/produces_consumes.rb +12 -0
- data/lib/grape-swagger/doc_methods/status_codes.rb +17 -0
- data/lib/grape-swagger/doc_methods/tag_name_description.rb +26 -0
- data/lib/grape-swagger/endpoint.rb +317 -0
- data/lib/grape-swagger/version.rb +1 -1
- data/spec/lib/data_type_spec.rb +57 -0
- data/spec/lib/endpoint_spec.rb +6 -0
- data/spec/lib/extensions_spec.rb +127 -0
- data/spec/lib/move_params_spec.rb +298 -0
- data/spec/lib/operation_id_spec.rb +24 -0
- data/spec/lib/optional_object_spec.rb +40 -0
- data/spec/lib/path_string_spec.rb +38 -0
- data/spec/lib/produces_consumes_spec.rb +98 -0
- data/spec/markdown/kramdown_adapter_spec.rb +2 -9
- data/spec/markdown/redcarpet_adapter_spec.rb +2 -16
- data/spec/spec_helper.rb +7 -13
- data/spec/support/api_swagger_v2_result.rb +204 -0
- data/spec/support/namespace_tags.rb +73 -0
- data/spec/support/the_api_entities.rb +52 -0
- data/spec/support/the_paths_definitions.rb +94 -0
- data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +32 -0
- data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +151 -0
- data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +109 -0
- data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +124 -0
- data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +51 -0
- data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +44 -0
- data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +56 -0
- data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +146 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +197 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +151 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +217 -0
- data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +64 -0
- data/spec/swagger_v2/api_swagger_v2_response_spec.rb +184 -0
- data/spec/swagger_v2/api_swagger_v2_spec.rb +207 -0
- data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +121 -0
- data/spec/{boolean_params_spec.rb → swagger_v2/boolean_params_spec.rb} +2 -2
- data/spec/{default_api_spec.rb → swagger_v2/default_api_spec.rb} +40 -36
- data/spec/swagger_v2/description_not_initialized.rb +39 -0
- data/spec/{float_api_spec.rb → swagger_v2/float_api_spec.rb} +2 -2
- data/spec/{form_params_spec.rb → swagger_v2/form_params_spec.rb} +9 -9
- data/spec/{grape-swagger_spec.rb → swagger_v2/grape-swagger_spec.rb} +0 -0
- data/spec/swagger_v2/hide_api_spec.rb +131 -0
- data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
- data/spec/swagger_v2/namespace_tags_prefix_spec.rb +84 -0
- data/spec/swagger_v2/namespace_tags_spec.rb +76 -0
- data/spec/{namespaced_api_spec.rb → swagger_v2/namespaced_api_spec.rb} +6 -26
- data/spec/{param_type_spec.rb → swagger_v2/param_type_spec.rb} +10 -8
- data/spec/{param_values_spec.rb → swagger_v2/param_values_spec.rb} +52 -22
- data/spec/swagger_v2/params_array_spec.rb +63 -0
- data/spec/swagger_v2/params_hash_spec.rb +65 -0
- data/spec/swagger_v2/params_nested_spec.rb +63 -0
- data/spec/{reference_entity.rb → swagger_v2/reference_entity.rb} +18 -23
- data/spec/swagger_v2/response_model_spec.rb +212 -0
- data/spec/swagger_v2/simple_mounted_api_spec.rb +264 -0
- metadata +175 -90
- data/example/api.rb +0 -66
- data/lib/grape-swagger/markdown.rb +0 -23
- data/spec/api_description_spec.rb +0 -43
- data/spec/api_global_models_spec.rb +0 -77
- data/spec/api_models_spec.rb +0 -364
- data/spec/api_paths_spec.rb +0 -128
- data/spec/api_root_spec.rb +0 -30
- data/spec/api_with_nil_types.rb +0 -50
- data/spec/api_with_path_versioning_spec.rb +0 -33
- data/spec/api_with_prefix_and_namespace_spec.rb +0 -32
- data/spec/api_with_standalone_namespace_spec.rb +0 -215
- data/spec/array_entity_spec.rb +0 -34
- data/spec/array_params_spec.rb +0 -85
- data/spec/grape-swagger_helper_spec.rb +0 -152
- data/spec/group_params_spec.rb +0 -31
- data/spec/hash_params_spec.rb +0 -30
- data/spec/hide_api_spec.rb +0 -124
- data/spec/i18n_spec.rb +0 -364
- data/spec/markdown/markdown_spec.rb +0 -27
- data/spec/mounted_target_class_spec.rb +0 -63
- data/spec/mutually_exclusive_spec.rb +0 -36
- data/spec/non_default_api_spec.rb +0 -733
- data/spec/response_model_spec.rb +0 -121
- data/spec/simple_mounted_api_spec.rb +0 -213
- data/spec/support/i18n_helper.rb +0 -8
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'format, content_type' do
|
4
|
+
include_context "the api entities"
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
module TheApi
|
8
|
+
class ProducesApi < Grape::API
|
9
|
+
format :json
|
10
|
+
|
11
|
+
desc 'This uses json (default) for produces',
|
12
|
+
failure: [{code: 400, model: Entities::ApiError}],
|
13
|
+
entity: Entities::UseResponse
|
14
|
+
get '/use_default' do
|
15
|
+
{ "declared_params" => declared(params) }
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'This uses formats for produces',
|
19
|
+
failure: [{code: 400, model: Entities::ApiError}],
|
20
|
+
formats: [:xml, :binary, "application/vdns"],
|
21
|
+
entity: Entities::UseResponse
|
22
|
+
get '/use_formats' do
|
23
|
+
{ "declared_params" => declared(params) }
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'This uses content_types for produces',
|
27
|
+
failure: [{code: 400, model: Entities::ApiError}],
|
28
|
+
content_types: [:xml, :binary, "application/vdns"],
|
29
|
+
entity: Entities::UseResponse
|
30
|
+
get '/use_content_types' do
|
31
|
+
{ "declared_params" => declared(params) }
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'This uses produces for produces',
|
35
|
+
failure: [{code: 400, model: Entities::ApiError}],
|
36
|
+
produces: [:xml, :binary, "application/vdns"],
|
37
|
+
entity: Entities::UseResponse
|
38
|
+
get '/use_produces' do
|
39
|
+
{ "declared_params" => declared(params) }
|
40
|
+
end
|
41
|
+
|
42
|
+
desc 'This uses produces for produces',
|
43
|
+
failure: [{code: 400, model: Entities::ApiError}],
|
44
|
+
consumes: ["application/www_url_encoded"],
|
45
|
+
entity: Entities::UseResponse
|
46
|
+
post '/use_consumes' do
|
47
|
+
{ "declared_params" => declared(params) }
|
48
|
+
end
|
49
|
+
|
50
|
+
add_swagger_documentation
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def app
|
56
|
+
TheApi::ProducesApi
|
57
|
+
end
|
58
|
+
|
59
|
+
let(:produced) {[
|
60
|
+
'application/xml',
|
61
|
+
'application/octet-stream',
|
62
|
+
'application/vdns'
|
63
|
+
]}
|
64
|
+
|
65
|
+
describe "formats" do
|
66
|
+
subject do
|
67
|
+
get '/swagger_doc/use_default'
|
68
|
+
JSON.parse(last_response.body)
|
69
|
+
end
|
70
|
+
|
71
|
+
specify do
|
72
|
+
expect(subject['paths']['/use_default']['get']).to include('produces')
|
73
|
+
expect(subject['paths']['/use_default']['get']['produces']).to eql(['application/json'])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "formats" do
|
78
|
+
subject do
|
79
|
+
get '/swagger_doc/use_formats'
|
80
|
+
JSON.parse(last_response.body)
|
81
|
+
end
|
82
|
+
|
83
|
+
specify do
|
84
|
+
expect(subject['paths']['/use_formats']['get']).to include('produces')
|
85
|
+
expect(subject['paths']['/use_formats']['get']['produces']).to eql(produced)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "content types" do
|
90
|
+
subject do
|
91
|
+
get '/swagger_doc/use_content_types'
|
92
|
+
JSON.parse(last_response.body)
|
93
|
+
end
|
94
|
+
|
95
|
+
specify do
|
96
|
+
expect(subject['paths']['/use_content_types']['get']).to include('produces')
|
97
|
+
expect(subject['paths']['/use_content_types']['get']['produces']).to eql(produced)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "produces" do
|
102
|
+
subject do
|
103
|
+
get '/swagger_doc/use_produces'
|
104
|
+
JSON.parse(last_response.body)
|
105
|
+
end
|
106
|
+
|
107
|
+
specify do
|
108
|
+
expect(subject['paths']['/use_produces']['get']).to include('produces')
|
109
|
+
expect(subject['paths']['/use_produces']['get']['produces']).to eql(produced)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "consumes" do
|
114
|
+
subject do
|
115
|
+
get '/swagger_doc/use_consumes'
|
116
|
+
JSON.parse(last_response.body)
|
117
|
+
end
|
118
|
+
|
119
|
+
specify do
|
120
|
+
expect(subject['paths']['/use_consumes']['post']).to include('consumes')
|
121
|
+
expect(subject['paths']['/use_consumes']['post']['consumes']).to eql ["application/www_url_encoded"]
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'global configuration stuff' do
|
4
|
+
|
5
|
+
before :all do
|
6
|
+
module TheApi
|
7
|
+
class ConfigurationApi < Grape::API
|
8
|
+
format :json
|
9
|
+
version 'v3', using: :path
|
10
|
+
|
11
|
+
desc 'This returns something',
|
12
|
+
failure: [{code: 400, message: 'NotFound'}]
|
13
|
+
params do
|
14
|
+
requires :foo, type: Integer
|
15
|
+
end
|
16
|
+
get :configuration do
|
17
|
+
{ "declared_params" => declared(params) }
|
18
|
+
end
|
19
|
+
|
20
|
+
add_swagger_documentation format: :json,
|
21
|
+
api_version: '23',
|
22
|
+
schemes: 'https',
|
23
|
+
host: -> { 'another.host.com' },
|
24
|
+
base_path: -> { 'somewhere/over/the/rainbow' },
|
25
|
+
mount_path: 'documentation',
|
26
|
+
add_base_path: true,
|
27
|
+
add_version: true
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def app
|
34
|
+
TheApi::ConfigurationApi
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "shows documentation paths" do
|
38
|
+
subject do
|
39
|
+
get '/v3/documentation'
|
40
|
+
JSON.parse(last_response.body)
|
41
|
+
end
|
42
|
+
|
43
|
+
specify do
|
44
|
+
expect(subject['host']).to eql 'another.host.com'
|
45
|
+
expect(subject['basePath']).to eql 'somewhere/over/the/rainbow'
|
46
|
+
expect(subject['paths'].keys.first).to eql '/somewhere/over/the/rainbow/v3/configuration'
|
47
|
+
expect(subject['schemes']).to eql ['https']
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'headers' do
|
4
|
+
include_context "the api entities"
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
module TheApi
|
8
|
+
class HeadersApi < Grape::API
|
9
|
+
format :json
|
10
|
+
|
11
|
+
desc 'This returns something',
|
12
|
+
failure: [{code: 400, model: Entities::ApiError}],
|
13
|
+
headers: {
|
14
|
+
"X-Rate-Limit-Limit" => {
|
15
|
+
"description" => "The number of allowed requests in the current period",
|
16
|
+
"type" => "integer"
|
17
|
+
}},
|
18
|
+
|
19
|
+
entity: Entities::UseResponse
|
20
|
+
get '/use_headers' do
|
21
|
+
{ "declared_params" => declared(params) }
|
22
|
+
end
|
23
|
+
|
24
|
+
add_swagger_documentation
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def app
|
30
|
+
TheApi::HeadersApi
|
31
|
+
end
|
32
|
+
|
33
|
+
subject do
|
34
|
+
get '/swagger_doc'
|
35
|
+
JSON.parse(last_response.body)
|
36
|
+
end
|
37
|
+
|
38
|
+
specify do
|
39
|
+
expect(subject['paths']['/use_headers']['get']).to include('headers')
|
40
|
+
expect(subject['paths']['/use_headers']['get']['headers']).to eql({
|
41
|
+
"X-Rate-Limit-Limit"=>{"description"=>"The number of allowed requests in the current period", "type"=>"integer"}
|
42
|
+
})
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'hide documentation path' do
|
4
|
+
include_context "the api entities"
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
module TheApi
|
8
|
+
class HideDocumentationApi < Grape::API
|
9
|
+
format :json
|
10
|
+
|
11
|
+
desc 'This returns something',
|
12
|
+
params: Entities::UseResponse.documentation,
|
13
|
+
failure: [{code: 400, message: 'NotFound', model: Entities::ApiError}]
|
14
|
+
params do
|
15
|
+
requires :foo, type: Integer
|
16
|
+
end
|
17
|
+
get '/params_response' do
|
18
|
+
{ "declared_params" => declared(params) }
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'This returns something',
|
22
|
+
entity: Entities::UseResponse,
|
23
|
+
failure: [{code: 400, message: 'NotFound', model: Entities::ApiError}]
|
24
|
+
get '/entity_response' do
|
25
|
+
{ "declared_params" => declared(params) }
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'This returns something',
|
29
|
+
failure: [{code: 400, message: 'NotFound', model: Entities::ApiError}]
|
30
|
+
get '/present_response' do
|
31
|
+
foo = OpenStruct.new id: 1, name: 'bar'
|
32
|
+
something = OpenStruct.new description: 'something', item: foo
|
33
|
+
present :somethings, something, with: Entities::UseResponse
|
34
|
+
end
|
35
|
+
|
36
|
+
add_swagger_documentation hide_documentation_path: false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def app
|
42
|
+
TheApi::HideDocumentationApi
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "shows documentation paths" do
|
46
|
+
subject do
|
47
|
+
get '/swagger_doc'
|
48
|
+
JSON.parse(last_response.body)
|
49
|
+
end
|
50
|
+
|
51
|
+
specify do
|
52
|
+
expect(subject['paths'].keys).to include '/swagger_doc', '/swagger_doc/{name}'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'swagger spec v2.0' do
|
4
|
+
describe 'mounted APIs' do
|
5
|
+
include_context "swagger example"
|
6
|
+
|
7
|
+
def app
|
8
|
+
Class.new(Grape::API) do
|
9
|
+
format :json
|
10
|
+
|
11
|
+
# Thing stuff
|
12
|
+
desc 'This gets Things.' do
|
13
|
+
params Entities::Something.documentation
|
14
|
+
http_codes [ { code: 401, message: 'Unauthorized', model: Entities::ApiError } ]
|
15
|
+
end
|
16
|
+
get '/thing' do
|
17
|
+
something = OpenStruct.new text: 'something'
|
18
|
+
present something, with: Entities::Something
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'This gets Things.' do
|
22
|
+
http_codes [
|
23
|
+
{ code: 200, message: 'get Horses', model: Entities::Something },
|
24
|
+
{ code: 401, message: 'HorsesOutError', model: Entities::ApiError }
|
25
|
+
]
|
26
|
+
end
|
27
|
+
get '/thing2' do
|
28
|
+
something = OpenStruct.new text: 'something'
|
29
|
+
present something, with: Entities::Something
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'This gets Thing.' do
|
33
|
+
http_codes [ { code: 200, message: 'getting a single thing' }, { code: 401, message: 'Unauthorized' } ]
|
34
|
+
end
|
35
|
+
params do
|
36
|
+
requires :id, type: Integer
|
37
|
+
end
|
38
|
+
get '/thing/:id' do
|
39
|
+
something = OpenStruct.new text: 'something'
|
40
|
+
present something, with: Entities::Something
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'This creates Thing.',
|
44
|
+
success: Entities::Something
|
45
|
+
params do
|
46
|
+
requires :text, type: String, documentation: { type: 'string', desc: 'Content of something.' }
|
47
|
+
requires :links, type: Array, documentation: { type: 'link', is_array: true }
|
48
|
+
end
|
49
|
+
post '/thing', http_codes: [ { code: 422, message: 'Unprocessible Entity' } ] do
|
50
|
+
something = OpenStruct.new text: 'something'
|
51
|
+
present something, with: Entities::Something
|
52
|
+
end
|
53
|
+
|
54
|
+
desc 'This updates Thing.',
|
55
|
+
success: Entities::Something
|
56
|
+
params do
|
57
|
+
requires :id, type: Integer
|
58
|
+
optional :text, type: String, desc: 'Content of something.'
|
59
|
+
optional :links, type: Array, documentation: { type: 'link', is_array: true }
|
60
|
+
end
|
61
|
+
put '/thing/:id' do
|
62
|
+
something = OpenStruct.new text: 'something'
|
63
|
+
present something, with: Entities::Something
|
64
|
+
end
|
65
|
+
|
66
|
+
desc 'This deletes Thing.',
|
67
|
+
entity: Entities::Something
|
68
|
+
params do
|
69
|
+
requires :id, type: Integer
|
70
|
+
end
|
71
|
+
delete '/thing/:id' do
|
72
|
+
something = OpenStruct.new text: 'something'
|
73
|
+
present something, with: Entities::Something
|
74
|
+
end
|
75
|
+
|
76
|
+
desc 'dummy route.',
|
77
|
+
failure: [{ code: 401, message: 'Unauthorized' }]
|
78
|
+
params do
|
79
|
+
requires :id, type: Integer
|
80
|
+
end
|
81
|
+
delete '/dummy/:id' do
|
82
|
+
end
|
83
|
+
|
84
|
+
namespace :other_thing do
|
85
|
+
desc 'nested route inside namespace',
|
86
|
+
entity: Entities::QueryInput,
|
87
|
+
x: {
|
88
|
+
'amazon-apigateway-auth' => {type: 'none'},
|
89
|
+
'amazon-apigateway-integration' => {type: 'aws', uri: 'foo_bar_uri', httpMethod: 'get'}
|
90
|
+
}
|
91
|
+
|
92
|
+
|
93
|
+
params do
|
94
|
+
requires :elements, documentation: {
|
95
|
+
type: 'QueryInputElement',
|
96
|
+
desc: 'Set of configuration',
|
97
|
+
param_type: 'body',
|
98
|
+
is_array: true,
|
99
|
+
required: true
|
100
|
+
}
|
101
|
+
end
|
102
|
+
get '/:elements' do
|
103
|
+
present something, with: Entities::QueryInput
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
version 'v3', using: :path
|
108
|
+
add_swagger_documentation hide_format: true,
|
109
|
+
base_path: '/api',
|
110
|
+
info: {
|
111
|
+
title: "The API title to be displayed on the API homepage.",
|
112
|
+
description: "A description of the API.",
|
113
|
+
contact_name: "Contact name",
|
114
|
+
contact_email: "Contact@email.com",
|
115
|
+
contact_url: "Contact URL",
|
116
|
+
license: "The name of the license.",
|
117
|
+
license_url: "www.The-URL-of-the-license.org",
|
118
|
+
terms_of_service_url: "www.The-URL-of-the-terms-and-service.com",
|
119
|
+
}
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
mounted_paths.each do |expected_path|
|
124
|
+
describe "documents only #{expected_path} paths" do
|
125
|
+
let(:mount_path) { "/v3/swagger_doc#{expected_path}" }
|
126
|
+
subject do
|
127
|
+
get mount_path
|
128
|
+
JSON.parse(last_response.body)['paths']
|
129
|
+
end
|
130
|
+
|
131
|
+
specify do
|
132
|
+
unexpected_paths = mounted_paths - [expected_path]
|
133
|
+
subject.keys.each do |path|
|
134
|
+
unexpected_paths.each do |unexpected_path|
|
135
|
+
expect(path).not_to start_with unexpected_path
|
136
|
+
end
|
137
|
+
expect(path).to start_with(expected_path).or include(expected_path)
|
138
|
+
expect(subject[path]).to eql swagger_json['paths'][path]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,197 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'setting of param type, such as `query`, `path`, `formData`, `body`, `header`' do
|
4
|
+
include_context "the api entities"
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
module TheApi
|
8
|
+
class NestedBodyParamTypeApi < Grape::API
|
9
|
+
namespace :simple_nested_params do
|
10
|
+
desc 'post in body with nested parameters',
|
11
|
+
detail: 'more details description',
|
12
|
+
success: TheApi::Entities::UseNestedWithAddress
|
13
|
+
params do
|
14
|
+
requires :name, type: String, documentation: { desc: 'name', in: 'body' }
|
15
|
+
optional :address, type: Hash do
|
16
|
+
requires :street, type: String, documentation: { desc: 'street', in: 'body' }
|
17
|
+
requires :postcode, type: String, documentation: { desc: 'postcode', in: 'body' }
|
18
|
+
requires :city, type: String, documentation: { desc: 'city', in: 'body' }
|
19
|
+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
post '/in_body' do
|
24
|
+
{ "declared_params" => declared(params) }
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'put in body with nested parameters',
|
28
|
+
detail: 'more details description',
|
29
|
+
success: TheApi::Entities::UseNestedWithAddress
|
30
|
+
params do
|
31
|
+
requires :id, type: Integer
|
32
|
+
optional :name, type: String, documentation: { desc: 'name', in: 'body' }
|
33
|
+
optional :address, type: Hash do
|
34
|
+
optional :street, type: String, documentation: { desc: 'street', in: 'body' }
|
35
|
+
optional :postcode, type: String, documentation: { desc: 'postcode', in: 'formData' }
|
36
|
+
optional :city, type: String, documentation: { desc: 'city', in: 'body' }
|
37
|
+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
put '/in_body/:id' do
|
42
|
+
{ "declared_params" => declared(params) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
namespace :multiple_nested_params do
|
47
|
+
desc 'put in body with multiple nested parameters',
|
48
|
+
success: TheApi::Entities::UseNestedWithAddress
|
49
|
+
params do
|
50
|
+
requires :id, type: Integer
|
51
|
+
optional :name, type: String, documentation: { desc: 'name', in: 'body' }
|
52
|
+
optional :address, type: Hash do
|
53
|
+
optional :street, type: String, documentation: { desc: 'street', in: 'body' }
|
54
|
+
requires :postcode, type: String, documentation: { desc: 'postcode', in: 'formData' }
|
55
|
+
optional :city, type: String, documentation: { desc: 'city', in: 'body' }
|
56
|
+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
|
57
|
+
end
|
58
|
+
optional :delivery_address, type: Hash do
|
59
|
+
optional :street, type: String, documentation: { desc: 'street', in: 'body' }
|
60
|
+
optional :postcode, type: String, documentation: { desc: 'postcode', in: 'formData' }
|
61
|
+
optional :city, type: String, documentation: { desc: 'city', in: 'body' }
|
62
|
+
optional :country, type: String, documentation: { desc: 'country', in: 'body' }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
put '/in_body/:id' do
|
67
|
+
{ "declared_params" => declared(params) }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
add_swagger_documentation
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def app
|
77
|
+
TheApi::NestedBodyParamTypeApi
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'nested body parameters given' do
|
81
|
+
subject do
|
82
|
+
get '/swagger_doc/simple_nested_params'
|
83
|
+
JSON.parse(last_response.body)
|
84
|
+
end
|
85
|
+
|
86
|
+
specify do
|
87
|
+
expect(subject['paths']['/simple_nested_params/in_body']['post']['parameters']).to eql([{
|
88
|
+
"name"=>"UseNestedWithAddress",
|
89
|
+
"in"=>"body",
|
90
|
+
"required"=>true,
|
91
|
+
"schema"=>{"$ref"=>"#/definitions/postRequestUseNestedWithAddress"}
|
92
|
+
}])
|
93
|
+
end
|
94
|
+
|
95
|
+
specify do
|
96
|
+
expect(subject['definitions']['postRequestUseNestedWithAddress']).to eql({
|
97
|
+
"description"=>"post in body with nested parameters\n more details description",
|
98
|
+
"type"=>"object",
|
99
|
+
"properties"=>{
|
100
|
+
"address"=>{"$ref"=>"#/definitions/postRequestUseNestedWithAddressAddress"},
|
101
|
+
"name"=>{"type"=>"string", "description"=>"name"}
|
102
|
+
},
|
103
|
+
"required"=>["name"]
|
104
|
+
})
|
105
|
+
expect(subject['definitions']['postRequestUseNestedWithAddressAddress']).to eql({
|
106
|
+
"description"=>"postRequestUseNestedWithAddress - address",
|
107
|
+
"type"=>"object",
|
108
|
+
"properties"=>{
|
109
|
+
"street"=>{"type"=>"string", "description"=>"street"},
|
110
|
+
"postcode"=>{"type"=>"string", "description"=>"postcode"},
|
111
|
+
"city"=>{"type"=>"string", "description"=>"city"},
|
112
|
+
"country"=>{"type"=>"string", "description"=>"country"}
|
113
|
+
},
|
114
|
+
"required"=>["street", "postcode", "city"]
|
115
|
+
})
|
116
|
+
end
|
117
|
+
|
118
|
+
specify do
|
119
|
+
expect(subject['paths']['/simple_nested_params/in_body/{id}']['put']['parameters']).to eql([
|
120
|
+
{"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "format"=>"int32", "required"=>true},
|
121
|
+
{
|
122
|
+
"name"=>"UseNestedWithAddress",
|
123
|
+
"in"=>"body",
|
124
|
+
"required"=>true,
|
125
|
+
"schema"=>{"$ref"=>"#/definitions/putRequestUseNestedWithAddress"}
|
126
|
+
}
|
127
|
+
])
|
128
|
+
end
|
129
|
+
|
130
|
+
specify do
|
131
|
+
expect(subject['definitions']['putRequestUseNestedWithAddress']).to eql({
|
132
|
+
"description" => "put in body with nested parameters\n more details description",
|
133
|
+
"type"=>"object",
|
134
|
+
"properties"=>{
|
135
|
+
"address"=>{"$ref"=>"#/definitions/putRequestUseNestedWithAddressAddress"},
|
136
|
+
"id"=>{"type"=>"integer", "format"=>"int32", "readOnly"=>true},
|
137
|
+
"name"=>{"type"=>"string", "description"=>"name"}
|
138
|
+
}
|
139
|
+
})
|
140
|
+
expect(subject['definitions']['putRequestUseNestedWithAddressAddress']).to eql({
|
141
|
+
"description"=>"putRequestUseNestedWithAddress - address",
|
142
|
+
"type"=>"object",
|
143
|
+
"properties"=>{
|
144
|
+
"street"=>{"type"=>"string", "description"=>"street"},
|
145
|
+
"postcode"=>{"type"=>"string", "description"=>"postcode"},
|
146
|
+
"city"=>{"type"=>"string", "description"=>"city"},
|
147
|
+
"country"=>{"type"=>"string", "description"=>"country"}}
|
148
|
+
})
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe 'multiple nested body parameters given' do
|
153
|
+
subject do
|
154
|
+
get '/swagger_doc/multiple_nested_params'
|
155
|
+
JSON.parse(last_response.body)
|
156
|
+
end
|
157
|
+
|
158
|
+
specify do
|
159
|
+
expect(subject['paths']['/multiple_nested_params/in_body/{id}']['put']['parameters']).to eql([
|
160
|
+
{"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "format"=>"int32", "required"=>true},
|
161
|
+
{"name"=>"UseNestedWithAddress", "in"=>"body", "required"=>true, "schema"=>{"$ref"=>"#/definitions/putRequestUseNestedWithAddress"}}
|
162
|
+
])
|
163
|
+
end
|
164
|
+
|
165
|
+
specify do
|
166
|
+
expect(subject['definitions']['putRequestUseNestedWithAddress']).to eql({
|
167
|
+
"description" => "put in body with multiple nested parameters",
|
168
|
+
"type"=>"object",
|
169
|
+
"properties"=>{
|
170
|
+
"address"=>{"$ref"=>"#/definitions/putRequestUseNestedWithAddressAddress"},
|
171
|
+
"delivery_address"=>{"$ref"=>"#/definitions/putRequestUseNestedWithAddressDeliveryAddress"},
|
172
|
+
"id"=>{"type"=>"integer", "format"=>"int32", "readOnly"=>true},
|
173
|
+
"name"=>{"type"=>"string", "description"=>"name"}
|
174
|
+
}})
|
175
|
+
expect(subject['definitions']['putRequestUseNestedWithAddressAddress']).to eql({
|
176
|
+
"description" => "putRequestUseNestedWithAddress - address",
|
177
|
+
"type"=>"object",
|
178
|
+
"properties"=>{
|
179
|
+
"street"=>{"type"=>"string", "description"=>"street"},
|
180
|
+
"postcode"=>{"type"=>"string", "description"=>"postcode"},
|
181
|
+
"city"=>{"type"=>"string", "description"=>"city"},
|
182
|
+
"country"=>{"type"=>"string", "description"=>"country"}
|
183
|
+
},
|
184
|
+
"required"=>["postcode"]
|
185
|
+
})
|
186
|
+
expect(subject['definitions']['putRequestUseNestedWithAddressDeliveryAddress']).to eql({
|
187
|
+
"description" => "putRequestUseNestedWithAddress - delivery_address",
|
188
|
+
"type"=>"object",
|
189
|
+
"properties"=>{
|
190
|
+
"street"=>{"type"=>"string", "description"=>"street"},
|
191
|
+
"postcode"=>{"type"=>"string", "description"=>"postcode"},
|
192
|
+
"city"=>{"type"=>"string", "description"=>"city"},
|
193
|
+
"country"=>{"type"=>"string", "description"=>"country"}
|
194
|
+
}})
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|