grape-swagger 0.32.1 → 0.33.0

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: 1c5f4e8337c6872fe55038b194145b68b2f590d5b0c1a69e21968ce3c2cd15ab
4
- data.tar.gz: 37af5bafa392cd66f53636e5e586b6763ecab6596d31250977644482183b5506
3
+ metadata.gz: fd05d00a2d4f16e3ecfa826aa0053928231d9e3f2e3316ea1f612c804284d858
4
+ data.tar.gz: 7b6637a45c95035939be0a52312f6f16f62bf1f67d788c1d1e6884b1611f6322
5
5
  SHA512:
6
- metadata.gz: f994d0712a3cdb78c37536ca4a636b986ed48178daeed7b0dcb86fe02b75ffb0b2844cacddac3f767213d461efc6f1ba15adea681622ee5add22c0a5ec31c74e
7
- data.tar.gz: 3ccae0f447dfd4e4517e3d5f02e33678b3733a999d2bd1c81a9a9f54d29d201cd0c378b64f20932dcbbe41b660ffad742d1940bab0094b64586802655cb44b49
6
+ metadata.gz: 05541a6f1670c730a7fb3c7c4acc422aeecdf562fc3f77477e0721628dac3aeec147ce0653d74a1a7560ddd36fc90075fae23ae58d1ed424445290e801d2856e
7
+ data.tar.gz: ac5ce4eadc5bc9dd4ecdac8bd514a339c9157b77f661c8e696c6a527dfc0a2d6d09acc8527351ac1ba0cbbf0e8894959baa5daa7eb0cd41997ca1b9dd9ddb065
@@ -4,12 +4,12 @@ AllCops:
4
4
  Exclude:
5
5
  - vendor/**/*
6
6
  - example/**/*
7
- TargetRubyVersion: 2.4
7
+ TargetRubyVersion: 2.6
8
8
 
9
9
  Layout/EmptyLinesAroundArguments:
10
10
  Enabled: false
11
11
 
12
- Layout/IndentHash:
12
+ Layout/IndentFirstHashElement:
13
13
  EnforcedStyle: consistent
14
14
 
15
15
  Metrics/BlockLength:
@@ -1,18 +1,11 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2018-11-04 01:12:26 +0100 using RuboCop version 0.60.0.
3
+ # on 2019-06-05 12:13:59 +0000 using RuboCop version 0.71.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
- # Offense count: 1
10
- # Configuration parameters: Include.
11
- # Include: **/*.gemfile, **/Gemfile, **/gems.rb
12
- Bundler/DuplicatedGem:
13
- Exclude:
14
- - 'Gemfile'
15
-
16
9
  # Offense count: 1
17
10
  # Configuration parameters: Include.
18
11
  # Include: **/*.gemspec
@@ -20,9 +13,15 @@ Gemspec/RequiredRubyVersion:
20
13
  Exclude:
21
14
  - 'grape-swagger.gemspec'
22
15
 
16
+ # Offense count: 1
17
+ # Cop supports --auto-correct.
18
+ Lint/UnneededCopEnableDirective:
19
+ Exclude:
20
+ - 'spec/lib/optional_object_spec.rb'
21
+
23
22
  # Offense count: 30
24
23
  Metrics/AbcSize:
25
- Max: 57
24
+ Max: 59
26
25
 
27
26
  # Offense count: 10
28
27
  Metrics/CyclomaticComplexity:
@@ -31,7 +30,7 @@ Metrics/CyclomaticComplexity:
31
30
  # Offense count: 22
32
31
  # Configuration parameters: CountComments, ExcludedMethods.
33
32
  Metrics/MethodLength:
34
- Max: 44
33
+ Max: 45
35
34
 
36
35
  # Offense count: 7
37
36
  Metrics/PerceivedComplexity:
@@ -1,25 +1,20 @@
1
1
  language: ruby
2
2
 
3
- sudo: false
4
-
5
3
  before_install:
6
- - gem update --system
7
4
  - gem install bundler
8
5
 
9
6
  after_success:
10
- # - coveralls
11
7
  - bundle exec danger
12
8
 
13
9
  rvm:
14
- - 2.4.5
15
- - 2.5.3
16
- - 2.6.0-rc1
10
+ - 2.4.6
11
+ - 2.5.5
12
+ - 2.6.3
17
13
  env:
18
14
  - MODEL_PARSER=grape-swagger-entity
19
15
  - MODEL_PARSER=grape-swagger-representable
20
- - GRAPE_VERSION=0.19.2
21
16
  - GRAPE_VERSION=1.0.3
22
- - GRAPE_VERSION=1.2.2
17
+ - GRAPE_VERSION=1.2.4
23
18
  - GRAPE_VERSION=HEAD
24
19
 
25
20
  matrix:
@@ -8,6 +8,15 @@
8
8
 
9
9
  * Your contribution here.
10
10
 
11
+ ### 0.33.0 (June 21, 2019)
12
+
13
+ #### Fixes
14
+
15
+ * [#747](https://github.com/ruby-grape/grape-swagger/pull/747): Allow multiple different success responses - [@charanftp3](https://github.com/charanpanchagnula).
16
+ * [#746](https://github.com/ruby-grape/grape-swagger/pull/746): Fix path with optional format - [@fnordfish](https://github.com/fnordfish).
17
+ * [#743](https://github.com/ruby-grape/grape-swagger/pull/743): CI: use 2.4.6, 2.5.5 - [@olleolleolle](https://github.com/olleolleolle).
18
+ * [#737](https://github.com/ruby-grape/grape-swagger/pull/737): Add swagger endpoint guard to both doc endpoints - [@urkle](https://github.com/urkle).
19
+
11
20
  ### 0.32.1 (December 7, 2018)
12
21
 
13
22
  #### Fixes
data/Gemfile CHANGED
@@ -25,17 +25,12 @@ group :development, :test do
25
25
  gem 'rack-test'
26
26
  gem 'rake'
27
27
  gem 'rdoc'
28
- gem 'rspec', '~> 3.0'
29
- # TODO: change back after 2.6.0 release and updated rubocop version
30
- if RUBY_VERSION == '2.6.0'
31
- gem 'rubocop', git: 'https://github.com/rubocop-hq/rubocop.git', require: false
32
- else
33
- gem 'rubocop', '~> 0.61', require: false
34
- end
28
+ gem 'rspec', '~> 3.8'
29
+ gem 'rubocop', '~> 0.71', require: false
35
30
  end
36
31
 
37
32
  group :test do
38
- gem 'coveralls', '~> 0.8', require: false
33
+ gem 'coveralls_reborn', require: false
39
34
  gem 'grape-swagger-entity'
40
35
  gem 'ruby-grape-danger', '~> 0.1.1', require: false
41
36
  gem 'simplecov', require: false
data/README.md CHANGED
@@ -900,6 +900,45 @@ end
900
900
  },
901
901
  ```
902
902
 
903
+ #### Multiple status codes for response <a name="multiple-status-response"></a>
904
+
905
+ Multiple values can be provided for `success` and `failure` attributes in the response.
906
+
907
+ ```ruby
908
+ desc 'Attach a field to an entity through a PUT',
909
+ success: [
910
+ { code: 201, model: Entities::UseResponse, message: 'Successfully created' },
911
+ { code: 204, message: 'Already exists' }
912
+ ],
913
+ failure: [
914
+ { code: 400, message: 'Bad request' },
915
+ { code: 404, message: 'Not found' }
916
+ ]
917
+ put do
918
+ # your code comes here
919
+ end
920
+ ```
921
+
922
+ ```json
923
+ "responses": {
924
+ "201": {
925
+ "description": "Successfully created",
926
+ "schema": {
927
+ "$ref": "#/definitions/UseResponse"
928
+ }
929
+ },
930
+ "204": {
931
+ "description": "Already exists"
932
+ },
933
+ "400": {
934
+ "description": "Bad request"
935
+ },
936
+ "404": {
937
+ "description": "Not found"
938
+ }
939
+ },
940
+ ```
941
+
903
942
 
904
943
  #### File response <a name="file-response"></a>
905
944
 
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
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.3'
16
+ s.required_ruby_version = '>= 2.4'
17
17
  s.add_runtime_dependency 'grape', '>= 0.16.2'
18
18
 
19
19
  s.files = `git ls-files`.split("\n")
@@ -45,8 +45,6 @@ module GrapeSwagger
45
45
 
46
46
  desc api_doc.delete(:desc), api_doc
47
47
 
48
- instance_eval(guard) unless guard.nil?
49
-
50
48
  output_path_definitions = proc do |combi_routes, endpoint|
51
49
  output = endpoint.swagger_object(
52
50
  target_class,
@@ -64,6 +62,8 @@ module GrapeSwagger
64
62
  output
65
63
  end
66
64
 
65
+ instance_eval(guard) unless guard.nil?
66
+
67
67
  get mount_path do
68
68
  header['Access-Control-Allow-Origin'] = '*'
69
69
  header['Access-Control-Request-Method'] = '*'
@@ -79,6 +79,8 @@ module GrapeSwagger
79
79
  optional :locale, type: Symbol, desc: 'Locale of API documentation'
80
80
  end
81
81
 
82
+ instance_eval(guard) unless guard.nil?
83
+
82
84
  get "#{mount_path}/:name" do
83
85
  I18n.locale = params[:locale] || I18n.default_locale
84
86
 
@@ -18,7 +18,7 @@ module GrapeSwagger
18
18
  operation = path.split('/').map(&:capitalize).join
19
19
  operation.gsub!(/\-(\w)/, &:upcase).delete!('-') if operation[/\-(\w)/]
20
20
  operation.gsub!(/\_(\w)/, &:upcase).delete!('_') if operation.include?('_')
21
- operation.gsub!(/\.(\w)/, &:upcase).delete!('.') if operation.include?('.')
21
+ operation.gsub!(/\.(\w)/, &:upcase).delete!('.') if operation[/\.(\w)/]
22
22
  if path.include?('{')
23
23
  operation.gsub!(/\{(\w)/, &:upcase)
24
24
  operation.delete!('{').delete!('}')
@@ -238,20 +238,13 @@ module Grape
238
238
  end
239
239
 
240
240
  def success_codes_from_route(route)
241
- default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
242
- if @entity.is_a?(Hash)
243
- default_code[:code] = @entity[:code] if @entity[:code].present?
244
- default_code[:model] = @entity[:model] if @entity[:model].present?
245
- default_code[:message] = @entity[:message] || route.description || default_code[:message].sub('{item}', @item)
246
- default_code[:examples] = @entity[:examples] if @entity[:examples]
247
- default_code[:headers] = @entity[:headers] if @entity[:headers]
248
- else
249
- default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
250
- default_code[:model] = @entity if @entity
251
- default_code[:message] = route.description || default_code[:message].sub('{item}', @item)
241
+ if @entity.is_a?(Array)
242
+ return @entity.map do |entity|
243
+ success_code_from_entity(route, entity)
244
+ end
252
245
  end
253
246
 
254
- [default_code]
247
+ [success_code_from_entity(route, @entity)]
255
248
  end
256
249
 
257
250
  def tag_object(route, path)
@@ -350,5 +343,22 @@ module Grape
350
343
 
351
344
  options[:token_owner] ? route_hidden.call(send(options[:token_owner].to_sym)) : route_hidden.call
352
345
  end
346
+
347
+ def success_code_from_entity(route, entity)
348
+ default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
349
+ if entity.is_a?(Hash)
350
+ default_code[:code] = entity[:code] if entity[:code].present?
351
+ default_code[:model] = entity[:model] if entity[:model].present?
352
+ default_code[:message] = entity[:message] || route.description || default_code[:message].sub('{item}', @item)
353
+ default_code[:examples] = entity[:examples] if entity[:examples]
354
+ default_code[:headers] = entity[:headers] if entity[:headers]
355
+ else
356
+ default_code = GrapeSwagger::DocMethods::StatusCodes.get[route.request_method.downcase.to_sym]
357
+ default_code[:model] = entity if entity
358
+ default_code[:message] = route.description || default_code[:message].sub('{item}', @item)
359
+ end
360
+
361
+ default_code
362
+ end
353
363
  end
354
364
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GrapeSwagger
4
- VERSION = '0.32.1'
4
+ VERSION = '0.33.0'
5
5
  end
@@ -47,6 +47,9 @@ describe GrapeSwagger::DocMethods::OperationId do
47
47
  it 'GET with path /simple_test/bar-foo{id}' do
48
48
  expect(subject.build(route, '/simple_test/bar-foo{id}')).to eql 'getSimpleTestBarFooId'
49
49
  end
50
+ it 'GET path with optional format' do
51
+ expect(subject.build(route, 'foo(.{format})')).to eql 'getFoo(.Format)'
52
+ end
50
53
  end
51
54
  end
52
55
  end
@@ -36,7 +36,6 @@ describe GrapeSwagger::DocMethods::OptionalObject do
36
36
  end
37
37
 
38
38
  describe 'option is a proc' do
39
- # rubocop:disable RegexpMatch
40
39
  let(:options) do
41
40
  { host: proc { |request| request.host =~ /^example/ ? '/api-example' : '/api' } }
42
41
  end
@@ -2,14 +2,14 @@
2
2
 
3
3
  if RUBY_ENGINE == 'ruby'
4
4
  require 'simplecov'
5
- # require 'coveralls'
5
+ require 'coveralls'
6
6
 
7
- # SimpleCov.formatter = Coveralls::SimpleCov::Formatter
7
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
8
8
  SimpleCov.start do
9
9
  add_filter 'spec/'
10
- # add_filter 'example/'
10
+ add_filter 'example/'
11
11
  end
12
- # Coveralls.wear!
12
+ Coveralls.wear!
13
13
  end
14
14
 
15
15
  $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
@@ -31,6 +31,17 @@ describe 'response' do
31
31
  { 'declared_params' => declared(params) }
32
32
  end
33
33
 
34
+ desc 'This returns something',
35
+ success: [
36
+ { code: 200, message: 'Request has succeeded' },
37
+ { code: 201, message: 'Successful Operation' },
38
+ { code: 204, message: 'Request was fulfilled' }
39
+ ],
40
+ failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }]
41
+ get '/multiple-success-responses' do
42
+ { 'declared_params' => declared(params) }
43
+ end
44
+
34
45
  add_swagger_documentation
35
46
  end
36
47
  end
@@ -106,4 +117,27 @@ describe 'response' do
106
117
  expect(subject['definitions']).to eql(swagger_params_as_response_object)
107
118
  end
108
119
  end
120
+
121
+ describe 'uses params as response object when response contains multiple values for success' do
122
+ subject do
123
+ get '/swagger_doc/multiple-success-responses'
124
+ JSON.parse(last_response.body)
125
+ end
126
+
127
+ specify do
128
+ expect(subject['paths']['/multiple-success-responses']['get']).to eql(
129
+ 'description' => 'This returns something',
130
+ 'produces' => ['application/json'],
131
+ 'responses' => {
132
+ '200' => { 'description' => 'Request has succeeded' },
133
+ '201' => { 'description' => 'Successful Operation' },
134
+ '204' => { 'description' => 'Request was fulfilled' },
135
+ '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } }
136
+ },
137
+ 'tags' => ['multiple-success-responses'],
138
+ 'operationId' => 'getMultipleSuccessResponses'
139
+ )
140
+ expect(subject['definitions']).to eql(swagger_params_as_response_object)
141
+ end
142
+ end
109
143
  end
@@ -68,47 +68,95 @@ describe 'a guarded api endpoint' do
68
68
  GuardedApi
69
69
  end
70
70
 
71
- context 'when a correct token is passed with the request' do
72
- subject do
73
- get '/swagger_doc.json', {}, 'HTTP_AUTHORIZATION' => '12345'
74
- JSON.parse(last_response.body)
75
- end
71
+ let(:endpoint) { '/swagger_doc.json' }
72
+ let(:auth_token) { nil }
73
+
74
+ subject do
75
+ get endpoint, {}, 'HTTP_AUTHORIZATION' => auth_token
76
+ JSON.parse(last_response.body)
77
+ end
76
78
 
77
- it 'retrieves swagger-documentation for the endpoint' do
78
- expect(subject).to eq(
79
- 'info' => { 'title' => 'API title', 'version' => '0.0.1' },
80
- 'swagger' => '2.0',
81
- 'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
82
- 'host' => 'example.org',
83
- 'tags' => [{ 'name' => 'auth', 'description' => 'Operations about auths' }],
84
- 'paths' => {
85
- '/auth' => {
86
- 'get' => {
87
- 'description' => 'Show endpoint if authenticated',
88
- 'produces' => ['application/json'],
89
- 'tags' => ['auth'],
90
- 'operationId' => 'getAuth',
91
- 'responses' => { '200' => { 'description' => 'Show endpoint if authenticated' } }
79
+ context 'accessing the main doc endpoint' do
80
+ let(:endpoint) { '/swagger_doc.json' }
81
+
82
+ context 'when a correct token is passed with the request' do
83
+ let(:auth_token) { '12345' }
84
+
85
+ it 'retrieves swagger-documentation for the endpoint' do
86
+ expect(subject).to eq(
87
+ 'info' => { 'title' => 'API title', 'version' => '0.0.1' },
88
+ 'swagger' => '2.0',
89
+ 'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
90
+ 'host' => 'example.org',
91
+ 'tags' => [{ 'name' => 'auth', 'description' => 'Operations about auths' }],
92
+ 'paths' => {
93
+ '/auth' => {
94
+ 'get' => {
95
+ 'description' => 'Show endpoint if authenticated',
96
+ 'produces' => ['application/json'],
97
+ 'tags' => ['auth'],
98
+ 'operationId' => 'getAuth',
99
+ 'responses' => { '200' => { 'description' => 'Show endpoint if authenticated' } }
100
+ }
92
101
  }
93
102
  }
94
- }
95
- )
103
+ )
104
+ end
105
+ end
106
+
107
+ context 'when a bad token is passed with the request' do
108
+ let(:auth_token) { '123456' }
109
+
110
+ it 'does not retrieve swagger-documentation for the endpoint - only the info_object' do
111
+ expect(subject).to eq(
112
+ 'info' => { 'title' => 'API title', 'version' => '0.0.1' },
113
+ 'swagger' => '2.0',
114
+ 'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
115
+ 'host' => 'example.org'
116
+ )
117
+ end
96
118
  end
97
119
  end
98
120
 
99
- context 'when a bad token is passed with the request' do
100
- subject do
101
- get '/swagger_doc.json', {}, 'HTTP_AUTHORIZATION' => '123456'
102
- JSON.parse(last_response.body)
121
+ context 'accessing the tag specific endpoint' do
122
+ let(:endpoint) { '/swagger_doc/auth.json' }
123
+
124
+ context 'when a correct token is passed with the request' do
125
+ let(:auth_token) { '12345' }
126
+
127
+ it 'retrieves swagger-documentation for the endpoint' do
128
+ expect(subject).to eq(
129
+ 'info' => { 'title' => 'API title', 'version' => '0.0.1' },
130
+ 'swagger' => '2.0',
131
+ 'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
132
+ 'host' => 'example.org',
133
+ 'tags' => [{ 'name' => 'auth', 'description' => 'Operations about auths' }],
134
+ 'paths' => {
135
+ '/auth' => {
136
+ 'get' => {
137
+ 'description' => 'Show endpoint if authenticated',
138
+ 'produces' => ['application/json'],
139
+ 'tags' => ['auth'],
140
+ 'operationId' => 'getAuth',
141
+ 'responses' => { '200' => { 'description' => 'Show endpoint if authenticated' } }
142
+ }
143
+ }
144
+ }
145
+ )
146
+ end
103
147
  end
104
148
 
105
- it 'does not retrieve swagger-documentation for the endpoint - only the info_object' do
106
- expect(subject).to eq(
107
- 'info' => { 'title' => 'API title', 'version' => '0.0.1' },
108
- 'swagger' => '2.0',
109
- 'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
110
- 'host' => 'example.org'
111
- )
149
+ context 'when a bad token is passed with the request' do
150
+ let(:auth_token) { '123456' }
151
+
152
+ it 'does not retrieve swagger-documentation for the endpoint - only the info_object' do
153
+ expect(subject).to eq(
154
+ 'info' => { 'title' => 'API title', 'version' => '0.0.1' },
155
+ 'swagger' => '2.0',
156
+ 'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
157
+ 'host' => 'example.org'
158
+ )
159
+ end
112
160
  end
113
161
  end
114
162
  end
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: 0.32.1
4
+ version: 0.33.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Vandecasteele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-07 00:00:00.000000000 Z
11
+ date: 2019-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -182,15 +182,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
182
  requirements:
183
183
  - - ">="
184
184
  - !ruby/object:Gem::Version
185
- version: '2.3'
185
+ version: '2.4'
186
186
  required_rubygems_version: !ruby/object:Gem::Requirement
187
187
  requirements:
188
188
  - - ">="
189
189
  - !ruby/object:Gem::Version
190
190
  version: '0'
191
191
  requirements: []
192
- rubyforge_project:
193
- rubygems_version: 2.7.8
192
+ rubygems_version: 3.0.3
194
193
  signing_key:
195
194
  specification_version: 4
196
195
  summary: Add auto generated documentation to your Grape API that can be displayed