grape-swagger 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2826417c60f2f12c57698a03f03a15b9c1603c9c19a5cfc5c6594aec5512f13
4
- data.tar.gz: ed6a07977e30c0ec647f41e60acf2d0184ba8f8531c626da87213aa1136fec1b
3
+ metadata.gz: 9cff361f819d42d8c3f847c605a31190f82764da573728d043debfe312d30069
4
+ data.tar.gz: faada4385a0db1fec73606585085deec766991007a9f050df1f2c0bbd9e9e018
5
5
  SHA512:
6
- metadata.gz: e032c09183d38e9ce114cb0c02f6c083a8d79339975678265ef2b6dc49945c8e977c4c2785a62f6aea0d0cc42ae72c8a6ece4cdd8d2fdb9db938eb27a819e25f
7
- data.tar.gz: 42779db4d2991ddc177027f9ae4526dd6ff999faaf7abdd6a8f9cf44978878a19ce300b1440f32eaf562ca1575d1cc7f0c1a9215366eb66683d53a299767c5b0
6
+ metadata.gz: 7a86553f29d5b5a338a0ac21d049845ab609ee4adc2864940813f61384a8bcc19c8b0a644bb484f2b79533a111ab16a51c192236b23f540ed672e5e7a161fb23
7
+ data.tar.gz: 9b5bc29e3ead5f683781cbc0418e3d18f4b056368885e470224bb57ebe03f4232725a971e8589e109240c949aad565e80d5b4ecd31e31f6e5721f0c86c8aaf2f
@@ -18,7 +18,7 @@ jobs:
18
18
  - uses: actions/checkout@v3
19
19
  - uses: ruby/setup-ruby@v1
20
20
  with:
21
- ruby-version: '3.1'
21
+ ruby-version: '3.2'
22
22
  bundler-cache: true
23
23
  - name: Run rubocop
24
24
  run: bundle exec rubocop --parallel --format progress
@@ -28,8 +28,8 @@ jobs:
28
28
  needs: ['rubocop']
29
29
  strategy:
30
30
  matrix:
31
- ruby-version: ['2.7', '3.0', '3.1', 'head']
32
- grape-version: [1.6.2, 1.5.3]
31
+ ruby-version: ['3.0', '3.1', '3.2', 'head']
32
+ grape-version: [1.6.2, 1.7.0]
33
33
  model-parser: [grape-swagger-entity, grape-swagger-representable, '']
34
34
  steps:
35
35
  - name: Check out branch
data/.rubocop.yml CHANGED
@@ -5,7 +5,7 @@ AllCops:
5
5
  - vendor/**/*
6
6
  - example/**/*
7
7
  NewCops: enable
8
- TargetRubyVersion: 3.1
8
+ TargetRubyVersion: 3.2
9
9
  SuggestExtensions: false
10
10
 
11
11
  # Layout stuff
data/CHANGELOG.md CHANGED
@@ -9,6 +9,14 @@
9
9
  * Your contribution here.
10
10
 
11
11
 
12
+ ### 1.6.0 (March 19, 2023)
13
+
14
+ #### Features
15
+
16
+ * [#872](https://github.com/ruby-grape/grape-swagger/pull/872): Add `consumes` and `produces` options to `add_swagger_documentation` - [@spaceraccoon](https://github.com/spaceraccoon)
17
+ * [#868](https://github.com/ruby-grape/grape-swagger/pull/868): Add `default` endpoint option to specify default response - [@dhruvCW](https://github.com/dhruvCW)
18
+
19
+
12
20
  ### 1.5.0 (July 28, 2022)
13
21
 
14
22
  #### 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.6')
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,7 +19,7 @@ group :development, :test do
21
19
  gem 'pry', platforms: [:mri]
22
20
  gem 'pry-byebug', platforms: [:mri]
23
21
 
24
- gem 'rack', '~> 2.2'
22
+ gem 'rack', '~> 3.0'
25
23
  gem 'rack-cors'
26
24
  gem 'rack-test'
27
25
  gem 'rake'
data/README.md CHANGED
@@ -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
 
@@ -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.http_codes || route.options[:failure] || [])
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]])
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrapeSwagger
4
- VERSION = '1.5.0'
4
+ VERSION = '1.6.0'
5
5
  end
@@ -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
@@ -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(::Api::Base).send(:api_class)).to eq(Api::Base)
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
@@ -21,21 +21,7 @@ describe 'API with additional options' do
21
21
  expect(subject).to eq(
22
22
  [
23
23
  { description: 'Swagger compatible API description' },
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
- }
24
+ { description: 'Swagger compatible API description for specific API', params: {} }
39
25
  ]
40
26
  )
41
27
  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.5.0
4
+ version: 1.6.0
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: 2022-07-28 00:00:00.000000000 Z
12
+ date: 2023-03-19 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.3.7
209
+ rubygems_version: 3.4.7
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