grape-swagger 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a3f65f49a31d6affe69a657daff3867582070436163992acb47f1166dde1cc6
4
- data.tar.gz: da9f13e2efd7e2b23dc92eea5b9deeaf4d9b8a860b13bae2b7f928b31d996be3
3
+ metadata.gz: 1d9d2fa1309701cf2c24f0993db15660df9be1c437861529a321e6344390fc90
4
+ data.tar.gz: 8e74a44fddb127a89ec914da70de6118a86fe17318fb16e4f68d2761cd69dc1c
5
5
  SHA512:
6
- metadata.gz: 0d51d5cde5ff558fefcbf8b4503d290bc535c48f22d1ee3c2662ad406c548b7ef72d196b36598b2d3ea68d6ebef8e6ce43e3dec8dab6f371df9d12aaadd34567
7
- data.tar.gz: b22301456da8cd150767fd614434bfad161959eb0836b028b4ff8d15893fd128c53513e87ab8180a6442b8fda9ec41a6e6648b67cb32a46b692d2b117e229ec1
6
+ metadata.gz: 79db26518f7697a7347ad92749eaa14722310e41e395177e839d04bfc5e490026989a687158f89f34ea52a62a390482430d79067aa887f4ef90e95bf991315d7
7
+ data.tar.gz: 95e85e0b5d337b53bd4ed260aed3fcaae2acae613fcf337552afe0b4be280da89587351008f1f2054440b61e868804c1710340214d2ad80ca4950439af3f5fbe
@@ -28,6 +28,10 @@ Layout/SpaceAroundMethodCallOperator:
28
28
 
29
29
  # Lint stuff
30
30
  #
31
+ Lint/ConstantDefinitionInBlock:
32
+ Exclude:
33
+ - spec/**/*
34
+
31
35
  Lint/DeprecatedOpenSSLConstant:
32
36
  Enabled: true
33
37
 
@@ -50,7 +54,7 @@ Metrics/BlockLength:
50
54
  - spec/**/*
51
55
 
52
56
  Metrics/ClassLength:
53
- Max: 300
57
+ Max: 350
54
58
 
55
59
  Metrics/CyclomaticComplexity:
56
60
  Max: 17
@@ -69,6 +73,9 @@ Naming:
69
73
  Style/AccessorGrouping:
70
74
  Enabled: true
71
75
 
76
+ Style/AsciiComments:
77
+ Enabled: false
78
+
72
79
  Style/ArrayCoercion:
73
80
  Enabled: true
74
81
 
@@ -28,7 +28,7 @@ Metrics/MethodLength:
28
28
 
29
29
  # Offense count: 7
30
30
  Metrics/PerceivedComplexity:
31
- Max: 14
31
+ Max: 16
32
32
 
33
33
  # Offense count: 3
34
34
  Style/ClassVars:
@@ -11,12 +11,13 @@ after_success:
11
11
  rvm:
12
12
  - 2.5.8
13
13
  - 2.6.6
14
- - 2.7.1
14
+ - 2.7.2
15
15
  env:
16
16
  - GRAPE_VERSION=1.3.3
17
- - GRAPE_VERSION=1.4.0 MODEL_PARSER=grape-swagger-entity
18
- - GRAPE_VERSION=1.4.0 MODEL_PARSER=grape-swagger-representable
19
17
  - GRAPE_VERSION=1.4.0
18
+ - GRAPE_VERSION=1.5.0 MODEL_PARSER=grape-swagger-entity
19
+ - GRAPE_VERSION=1.5.0 MODEL_PARSER=grape-swagger-representable
20
+ - GRAPE_VERSION=1.5.0
20
21
  - GRAPE_VERSION=HEAD
21
22
 
22
23
  jobs:
@@ -9,6 +9,17 @@
9
9
  * Your contribution here.
10
10
 
11
11
 
12
+ ### 1.3.1 (November 1, 2020)
13
+
14
+ #### Features
15
+
16
+ * [#813](https://github.com/ruby-grape/grape-swagger/pull/813): Handle multiple presents - [@AntoineGuestin](https://github.com/AntoineGuestin).
17
+
18
+ #### Fixes
19
+
20
+ * [#811](https://github.com/ruby-grape/grape-swagger/pull/811): Fixes #809: supports utf8 route names - [@LeFnord](https://github.com/LeFnord).
21
+
22
+
12
23
  ### 1.3.0 (September 3, 2020)
13
24
 
14
25
  #### Features
data/Gemfile CHANGED
@@ -6,7 +6,7 @@ ruby RUBY_VERSION
6
6
 
7
7
  gemspec
8
8
 
9
- gem 'grape', case version = ENV['GRAPE_VERSION'] || '>= 1.4.0'
9
+ gem 'grape', case version = ENV['GRAPE_VERSION'] || '>= 1.5.0'
10
10
  when 'HEAD'
11
11
  { git: 'https://github.com/ruby-grape/grape' }
12
12
  else
@@ -27,7 +27,7 @@ group :development, :test do
27
27
  gem 'rake'
28
28
  gem 'rdoc'
29
29
  gem 'rspec', '~> 3.9'
30
- gem 'rubocop', '~> 0.90', require: false
30
+ gem 'rubocop', '~> 1.0', require: false
31
31
  end
32
32
 
33
33
  group :test do
data/README.md CHANGED
@@ -458,6 +458,7 @@ add_swagger_documentation \
458
458
  * [Response examples documentation](#response-examples)
459
459
  * [Response headers documentation](#response-headers)
460
460
  * [Adding root element to responses](#response-root)
461
+ * [Multiple present Response](#multiple-response)
461
462
 
462
463
  #### Swagger Header Parameters <a name="headers"></a>
463
464
 
@@ -1262,6 +1263,75 @@ The result will look like following:
1262
1263
  }
1263
1264
  }
1264
1265
  ```
1266
+ #### Multiple present Response <a name="multiple-response"></a>
1267
+
1268
+ You can specify a custom multiple response by using the `as` key:
1269
+ ```ruby
1270
+ desc 'Multiple response',
1271
+ success: [
1272
+ { model: Entities::EnumValues, as: :gender },
1273
+ { model: Entities::Something, as: :somethings }
1274
+ ]
1275
+ end
1276
+
1277
+ get '/things' do
1278
+ ...
1279
+ end
1280
+ ```
1281
+ The result will look like following:
1282
+ ```
1283
+ "responses": {
1284
+ "200": {
1285
+ "description": "Multiple response",
1286
+ "schema":{
1287
+ "type":"object",
1288
+ "properties":{
1289
+ "gender":{
1290
+ "$ref":"#/definitions/EnumValues"
1291
+ },
1292
+ "somethings":{
1293
+ "$ref":"#/definitions/Something"
1294
+ }
1295
+ }
1296
+ }
1297
+ }
1298
+ }
1299
+ ```
1300
+ You can also specify if the response is an array, with the `is_array` key:
1301
+ ```ruby
1302
+ desc 'Multiple response with array',
1303
+ success: [
1304
+ { model: Entities::EnumValues, as: :gender },
1305
+ { model: Entities::Something, as: :somethings, is_array: true }
1306
+ ]
1307
+ end
1308
+
1309
+ get '/things' do
1310
+ ...
1311
+ end
1312
+ ```
1313
+ The result will look like following:
1314
+ ```
1315
+ "responses": {
1316
+ "200": {
1317
+ "description": "Multiple response with array",
1318
+ "schema":{
1319
+ "type":"object",
1320
+ "properties":{
1321
+ "gender":{
1322
+ "$ref":"#/definitions/EnumValues"
1323
+ },
1324
+ "somethings":{
1325
+ "type":"array",
1326
+ "items":{
1327
+ "$ref":"#/definitions/Something"
1328
+ }
1329
+ }
1330
+ }
1331
+ }
1332
+ }
1333
+ }
1334
+ ```
1265
1335
 
1266
1336
  ## Using Grape Entities <a name="grape-entity"></a>
1267
1337
 
@@ -29,7 +29,10 @@ module SwaggerRouting
29
29
  route_match = route_path.split(/^.*?#{route.prefix}/).last
30
30
  next unless route_match
31
31
 
32
- route_match = route_match.match('\/([\w|-]*?)[\.\/\(]') || route_match.match('\/([\w|-]*)$')
32
+ # want to match emojis … ;)
33
+ # route_match = route_match
34
+ # .match('\/([\p{Alnum}|\p{Emoji}|\-|\_]*?)[\.\/\(]') || route_match.match('\/([\p{Alpha}|\p{Emoji}|\-|\_]*)$')
35
+ route_match = route_match.match('\/([\p{Alnum}|\-|\_]*?)[\.\/\(]') || route_match.match('\/([\p{Alpha}|\-|\_]*)$')
33
36
  next unless route_match
34
37
 
35
38
  resource = route_match.captures.first
@@ -85,7 +88,7 @@ module SwaggerRouting
85
88
  route_name = name.match(%r{^/?([^/]*).*$})[1]
86
89
  return route_name unless route_name.include? ':'
87
90
 
88
- matches = name.match(/\/[a-z]+/)
91
+ matches = name.match(/\/\p{Alpha}+/)
89
92
  matches.nil? ? route_name : matches[0].delete('/')
90
93
  end
91
94
 
@@ -201,8 +201,7 @@ module Grape
201
201
  def response_object(route, options)
202
202
  codes(route).each_with_object({}) do |value, memo|
203
203
  value[:message] ||= ''
204
- memo[value[:code]] = { description: value[:message] }
205
-
204
+ memo[value[:code]] = { description: value[:message] ||= '' } unless memo[value[:code]].present?
206
205
  memo[value[:code]][:headers] = value[:headers] if value[:headers]
207
206
 
208
207
  next build_file_response(memo[value[:code]]) if file_response?(value[:model])
@@ -221,7 +220,7 @@ module Grape
221
220
  next if response_model.start_with?('Swagger_doc')
222
221
 
223
222
  @definitions[response_model][:description] ||= "#{response_model} model"
224
- memo[value[:code]][:schema] = build_reference(route, value, response_model, options)
223
+ build_memo_schema(memo, route, value, response_model, options)
225
224
  memo[value[:code]][:examples] = value[:examples] if value[:examples]
226
225
  end
227
226
  end
@@ -268,15 +267,45 @@ module Grape
268
267
 
269
268
  private
270
269
 
270
+ def build_memo_schema(memo, route, value, response_model, options)
271
+ if memo[value[:code]][:schema] && value[:as]
272
+ memo[value[:code]][:schema][:properties].merge!(build_reference(route, value, response_model, options))
273
+ elsif value[:as]
274
+ memo[value[:code]][:schema] = {
275
+ type: :object,
276
+ properties: build_reference(route, value, response_model, options)
277
+ }
278
+ else
279
+ memo[value[:code]][:schema] = build_reference(route, value, response_model, options)
280
+ end
281
+ end
282
+
271
283
  def build_reference(route, value, response_model, settings)
272
284
  # TODO: proof that the definition exist, if model isn't specified
273
- reference = { '$ref' => "#/definitions/#{response_model}" }
285
+ reference = if value.key?(:as)
286
+ { value[:as] => build_reference_hash(response_model) }
287
+ else
288
+ build_reference_hash(response_model)
289
+ end
274
290
  return reference unless value[:code] < 300
275
291
 
276
- reference = { type: 'array', items: reference } if route.options[:is_array]
292
+ if value.key?(:as) && value.key?(:is_array)
293
+ reference[value[:as]] = build_reference_array(reference[value[:as]])
294
+ elsif route.options[:is_array]
295
+ reference = build_reference_array(reference)
296
+ end
297
+
277
298
  build_root(route, reference, response_model, settings)
278
299
  end
279
300
 
301
+ def build_reference_hash(response_model)
302
+ { '$ref' => "#/definitions/#{response_model}" }
303
+ end
304
+
305
+ def build_reference_array(reference)
306
+ { type: 'array', items: reference }
307
+ end
308
+
280
309
  def build_root(route, reference, response_model, settings)
281
310
  default_root = response_model.underscore
282
311
  default_root = default_root.pluralize if route.options[:is_array]
@@ -382,6 +411,8 @@ module Grape
382
411
  default_code[:message] = entity[:message] || route.description || default_code[:message].sub('{item}', @item)
383
412
  default_code[:examples] = entity[:examples] if entity[:examples]
384
413
  default_code[:headers] = entity[:headers] if entity[:headers]
414
+ default_code[:as] = entity[:as] if entity[:as]
415
+ default_code[:is_array] = entity[:is_array] if entity[:is_array]
385
416
  else
386
417
  default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
387
418
  default_code[:model] = entity if entity
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrapeSwagger
4
- VERSION = '1.3.0'
4
+ VERSION = '1.3.1'
5
5
  end
@@ -15,6 +15,7 @@ describe '#537 enum values spec' do
15
15
  desc 'create account',
16
16
  success: Spec
17
17
  get do
18
+ { message: 'hi' }
18
19
  end
19
20
  end
20
21
 
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe '#776 multiple presents spec' do
6
+ include_context "#{MODEL_PARSER} swagger example"
7
+
8
+ let(:app) do
9
+ Class.new(Grape::API) do
10
+ namespace :issue_776 do
11
+ desc 'Get multiple presents',
12
+ success: [
13
+ { model: Entities::EnumValues, as: :gender },
14
+ { model: Entities::Something, as: :somethings, is_array: true }
15
+ ]
16
+
17
+ get do
18
+ present :gender, { number: 1, gender: 'Male' }, with: Entities::EnumValues
19
+ present :somethings, [
20
+ { id: 1, text: 'element_1', links: %w[link1 link2] },
21
+ { id: 2, text: 'element_2', links: %w[link1 link2] }
22
+ ], with: Entities::Something, is_array: true
23
+ end
24
+ end
25
+
26
+ add_swagger_documentation format: :json
27
+ end
28
+ end
29
+
30
+ subject do
31
+ get '/swagger_doc'
32
+ JSON.parse(last_response.body)
33
+ end
34
+
35
+ let(:definitions) { subject['definitions'] }
36
+ let(:schema) { subject['paths']['/issue_776']['get']['responses']['200']['schema'] }
37
+
38
+ specify { expect(definitions.keys).to include 'EnumValues', 'Something' }
39
+
40
+ specify do
41
+ expect(schema).to eql({
42
+ 'properties' => {
43
+ 'somethings' => {
44
+ 'items' => {
45
+ '$ref' => '#/definitions/Something'
46
+ },
47
+ 'type' => 'array'
48
+ },
49
+ 'gender' => {
50
+ '$ref' => '#/definitions/EnumValues'
51
+ }
52
+ },
53
+ 'type' => 'object'
54
+ })
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe '#605 root route documentation' do
6
+ let(:app) do
7
+ Class.new(Grape::API) do
8
+ resource :grunnbeløp do
9
+ desc 'returnerer grunnbeløp'
10
+ get do
11
+ { message: 'hello world' }
12
+ end
13
+ end
14
+
15
+ resource :εσόδων do
16
+ desc 'εσόδων'
17
+ get do
18
+ { message: 'hello world' }
19
+ end
20
+ end
21
+
22
+ resource :数 do
23
+ desc '数'
24
+ get do
25
+ { message: 'hello world' }
26
+ end
27
+ end
28
+
29
+ resource :amount do
30
+ desc 'returns amount'
31
+ get do
32
+ { message: 'hello world' }
33
+ end
34
+ end
35
+
36
+ resource :👍 do
37
+ desc 'returns 👍'
38
+ get do
39
+ { message: 'hello world' }
40
+ end
41
+ end
42
+
43
+ add_swagger_documentation
44
+ end
45
+ end
46
+
47
+ subject do
48
+ get '/swagger_doc'
49
+ JSON.parse(last_response.body)['paths']
50
+ end
51
+
52
+ specify do
53
+ expect(subject.keys).to match_array ['/grunnbeløp', '/amount', '/εσόδων', '/数']
54
+ end
55
+ end
@@ -9,6 +9,7 @@ RSpec.shared_context 'namespace example' do
9
9
  namespace :hudson do
10
10
  desc 'Document root'
11
11
  get '/' do
12
+ { message: 'hi' }
12
13
  end
13
14
  end
14
15
 
@@ -81,6 +81,7 @@ describe 'swagger spec v2.0' do
81
81
  requires :id, type: Integer
82
82
  end
83
83
  delete '/dummy/:id' do
84
+ {}
84
85
  end
85
86
 
86
87
  namespace :other_thing do
@@ -80,6 +80,7 @@ describe 'swagger spec v2.0' do
80
80
  requires :id, type: Integer
81
81
  end
82
82
  delete '/dummy/:id' do
83
+ {}
83
84
  end
84
85
 
85
86
  namespace :other_thing do
@@ -11,6 +11,7 @@ describe 'Boolean Params' do
11
11
  requires :a_boolean, type: Grape::API::Boolean
12
12
  end
13
13
  post :splines do
14
+ { message: 'hi' }
14
15
  end
15
16
 
16
17
  add_swagger_documentation
@@ -11,6 +11,7 @@ describe 'Float Params' do
11
11
  requires :a_float, type: Float
12
12
  end
13
13
  post :splines do
14
+ { message: 'hi' }
14
15
  end
15
16
 
16
17
  add_swagger_documentation
@@ -17,6 +17,7 @@ describe 'namespace tags check while using prefix and version' do
17
17
  namespace :hudson do
18
18
  desc 'Document root'
19
19
  get '/' do
20
+ { message: 'hi' }
20
21
  end
21
22
  end
22
23
 
@@ -16,6 +16,7 @@ describe 'Params Multi Types' do
16
16
  requires :another_input, type: [String, Integer]
17
17
  end
18
18
  post :action do
19
+ { message: 'hi' }
19
20
  end
20
21
 
21
22
  add_swagger_documentation
@@ -61,6 +62,7 @@ describe 'Params Multi Types' do
61
62
  requires :another_input, type: [String, Integer]
62
63
  end
63
64
  post :action do
65
+ { message: 'hi' }
64
66
  end
65
67
 
66
68
  add_swagger_documentation
@@ -11,12 +11,14 @@ describe 'Params Types' do
11
11
  requires :input, type: String
12
12
  end
13
13
  post :action do
14
+ { message: 'hi' }
14
15
  end
15
16
 
16
17
  params do
17
18
  requires :input, type: String, default: '14', documentation: { type: 'email', default: '42' }
18
19
  end
19
20
  post :action_with_doc do
21
+ { message: 'hi' }
20
22
  end
21
23
 
22
24
  add_swagger_documentation
@@ -49,6 +51,7 @@ describe 'Params Types' do
49
51
  requires :input, type: String
50
52
  end
51
53
  post :action do
54
+ { message: 'hi' }
52
55
  end
53
56
 
54
57
  add_swagger_documentation
@@ -12,24 +12,28 @@ describe 'Convert values to enum or Range' do
12
12
  requires :letter, type: String, values: %w[a b c]
13
13
  end
14
14
  post :plain_array do
15
+ { message: 'hi' }
15
16
  end
16
17
 
17
18
  params do
18
19
  requires :letter, type: String, values: proc { %w[d e f] }
19
20
  end
20
21
  post :array_in_proc do
22
+ { message: 'hi' }
21
23
  end
22
24
 
23
25
  params do
24
26
  requires :letter, type: String, values: 'a'..'z'
25
27
  end
26
28
  post :range_letter do
29
+ { message: 'hi' }
27
30
  end
28
31
 
29
32
  params do
30
33
  requires :integer, type: Integer, values: -5..5
31
34
  end
32
35
  post :range_integer do
36
+ { message: 'hi' }
33
37
  end
34
38
 
35
39
  add_swagger_documentation
@@ -107,12 +111,14 @@ describe 'Convert values to enum for float range and not arrays inside a proc',
107
111
  requires :letter, type: String, values: proc { 'string' }
108
112
  end
109
113
  post :non_array_in_proc do
114
+ { message: 'hi' }
110
115
  end
111
116
 
112
117
  params do
113
118
  requires :float, type: Float, values: -5.0..5.0
114
119
  end
115
120
  post :range_float do
121
+ { message: 'hi' }
116
122
  end
117
123
 
118
124
  add_swagger_documentation
@@ -9,6 +9,7 @@ describe 'a simple mounted api' do
9
9
  class SimpleMountedApi < Grape::API
10
10
  desc 'Document root'
11
11
  get do
12
+ { message: 'hi' }
12
13
  end
13
14
 
14
15
  desc 'This gets something.',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Vandecasteele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-04 00:00:00.000000000 Z
11
+ date: 2020-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -95,7 +95,9 @@ files:
95
95
  - spec/issues/650_params_array_spec.rb
96
96
  - spec/issues/680_keep_204_error_schemas_spec.rb
97
97
  - spec/issues/751_deeply_nested_objects_spec.rb
98
+ - spec/issues/776_multiple_presents_spec.rb
98
99
  - spec/issues/784_extensions_on_params_spec.rb
100
+ - spec/issues/809_utf8_routes_spec.rb
99
101
  - spec/lib/data_type_spec.rb
100
102
  - spec/lib/endpoint/params_parser_spec.rb
101
103
  - spec/lib/endpoint_spec.rb
@@ -220,7 +222,9 @@ test_files:
220
222
  - spec/issues/650_params_array_spec.rb
221
223
  - spec/issues/680_keep_204_error_schemas_spec.rb
222
224
  - spec/issues/751_deeply_nested_objects_spec.rb
225
+ - spec/issues/776_multiple_presents_spec.rb
223
226
  - spec/issues/784_extensions_on_params_spec.rb
227
+ - spec/issues/809_utf8_routes_spec.rb
224
228
  - spec/lib/data_type_spec.rb
225
229
  - spec/lib/endpoint/params_parser_spec.rb
226
230
  - spec/lib/endpoint_spec.rb