grape-swagger 0.20.3 → 0.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +10 -10
- data/.travis.yml +8 -3
- data/CHANGELOG.md +27 -0
- data/Gemfile +2 -0
- data/README.md +111 -12
- data/UPGRADING.md +9 -0
- data/grape-swagger.gemspec +1 -3
- data/lib/grape-swagger.rb +8 -1
- data/lib/grape-swagger/doc_methods/optional_object.rb +14 -2
- data/lib/grape-swagger/doc_methods/parse_params.rb +3 -4
- data/lib/grape-swagger/doc_methods/path_string.rb +4 -3
- data/lib/grape-swagger/endpoint.rb +25 -55
- data/lib/grape-swagger/errors.rb +3 -0
- data/lib/grape-swagger/grape/route.rb +2 -1
- data/lib/grape-swagger/model_parsers.rb +33 -0
- data/lib/grape-swagger/version.rb +1 -1
- data/spec/issues/403_versions_spec.rb +20 -4
- data/spec/lib/model_parsers_spec.rb +102 -0
- data/spec/lib/optional_object_spec.rb +15 -11
- data/spec/lib/path_string_spec.rb +72 -18
- data/spec/lib/produces_consumes_spec.rb +10 -5
- data/spec/spec_helper.rb +4 -2
- data/spec/support/empty_model_parser.rb +20 -0
- data/spec/support/mock_parser.rb +22 -0
- data/spec/support/model_parsers/entity_parser.rb +325 -0
- data/spec/support/{api_swagger_v2_result.rb → model_parsers/mock_parser.rb} +186 -60
- data/spec/support/model_parsers/representable_parser.rb +394 -0
- data/spec/support/the_paths_definitions.rb +7 -3
- data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +5 -4
- data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +3 -3
- data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +1 -1
- data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +1 -1
- data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +5 -3
- data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +1 -1
- data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +1 -1
- data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +25 -14
- data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +22 -12
- data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +30 -18
- data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +6 -3
- data/spec/swagger_v2/api_swagger_v2_response_spec.rb +13 -40
- data/spec/swagger_v2/api_swagger_v2_spec.rb +4 -2
- data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +6 -36
- data/spec/swagger_v2/default_api_spec.rb +10 -2
- data/spec/swagger_v2/endpoint_versioned_path_spec.rb +30 -0
- data/spec/swagger_v2/errors_spec.rb +75 -0
- data/spec/swagger_v2/hide_api_spec.rb +22 -4
- data/spec/swagger_v2/mounted_target_class_spec.rb +6 -2
- data/spec/swagger_v2/namespace_tags_prefix_spec.rb +6 -3
- data/spec/swagger_v2/namespace_tags_spec.rb +6 -3
- data/spec/swagger_v2/params_array_spec.rb +4 -2
- data/spec/swagger_v2/params_hash_spec.rb +4 -2
- data/spec/swagger_v2/params_nested_spec.rb +4 -2
- data/spec/swagger_v2/simple_mounted_api_spec.rb +66 -24
- metadata +23 -40
- data/spec/support/the_api_entities.rb +0 -50
- data/spec/swagger_v2/response_model_spec.rb +0 -208
@@ -1,50 +0,0 @@
|
|
1
|
-
RSpec.shared_context 'the api entities' do
|
2
|
-
before :all do
|
3
|
-
module TheApi
|
4
|
-
module Entities
|
5
|
-
class ApiError < Grape::Entity
|
6
|
-
expose :code, documentation: { type: Integer }
|
7
|
-
expose :message, documentation: { type: String }
|
8
|
-
end
|
9
|
-
|
10
|
-
class SecondApiError < Grape::Entity
|
11
|
-
expose :code, documentation: { type: Integer }
|
12
|
-
expose :severity, documentation: { type: String }
|
13
|
-
expose :message, documentation: { type: String }
|
14
|
-
end
|
15
|
-
|
16
|
-
class ResponseItem < Grape::Entity
|
17
|
-
expose :id, documentation: { type: Integer }
|
18
|
-
expose :name, documentation: { type: String }
|
19
|
-
end
|
20
|
-
|
21
|
-
class OtherItem < Grape::Entity
|
22
|
-
expose :key, documentation: { type: Integer }
|
23
|
-
expose :symbol, documentation: { type: String }
|
24
|
-
end
|
25
|
-
|
26
|
-
class UseResponse < Grape::Entity
|
27
|
-
expose :description, documentation: { type: String }
|
28
|
-
expose :items, as: '$responses', using: Entities::ResponseItem, documentation: { is_array: true }
|
29
|
-
end
|
30
|
-
|
31
|
-
class UseItemResponseAsType < Grape::Entity
|
32
|
-
expose :description, documentation: { type: String }
|
33
|
-
expose :responses, documentation: { type: Entities::ResponseItem, is_array: false }
|
34
|
-
end
|
35
|
-
|
36
|
-
class UseAddress < Grape::Entity
|
37
|
-
expose :street, documentation: { type: String, desc: 'street' }
|
38
|
-
expose :postcode, documentation: { type: String, desc: 'postcode' }
|
39
|
-
expose :city, documentation: { type: String, desc: 'city' }
|
40
|
-
expose :country, documentation: { type: String, desc: 'country' }
|
41
|
-
end
|
42
|
-
|
43
|
-
class UseNestedWithAddress < Grape::Entity
|
44
|
-
expose :name, documentation: { type: String }
|
45
|
-
expose :address, using: Entities::UseAddress
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,208 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'responseModel' do
|
4
|
-
before :all do
|
5
|
-
module ThisApi
|
6
|
-
module Entities
|
7
|
-
class Kind < Grape::Entity
|
8
|
-
expose :title, documentation: { type: 'string', desc: 'Title of the kind.' }
|
9
|
-
end
|
10
|
-
|
11
|
-
class Relation < Grape::Entity
|
12
|
-
expose :name, documentation: { type: 'string', desc: 'Name' }
|
13
|
-
end
|
14
|
-
class Tag < Grape::Entity
|
15
|
-
expose :name, documentation: { type: 'string', desc: 'Name' }
|
16
|
-
end
|
17
|
-
class Error < Grape::Entity
|
18
|
-
expose :code, documentation: { type: 'string', desc: 'Error code' }
|
19
|
-
expose :message, documentation: { type: 'string', desc: 'Error message' }
|
20
|
-
end
|
21
|
-
|
22
|
-
class Something < Grape::Entity
|
23
|
-
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
|
24
|
-
expose :kind, using: Kind, documentation: { type: 'ThisApi::Kind', desc: 'The kind of this something.' }
|
25
|
-
expose :kind2, using: Kind, documentation: { desc: 'Secondary kind.' }
|
26
|
-
expose :kind3, using: ThisApi::Entities::Kind, documentation: { desc: 'Tertiary kind.' }
|
27
|
-
expose :tags, using: ThisApi::Entities::Tag, documentation: { desc: 'Tags.', is_array: true }
|
28
|
-
expose :relation, using: ThisApi::Entities::Relation, documentation: { type: 'ThisApi::Relation', desc: 'A related model.' }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
class ResponseModelApi < Grape::API
|
33
|
-
format :json
|
34
|
-
desc 'This returns something',
|
35
|
-
is_array: true,
|
36
|
-
http_codes: [{ code: 200, message: 'OK', model: Entities::Something }]
|
37
|
-
get '/something' do
|
38
|
-
something = OpenStruct.new text: 'something'
|
39
|
-
present something, with: Entities::Something
|
40
|
-
end
|
41
|
-
|
42
|
-
# something like an index action
|
43
|
-
desc 'This returns something or an error',
|
44
|
-
entity: Entities::Something,
|
45
|
-
http_codes: [
|
46
|
-
{ code: 200, message: 'OK', model: Entities::Something },
|
47
|
-
{ code: 403, message: 'Refused to return something', model: Entities::Error }
|
48
|
-
]
|
49
|
-
params do
|
50
|
-
optional :id, type: Integer
|
51
|
-
end
|
52
|
-
get '/something/:id' do
|
53
|
-
if params[:id] == 1
|
54
|
-
something = OpenStruct.new text: 'something'
|
55
|
-
present something, with: Entities::Something
|
56
|
-
else
|
57
|
-
error = OpenStruct.new code: 'some_error', message: 'Some error'
|
58
|
-
present error, with: Entities::Error
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
add_swagger_documentation
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def app
|
68
|
-
ThisApi::ResponseModelApi
|
69
|
-
end
|
70
|
-
|
71
|
-
subject do
|
72
|
-
get '/swagger_doc/something'
|
73
|
-
JSON.parse(last_response.body)
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'documents index action' do
|
77
|
-
expect(subject['paths']['/something']['get']['responses']).to eq(
|
78
|
-
'200' => {
|
79
|
-
'description' => 'OK',
|
80
|
-
'schema' => {
|
81
|
-
'type' => 'array',
|
82
|
-
'items' => { '$ref' => '#/definitions/Something' } }
|
83
|
-
}
|
84
|
-
)
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'should document specified models as show action' do
|
88
|
-
expect(subject['paths']['/something/{id}']['get']['responses']).to eq(
|
89
|
-
'200' => {
|
90
|
-
'description' => 'OK',
|
91
|
-
'schema' => { '$ref' => '#/definitions/Something' }
|
92
|
-
},
|
93
|
-
'403' => {
|
94
|
-
'description' => 'Refused to return something',
|
95
|
-
'schema' => { '$ref' => '#/definitions/Error' }
|
96
|
-
}
|
97
|
-
)
|
98
|
-
expect(subject['definitions'].keys).to include 'Error'
|
99
|
-
expect(subject['definitions']['Error']).to eq(
|
100
|
-
'type' => 'object',
|
101
|
-
'description' => 'This returns something or an error',
|
102
|
-
'properties' => {
|
103
|
-
'code' => { 'type' => 'string', 'description' => 'Error code' },
|
104
|
-
'message' => { 'type' => 'string', 'description' => 'Error message' }
|
105
|
-
}
|
106
|
-
)
|
107
|
-
|
108
|
-
expect(subject['definitions'].keys).to include 'Something'
|
109
|
-
expect(subject['definitions']['Something']).to eq(
|
110
|
-
'type' => 'object',
|
111
|
-
'description' => 'This returns something or an error',
|
112
|
-
'properties' =>
|
113
|
-
{ 'text' => { 'type' => 'string', 'description' => 'Content of something.' },
|
114
|
-
'kind' => { '$ref' => '#/definitions/Kind', 'description' => 'The kind of this something.' },
|
115
|
-
'kind2' => { '$ref' => '#/definitions/Kind', 'description' => 'Secondary kind.' },
|
116
|
-
'kind3' => { '$ref' => '#/definitions/Kind', 'description' => 'Tertiary kind.' },
|
117
|
-
'tags' => { 'type' => 'array', 'items' => { '$ref' => '#/definitions/Tag' }, 'description' => 'Tags.' },
|
118
|
-
'relation' => { '$ref' => '#/definitions/Relation', 'description' => 'A related model.' } }
|
119
|
-
)
|
120
|
-
|
121
|
-
expect(subject['definitions'].keys).to include 'Kind'
|
122
|
-
expect(subject['definitions']['Kind']).to eq(
|
123
|
-
'type' => 'object', 'properties' => { 'title' => { 'type' => 'string', 'description' => 'Title of the kind.' } }
|
124
|
-
)
|
125
|
-
|
126
|
-
expect(subject['definitions'].keys).to include 'Relation'
|
127
|
-
expect(subject['definitions']['Relation']).to eq(
|
128
|
-
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } }
|
129
|
-
)
|
130
|
-
|
131
|
-
expect(subject['definitions'].keys).to include 'Tag'
|
132
|
-
expect(subject['definitions']['Tag']).to eq(
|
133
|
-
'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } }
|
134
|
-
)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
describe 'should build definition from given entity' do
|
139
|
-
before :all do
|
140
|
-
module TheseApi
|
141
|
-
module Entities
|
142
|
-
class Kind < Grape::Entity
|
143
|
-
expose :id, documentation: { type: Integer, desc: 'Title of the kind.' }
|
144
|
-
end
|
145
|
-
|
146
|
-
class Relation < Grape::Entity
|
147
|
-
expose :name, documentation: { type: String, desc: 'Name' }
|
148
|
-
end
|
149
|
-
class Tag < Grape::Entity
|
150
|
-
expose :name, documentation: { type: 'string', desc: 'Name' }
|
151
|
-
end
|
152
|
-
|
153
|
-
class SomeEntity < Grape::Entity
|
154
|
-
expose :text, documentation: { type: 'string', desc: 'Content of something.' }
|
155
|
-
expose :kind, using: Kind, documentation: { type: 'TheseApi::Kind', desc: 'The kind of this something.' }
|
156
|
-
expose :kind2, using: Kind, documentation: { desc: 'Secondary kind.' }
|
157
|
-
expose :kind3, using: TheseApi::Entities::Kind, documentation: { desc: 'Tertiary kind.' }
|
158
|
-
expose :tags, using: TheseApi::Entities::Tag, documentation: { desc: 'Tags.', is_array: true }
|
159
|
-
expose :relation, using: TheseApi::Entities::Relation, documentation: { type: 'TheseApi::Relation', desc: 'A related model.' }
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
class ResponseEntityApi < Grape::API
|
164
|
-
format :json
|
165
|
-
desc 'This returns something',
|
166
|
-
is_array: true,
|
167
|
-
entity: Entities::SomeEntity
|
168
|
-
get '/some_entity' do
|
169
|
-
something = OpenStruct.new text: 'something'
|
170
|
-
present something, with: Entities::SomeEntity
|
171
|
-
end
|
172
|
-
|
173
|
-
add_swagger_documentation
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
def app
|
179
|
-
TheseApi::ResponseEntityApi
|
180
|
-
end
|
181
|
-
|
182
|
-
subject do
|
183
|
-
get '/swagger_doc'
|
184
|
-
JSON.parse(last_response.body)
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'it prefer entity over others' do
|
188
|
-
expect(subject['definitions']).to eql(
|
189
|
-
'Kind' => {
|
190
|
-
'type' => 'object',
|
191
|
-
'properties' => {
|
192
|
-
'id' => { 'type' => 'integer', 'format' => 'int32', 'description' => 'Title of the kind.' } } },
|
193
|
-
'Tag' => { 'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } } },
|
194
|
-
'Relation' => { 'type' => 'object', 'properties' => { 'name' => { 'type' => 'string', 'description' => 'Name' } } },
|
195
|
-
'SomeEntity' => {
|
196
|
-
'type' => 'object',
|
197
|
-
'properties' => {
|
198
|
-
'text' => { 'type' => 'string', 'description' => 'Content of something.' },
|
199
|
-
'kind' => { '$ref' => '#/definitions/Kind', 'description' => 'The kind of this something.' },
|
200
|
-
'kind2' => { '$ref' => '#/definitions/Kind', 'description' => 'Secondary kind.' },
|
201
|
-
'kind3' => { '$ref' => '#/definitions/Kind', 'description' => 'Tertiary kind.' },
|
202
|
-
'tags' => { 'type' => 'array', 'items' => { '$ref' => '#/definitions/Tag' }, 'description' => 'Tags.' },
|
203
|
-
'relation' => { '$ref' => '#/definitions/Relation', 'description' => 'A related model.' }
|
204
|
-
},
|
205
|
-
'description' => 'This returns something'
|
206
|
-
})
|
207
|
-
end
|
208
|
-
end
|