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.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -1
  3. data/.rubocop.yml +3 -0
  4. data/.rubocop_todo.yml +14 -22
  5. data/.travis.yml +7 -4
  6. data/CHANGELOG.md +53 -26
  7. data/Gemfile +1 -1
  8. data/README.md +414 -327
  9. data/RELEASING.md +3 -4
  10. data/example/api/endpoints.rb +132 -0
  11. data/example/api/entities.rb +18 -0
  12. data/example/config.ru +36 -2
  13. data/example/example_requests.postman_collection +146 -0
  14. data/example/swagger-example.png +0 -0
  15. data/grape-swagger.gemspec +9 -6
  16. data/lib/grape-swagger.rb +69 -99
  17. data/lib/grape-swagger/doc_methods.rb +69 -544
  18. data/lib/grape-swagger/doc_methods/data_type.rb +77 -0
  19. data/lib/grape-swagger/doc_methods/extensions.rb +75 -0
  20. data/lib/grape-swagger/doc_methods/move_params.rb +153 -0
  21. data/lib/grape-swagger/doc_methods/operation_id.rb +27 -0
  22. data/lib/grape-swagger/doc_methods/optional_object.rb +15 -0
  23. data/lib/grape-swagger/doc_methods/parse_params.rb +113 -0
  24. data/lib/grape-swagger/doc_methods/path_string.rb +29 -0
  25. data/lib/grape-swagger/doc_methods/produces_consumes.rb +12 -0
  26. data/lib/grape-swagger/doc_methods/status_codes.rb +17 -0
  27. data/lib/grape-swagger/doc_methods/tag_name_description.rb +26 -0
  28. data/lib/grape-swagger/endpoint.rb +317 -0
  29. data/lib/grape-swagger/version.rb +1 -1
  30. data/spec/lib/data_type_spec.rb +57 -0
  31. data/spec/lib/endpoint_spec.rb +6 -0
  32. data/spec/lib/extensions_spec.rb +127 -0
  33. data/spec/lib/move_params_spec.rb +298 -0
  34. data/spec/lib/operation_id_spec.rb +24 -0
  35. data/spec/lib/optional_object_spec.rb +40 -0
  36. data/spec/lib/path_string_spec.rb +38 -0
  37. data/spec/lib/produces_consumes_spec.rb +98 -0
  38. data/spec/markdown/kramdown_adapter_spec.rb +2 -9
  39. data/spec/markdown/redcarpet_adapter_spec.rb +2 -16
  40. data/spec/spec_helper.rb +7 -13
  41. data/spec/support/api_swagger_v2_result.rb +204 -0
  42. data/spec/support/namespace_tags.rb +73 -0
  43. data/spec/support/the_api_entities.rb +52 -0
  44. data/spec/support/the_paths_definitions.rb +94 -0
  45. data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +32 -0
  46. data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +151 -0
  47. data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +109 -0
  48. data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +124 -0
  49. data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +51 -0
  50. data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +44 -0
  51. data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +56 -0
  52. data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +146 -0
  53. data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +197 -0
  54. data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +151 -0
  55. data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +217 -0
  56. data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +64 -0
  57. data/spec/swagger_v2/api_swagger_v2_response_spec.rb +184 -0
  58. data/spec/swagger_v2/api_swagger_v2_spec.rb +207 -0
  59. data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +121 -0
  60. data/spec/{boolean_params_spec.rb → swagger_v2/boolean_params_spec.rb} +2 -2
  61. data/spec/{default_api_spec.rb → swagger_v2/default_api_spec.rb} +40 -36
  62. data/spec/swagger_v2/description_not_initialized.rb +39 -0
  63. data/spec/{float_api_spec.rb → swagger_v2/float_api_spec.rb} +2 -2
  64. data/spec/{form_params_spec.rb → swagger_v2/form_params_spec.rb} +9 -9
  65. data/spec/{grape-swagger_spec.rb → swagger_v2/grape-swagger_spec.rb} +0 -0
  66. data/spec/swagger_v2/hide_api_spec.rb +131 -0
  67. data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
  68. data/spec/swagger_v2/namespace_tags_prefix_spec.rb +84 -0
  69. data/spec/swagger_v2/namespace_tags_spec.rb +76 -0
  70. data/spec/{namespaced_api_spec.rb → swagger_v2/namespaced_api_spec.rb} +6 -26
  71. data/spec/{param_type_spec.rb → swagger_v2/param_type_spec.rb} +10 -8
  72. data/spec/{param_values_spec.rb → swagger_v2/param_values_spec.rb} +52 -22
  73. data/spec/swagger_v2/params_array_spec.rb +63 -0
  74. data/spec/swagger_v2/params_hash_spec.rb +65 -0
  75. data/spec/swagger_v2/params_nested_spec.rb +63 -0
  76. data/spec/{reference_entity.rb → swagger_v2/reference_entity.rb} +18 -23
  77. data/spec/swagger_v2/response_model_spec.rb +212 -0
  78. data/spec/swagger_v2/simple_mounted_api_spec.rb +264 -0
  79. metadata +175 -90
  80. data/example/api.rb +0 -66
  81. data/lib/grape-swagger/markdown.rb +0 -23
  82. data/spec/api_description_spec.rb +0 -43
  83. data/spec/api_global_models_spec.rb +0 -77
  84. data/spec/api_models_spec.rb +0 -364
  85. data/spec/api_paths_spec.rb +0 -128
  86. data/spec/api_root_spec.rb +0 -30
  87. data/spec/api_with_nil_types.rb +0 -50
  88. data/spec/api_with_path_versioning_spec.rb +0 -33
  89. data/spec/api_with_prefix_and_namespace_spec.rb +0 -32
  90. data/spec/api_with_standalone_namespace_spec.rb +0 -215
  91. data/spec/array_entity_spec.rb +0 -34
  92. data/spec/array_params_spec.rb +0 -85
  93. data/spec/grape-swagger_helper_spec.rb +0 -152
  94. data/spec/group_params_spec.rb +0 -31
  95. data/spec/hash_params_spec.rb +0 -30
  96. data/spec/hide_api_spec.rb +0 -124
  97. data/spec/i18n_spec.rb +0 -364
  98. data/spec/markdown/markdown_spec.rb +0 -27
  99. data/spec/mounted_target_class_spec.rb +0 -63
  100. data/spec/mutually_exclusive_spec.rb +0 -36
  101. data/spec/non_default_api_spec.rb +0 -733
  102. data/spec/response_model_spec.rb +0 -121
  103. data/spec/simple_mounted_api_spec.rb +0 -213
  104. data/spec/support/i18n_helper.rb +0 -8
@@ -1,121 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'responseModel' do
4
- before :all do
5
- module MyAPI
6
- module Entities
7
- class Kind < Grape::Entity
8
- expose :title, documentation: { type: 'string', desc: 'Title of the kind.' }
9
- end
10
-
11
- class Something < Grape::Entity
12
- expose :text, documentation: { type: 'string', desc: 'Content of something.' }
13
- expose :kind, using: Kind, documentation: { type: 'MyAPI::Kind', desc: 'The kind of this something.' }
14
- expose :kind2, using: Kind, documentation: { desc: 'Secondary kind.' }
15
- expose :kind3, using: 'MyAPI::Entities::Kind', documentation: { desc: 'Tertiary kind.' }
16
- expose :tags, using: 'MyAPI::Entities::Tag', documentation: { desc: 'Tags.', is_array: true }
17
- expose :relation, using: 'MyAPI::Entities::Relation', documentation: { type: 'MyAPI::Relation', desc: 'A related model.' }
18
- end
19
-
20
- class Relation < Grape::Entity
21
- expose :name, documentation: { type: 'string', desc: 'Name' }
22
- end
23
-
24
- class Tag < Grape::Entity
25
- expose :name, documentation: { type: 'string', desc: 'Name' }
26
- end
27
-
28
- class Error < Grape::Entity
29
- expose :code, documentation: { type: 'string', desc: 'Error code' }
30
- expose :message, documentation: { type: 'string', desc: 'Error message' }
31
- end
32
- end
33
-
34
- class ResponseModelApi < Grape::API
35
- format :json
36
- desc 'This returns something or an error',
37
- entity: Entities::Something,
38
- http_codes: [
39
- [200, 'OK', Entities::Something],
40
- [403, 'Refused to return something', Entities::Error]
41
- ]
42
-
43
- get '/something/:id' do
44
- if params[:id] == 1
45
- something = OpenStruct.new text: 'something'
46
- present something, with: Entities::Something
47
- else
48
- error = OpenStruct.new code: 'some_error', message: 'Some error'
49
- present error, with: Entities::Error
50
- end
51
- end
52
-
53
- add_swagger_documentation
54
- end
55
- end
56
- end
57
-
58
- def app
59
- MyAPI::ResponseModelApi
60
- end
61
-
62
- subject do
63
- get '/swagger_doc/something'
64
- JSON.parse(last_response.body)
65
- end
66
-
67
- it 'should document specified models' do
68
- expect(subject['apis'][0]['operations'][0]['responseMessages']).to eq(
69
- [
70
- {
71
- 'code' => 200,
72
- 'message' => 'OK',
73
- 'responseModel' => 'MyAPI::Something'
74
- },
75
- {
76
- 'code' => 403,
77
- 'message' => 'Refused to return something',
78
- 'responseModel' => 'MyAPI::Error'
79
- }
80
- ]
81
- )
82
-
83
- expect(subject['models'].keys).to include 'MyAPI::Error'
84
- expect(subject['models']['MyAPI::Error']).to eq(
85
- 'id' => 'MyAPI::Error',
86
- 'properties' => {
87
- 'code' => { 'type' => 'string', 'description' => 'Error code' },
88
- 'message' => { 'type' => 'string', 'description' => 'Error message' }
89
- }
90
- )
91
-
92
- expect(subject['models'].keys).to include 'MyAPI::Something'
93
- expect(subject['models']['MyAPI::Something']).to eq(
94
- 'id' => 'MyAPI::Something',
95
- 'properties' => {
96
- 'text' => { 'type' => 'string', 'description' => 'Content of something.' },
97
- 'kind' => { '$ref' => 'MyAPI::Kind', 'description' => 'The kind of this something.' },
98
- 'kind2' => { '$ref' => 'MyAPI::Kind', 'description' => 'Secondary kind.' },
99
- 'kind3' => { '$ref' => 'MyAPI::Kind', 'description' => 'Tertiary kind.' },
100
- 'tags' => { 'items' => { '$ref' => 'MyAPI::Tag' }, 'type' => 'array', 'description' => 'Tags.' },
101
- 'relation' => { '$ref' => 'MyAPI::Relation', 'description' => 'A related model.' }
102
- }
103
- )
104
-
105
- expect(subject['models'].keys).to include 'MyAPI::Kind'
106
- expect(subject['models']['MyAPI::Kind']).to eq(
107
- 'id' => 'MyAPI::Kind',
108
- 'properties' => {
109
- 'title' => { 'type' => 'string', 'description' => 'Title of the kind.' }
110
- }
111
- )
112
-
113
- expect(subject['models'].keys).to include 'MyAPI::Relation'
114
- expect(subject['models']['MyAPI::Relation']).to eq(
115
- 'id' => 'MyAPI::Relation',
116
- 'properties' => {
117
- 'name' => { 'type' => 'string', 'description' => 'Name' }
118
- }
119
- )
120
- end
121
- end
@@ -1,213 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'a simple mounted api' do
4
- before :all do
5
- class CustomType; end
6
-
7
- class SimpleMountedApi < Grape::API
8
- desc 'Document root'
9
- get do
10
- end
11
-
12
- desc 'This gets something.',
13
- notes: '_test_'
14
-
15
- get '/simple' do
16
- { bla: 'something' }
17
- end
18
-
19
- desc 'This gets something for URL using - separator.',
20
- notes: '_test_'
21
-
22
- get '/simple-test' do
23
- { bla: 'something' }
24
- end
25
-
26
- desc 'This gets something for URL in camelcase.',
27
- notes: '_test_'
28
- get '/SimpleTest' do
29
- { bla: 'something else' }
30
- end
31
-
32
- desc 'this gets something else',
33
- headers: {
34
- 'XAuthToken' => { description: 'A required header.', required: true },
35
- 'XOtherHeader' => { description: 'An optional header.', required: false }
36
- },
37
- http_codes: {
38
- 403 => 'invalid pony',
39
- 405 => 'no ponies left!'
40
- }
41
-
42
- get '/simple_with_headers' do
43
- { bla: 'something_else' }
44
- end
45
-
46
- desc 'this takes an array of parameters',
47
- params: {
48
- 'items[]' => { description: 'array of items' }
49
- }
50
-
51
- post '/items' do
52
- {}
53
- end
54
-
55
- desc 'this uses a custom parameter',
56
- params: {
57
- 'custom' => { type: CustomType, description: 'array of items' }
58
- }
59
-
60
- get '/custom' do
61
- {}
62
- end
63
- end
64
-
65
- class SimpleApi < Grape::API
66
- mount SimpleMountedApi
67
- add_swagger_documentation
68
- end
69
- end
70
-
71
- def app
72
- SimpleApi
73
- end
74
-
75
- it 'retrieves swagger-documentation on /swagger_doc' do
76
- get '/swagger_doc.json'
77
- expect(JSON.parse(last_response.body)).to eq(
78
- 'apiVersion' => '0.1',
79
- 'swaggerVersion' => '1.2',
80
- 'info' => {},
81
- 'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
82
- 'apis' => [
83
- { 'path' => '/simple.{format}', 'description' => 'Operations about simples' },
84
- { 'path' => '/simple-test.{format}', 'description' => 'Operations about simple-tests' },
85
- { 'path' => '/SimpleTest.{format}', 'description' => 'Operations about SimpleTests' },
86
- { 'path' => '/simple_with_headers.{format}', 'description' => 'Operations about simple_with_headers' },
87
- { 'path' => '/items.{format}', 'description' => 'Operations about items' },
88
- { 'path' => '/custom.{format}', 'description' => 'Operations about customs' },
89
- { 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }
90
- ]
91
- )
92
- end
93
-
94
- it 'retrieves the documentation for mounted-api' do
95
- get '/swagger_doc/simple.json'
96
- expect(JSON.parse(last_response.body)).to eq(
97
- 'apiVersion' => '0.1',
98
- 'swaggerVersion' => '1.2',
99
- 'basePath' => 'http://example.org',
100
- 'resourcePath' => '/simple',
101
- 'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
102
- 'apis' => [{
103
- 'path' => '/simple.{format}',
104
- 'operations' => [{
105
- 'notes' => '_test_',
106
- 'summary' => 'This gets something.',
107
- 'nickname' => 'GET-simple---format-',
108
- 'method' => 'GET',
109
- 'parameters' => [],
110
- 'type' => 'void'
111
- }]
112
- }]
113
- )
114
- end
115
-
116
- context 'retrieves the documentation for mounted-api that' do
117
- it "contains '-' in URL" do
118
- get '/swagger_doc/simple-test.json'
119
- expect(JSON.parse(last_response.body)).to eq(
120
- 'apiVersion' => '0.1',
121
- 'swaggerVersion' => '1.2',
122
- 'basePath' => 'http://example.org',
123
- 'resourcePath' => '/simple-test',
124
- 'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
125
- 'apis' => [{
126
- 'path' => '/simple-test.{format}',
127
- 'operations' => [{
128
- 'notes' => '_test_',
129
- 'summary' => 'This gets something for URL using - separator.',
130
- 'nickname' => 'GET-simple-test---format-',
131
- 'method' => 'GET',
132
- 'parameters' => [],
133
- 'type' => 'void'
134
- }]
135
- }]
136
- )
137
- end
138
-
139
- it 'uses camelcase' do
140
- get '/swagger_doc/SimpleTest.json'
141
- expect(JSON.parse(last_response.body)).to eq(
142
- 'apiVersion' => '0.1',
143
- 'swaggerVersion' => '1.2',
144
- 'basePath' => 'http://example.org',
145
- 'resourcePath' => '/SimpleTest',
146
- 'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
147
- 'apis' => [{
148
- 'path' => '/SimpleTest.{format}',
149
- 'operations' => [{
150
- 'notes' => '_test_',
151
- 'summary' => 'This gets something for URL in camelcase.',
152
- 'nickname' => 'GET-SimpleTest---format-',
153
- 'method' => 'GET',
154
- 'parameters' => [],
155
- 'type' => 'void'
156
- }]
157
- }]
158
- )
159
- end
160
-
161
- it 'includes headers' do
162
- get '/swagger_doc/simple_with_headers.json'
163
- expect(JSON.parse(last_response.body)['apis']).to eq [{
164
- 'path' => '/simple_with_headers.{format}',
165
- 'operations' => [{
166
- 'notes' => '',
167
- 'summary' => 'this gets something else',
168
- 'nickname' => 'GET-simple_with_headers---format-',
169
- 'method' => 'GET',
170
- 'parameters' => [
171
- { 'paramType' => 'header', 'name' => 'XAuthToken', 'description' => 'A required header.', 'type' => 'string', 'required' => true },
172
- { 'paramType' => 'header', 'name' => 'XOtherHeader', 'description' => 'An optional header.', 'type' => 'string', 'required' => false }
173
- ],
174
- 'type' => 'void',
175
- 'responseMessages' => [
176
- { 'code' => 403, 'message' => 'invalid pony' },
177
- { 'code' => 405, 'message' => 'no ponies left!' }
178
- ]
179
- }]
180
- }]
181
- end
182
-
183
- it 'supports multiple parameters' do
184
- get '/swagger_doc/items.json'
185
- expect(JSON.parse(last_response.body)['apis']).to eq [{
186
- 'path' => '/items.{format}',
187
- 'operations' => [{
188
- 'notes' => '',
189
- 'summary' => 'this takes an array of parameters',
190
- 'nickname' => 'POST-items---format-',
191
- 'method' => 'POST',
192
- 'parameters' => [{ 'paramType' => 'form', 'name' => 'items[]', 'description' => 'array of items', 'type' => 'string', 'required' => false, 'allowMultiple' => false }],
193
- 'type' => 'void'
194
- }]
195
- }]
196
- end
197
-
198
- it 'supports custom types' do
199
- get '/swagger_doc/custom.json'
200
- expect(JSON.parse(last_response.body)['apis']).to eq [{
201
- 'path' => '/custom.{format}',
202
- 'operations' => [{
203
- 'notes' => '',
204
- 'summary' => 'this uses a custom parameter',
205
- 'nickname' => 'GET-custom---format-',
206
- 'method' => 'GET',
207
- 'parameters' => [{ 'paramType' => 'query', 'name' => 'custom', 'description' => 'array of items', 'type' => 'CustomType', 'required' => false, 'allowMultiple' => false }],
208
- 'type' => 'void'
209
- }]
210
- }]
211
- end
212
- end
213
- end
@@ -1,8 +0,0 @@
1
- module I18nHelper
2
- def with_translations(locale, translations)
3
- I18n.backend.store_translations locale, translations
4
- yield
5
- ensure
6
- I18n.reload!
7
- end
8
- end