grape-swagger 1.2.1 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +14 -0
- data/.github/workflows/rubocop.yml +26 -0
- data/.github/workflows/ruby.yml +32 -0
- data/.rubocop.yml +15 -4
- data/.rubocop_todo.yml +1 -1
- data/CHANGELOG.md +37 -0
- data/Gemfile +2 -2
- data/README.md +84 -3
- data/UPGRADING.md +8 -0
- data/grape-swagger.gemspec +3 -3
- data/lib/grape-swagger/doc_methods/format_data.rb +2 -2
- data/lib/grape-swagger/doc_methods/move_params.rb +1 -1
- data/lib/grape-swagger/doc_methods/parse_params.rb +6 -0
- data/lib/grape-swagger/doc_methods.rb +65 -62
- data/lib/grape-swagger/endpoint.rb +55 -15
- data/lib/grape-swagger/errors.rb +2 -0
- data/lib/grape-swagger/model_parsers.rb +2 -2
- data/lib/grape-swagger/rake/oapi_tasks.rb +2 -0
- data/lib/grape-swagger/version.rb +1 -1
- data/lib/grape-swagger.rb +5 -2
- data/spec/issues/537_enum_values_spec.rb +1 -0
- data/spec/issues/776_multiple_presents_spec.rb +59 -0
- data/spec/issues/809_utf8_routes_spec.rb +55 -0
- data/spec/lib/format_data_spec.rb +24 -0
- data/spec/lib/move_params_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/support/empty_model_parser.rb +2 -0
- data/spec/support/model_parsers/entity_parser.rb +8 -8
- data/spec/support/model_parsers/mock_parser.rb +24 -8
- data/spec/support/model_parsers/representable_parser.rb +8 -8
- data/spec/support/namespace_tags.rb +3 -0
- data/spec/support/the_paths_definitions.rb +4 -4
- data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +1 -0
- data/spec/swagger_v2/api_swagger_v2_response_with_headers_spec.rb +4 -2
- data/spec/swagger_v2/api_swagger_v2_response_with_models_spec.rb +53 -0
- data/spec/swagger_v2/api_swagger_v2_spec.rb +1 -0
- data/spec/swagger_v2/boolean_params_spec.rb +1 -0
- data/spec/swagger_v2/float_api_spec.rb +1 -0
- data/spec/swagger_v2/inheritance_and_discriminator_spec.rb +1 -0
- data/spec/swagger_v2/namespace_tags_prefix_spec.rb +1 -0
- data/spec/swagger_v2/param_multi_type_spec.rb +2 -0
- data/spec/swagger_v2/param_type_spec.rb +3 -0
- data/spec/swagger_v2/param_values_spec.rb +6 -0
- data/spec/swagger_v2/{params_array_collection_fromat_spec.rb → params_array_collection_format_spec.rb} +0 -0
- data/spec/swagger_v2/params_example_spec.rb +40 -0
- data/spec/swagger_v2/reference_entity_spec.rb +2 -2
- data/spec/swagger_v2/simple_mounted_api_spec.rb +3 -0
- metadata +19 -7
- data/.travis.yml +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2bc71f9cae392ed1e1fcb574570c75d800d99120c98ad9061b263edf80dc997d
|
4
|
+
data.tar.gz: f2cf0bca726860789875007043edb8ce689e7e779eb3273b1f376cb5b161949f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e6beba3aaba4f3fa5fc152127ad84c5dcae6c44bffecf276ae778e9ce779eba2a90feb40a7110afaffff1afc4ab0af7d2128fddf993ea304621e2a9428e2435
|
7
|
+
data.tar.gz: 3190175c602571c28e1cb2678b7c94ce0ac4f663cc163d6dd03c03a0e3fee5b75a80e3ff3a3e3f6e0eb964727cccd5dc7914db32dee98901e2000c0ebd2d33a8
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# To get started with Dependabot version updates, you'll need to specify which
|
2
|
+
# package ecosystems to update and where the package manifests are located.
|
3
|
+
# Please see the documentation for all configuration options:
|
4
|
+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
5
|
+
|
6
|
+
version: 2
|
7
|
+
updates:
|
8
|
+
- package-ecosystem: "bundler" # See documentation for possible values
|
9
|
+
directory: "/" # Location of package manifests
|
10
|
+
schedule:
|
11
|
+
interval: "weekly"
|
12
|
+
day: "friday"
|
13
|
+
assignees:
|
14
|
+
- "LeFnord"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
name: Rubocop
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- '*'
|
7
|
+
pull_request:
|
8
|
+
branches:
|
9
|
+
- '*'
|
10
|
+
|
11
|
+
jobs:
|
12
|
+
rubocop:
|
13
|
+
name: Rubocop
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
steps:
|
16
|
+
- uses: actions/checkout@v2
|
17
|
+
- uses: actions/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: '3.0'
|
20
|
+
- run: gem install rubocop --no-doc
|
21
|
+
- run: rubocop --format progress --format json --out rubocop.json
|
22
|
+
id: rubocop
|
23
|
+
- uses: duderman/rubocop-annotate-action@v0.1.0
|
24
|
+
with:
|
25
|
+
path: rubocop.json
|
26
|
+
if: ${{ failure() }}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- '*'
|
7
|
+
pull_request:
|
8
|
+
branches:
|
9
|
+
- '*'
|
10
|
+
|
11
|
+
jobs:
|
12
|
+
rspec:
|
13
|
+
runs-on: ubuntu-latest
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
17
|
+
grape-version: [1.5.3, 1.4.0, 1.3.3]
|
18
|
+
model-parser: [grape-swagger-entity, grape-swagger-representable, '']
|
19
|
+
|
20
|
+
steps:
|
21
|
+
- uses: actions/checkout@v2
|
22
|
+
- name: Set up Ruby
|
23
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
24
|
+
with:
|
25
|
+
ruby-version: ${{ matrix.ruby-version }}
|
26
|
+
GRAPE_VERSION: ${{ matrix.grape-version }}
|
27
|
+
MODEL_PARSER: ${{ matrix.model-parser }}
|
28
|
+
bundler-cache: true
|
29
|
+
- name: Run rspec
|
30
|
+
run: bundle exec rspec
|
31
|
+
- name: Run rubocop
|
32
|
+
run: bundle exec rubocop
|
data/.rubocop.yml
CHANGED
@@ -4,7 +4,9 @@ AllCops:
|
|
4
4
|
Exclude:
|
5
5
|
- vendor/**/*
|
6
6
|
- example/**/*
|
7
|
-
|
7
|
+
NewCops: enable
|
8
|
+
TargetRubyVersion: 3.0
|
9
|
+
SuggestExtensions: false
|
8
10
|
|
9
11
|
# Layout stuff
|
10
12
|
#
|
@@ -27,6 +29,10 @@ Layout/SpaceAroundMethodCallOperator:
|
|
27
29
|
|
28
30
|
# Lint stuff
|
29
31
|
#
|
32
|
+
Lint/ConstantDefinitionInBlock:
|
33
|
+
Exclude:
|
34
|
+
- spec/**/*
|
35
|
+
|
30
36
|
Lint/DeprecatedOpenSSLConstant:
|
31
37
|
Enabled: true
|
32
38
|
|
@@ -49,7 +55,7 @@ Metrics/BlockLength:
|
|
49
55
|
- spec/**/*
|
50
56
|
|
51
57
|
Metrics/ClassLength:
|
52
|
-
Max:
|
58
|
+
Max: 350
|
53
59
|
|
54
60
|
Metrics/CyclomaticComplexity:
|
55
61
|
Max: 17
|
@@ -68,6 +74,9 @@ Naming:
|
|
68
74
|
Style/AccessorGrouping:
|
69
75
|
Enabled: true
|
70
76
|
|
77
|
+
Style/AsciiComments:
|
78
|
+
Enabled: false
|
79
|
+
|
71
80
|
Style/ArrayCoercion:
|
72
81
|
Enabled: true
|
73
82
|
|
@@ -80,8 +89,12 @@ Style/CaseLikeIf:
|
|
80
89
|
Style/ExponentialNotation:
|
81
90
|
Enabled: true
|
82
91
|
|
92
|
+
Style/ExplicitBlockArgument:
|
93
|
+
Enabled: false
|
94
|
+
|
83
95
|
Style/HashAsLastArrayItem:
|
84
96
|
Enabled: true
|
97
|
+
|
85
98
|
Style/HashEachMethods:
|
86
99
|
Enabled: true
|
87
100
|
|
@@ -114,5 +127,3 @@ Style/RedundantRegexpEscape:
|
|
114
127
|
|
115
128
|
Style/SlicingWithRange:
|
116
129
|
Enabled: false
|
117
|
-
|
118
|
-
|
data/.rubocop_todo.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -9,6 +9,43 @@
|
|
9
9
|
* Your contribution here.
|
10
10
|
|
11
11
|
|
12
|
+
### 1.4.1 (September 15, 2021)
|
13
|
+
|
14
|
+
#### Fixes
|
15
|
+
|
16
|
+
* [#833](https://github.com/ruby-grape/grape-swagger/pull/833): Fixes issue of examples not showing for `in: 'body'` parameters - [@stevenou](https://github.com/stevenou)
|
17
|
+
|
18
|
+
|
19
|
+
### 1.4.0 (March 20, 2021)
|
20
|
+
|
21
|
+
#### Features
|
22
|
+
|
23
|
+
* [#818](https://github.com/ruby-grape/grape-swagger/pull/818): Adds ruby 3.0 support - [@LeFnord](https://github.com/LeFnord).
|
24
|
+
* [#815](https://github.com/ruby-grape/grape-swagger/pull/815): Add required for multiple presents - [@MaximeRDY](https://github.com/MaximeRDY).
|
25
|
+
|
26
|
+
#### Fixes
|
27
|
+
|
28
|
+
* [#822](https://github.com/ruby-grape/grape-swagger/pull/822): Corrected the related parameter lookup on request params - [@Jack12816](https://github.com/Jack12816).
|
29
|
+
|
30
|
+
|
31
|
+
### 1.3.1 (November 1, 2020)
|
32
|
+
|
33
|
+
#### Features
|
34
|
+
|
35
|
+
* [#813](https://github.com/ruby-grape/grape-swagger/pull/813): Handle multiple presents - [@AntoineGuestin](https://github.com/AntoineGuestin).
|
36
|
+
|
37
|
+
#### Fixes
|
38
|
+
|
39
|
+
* [#811](https://github.com/ruby-grape/grape-swagger/pull/811): Fixes #809: supports utf8 route names - [@LeFnord](https://github.com/LeFnord).
|
40
|
+
|
41
|
+
|
42
|
+
### 1.3.0 (September 3, 2020)
|
43
|
+
|
44
|
+
#### Features
|
45
|
+
|
46
|
+
* [#804](https://github.com/ruby-grape/grape-swagger/pull/804): Don't overwrite model description with the route description - [@Bhacaz](https://github.com/Bhacaz).
|
47
|
+
|
48
|
+
|
12
49
|
### 1.2.1 (July 15, 2020)
|
13
50
|
|
14
51
|
#### Fixes
|
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.
|
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
|
30
|
+
gem 'rubocop', '~> 1.0', require: false
|
31
31
|
end
|
32
32
|
|
33
33
|
group :test do
|
data/README.md
CHANGED
@@ -451,6 +451,7 @@ add_swagger_documentation \
|
|
451
451
|
* [Collection Format](#collection-format)
|
452
452
|
* [Hiding parameters](#hiding-parameters)
|
453
453
|
* [Setting a Swagger default value](#default-value)
|
454
|
+
* [Example parameter value](#param-example)
|
454
455
|
* [Response documentation](#response)
|
455
456
|
* [Changing default status codes](#change-status)
|
456
457
|
* [File response](#file-response)
|
@@ -458,6 +459,7 @@ add_swagger_documentation \
|
|
458
459
|
* [Response examples documentation](#response-examples)
|
459
460
|
* [Response headers documentation](#response-headers)
|
460
461
|
* [Adding root element to responses](#response-root)
|
462
|
+
* [Multiple present Response](#multiple-response)
|
461
463
|
|
462
464
|
#### Swagger Header Parameters <a name="headers"></a>
|
463
465
|
|
@@ -768,8 +770,6 @@ params do
|
|
768
770
|
end
|
769
771
|
```
|
770
772
|
|
771
|
-
The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
|
772
|
-
|
773
773
|
Grape uses the option `default` to set a default value for optional parameters. This is different in that Grape will set your parameter to the provided default if the parameter is omitted, whereas the example value above will only set the value in the UI itself. This will set the Swagger `defaultValue` to the provided value. Note that the example value will override the Grape default value.
|
774
774
|
|
775
775
|
```ruby
|
@@ -779,6 +779,16 @@ params do
|
|
779
779
|
end
|
780
780
|
```
|
781
781
|
|
782
|
+
#### Example parameter value <a name="param-example"></a>
|
783
|
+
|
784
|
+
The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
|
785
|
+
|
786
|
+
```ruby
|
787
|
+
params do
|
788
|
+
requires :id, type: Integer, documentation: { example: 123 }
|
789
|
+
optional :name, type String, documentation: { example: 'Buddy Guy' }
|
790
|
+
end
|
791
|
+
```
|
782
792
|
|
783
793
|
#### Expose nested namespace as standalone route
|
784
794
|
|
@@ -859,10 +869,11 @@ get '/thing', failure: [
|
|
859
869
|
end
|
860
870
|
```
|
861
871
|
|
862
|
-
By adding a `model` key, e.g. this would be taken.
|
872
|
+
By adding a `model` key, e.g. this would be taken. Setting an empty string will act like an empty body.
|
863
873
|
```ruby
|
864
874
|
get '/thing', failure: [
|
865
875
|
{ code: 400, message: 'General error' },
|
876
|
+
{ code: 403, message: 'Forbidden error', model: '' },
|
866
877
|
{ code: 422, message: 'Invalid parameter entry', model: Entities::ApiError }
|
867
878
|
] do
|
868
879
|
# ...
|
@@ -1261,6 +1272,76 @@ The result will look like following:
|
|
1261
1272
|
}
|
1262
1273
|
}
|
1263
1274
|
```
|
1275
|
+
#### Multiple present Response <a name="multiple-response"></a>
|
1276
|
+
|
1277
|
+
You can specify a custom multiple response by using the `as` key:
|
1278
|
+
```ruby
|
1279
|
+
desc 'Multiple response',
|
1280
|
+
success: [
|
1281
|
+
{ model: Entities::EnumValues, as: :gender },
|
1282
|
+
{ model: Entities::Something, as: :somethings }
|
1283
|
+
]
|
1284
|
+
end
|
1285
|
+
|
1286
|
+
get '/things' do
|
1287
|
+
...
|
1288
|
+
end
|
1289
|
+
```
|
1290
|
+
The result will look like following:
|
1291
|
+
```
|
1292
|
+
"responses": {
|
1293
|
+
"200": {
|
1294
|
+
"description": "Multiple response",
|
1295
|
+
"schema":{
|
1296
|
+
"type":"object",
|
1297
|
+
"properties":{
|
1298
|
+
"gender":{
|
1299
|
+
"$ref":"#/definitions/EnumValues"
|
1300
|
+
},
|
1301
|
+
"somethings":{
|
1302
|
+
"$ref":"#/definitions/Something"
|
1303
|
+
}
|
1304
|
+
}
|
1305
|
+
}
|
1306
|
+
}
|
1307
|
+
}
|
1308
|
+
```
|
1309
|
+
You can also specify if the response is an array, with the `is_array` key:
|
1310
|
+
```ruby
|
1311
|
+
desc 'Multiple response with array',
|
1312
|
+
success: [
|
1313
|
+
{ model: Entities::EnumValues, as: :gender },
|
1314
|
+
{ model: Entities::Something, as: :somethings, is_array: true, required: true }
|
1315
|
+
]
|
1316
|
+
end
|
1317
|
+
|
1318
|
+
get '/things' do
|
1319
|
+
...
|
1320
|
+
end
|
1321
|
+
```
|
1322
|
+
The result will look like following:
|
1323
|
+
```
|
1324
|
+
"responses": {
|
1325
|
+
"200": {
|
1326
|
+
"description": "Multiple response with array",
|
1327
|
+
"schema":{
|
1328
|
+
"type":"object",
|
1329
|
+
"properties":{
|
1330
|
+
"gender":{
|
1331
|
+
"$ref":"#/definitions/EnumValues"
|
1332
|
+
},
|
1333
|
+
"somethings":{
|
1334
|
+
"type":"array",
|
1335
|
+
"items":{
|
1336
|
+
"$ref":"#/definitions/Something"
|
1337
|
+
}
|
1338
|
+
}
|
1339
|
+
},
|
1340
|
+
"required": ["somethings"]
|
1341
|
+
}
|
1342
|
+
}
|
1343
|
+
}
|
1344
|
+
```
|
1264
1345
|
|
1265
1346
|
## Using Grape Entities <a name="grape-entity"></a>
|
1266
1347
|
|
data/UPGRADING.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## Upgrading Grape-swagger
|
2
2
|
|
3
|
+
### Upgrading to >= 1.4.0
|
4
|
+
|
5
|
+
- Official support for ruby < 2.5 removed, ruby 2.5 only in testing mode, but no support.
|
6
|
+
|
7
|
+
### Upgrading to >= 1.3.0
|
8
|
+
|
9
|
+
- The model (entity) description no longer comes from the route description. It will have a default value: `<<EntityName>> model`.
|
10
|
+
|
3
11
|
### Upgrading to >= 1.2.0
|
4
12
|
|
5
13
|
- The entity_name class method is now called on parent classes for inherited entities. Now you can do this
|
data/grape-swagger.gemspec
CHANGED
@@ -7,13 +7,13 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.name = 'grape-swagger'
|
8
8
|
s.version = GrapeSwagger::VERSION
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
|
-
s.authors = ['Tim Vandecasteele']
|
11
|
-
s.email = ['tim.vandecasteele@gmail.com']
|
10
|
+
s.authors = ['LeFnord', 'Tim Vandecasteele']
|
11
|
+
s.email = ['pscholz.le@gmail.com', 'tim.vandecasteele@gmail.com']
|
12
12
|
s.homepage = 'https://github.com/ruby-grape/grape-swagger'
|
13
13
|
s.summary = 'Add auto generated documentation to your Grape API that can be displayed with Swagger.'
|
14
14
|
s.license = 'MIT'
|
15
15
|
|
16
|
-
s.required_ruby_version = '>= 2.
|
16
|
+
s.required_ruby_version = '>= 2.5'
|
17
17
|
s.add_runtime_dependency 'grape', '~> 1.3'
|
18
18
|
|
19
19
|
s.files = `git ls-files`.split("\n")
|
@@ -7,7 +7,7 @@ module GrapeSwagger
|
|
7
7
|
def to_format(parameters)
|
8
8
|
parameters.reject { |parameter| parameter[:in] == 'body' }.each do |b|
|
9
9
|
related_parameters = parameters.select do |p|
|
10
|
-
p[:name] != b[:name] && p[:name].to_s.
|
10
|
+
p[:name] != b[:name] && p[:name].to_s.start_with?("#{b[:name].to_s.gsub(/\[\]\z/, '')}[")
|
11
11
|
end
|
12
12
|
parameters.reject! { |p| p[:name] == b[:name] } if move_down(b, related_parameters)
|
13
13
|
end
|
@@ -30,7 +30,7 @@ module GrapeSwagger
|
|
30
30
|
|
31
31
|
def add_braces(parameter, related_parameters)
|
32
32
|
param_name = parameter[:name].gsub(/\A(.*)\[\]\z/, '\1')
|
33
|
-
related_parameters.each { |p| p[:name] = p[:name].gsub(param_name, param_name
|
33
|
+
related_parameters.each { |p| p[:name] = p[:name].gsub(param_name, "#{param_name}[]") }
|
34
34
|
end
|
35
35
|
|
36
36
|
def add_array(parameter, related_parameters)
|
@@ -181,7 +181,7 @@ module GrapeSwagger
|
|
181
181
|
end
|
182
182
|
|
183
183
|
def property_keys
|
184
|
-
%i[type format description minimum maximum items enum default additionalProperties]
|
184
|
+
%i[type format description minimum maximum items enum default additionalProperties example]
|
185
185
|
end
|
186
186
|
|
187
187
|
def deletable?(param)
|
@@ -27,6 +27,7 @@ module GrapeSwagger
|
|
27
27
|
document_required(settings)
|
28
28
|
document_additional_properties(settings)
|
29
29
|
document_add_extensions(settings)
|
30
|
+
document_example(settings)
|
30
31
|
|
31
32
|
@parsed_param
|
32
33
|
end
|
@@ -112,6 +113,11 @@ module GrapeSwagger
|
|
112
113
|
@parsed_param[:additionalProperties] = additional_properties if additional_properties
|
113
114
|
end
|
114
115
|
|
116
|
+
def document_example(settings)
|
117
|
+
example = settings[:example]
|
118
|
+
@parsed_param[:example] = example if example
|
119
|
+
end
|
120
|
+
|
115
121
|
def param_type(value_type)
|
116
122
|
param_type = value_type[:param_type] || value_type[:in]
|
117
123
|
if value_type[:path].include?("{#{value_type[:param_name]}}")
|
@@ -17,6 +17,61 @@ require 'grape-swagger/doc_methods/version'
|
|
17
17
|
|
18
18
|
module GrapeSwagger
|
19
19
|
module DocMethods
|
20
|
+
DEFAULTS =
|
21
|
+
{
|
22
|
+
info: {},
|
23
|
+
models: [],
|
24
|
+
doc_version: '0.0.1',
|
25
|
+
target_class: nil,
|
26
|
+
mount_path: '/swagger_doc',
|
27
|
+
host: nil,
|
28
|
+
base_path: nil,
|
29
|
+
add_base_path: false,
|
30
|
+
add_version: true,
|
31
|
+
add_root: false,
|
32
|
+
hide_documentation_path: true,
|
33
|
+
format: :json,
|
34
|
+
authorizations: nil,
|
35
|
+
security_definitions: nil,
|
36
|
+
security: nil,
|
37
|
+
api_documentation: { desc: 'Swagger compatible API description' },
|
38
|
+
specific_api_documentation: { desc: 'Swagger compatible API description for specific API' },
|
39
|
+
endpoint_auth_wrapper: nil,
|
40
|
+
swagger_endpoint_guard: nil,
|
41
|
+
token_owner: nil
|
42
|
+
}.freeze
|
43
|
+
|
44
|
+
FORMATTER_METHOD = %i[format default_format default_error_formatter].freeze
|
45
|
+
|
46
|
+
def self.output_path_definitions(combi_routes, endpoint, target_class, options)
|
47
|
+
output = endpoint.swagger_object(
|
48
|
+
target_class,
|
49
|
+
endpoint.request,
|
50
|
+
options
|
51
|
+
)
|
52
|
+
|
53
|
+
paths, definitions = endpoint.path_and_definition_objects(combi_routes, options)
|
54
|
+
tags = tags_from(paths, options)
|
55
|
+
|
56
|
+
output[:tags] = tags unless tags.empty? || paths.blank?
|
57
|
+
output[:paths] = paths unless paths.blank?
|
58
|
+
output[:definitions] = definitions unless definitions.blank?
|
59
|
+
|
60
|
+
output
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.tags_from(paths, options)
|
64
|
+
tags = GrapeSwagger::DocMethods::TagNameDescription.build(paths)
|
65
|
+
|
66
|
+
if options[:tags]
|
67
|
+
names = options[:tags].map { |t| t[:name] }
|
68
|
+
tags.reject! { |t| names.include?(t[:name]) }
|
69
|
+
tags += options[:tags]
|
70
|
+
end
|
71
|
+
|
72
|
+
tags
|
73
|
+
end
|
74
|
+
|
20
75
|
def hide_documentation_path
|
21
76
|
@@hide_documentation_path
|
22
77
|
end
|
@@ -26,54 +81,32 @@ module GrapeSwagger
|
|
26
81
|
end
|
27
82
|
|
28
83
|
def setup(options)
|
29
|
-
options =
|
84
|
+
options = DEFAULTS.merge(options)
|
30
85
|
|
31
86
|
# options could be set on #add_swagger_documentation call,
|
32
87
|
# for available options see #defaults
|
33
88
|
target_class = options[:target_class]
|
34
89
|
guard = options[:swagger_endpoint_guard]
|
35
|
-
formatter = options[:format]
|
36
90
|
api_doc = options[:api_documentation].dup
|
37
91
|
specific_api_doc = options[:specific_api_documentation].dup
|
38
92
|
|
39
93
|
class_variables_from(options)
|
40
94
|
|
41
|
-
|
42
|
-
%i[format default_format default_error_formatter].each do |method|
|
43
|
-
send(method, formatter)
|
44
|
-
end
|
45
|
-
end
|
95
|
+
setup_formatter(options[:format])
|
46
96
|
|
47
97
|
desc api_doc.delete(:desc), api_doc
|
48
98
|
|
49
|
-
output_path_definitions = proc do |combi_routes, endpoint|
|
50
|
-
output = endpoint.swagger_object(
|
51
|
-
target_class,
|
52
|
-
endpoint.request,
|
53
|
-
options
|
54
|
-
)
|
55
|
-
|
56
|
-
paths, definitions = endpoint.path_and_definition_objects(combi_routes, options)
|
57
|
-
tags = tags_from(paths, options)
|
58
|
-
|
59
|
-
output[:tags] = tags unless tags.empty? || paths.blank?
|
60
|
-
output[:paths] = paths unless paths.blank?
|
61
|
-
output[:definitions] = definitions unless definitions.blank?
|
62
|
-
|
63
|
-
output
|
64
|
-
end
|
65
|
-
|
66
99
|
instance_eval(guard) unless guard.nil?
|
67
100
|
|
68
101
|
get mount_path do
|
69
102
|
header['Access-Control-Allow-Origin'] = '*'
|
70
103
|
header['Access-Control-Request-Method'] = '*'
|
71
104
|
|
72
|
-
|
105
|
+
GrapeSwagger::DocMethods
|
106
|
+
.output_path_definitions(target_class.combined_namespace_routes, self, target_class, options)
|
73
107
|
end
|
74
108
|
|
75
|
-
desc specific_api_doc.delete(:desc), { params:
|
76
|
-
specific_api_doc.delete(:params) || {} }.merge(specific_api_doc)
|
109
|
+
desc specific_api_doc.delete(:desc), { params: specific_api_doc.delete(:params) || {}, **specific_api_doc }
|
77
110
|
|
78
111
|
params do
|
79
112
|
requires :name, type: String, desc: 'Resource name of mounted API'
|
@@ -88,51 +121,21 @@ module GrapeSwagger
|
|
88
121
|
combined_routes = target_class.combined_namespace_routes[params[:name]]
|
89
122
|
error!({ error: 'named resource not exist' }, 400) if combined_routes.nil?
|
90
123
|
|
91
|
-
|
124
|
+
GrapeSwagger::DocMethods
|
125
|
+
.output_path_definitions({ params[:name] => combined_routes }, self, target_class, options)
|
92
126
|
end
|
93
127
|
end
|
94
128
|
|
95
|
-
def defaults
|
96
|
-
{
|
97
|
-
info: {},
|
98
|
-
models: [],
|
99
|
-
doc_version: '0.0.1',
|
100
|
-
target_class: nil,
|
101
|
-
mount_path: '/swagger_doc',
|
102
|
-
host: nil,
|
103
|
-
base_path: nil,
|
104
|
-
add_base_path: false,
|
105
|
-
add_version: true,
|
106
|
-
add_root: false,
|
107
|
-
hide_documentation_path: true,
|
108
|
-
format: :json,
|
109
|
-
authorizations: nil,
|
110
|
-
security_definitions: nil,
|
111
|
-
security: nil,
|
112
|
-
api_documentation: { desc: 'Swagger compatible API description' },
|
113
|
-
specific_api_documentation: { desc: 'Swagger compatible API description for specific API' },
|
114
|
-
endpoint_auth_wrapper: nil,
|
115
|
-
swagger_endpoint_guard: nil,
|
116
|
-
token_owner: nil
|
117
|
-
}
|
118
|
-
end
|
119
|
-
|
120
129
|
def class_variables_from(options)
|
121
130
|
@@mount_path = options[:mount_path]
|
122
131
|
@@class_name = options[:class_name] || options[:mount_path].delete('/')
|
123
132
|
@@hide_documentation_path = options[:hide_documentation_path]
|
124
133
|
end
|
125
134
|
|
126
|
-
def
|
127
|
-
|
135
|
+
def setup_formatter(formatter)
|
136
|
+
return unless formatter
|
128
137
|
|
129
|
-
|
130
|
-
names = options[:tags].map { |t| t[:name] }
|
131
|
-
tags.reject! { |t| names.include?(t[:name]) }
|
132
|
-
tags += options[:tags]
|
133
|
-
end
|
134
|
-
|
135
|
-
tags
|
138
|
+
FORMATTER_METHOD.each { |method| send(method, formatter) }
|
136
139
|
end
|
137
140
|
end
|
138
141
|
end
|