rswag-specs 1.0.1 → 1.0.2
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/rswag/specs.rb +5 -0
- data/lib/rswag/specs/configuration.rb +37 -0
- data/lib/rswag/specs/example_group_helpers.rb +11 -5
- data/lib/rswag/specs/example_helpers.rb +6 -5
- data/lib/rswag/specs/request_factory.rb +15 -5
- data/lib/rswag/specs/swagger_formatter.rb +12 -24
- data/lib/rswag/specs/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ad505db2b445cd38fdcdd4c78aadefe9b996b63
|
4
|
+
data.tar.gz: 822fc014d46cb7c1b70e98caa330d41020b2a909
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5001a2a51c3bbd6693466d5f6674ffa7a2662c5e9017814df96e2461a20aa854df6e8ffd63ff483f4e17f9ca62da6ee3ff35f37f23eff35b6d632530d7de095d
|
7
|
+
data.tar.gz: 6d939b5b8bdb64cb2542e4012bad4c4bc14166bc2a92f3ed6da1a0eb4d8af0632c3257ee94fddd4bdc9c9938db5b01fbba2967c8c50222346b5af50d85e6e2c0
|
data/lib/rswag/specs.rb
CHANGED
@@ -2,6 +2,7 @@ require 'rspec/core'
|
|
2
2
|
require 'rswag/specs/version'
|
3
3
|
require 'rswag/specs/example_group_helpers'
|
4
4
|
require 'rswag/specs/example_helpers'
|
5
|
+
require 'rswag/specs/configuration'
|
5
6
|
require 'rswag/specs/railtie' if defined?(Rails::Railtie)
|
6
7
|
|
7
8
|
module Rswag
|
@@ -15,6 +16,10 @@ module Rswag
|
|
15
16
|
c.include ExampleHelpers, type: :request
|
16
17
|
end
|
17
18
|
|
19
|
+
def self.config
|
20
|
+
@config ||= Configuration.new(RSpec.configuration)
|
21
|
+
end
|
22
|
+
|
18
23
|
# Support Rails 3+ and RSpec 2+ (sigh!)
|
19
24
|
RAILS_VERSION = Rails::VERSION::MAJOR
|
20
25
|
RSPEC_VERSION = RSpec::Core::Version::STRING.split('.').first.to_i
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Rswag
|
2
|
+
module Specs
|
3
|
+
|
4
|
+
class Configuration
|
5
|
+
|
6
|
+
def initialize(rspec_config)
|
7
|
+
@rspec_config = rspec_config
|
8
|
+
end
|
9
|
+
|
10
|
+
def swagger_root
|
11
|
+
@swagger_root ||= begin
|
12
|
+
if @rspec_config.swagger_root.nil?
|
13
|
+
raise ConfigurationError, 'No swagger_root provided. See swagger_helper.rb'
|
14
|
+
end
|
15
|
+
@rspec_config.swagger_root
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def swagger_docs
|
20
|
+
@swagger_docs ||= begin
|
21
|
+
if @rspec_config.swagger_docs.nil? || @rspec_config.swagger_docs.empty?
|
22
|
+
raise ConfigurationError, 'No swagger_docs defined. See swagger_helper.rb'
|
23
|
+
end
|
24
|
+
@rspec_config.swagger_docs
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_swagger_doc(name)
|
29
|
+
return swagger_docs.values.first if name.nil?
|
30
|
+
raise ConfigurationError, "Unknown swagger_doc '#{name}'" unless swagger_docs[name]
|
31
|
+
swagger_docs[name]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class ConfigurationError < StandardError; end
|
36
|
+
end
|
37
|
+
end
|
@@ -2,9 +2,9 @@ module Rswag
|
|
2
2
|
module Specs
|
3
3
|
module ExampleGroupHelpers
|
4
4
|
|
5
|
-
def path(
|
6
|
-
api_metadata = {
|
7
|
-
describe(
|
5
|
+
def path(template, &block)
|
6
|
+
api_metadata = { path_item: { template: template } }
|
7
|
+
describe(template, api_metadata, &block)
|
8
8
|
end
|
9
9
|
|
10
10
|
[ :get, :post, :patch, :put, :delete, :head ].each do |verb|
|
@@ -37,8 +37,14 @@ module Rswag
|
|
37
37
|
|
38
38
|
def parameter(attributes)
|
39
39
|
attributes[:required] = true if attributes[:in].to_sym == :path
|
40
|
-
|
41
|
-
metadata
|
40
|
+
|
41
|
+
if metadata.has_key?(:operation)
|
42
|
+
metadata[:operation][:parameters] ||= []
|
43
|
+
metadata[:operation][:parameters] << attributes
|
44
|
+
else
|
45
|
+
metadata[:path_item][:parameters] ||= []
|
46
|
+
metadata[:path_item][:parameters] << attributes
|
47
|
+
end
|
42
48
|
end
|
43
49
|
|
44
50
|
def response(code, description, &block)
|
@@ -6,7 +6,8 @@ module Rswag
|
|
6
6
|
module ExampleHelpers
|
7
7
|
|
8
8
|
def submit_request(api_metadata)
|
9
|
-
|
9
|
+
global_metadata = rswag_config.get_swagger_doc(api_metadata[:swagger_doc])
|
10
|
+
factory = RequestFactory.new(api_metadata, global_metadata)
|
10
11
|
|
11
12
|
if RAILS_VERSION < 5
|
12
13
|
send(
|
@@ -28,15 +29,15 @@ module Rswag
|
|
28
29
|
end
|
29
30
|
|
30
31
|
def assert_response_matches_metadata(api_metadata)
|
31
|
-
|
32
|
+
global_metadata = rswag_config.get_swagger_doc(api_metadata[:swagger_doc])
|
33
|
+
validator = ResponseValidator.new(api_metadata, global_metadata)
|
32
34
|
validator.validate!(response)
|
33
35
|
end
|
34
36
|
|
35
37
|
private
|
36
38
|
|
37
|
-
def
|
38
|
-
|
39
|
-
swagger_doc.nil? ? swagger_docs.values.first : swagger_docs[swagger_doc]
|
39
|
+
def rswag_config
|
40
|
+
::Rswag::Specs.config
|
40
41
|
end
|
41
42
|
end
|
42
43
|
end
|
@@ -11,7 +11,7 @@ module Rswag
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def build_fullpath(example)
|
14
|
-
@api_metadata[:
|
14
|
+
@api_metadata[:path_item][:template].dup.tap do |t|
|
15
15
|
t.prepend(@global_metadata[:basePath] || '')
|
16
16
|
parameters_in(:path).each { |p| t.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s) }
|
17
17
|
t.concat(build_query_string(example))
|
@@ -44,7 +44,13 @@ module Rswag
|
|
44
44
|
private
|
45
45
|
|
46
46
|
def parameters_in(location)
|
47
|
-
|
47
|
+
path_item_params = @api_metadata[:path_item][:parameters] || []
|
48
|
+
operation_params = @api_metadata[:operation][:parameters] || []
|
49
|
+
applicable_params = operation_params
|
50
|
+
.concat(path_item_params)
|
51
|
+
.uniq { |p| p[:name] } # operation params should override path_item params
|
52
|
+
|
53
|
+
applicable_params
|
48
54
|
.map { |p| p['$ref'] ? resolve_parameter(p['$ref']) : p } # resolve any references
|
49
55
|
.concat(resolve_api_key_parameters)
|
50
56
|
.select { |p| p[:in] == location }
|
@@ -59,9 +65,13 @@ module Rswag
|
|
59
65
|
|
60
66
|
def resolve_api_key_parameters
|
61
67
|
@api_key_params ||= begin
|
62
|
-
|
63
|
-
|
64
|
-
|
68
|
+
# First figure out the security requirement applicable to the operation
|
69
|
+
global_requirements = (@global_metadata[:security] || [] ).map { |r| r.keys.first }
|
70
|
+
operation_requirements = (@api_metadata[:operation][:security] || [] ).map { |r| r.keys.first }
|
71
|
+
requirements = global_requirements | operation_requirements
|
72
|
+
|
73
|
+
# Then obtain the scheme definitions for those requirements
|
74
|
+
definitions = (@global_metadata[:securityDefinitions] || {}).slice(*requirements)
|
65
75
|
definitions.values.select { |d| d[:type] == :apiKey }
|
66
76
|
end
|
67
77
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'active_support/core_ext/hash/deep_merge'
|
2
|
-
require 'rspec/core/formatters/base_text_formatter'
|
3
2
|
require 'swagger_helper'
|
4
3
|
|
5
4
|
module Rswag
|
@@ -11,12 +10,9 @@ module Rswag
|
|
11
10
|
::RSpec::Core::Formatters.register self, :example_group_finished, :stop
|
12
11
|
end
|
13
12
|
|
14
|
-
def initialize(output)
|
13
|
+
def initialize(output, config = Rswag::Specs.config)
|
15
14
|
@output = output
|
16
|
-
@
|
17
|
-
raise ConfigurationError, 'Missing swagger_root. See swagger_helper.rb' if @swagger_root.nil?
|
18
|
-
@swagger_docs = ::RSpec.configuration.swagger_docs || []
|
19
|
-
raise ConfigurationError, 'Missing swagger_docs. See swagger_helper.rb' if @swagger_docs.empty?
|
15
|
+
@config = config
|
20
16
|
|
21
17
|
@output.puts 'Generating Swagger docs ...'
|
22
18
|
end
|
@@ -30,13 +26,13 @@ module Rswag
|
|
30
26
|
end
|
31
27
|
|
32
28
|
return unless metadata.has_key?(:response)
|
33
|
-
swagger_doc = get_swagger_doc(metadata[:swagger_doc])
|
29
|
+
swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])
|
34
30
|
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
|
35
31
|
end
|
36
32
|
|
37
33
|
def stop(notification=nil)
|
38
|
-
@swagger_docs.each do |url_path, doc|
|
39
|
-
file_path = File.join(@swagger_root, url_path)
|
34
|
+
@config.swagger_docs.each do |url_path, doc|
|
35
|
+
file_path = File.join(@config.swagger_root, url_path)
|
40
36
|
dirname = File.dirname(file_path)
|
41
37
|
FileUtils.mkdir_p dirname unless File.exists?(dirname)
|
42
38
|
|
@@ -50,30 +46,22 @@ module Rswag
|
|
50
46
|
|
51
47
|
private
|
52
48
|
|
53
|
-
def get_swagger_doc(tag)
|
54
|
-
return @swagger_docs.values.first if tag.nil?
|
55
|
-
raise ConfigurationError, "Unknown swagger_doc '#{tag}'" unless @swagger_docs.has_key?(tag)
|
56
|
-
@swagger_docs[tag]
|
57
|
-
end
|
58
|
-
|
59
49
|
def metadata_to_swagger(metadata)
|
60
50
|
response_code = metadata[:response][:code]
|
61
51
|
response = metadata[:response].reject { |k,v| k == :code }
|
52
|
+
|
62
53
|
verb = metadata[:operation][:verb]
|
63
54
|
operation = metadata[:operation]
|
64
55
|
.reject { |k,v| k == :verb }
|
65
56
|
.merge(responses: { response_code => response })
|
66
57
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
}
|
58
|
+
path_template = metadata[:path_item][:template]
|
59
|
+
path_item = metadata[:path_item]
|
60
|
+
.reject { |k,v| k == :template }
|
61
|
+
.merge(verb => operation)
|
62
|
+
|
63
|
+
{ paths: { path_template => path_item } }
|
74
64
|
end
|
75
65
|
end
|
76
|
-
|
77
|
-
class ConfigurationError < StandardError; end
|
78
66
|
end
|
79
67
|
end
|
data/lib/rswag/specs/version.rb
CHANGED
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: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richie Morris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- lib/generators/rswag/specs/install/install_generator.rb
|
79
79
|
- lib/generators/rswag/specs/install/templates/swagger_helper.rb
|
80
80
|
- lib/rswag/specs.rb
|
81
|
+
- lib/rswag/specs/configuration.rb
|
81
82
|
- lib/rswag/specs/example_group_helpers.rb
|
82
83
|
- lib/rswag/specs/example_helpers.rb
|
83
84
|
- lib/rswag/specs/railtie.rb
|