grape-swagger 1.5.0 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +86 -11
- data/.rubocop.yml +1 -1
- data/.rubocop_todo.yml +13 -28
- data/CHANGELOG.md +15 -0
- data/Gemfile +3 -5
- data/README.md +69 -5
- data/lib/grape-swagger/doc_methods/format_data.rb +2 -2
- data/lib/grape-swagger/doc_methods/parse_params.rb +1 -1
- data/lib/grape-swagger/endpoint.rb +22 -5
- data/lib/grape-swagger/version.rb +1 -1
- data/spec/issues/677_consumes_produces_add_swagger_documentation_options_spec.rb +100 -0
- data/spec/lib/oapi_tasks_spec.rb +1 -1
- data/spec/lib/parse_params_spec.rb +14 -0
- data/spec/swagger_v2/api_documentation_spec.rb +4 -20
- data/spec/swagger_v2/api_swagger_v2_response_with_models_spec.rb +4 -2
- metadata +4 -4
- data/.ruby-gemset +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 994c2be4c9a0a44106876c31785adc73458d67c669ac80db7352e831511b8b2d
|
4
|
+
data.tar.gz: ae834e4ab3533e107eec6f74763a3679da626fd1fffe0334fe9546b54b6f12e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8a3cafba1066fef61afcc744f9fc39f7aa607765a772cc071e4ac6d931c61a7810b6876bf2c35ba1722ce20b939e19a512983f4e185bae473c09486b06d4c8a
|
7
|
+
data.tar.gz: cc5f17f8c99c2abccc49bf1da169134dcfdbf249d5f039c10eb00ab8d373cd03a29afc1c46534a296d8b4fc4d2b82eae969561bf64ca47abd7405e67c26ee6fa
|
data/.github/workflows/ci.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
name:
|
1
|
+
name: RSpec
|
2
2
|
|
3
3
|
on:
|
4
4
|
push:
|
@@ -18,19 +18,19 @@ jobs:
|
|
18
18
|
- uses: actions/checkout@v3
|
19
19
|
- uses: ruby/setup-ruby@v1
|
20
20
|
with:
|
21
|
-
ruby-version: '3.
|
21
|
+
ruby-version: '3.2'
|
22
22
|
bundler-cache: true
|
23
23
|
- name: Run rubocop
|
24
24
|
run: bundle exec rubocop --parallel --format progress
|
25
25
|
|
26
|
-
|
26
|
+
grape-16:
|
27
27
|
runs-on: ubuntu-latest
|
28
28
|
needs: ['rubocop']
|
29
|
+
env:
|
30
|
+
GRAPE_VERSION: '1.6.2'
|
29
31
|
strategy:
|
30
32
|
matrix:
|
31
|
-
ruby-version: ['
|
32
|
-
grape-version: [1.6.2, 1.5.3]
|
33
|
-
model-parser: [grape-swagger-entity, grape-swagger-representable, '']
|
33
|
+
ruby-version: ['3.0', '3.1', '3.2', 'head']
|
34
34
|
steps:
|
35
35
|
- name: Check out branch
|
36
36
|
uses: actions/checkout@v3
|
@@ -38,8 +38,83 @@ jobs:
|
|
38
38
|
uses: ruby/setup-ruby@v1
|
39
39
|
with:
|
40
40
|
ruby-version: ${{ matrix.ruby-version }}
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
- name: Run rspec wo model parser
|
42
|
+
run: |
|
43
|
+
bundle update
|
44
|
+
bundle exec rspec
|
45
|
+
- name: Run rspec w entity parser
|
46
|
+
env:
|
47
|
+
MODEL_PARSER: grape-swagger-entity
|
48
|
+
run: |
|
49
|
+
bundle update
|
50
|
+
bundle exec rspec
|
51
|
+
- name: Run rspec w representable parser
|
52
|
+
env:
|
53
|
+
MODEL_PARSER: grape-swagger-representable
|
54
|
+
run: |
|
55
|
+
bundle update
|
56
|
+
bundle exec rspec
|
57
|
+
|
58
|
+
grape-17:
|
59
|
+
runs-on: ubuntu-latest
|
60
|
+
needs: ['rubocop']
|
61
|
+
env:
|
62
|
+
GRAPE_VERSION: '1.7.1'
|
63
|
+
strategy:
|
64
|
+
matrix:
|
65
|
+
ruby-version: ['3.0', '3.1', '3.2', 'head']
|
66
|
+
steps:
|
67
|
+
- name: Check out branch
|
68
|
+
uses: actions/checkout@v3
|
69
|
+
- name: Set up Ruby
|
70
|
+
uses: ruby/setup-ruby@v1
|
71
|
+
with:
|
72
|
+
ruby-version: ${{ matrix.ruby-version }}
|
73
|
+
- name: Run rspec wo model parser
|
74
|
+
run: |
|
75
|
+
bundle update
|
76
|
+
bundle exec rspec
|
77
|
+
- name: Run rspec w entity parser
|
78
|
+
env:
|
79
|
+
MODEL_PARSER: grape-swagger-entity
|
80
|
+
run: |
|
81
|
+
bundle update
|
82
|
+
bundle exec rspec
|
83
|
+
- name: Run rspec w representable parser
|
84
|
+
env:
|
85
|
+
MODEL_PARSER: grape-swagger-representable
|
86
|
+
run: |
|
87
|
+
bundle update
|
88
|
+
bundle exec rspec
|
89
|
+
|
90
|
+
grape-HEAD:
|
91
|
+
runs-on: ubuntu-latest
|
92
|
+
needs: ['rubocop']
|
93
|
+
env:
|
94
|
+
GRAPE_VERSION: 'HEAD'
|
95
|
+
strategy:
|
96
|
+
matrix:
|
97
|
+
ruby-version: ['3.0', '3.1', '3.2', 'head']
|
98
|
+
steps:
|
99
|
+
- name: Check out branch
|
100
|
+
uses: actions/checkout@v3
|
101
|
+
- name: Set up Ruby
|
102
|
+
uses: ruby/setup-ruby@v1
|
103
|
+
with:
|
104
|
+
ruby-version: ${{ matrix.ruby-version }}
|
105
|
+
- name: Run rspec wo model parser
|
106
|
+
run: |
|
107
|
+
bundle update
|
108
|
+
bundle exec rspec
|
109
|
+
- name: Run rspec w entity parser
|
110
|
+
env:
|
111
|
+
MODEL_PARSER: grape-swagger-entity
|
112
|
+
run: |
|
113
|
+
bundle update
|
114
|
+
bundle exec rspec
|
115
|
+
- name: Run rspec w representable parser
|
116
|
+
env:
|
117
|
+
MODEL_PARSER: grape-swagger-representable
|
118
|
+
run: |
|
119
|
+
bundle update
|
120
|
+
bundle exec rspec
|
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -1,38 +1,30 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on
|
3
|
+
# on 2023-05-20 18:23:47 UTC using RuboCop version 1.51.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
9
|
# Offense count: 1
|
10
|
-
#
|
11
|
-
# Configuration parameters: Include.
|
12
|
-
# Include: **/*.gemspec
|
13
|
-
Gemspec/RequireMFA:
|
14
|
-
Exclude:
|
15
|
-
- 'grape-swagger.gemspec'
|
16
|
-
|
17
|
-
# Offense count: 1
|
18
|
-
# Configuration parameters: Include.
|
10
|
+
# Configuration parameters: Severity, Include.
|
19
11
|
# Include: **/*.gemspec
|
20
12
|
Gemspec/RequiredRubyVersion:
|
21
13
|
Exclude:
|
22
14
|
- 'grape-swagger.gemspec'
|
23
15
|
|
24
|
-
# Offense count:
|
25
|
-
# Configuration parameters:
|
16
|
+
# Offense count: 32
|
17
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
|
26
18
|
Metrics/AbcSize:
|
27
19
|
Max: 56
|
28
20
|
|
29
21
|
# Offense count: 30
|
30
|
-
# Configuration parameters: CountComments, CountAsOne,
|
22
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
31
23
|
Metrics/MethodLength:
|
32
24
|
Max: 28
|
33
25
|
|
34
|
-
# Offense count:
|
35
|
-
# Configuration parameters:
|
26
|
+
# Offense count: 8
|
27
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns.
|
36
28
|
Metrics/PerceivedComplexity:
|
37
29
|
Max: 16
|
38
30
|
|
@@ -41,20 +33,13 @@ Style/ClassVars:
|
|
41
33
|
Exclude:
|
42
34
|
- 'lib/grape-swagger/doc_methods.rb'
|
43
35
|
|
36
|
+
# Offense count: 1
|
37
|
+
# This cop supports unsafe autocorrection (--autocorrect-all).
|
38
|
+
Style/CollectionCompact:
|
39
|
+
Exclude:
|
40
|
+
- 'lib/grape-swagger/endpoint.rb'
|
41
|
+
|
44
42
|
# Offense count: 23
|
45
43
|
# Configuration parameters: AllowedConstants.
|
46
44
|
Style/Documentation:
|
47
45
|
Enabled: false
|
48
|
-
|
49
|
-
# Offense count: 43
|
50
|
-
Style/OpenStructUse:
|
51
|
-
Exclude:
|
52
|
-
- 'spec/lib/endpoint_spec.rb'
|
53
|
-
- 'spec/lib/version_spec.rb'
|
54
|
-
- 'spec/support/mock_parser.rb'
|
55
|
-
- 'spec/support/model_parsers/mock_parser.rb'
|
56
|
-
- 'spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb'
|
57
|
-
- 'spec/swagger_v2/api_swagger_v2_mounted_spec.rb'
|
58
|
-
- 'spec/swagger_v2/api_swagger_v2_spec.rb'
|
59
|
-
- 'spec/swagger_v2/errors_spec.rb'
|
60
|
-
- 'spec/swagger_v2/reference_entity_spec.rb'
|
data/CHANGELOG.md
CHANGED
@@ -9,6 +9,21 @@
|
|
9
9
|
* Your contribution here.
|
10
10
|
|
11
11
|
|
12
|
+
### 1.6.1 (May 21, 2023)
|
13
|
+
|
14
|
+
#### Fixes
|
15
|
+
|
16
|
+
* [#868](https://github.com/ruby-grape/grape-swagger/pull/896): Fix parsing endless range values - [@dhruvCW](https://github.com/dhruvCW)
|
17
|
+
|
18
|
+
|
19
|
+
### 1.6.0 (March 19, 2023)
|
20
|
+
|
21
|
+
#### Features
|
22
|
+
|
23
|
+
* [#872](https://github.com/ruby-grape/grape-swagger/pull/872): Add `consumes` and `produces` options to `add_swagger_documentation` - [@spaceraccoon](https://github.com/spaceraccoon)
|
24
|
+
* [#868](https://github.com/ruby-grape/grape-swagger/pull/868): Add `default` endpoint option to specify default response - [@dhruvCW](https://github.com/dhruvCW)
|
25
|
+
|
26
|
+
|
12
27
|
### 1.5.0 (July 28, 2022)
|
13
28
|
|
14
29
|
#### Features
|
data/Gemfile
CHANGED
@@ -2,11 +2,9 @@
|
|
2
2
|
|
3
3
|
source 'http://rubygems.org'
|
4
4
|
|
5
|
-
ruby RUBY_VERSION
|
6
|
-
|
7
5
|
gemspec
|
8
6
|
|
9
|
-
gem 'grape', case version = ENV.fetch('GRAPE_VERSION', '~> 1.
|
7
|
+
gem 'grape', case version = ENV.fetch('GRAPE_VERSION', '~> 1.7')
|
10
8
|
when 'HEAD'
|
11
9
|
{ git: 'https://github.com/ruby-grape/grape' }
|
12
10
|
else
|
@@ -21,13 +19,13 @@ group :development, :test do
|
|
21
19
|
gem 'pry', platforms: [:mri]
|
22
20
|
gem 'pry-byebug', platforms: [:mri]
|
23
21
|
|
24
|
-
gem 'rack'
|
22
|
+
gem 'rack'
|
25
23
|
gem 'rack-cors'
|
26
24
|
gem 'rack-test'
|
27
25
|
gem 'rake'
|
28
26
|
gem 'rdoc'
|
29
27
|
gem 'rspec', '~> 3.9'
|
30
|
-
gem 'rubocop', '~> 1.
|
28
|
+
gem 'rubocop', '~> 1.50', require: false
|
31
29
|
gem 'webrick'
|
32
30
|
end
|
33
31
|
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@
|
|
22
22
|
|
23
23
|
## What is grape-swagger? <a name="what"></a>
|
24
24
|
|
25
|
-
The grape-swagger gem provides an autogenerated documentation for your [Grape](https://github.com/ruby-grape/grape) API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in [Swagger UI](https://github.com/
|
25
|
+
The grape-swagger gem provides an autogenerated documentation for your [Grape](https://github.com/ruby-grape/grape) API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in [Swagger UI](https://github.com/swagger-api/swagger-ui). You should be able to point [the petstore demo](http://petstore.swagger.io/) to your API.
|
26
26
|
|
27
27
|
![Demo Screenshot](example/swagger-example.png)
|
28
28
|
|
@@ -35,7 +35,7 @@ This screenshot is based on the [Hussars](https://github.com/LeFnord/hussars) sa
|
|
35
35
|
* [Grape Swagger Entity](https://github.com/ruby-grape/grape-swagger-entity)
|
36
36
|
* [Grape Entity](https://github.com/ruby-grape/grape-entity)
|
37
37
|
* [Grape Swagger Representable](https://github.com/ruby-grape/grape-swagger-representable)
|
38
|
-
* [Swagger UI](https://github.com/
|
38
|
+
* [Swagger UI](https://github.com/swagger-api/swagger-ui)
|
39
39
|
|
40
40
|
|
41
41
|
|
@@ -95,14 +95,14 @@ module API
|
|
95
95
|
end
|
96
96
|
```
|
97
97
|
|
98
|
-
To explore your API, either download [Swagger UI](https://github.com/
|
98
|
+
To explore your API, either download [Swagger UI](https://github.com/swagger-api/swagger-ui) and set it up yourself or go to the [online swagger demo](http://petstore.swagger.io/) and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc).
|
99
99
|
|
100
100
|
|
101
101
|
|
102
102
|
## Model Parsers <a name="model_parsers"></a>
|
103
103
|
|
104
104
|
Since 0.21.0, `Grape::Entity` is not a part of grape-swagger, you need to add `grape-swagger-entity` manually to your Gemfile.
|
105
|
-
Also added support for [representable](https://github.com/
|
105
|
+
Also added support for [representable](https://github.com/trailblazer/representable) via `grape-swagger-representable`.
|
106
106
|
|
107
107
|
```ruby
|
108
108
|
# For Grape::Entity ( https://github.com/ruby-grape/grape-entity )
|
@@ -115,7 +115,7 @@ If you are not using Rails, make sure to load the parser inside your application
|
|
115
115
|
|
116
116
|
### Custom Model Parsers
|
117
117
|
|
118
|
-
You can create your own model parser, for example for [roar](https://github.com/
|
118
|
+
You can create your own model parser, for example for [roar](https://github.com/trailblazer/roar).
|
119
119
|
|
120
120
|
```ruby
|
121
121
|
module GrapeSwagger
|
@@ -206,6 +206,8 @@ end
|
|
206
206
|
* [array_use_braces](#array_use_braces)
|
207
207
|
* [api_documentation](#api_documentation)
|
208
208
|
* [specific_api_documentation](#specific_api_documentation)
|
209
|
+
* [consumes](#consumes)
|
210
|
+
* [produces](#produces)
|
209
211
|
|
210
212
|
You can pass a hash with optional configuration settings to ```add_swagger_documentation```.
|
211
213
|
The examples show the default value.
|
@@ -432,6 +434,24 @@ add_swagger_documentation \
|
|
432
434
|
specific_api_documentation: { desc: 'Reticulated splines API swagger-compatible endpoint documentation.' }
|
433
435
|
```
|
434
436
|
|
437
|
+
#### consumes
|
438
|
+
|
439
|
+
Customize the Swagger API default global `consumes` field value.
|
440
|
+
|
441
|
+
```ruby
|
442
|
+
add_swagger_documentation \
|
443
|
+
consumes: ['application/json', 'application/x-www-form-urlencoded']
|
444
|
+
```
|
445
|
+
|
446
|
+
#### produces
|
447
|
+
|
448
|
+
Customize the Swagger API default global `produces` field value.
|
449
|
+
|
450
|
+
```ruby
|
451
|
+
add_swagger_documentation \
|
452
|
+
produces: ['text/plain']
|
453
|
+
```
|
454
|
+
|
435
455
|
## Routes Configuration <a name="routes"></a>
|
436
456
|
|
437
457
|
* [Swagger Header Parameters](#headers)
|
@@ -1022,6 +1042,50 @@ end
|
|
1022
1042
|
}
|
1023
1043
|
```
|
1024
1044
|
|
1045
|
+
#### Default response <a name="default-response"></a>
|
1046
|
+
|
1047
|
+
By setting the `default` option you can also define a default response that is the result returned for all unspecified status codes.
|
1048
|
+
The definition supports the same syntax as `success` or `failure`.
|
1049
|
+
|
1050
|
+
In the following cases, the schema ref would be taken from route.
|
1051
|
+
|
1052
|
+
```ruby
|
1053
|
+
desc 'thing', default: { message: 'the default response' }
|
1054
|
+
get '/thing' do
|
1055
|
+
# ...
|
1056
|
+
end
|
1057
|
+
```
|
1058
|
+
The generated swagger section will be something like
|
1059
|
+
|
1060
|
+
```json
|
1061
|
+
"responses": {
|
1062
|
+
"default": {
|
1063
|
+
"description": "the default response"
|
1064
|
+
}
|
1065
|
+
},
|
1066
|
+
```
|
1067
|
+
Just like with `success` or `failure` you can also provide a `model` parameter.
|
1068
|
+
|
1069
|
+
```ruby
|
1070
|
+
desc 'Get a list of stuff',
|
1071
|
+
default: { model: Entities::UseResponse, message: 'the default response' }
|
1072
|
+
get do
|
1073
|
+
# your code comes here
|
1074
|
+
end
|
1075
|
+
```
|
1076
|
+
The generated swagger document will then correctly reference the model schema.
|
1077
|
+
|
1078
|
+
```json
|
1079
|
+
"responses": {
|
1080
|
+
"default": {
|
1081
|
+
"description": "the default response",
|
1082
|
+
"schema": {
|
1083
|
+
"$ref": "#/definitions/UseResponse"
|
1084
|
+
}
|
1085
|
+
}
|
1086
|
+
},
|
1087
|
+
```
|
1088
|
+
|
1025
1089
|
|
1026
1090
|
#### Extensions <a name="extensions"></a>
|
1027
1091
|
|
@@ -39,12 +39,12 @@ module GrapeSwagger
|
|
39
39
|
|
40
40
|
p_type = p[:type] == 'array' ? 'string' : p[:type]
|
41
41
|
p[:items] = { type: p_type, format: p[:format], enum: p[:enum], is_array: p[:is_array] }
|
42
|
-
p[:items].
|
42
|
+
p[:items].compact!
|
43
43
|
p[:type] = 'array'
|
44
44
|
p[:is_array] = parameter[:is_array]
|
45
45
|
p.delete(:format)
|
46
46
|
p.delete(:enum)
|
47
|
-
p.
|
47
|
+
p.compact!
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
@@ -5,7 +5,7 @@ require 'active_support/core_ext/string/inflections'
|
|
5
5
|
require 'grape-swagger/endpoint/params_parser'
|
6
6
|
|
7
7
|
module Grape
|
8
|
-
class Endpoint
|
8
|
+
class Endpoint # rubocop:disable Metrics/ClassLength
|
9
9
|
def content_types_for(target_class)
|
10
10
|
content_types = (target_class.content_types || {}).values
|
11
11
|
|
@@ -27,7 +27,8 @@ module Grape
|
|
27
27
|
object = {
|
28
28
|
info: info_object(options[:info].merge(version: options[:doc_version])),
|
29
29
|
swagger: '2.0',
|
30
|
-
produces: content_types_for(target_class),
|
30
|
+
produces: options[:produces] || content_types_for(target_class),
|
31
|
+
consumes: options[:consumes],
|
31
32
|
authorizations: options[:authorizations],
|
32
33
|
securityDefinitions: options[:security_definitions],
|
33
34
|
security: options[:security],
|
@@ -117,7 +118,7 @@ module Grape
|
|
117
118
|
method[:summary] = summary_object(route)
|
118
119
|
method[:description] = description_object(route)
|
119
120
|
method[:produces] = produces_object(route, options[:produces] || options[:format])
|
120
|
-
method[:consumes] = consumes_object(route, options[:format])
|
121
|
+
method[:consumes] = consumes_object(route, options[:consumes] || options[:format])
|
121
122
|
method[:parameters] = params_object(route, options, path)
|
122
123
|
method[:security] = security_object(route)
|
123
124
|
method[:responses] = response_object(route, options)
|
@@ -240,7 +241,8 @@ module Grape
|
|
240
241
|
if route.http_codes.is_a?(Array) && route.http_codes.any? { |code| success_code?(code) }
|
241
242
|
route.http_codes.clone
|
242
243
|
else
|
243
|
-
success_codes_from_route(route) + (route
|
244
|
+
success_codes_from_route(route) + default_code_from_route(route) +
|
245
|
+
(route.http_codes || route.options[:failure] || [])
|
244
246
|
end
|
245
247
|
end
|
246
248
|
|
@@ -267,6 +269,21 @@ module Grape
|
|
267
269
|
|
268
270
|
private
|
269
271
|
|
272
|
+
def default_code_from_route(route)
|
273
|
+
entity = route.options[:default_response]
|
274
|
+
return [] if entity.nil?
|
275
|
+
|
276
|
+
default_code = { code: 'default', message: 'Default Response' }
|
277
|
+
if entity.is_a?(Hash)
|
278
|
+
default_code[:message] = entity[:message] || default_code[:message]
|
279
|
+
default_code[:model] = entity[:model] if entity[:model].present?
|
280
|
+
else
|
281
|
+
default_code[:model] = entity
|
282
|
+
end
|
283
|
+
|
284
|
+
[default_code]
|
285
|
+
end
|
286
|
+
|
270
287
|
def build_memo_schema(memo, route, value, response_model, options)
|
271
288
|
if memo[value[:code]][:schema] && value[:as]
|
272
289
|
memo[value[:code]][:schema][:properties].merge!(build_reference(route, value, response_model, options))
|
@@ -294,7 +311,7 @@ module Grape
|
|
294
311
|
else
|
295
312
|
build_reference_hash(response_model)
|
296
313
|
end
|
297
|
-
return reference unless value[:code] < 300
|
314
|
+
return reference unless value[:code] == 'default' || value[:code] < 300
|
298
315
|
|
299
316
|
if value.key?(:as) && value.key?(:is_array)
|
300
317
|
reference[value[:as]] = build_reference_array(reference[value[:as]])
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe '#677 consumes and produces options are included in add_swagger_documentation options' do
|
6
|
+
describe 'no override' do
|
7
|
+
let(:app) do
|
8
|
+
Class.new(Grape::API) do
|
9
|
+
resource :accounts do
|
10
|
+
route_param :account_number, type: String do
|
11
|
+
resource :records do
|
12
|
+
route_param :id do
|
13
|
+
post do
|
14
|
+
{ message: 'hello world' }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
add_swagger_documentation \
|
22
|
+
format: :json
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
subject do
|
27
|
+
get '/swagger_doc'
|
28
|
+
JSON.parse(last_response.body)
|
29
|
+
end
|
30
|
+
|
31
|
+
specify do
|
32
|
+
expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['produces']).to eq ['application/json']
|
33
|
+
expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['consumes']).to eq ['application/json']
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'override produces' do
|
38
|
+
let(:app) do
|
39
|
+
Class.new(Grape::API) do
|
40
|
+
resource :accounts do
|
41
|
+
route_param :account_number, type: String do
|
42
|
+
resource :records do
|
43
|
+
route_param :id do
|
44
|
+
post do
|
45
|
+
{ message: 'hello world' }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
add_swagger_documentation \
|
53
|
+
format: :json,
|
54
|
+
produces: ['text/plain']
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
subject do
|
59
|
+
get '/swagger_doc'
|
60
|
+
JSON.parse(last_response.body)
|
61
|
+
end
|
62
|
+
|
63
|
+
specify do
|
64
|
+
expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['produces']).to eq ['text/plain']
|
65
|
+
expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['consumes']).to eq ['application/json']
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'override consumes' do
|
70
|
+
let(:app) do
|
71
|
+
Class.new(Grape::API) do
|
72
|
+
resource :accounts do
|
73
|
+
route_param :account_number, type: String do
|
74
|
+
resource :records do
|
75
|
+
route_param :id do
|
76
|
+
post do
|
77
|
+
{ message: 'hello world' }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
add_swagger_documentation \
|
85
|
+
format: :json, \
|
86
|
+
consumes: ['application/json', 'application/x-www-form-urlencoded']
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
subject do
|
91
|
+
get '/swagger_doc'
|
92
|
+
JSON.parse(last_response.body)
|
93
|
+
end
|
94
|
+
|
95
|
+
specify do
|
96
|
+
expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['produces']).to eq ['application/json']
|
97
|
+
expect(subject['paths']['/accounts/{account_number}/records/{id}']['post']['consumes']).to eq ['application/json', 'application/x-www-form-urlencoded']
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/lib/oapi_tasks_spec.rb
CHANGED
@@ -30,7 +30,7 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
|
|
30
30
|
|
31
31
|
describe '.new' do
|
32
32
|
it 'accepts class name as a constant' do
|
33
|
-
expect(described_class.new(
|
33
|
+
expect(described_class.new(Api::Base).send(:api_class)).to eq(Api::Base)
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'accepts class name as a string' do
|
@@ -12,6 +12,20 @@ describe GrapeSwagger::DocMethods::ParseParams do
|
|
12
12
|
parsed_range = subject.send(:parse_range_values, start_value..end_value)
|
13
13
|
expect(parsed_range).to eql(minimum: start_value, maximum: end_value)
|
14
14
|
end
|
15
|
+
|
16
|
+
describe 'endless range' do
|
17
|
+
specify do
|
18
|
+
parsed_range = subject.send(:parse_range_values, start_value..)
|
19
|
+
expect(parsed_range).to eql(minimum: start_value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'beginless range' do
|
24
|
+
specify do
|
25
|
+
parsed_range = subject.send(:parse_range_values, ..end_value)
|
26
|
+
expect(parsed_range).to eql(maximum: end_value)
|
27
|
+
end
|
28
|
+
end
|
15
29
|
end
|
16
30
|
|
17
31
|
describe '#parse_enum_or_range_values' do
|
@@ -18,25 +18,9 @@ describe 'API with additional options' do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'documents api' do
|
21
|
-
expect(subject).to
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
description: 'Swagger compatible API description for specific API',
|
26
|
-
params: {
|
27
|
-
'locale' => {
|
28
|
-
desc: 'Locale of API documentation',
|
29
|
-
required: false,
|
30
|
-
type: 'Symbol'
|
31
|
-
},
|
32
|
-
'name' => {
|
33
|
-
desc: 'Resource name of mounted API',
|
34
|
-
required: true,
|
35
|
-
type: 'String'
|
36
|
-
}
|
37
|
-
}
|
38
|
-
}
|
39
|
-
]
|
40
|
-
)
|
21
|
+
expect(subject.pluck(:description)).to match_array [
|
22
|
+
'Swagger compatible API description',
|
23
|
+
'Swagger compatible API description for specific API'
|
24
|
+
]
|
41
25
|
end
|
42
26
|
end
|
@@ -15,7 +15,8 @@ describe 'response' do
|
|
15
15
|
failure: [
|
16
16
|
{ code: 400, message: 'NotFound', model: '' },
|
17
17
|
{ code: 404, message: 'BadRequest', model: Entities::ApiError }
|
18
|
-
]
|
18
|
+
],
|
19
|
+
default_response: { message: 'Error', model: Entities::ApiError }
|
19
20
|
get '/use-response' do
|
20
21
|
{ 'declared_params' => declared(params) }
|
21
22
|
end
|
@@ -42,7 +43,8 @@ describe 'response' do
|
|
42
43
|
'responses' => {
|
43
44
|
'200' => { 'description' => 'This returns something', 'schema' => { '$ref' => '#/definitions/UseResponse' } },
|
44
45
|
'400' => { 'description' => 'NotFound' },
|
45
|
-
'404' => { 'description' => 'BadRequest', 'schema' => { '$ref' => '#/definitions/ApiError' } }
|
46
|
+
'404' => { 'description' => 'BadRequest', 'schema' => { '$ref' => '#/definitions/ApiError' } },
|
47
|
+
'default' => { 'description' => 'Error', 'schema' => { '$ref' => '#/definitions/ApiError' } }
|
46
48
|
},
|
47
49
|
'tags' => ['use-response'],
|
48
50
|
'operationId' => 'getUseResponse'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-swagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LeFnord
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-05-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: grape
|
@@ -40,7 +40,6 @@ files:
|
|
40
40
|
- ".rspec"
|
41
41
|
- ".rubocop.yml"
|
42
42
|
- ".rubocop_todo.yml"
|
43
|
-
- ".ruby-gemset"
|
44
43
|
- CHANGELOG.md
|
45
44
|
- CONTRIBUTING.md
|
46
45
|
- Dangerfile
|
@@ -96,6 +95,7 @@ files:
|
|
96
95
|
- spec/issues/587_range_parameter_delimited_by_dash_spec.rb
|
97
96
|
- spec/issues/605_root_route_documentation_spec.rb
|
98
97
|
- spec/issues/650_params_array_spec.rb
|
98
|
+
- spec/issues/677_consumes_produces_add_swagger_documentation_options_spec.rb
|
99
99
|
- spec/issues/680_keep_204_error_schemas_spec.rb
|
100
100
|
- spec/issues/751_deeply_nested_objects_spec.rb
|
101
101
|
- spec/issues/776_multiple_presents_spec.rb
|
@@ -206,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
206
206
|
- !ruby/object:Gem::Version
|
207
207
|
version: '0'
|
208
208
|
requirements: []
|
209
|
-
rubygems_version: 3.
|
209
|
+
rubygems_version: 3.4.10
|
210
210
|
signing_key:
|
211
211
|
specification_version: 4
|
212
212
|
summary: Add auto generated documentation to your Grape API that can be displayed
|
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
grape-swagger
|