rswag-specs 2.0.6 → 2.1.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 +4 -4
- data/lib/generators/rspec/USAGE +9 -0
- data/lib/generators/rspec/swagger_generator.rb +22 -0
- data/lib/generators/rspec/templates/spec.rb +30 -0
- data/lib/rswag/route_parser.rb +58 -0
- data/lib/rswag/specs/example_group_helpers.rb +1 -1
- data/lib/rswag/specs/extended_schema.rb +2 -2
- data/lib/rswag/specs/railtie.rb +4 -0
- data/lib/rswag/specs/request_factory.rb +2 -2
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d360f480ad502e00f5951e40882af98b2b1cc9f4d414c65e41495ca77494a3b0
|
4
|
+
data.tar.gz: 7492e978e971b80560cc1eba26b0b7426c6145745e6dea93be46a58ff200965f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 352cd0dd07a2c39626f58388ffa1b069f30c538d666bab8206f7c027df7412a3fcaac79a9855e7c260b0c4a98ea0d5c58e12503cd665701a9ccfdeac6c9bf19c
|
7
|
+
data.tar.gz: bf66adcb9ff47d01fe283782f87bb4a6ec996e72eddd1850cb498f81b665aecf49393bb480fad81b1ee71f68cd03dc90307658b9b1b02c66be976dd547bba816
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Description:
|
2
|
+
This creates an RSpec request spec to define Swagger documentation for a
|
3
|
+
controller. It will create a test for each of the controller's methods.
|
4
|
+
|
5
|
+
Example:
|
6
|
+
rails generate rspec:swagger V3::AccountsController
|
7
|
+
|
8
|
+
This will create:
|
9
|
+
spec/requests/v3/accounts_spec.rb
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rswag/route_parser'
|
2
|
+
require 'rails/generators'
|
3
|
+
|
4
|
+
module Rspec
|
5
|
+
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@routes = Rswag::RouteParser.new(controller_path).routes
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_spec_file
|
13
|
+
template 'spec.rb', File.join('spec', 'requests', "#{controller_path}_spec.rb")
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def controller_path
|
19
|
+
file_path.chomp('_controller')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'swagger_helper'
|
2
|
+
|
3
|
+
RSpec.describe '<%= controller_path %>', type: :request do
|
4
|
+
<% @routes.each do | template, path_item | %>
|
5
|
+
path '<%= template %>' do
|
6
|
+
<% unless path_item[:params].empty? -%>
|
7
|
+
# You'll want to customize the parameter types...
|
8
|
+
<% path_item[:params].each do |param| -%>
|
9
|
+
parameter name: '<%= param %>', in: :path, type: :string, description: '<%= param %>'
|
10
|
+
<% end -%>
|
11
|
+
<% end -%>
|
12
|
+
<% path_item[:actions].each do | action, details | %>
|
13
|
+
<%= action %>('<%= details[:summary] %>') do
|
14
|
+
response(200, 'successful') do
|
15
|
+
<% unless path_item[:params].empty? -%>
|
16
|
+
<% path_item[:params].each do |param| -%>
|
17
|
+
let(:<%= param %>) { '123' }
|
18
|
+
<% end -%>
|
19
|
+
<% end -%>
|
20
|
+
|
21
|
+
after do |example|
|
22
|
+
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
|
23
|
+
end
|
24
|
+
run_test!
|
25
|
+
end
|
26
|
+
end
|
27
|
+
<% end -%>
|
28
|
+
end
|
29
|
+
<% end -%>
|
30
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Rswag
|
2
|
+
class RouteParser
|
3
|
+
attr_reader :controller
|
4
|
+
|
5
|
+
def initialize(controller)
|
6
|
+
@controller = controller
|
7
|
+
end
|
8
|
+
|
9
|
+
def routes
|
10
|
+
::Rails.application.routes.routes.select do |route|
|
11
|
+
route.defaults[:controller] == controller
|
12
|
+
end.reduce({}) do |tree, route|
|
13
|
+
path = path_from(route)
|
14
|
+
verb = verb_from(route)
|
15
|
+
tree[path] ||= { params: params_from(route), actions: {} }
|
16
|
+
tree[path][:actions][verb] = { summary: summary_from(route) }
|
17
|
+
tree
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def path_from(route)
|
24
|
+
route.path.spec.to_s
|
25
|
+
.chomp('(.:format)') # Ignore any format suffix
|
26
|
+
.gsub(/:([^\/.?]+)/, '{\1}') # Convert :id to {id}
|
27
|
+
end
|
28
|
+
|
29
|
+
def verb_from(route)
|
30
|
+
verb = route.verb
|
31
|
+
if verb.kind_of? String
|
32
|
+
verb.downcase
|
33
|
+
else
|
34
|
+
verb.source.gsub(/[$^]/, '').downcase
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def summary_from(route)
|
39
|
+
verb = route.requirements[:action]
|
40
|
+
noun = route.requirements[:controller].split('/').last.singularize
|
41
|
+
|
42
|
+
# Apply a few customizations to make things more readable
|
43
|
+
case verb
|
44
|
+
when 'index'
|
45
|
+
verb = 'list'
|
46
|
+
noun = noun.pluralize
|
47
|
+
when 'destroy'
|
48
|
+
verb = 'delete'
|
49
|
+
end
|
50
|
+
|
51
|
+
"#{verb} #{noun}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def params_from(route)
|
55
|
+
route.segments - ['format']
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -7,7 +7,7 @@ module Rswag
|
|
7
7
|
describe(template, metadata, &block)
|
8
8
|
end
|
9
9
|
|
10
|
-
[ :get, :post, :patch, :put, :delete, :head ].each do |verb|
|
10
|
+
[ :get, :post, :patch, :put, :delete, :head, :options, :trace ].each do |verb|
|
11
11
|
define_method(verb) do |summary, &block|
|
12
12
|
api_metadata = { operation: { verb: verb, summary: summary } }
|
13
13
|
describe(verb, api_metadata, &block)
|
@@ -3,7 +3,7 @@ require 'json-schema'
|
|
3
3
|
module Rswag
|
4
4
|
module Specs
|
5
5
|
class ExtendedSchema < JSON::Schema::Draft4
|
6
|
-
|
6
|
+
|
7
7
|
def initialize
|
8
8
|
super
|
9
9
|
@attributes['type'] = ExtendedTypeAttribute
|
@@ -13,7 +13,7 @@ module Rswag
|
|
13
13
|
end
|
14
14
|
|
15
15
|
class ExtendedTypeAttribute < JSON::Schema::TypeV4Attribute
|
16
|
-
|
16
|
+
|
17
17
|
def self.validate(current_schema, data, fragments, processor, validator, options={})
|
18
18
|
return if data.nil? && current_schema.schema['x-nullable'] == true
|
19
19
|
super
|
data/lib/rswag/specs/railtie.rb
CHANGED
@@ -54,7 +54,7 @@ module Rswag
|
|
54
54
|
definitions[key]
|
55
55
|
end
|
56
56
|
|
57
|
-
def add_verb(request, metadata)
|
57
|
+
def add_verb(request, metadata)
|
58
58
|
request[:verb] = metadata[:operation][:verb]
|
59
59
|
end
|
60
60
|
|
@@ -104,7 +104,7 @@ module Rswag
|
|
104
104
|
end
|
105
105
|
|
106
106
|
# Content-Type header
|
107
|
-
consumes = metadata[:operation][:consumes] || swagger_doc[:consumes]
|
107
|
+
consumes = metadata[:operation][:consumes] || swagger_doc[:consumes]
|
108
108
|
if consumes
|
109
109
|
content_type = example.respond_to?(:'Content-Type') ? example.send(:'Content-Type') : consumes.first
|
110
110
|
tuples << [ 'Content-Type', content_type ]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rswag-specs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richie Morris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -74,9 +74,13 @@ extra_rdoc_files: []
|
|
74
74
|
files:
|
75
75
|
- MIT-LICENSE
|
76
76
|
- Rakefile
|
77
|
+
- lib/generators/rspec/USAGE
|
78
|
+
- lib/generators/rspec/swagger_generator.rb
|
79
|
+
- lib/generators/rspec/templates/spec.rb
|
77
80
|
- lib/generators/rswag/specs/install/USAGE
|
78
81
|
- lib/generators/rswag/specs/install/install_generator.rb
|
79
82
|
- lib/generators/rswag/specs/install/templates/swagger_helper.rb
|
83
|
+
- lib/rswag/route_parser.rb
|
80
84
|
- lib/rswag/specs.rb
|
81
85
|
- lib/rswag/specs/configuration.rb
|
82
86
|
- lib/rswag/specs/example_group_helpers.rb
|