swagalicious 0.3.1 → 0.4.4

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: 660f3ec40bfe890817b3e6bdb147ed5d7499a3bcf5b5f966c2310292fb76f8d7
4
- data.tar.gz: 4028bd843b9f12e82ff4c771a5389bf4504549bf304bc30d8fe3b0d66e057d6c
3
+ metadata.gz: 8d1f1a314f7e3c84ef64184ba490e5d90971fc56213f74829f5af6f0fc6b0274
4
+ data.tar.gz: a6e07a214f9e574c8151ea7c459d0445790ed79f1a9b6f9dc9958bab9aef9406
5
5
  SHA512:
6
- metadata.gz: e8250341f3eef2fef73ec5c0db76b38771e8a127c80af680f844fd0c8e342e8be23ac8e8d62b262084b6024177b42076d6544e1ffd7546c1d3689142e0c4618e
7
- data.tar.gz: 9ad802f13a6831eb15cccaa1491bbb52ff248b18e21dbf71c8cff0dde5093b52f5170b19f9f3314e4a1bafeec8e64a89ad998e10bda22122d1210fa94606a040
6
+ metadata.gz: 87bc78e516ccf7ce48eb88b3aaf8045415dd147e6de880b422366fd679b86eac12fb0df34b97358578da75129e49e2dd08285297449a3f26ea85eee503c18098
7
+ data.tar.gz: 4b083368ef0dc6fb3cb90bb8fd39dbdbde0731de0e41df4583b188822b9387df4c53620941f91a3173b4a54e56fd99f8ba581f0b3de791672d729ed519f00e8c
data/Gemfile.lock CHANGED
@@ -1,28 +1,48 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swagalicious (0.2.0)
4
+ swagalicious (0.4.3)
5
5
  faraday (~> 1.3.0)
6
6
  json-schema (~> 2.8.1)
7
7
  oj (~> 3.11.0)
8
- rack-test (~> 1.1.0)
9
8
  rspec (~> 3.10.0)
10
9
 
11
10
  GEM
12
11
  remote: https://rubygems.org/
13
12
  specs:
13
+ activesupport (6.1.1)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ tzinfo (~> 2.0)
18
+ zeitwerk (~> 2.3)
14
19
  addressable (2.7.0)
15
20
  public_suffix (>= 2.0.2, < 5.0)
21
+ byebug (11.1.3)
22
+ coderay (1.1.3)
23
+ concurrent-ruby (1.1.8)
16
24
  diff-lcs (1.4.4)
25
+ factory_bot (6.1.0)
26
+ activesupport (>= 5.0.0)
17
27
  faraday (1.3.0)
18
28
  faraday-net_http (~> 1.0)
19
29
  multipart-post (>= 1.2, < 3)
20
30
  ruby2_keywords
21
31
  faraday-net_http (1.0.1)
32
+ i18n (1.8.7)
33
+ concurrent-ruby (~> 1.0)
22
34
  json-schema (2.8.1)
23
35
  addressable (>= 2.4)
36
+ method_source (1.0.0)
37
+ minitest (5.14.3)
24
38
  multipart-post (2.1.1)
25
- oj (3.11.0)
39
+ oj (3.11.3)
40
+ pry (0.13.1)
41
+ coderay (~> 1.1)
42
+ method_source (~> 1.0)
43
+ pry-byebug (3.9.0)
44
+ byebug (~> 11.0)
45
+ pry (~> 0.13.0)
26
46
  public_suffix (4.0.6)
27
47
  rack (2.2.3)
28
48
  rack-test (1.1.0)
@@ -42,14 +62,20 @@ GEM
42
62
  rspec-support (~> 3.10.0)
43
63
  rspec-support (3.10.1)
44
64
  ruby2_keywords (0.0.4)
65
+ tzinfo (2.0.4)
66
+ concurrent-ruby (~> 1.0)
67
+ zeitwerk (2.4.2)
45
68
 
46
69
  PLATFORMS
47
70
  ruby
48
71
 
49
72
  DEPENDENCIES
73
+ factory_bot (~> 6.1.0)
74
+ pry-byebug (~> 3.9.0)
75
+ rack-test (~> 1.1.0)
50
76
  rake (~> 12.0)
51
77
  rspec (~> 3.0)
52
78
  swagalicious!
53
79
 
54
80
  BUNDLED WITH
55
- 2.2.6
81
+ 2.2.16
data/lib/swagalicious.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "rspec/core"
2
2
 
3
- require_relative "swagalicious/version"
3
+ require_relative "./swagalicious/version"
4
4
 
5
5
  class Swagalicious
6
6
  class Error < StandardError; end
@@ -9,13 +9,13 @@ class Swagalicious
9
9
  @config ||= Swagalicious::Configuration.new(RSpec.configuration)
10
10
  end
11
11
 
12
- require_relative "swagalicious/configuration"
13
- require_relative "swagalicious/example_group_helpers"
14
- require_relative "swagalicious/example_helpers"
15
- require_relative "swagalicious/extended_schema"
16
- require_relative "swagalicious/request_factory"
17
- require_relative "swagalicious/response_validator"
18
- require_relative "swagalicious/swagger_formatter"
12
+ require_relative "./swagalicious/configuration"
13
+ require_relative "./swagalicious/example_group_helpers"
14
+ require_relative "./swagalicious/example_helpers"
15
+ require_relative "./swagalicious/extended_schema"
16
+ require_relative "./swagalicious/request_factory"
17
+ require_relative "./swagalicious/response_validator"
18
+ require_relative "./swagalicious/swagger_formatter"
19
19
 
20
20
  ::RSpec::Core::ExampleGroup.define_example_group_method :path
21
21
 
@@ -49,12 +49,28 @@ class Swagalicious
49
49
 
50
50
  @body = Oj.load(body, symbol_keys: true)
51
51
 
52
- if request[:payload]
53
- metadata[:response][:request] = Oj.load(request[:payload])
54
- end
52
+ metadata[:paths] ||= []
53
+ metadata[:paths] << request[:path]
54
+
55
+ metadata[:response][:requestBody] ||= {}
56
+ metadata[:response][:examples] ||= {}
57
+
58
+ mime_types = metadata[:response][:produces] || ["application/json"]
59
+ full_title = metadata[:swagger_example_name] || "#{metadata[:operation][:summary]}: #{metadata[:description]}"
55
60
 
56
- metadata[:response][:examples] ||= {}
57
- metadata[:response][:examples]["application/json"] = @body
61
+ mime_types.each do |mime_type|
62
+ if request[:payload]
63
+ metadata[:response][:requestBody][:content] ||= {}
64
+ metadata[:response][:requestBody][:content][mime_type] ||= {}
65
+ metadata[:response][:requestBody][:content][mime_type][:examples] ||= {}
66
+ metadata[:response][:requestBody][:content][mime_type][:examples][full_title] ||= {}
67
+
68
+ metadata[:response][:requestBody][:content][mime_type][:examples][full_title][:value] = Oj.load(request[:payload]).dup
69
+ end
70
+
71
+ metadata[:response][:examples][mime_type] ||= {}
72
+ metadata[:response][:examples][mime_type][full_title] = @body
73
+ end
58
74
 
59
75
  # Validates response matches the proper schema
60
76
  Swagalicious::ResponseValidator.new.validate!(metadata, response)
@@ -86,7 +86,7 @@ class Swagalicious
86
86
  end
87
87
 
88
88
  def add_path(request, metadata, swagger_doc, parameters, example)
89
- template = (swagger_doc[:basePath] || "") + metadata[:path_item][:template]
89
+ template = (swagger_doc[:basePath] || (swagger_doc[:servers]&.first || {})[:url] || "") + metadata[:path_item][:template]
90
90
 
91
91
  request[:path] = template.tap do |path_template|
92
92
  parameters.select { |p| p[:in] == :path }.each do |p|
@@ -44,7 +44,7 @@ class Swagalicious
44
44
  .merge(schemas)
45
45
 
46
46
  errors = JSON::Validator.fully_validate(validation_schema, body)
47
- raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
47
+ raise UnexpectedResponse, "Expected response body to match schema: #{errors.join(", ")}" unless errors.empty?
48
48
  end
49
49
 
50
50
  def definitions_or_component_schemas(swagger_doc, version)
@@ -31,27 +31,6 @@ class Swagalicious
31
31
  upgrade_response_produces!(doc, metadata)
32
32
  upgrade_request_type!(metadata)
33
33
 
34
- unless doc_version(doc).start_with?("2")
35
- doc[:paths]&.each_pair do |_k, v|
36
- v.each_pair do |_verb, value|
37
- is_hash = value.is_a?(Hash)
38
- if is_hash && value.dig(:parameters)
39
- schema_param = value.dig(:parameters)&.find { |p| (p[:in] == :body || p[:in] == :formData) && p[:schema] }
40
- mime_list = value.dig(:consumes)
41
- if value && schema_param && mime_list
42
- value[:requestBody] = { content: {} } unless value.dig(:requestBody, :content)
43
- mime_list.each do |mime|
44
- value[:requestBody][:content][mime] = { schema: schema_param[:schema] }
45
- end
46
- end
47
-
48
- value[:parameters].reject! { |p| p[:in] == :body || p[:in] == :formData }
49
- end
50
- remove_invalid_operation_keys!(value)
51
- end
52
- end
53
- end
54
-
55
34
  doc.deep_merge!(metadata_to_swagger(metadata))
56
35
  end
57
36
 
@@ -92,12 +71,40 @@ class Swagalicious
92
71
  def metadata_to_swagger(metadata)
93
72
  response_code = metadata[:response][:code]
94
73
  response = metadata[:response].reject { |k, _v| k == :code }
74
+ examples = response.delete(:examples) || []
75
+ body = response.delete(:requestBody) || {}
76
+
77
+ examples.each do |mime_type, titles|
78
+ titles.each do |title, example|
79
+ next unless response[:content][mime_type]
80
+
81
+ response[:content][mime_type][:examples] ||= {}
82
+
83
+ response[:content][mime_type][:examples][title] ||= {}
84
+
85
+ response[:content][mime_type][:examples][title][:value] = example
86
+ end
87
+ end
95
88
 
96
89
  verb = metadata[:operation][:verb]
97
90
  operation = metadata[:operation]
98
91
  .reject { |k, _v| k == :verb }
99
92
  .merge(responses: { response_code => response })
100
93
 
94
+ is_hash = operation.is_a?(Hash)
95
+ if is_hash && operation.dig(:parameters)
96
+ schema_param = operation.dig(:parameters)&.find { |p| (p[:in] == :body || p[:in] == :formData) && p[:schema] }
97
+ mime_list = operation.dig(:consumes)
98
+ if operation && schema_param && mime_list
99
+ operation[:requestBody] = { content: {} }
100
+ mime_list.each do |mime|
101
+ operation[:requestBody][:content][mime] = { schema: schema_param[:schema] }.merge((body.dig(:content, mime) || {}))
102
+ end
103
+ end
104
+
105
+ operation[:parameters].reject! { |p| p[:in] == :body || p[:in] == :formData }
106
+ end
107
+
101
108
  path_template = metadata[:path_item][:template]
102
109
  path_item = metadata[:path_item]
103
110
  .reject { |k, _v| k == :template }
@@ -112,7 +119,7 @@ class Swagalicious
112
119
 
113
120
  def upgrade_response_produces!(swagger_doc, metadata)
114
121
  # Accept header
115
- mime_list = Array(metadata[:operation][:produces] || swagger_doc[:produces])
122
+ mime_list = Array(metadata[:operation].delete(:produces) || swagger_doc[:produces])
116
123
  target_node = metadata[:response]
117
124
  upgrade_content!(mime_list, target_node)
118
125
  metadata[:response].delete(:schema)
@@ -125,7 +132,11 @@ class Swagalicious
125
132
 
126
133
  mime_list.each do |mime_type|
127
134
  # TODO upgrade to have content-type specific schema
128
- target_node[:content][mime_type] = { schema: schema }
135
+ body = target_node
136
+ .fetch(:body, {})
137
+ .fetch(mime_type, {})
138
+
139
+ target_node[:content][mime_type] = { schema: schema }.merge(body)
129
140
  end
130
141
  end
131
142
 
@@ -1,3 +1,3 @@
1
1
  class Swagalicious
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.4"
3
3
  end
data/swagalicious.gemspec CHANGED
@@ -27,6 +27,9 @@ Gem::Specification.new do |spec|
27
27
  spec.add_dependency "faraday", "~> 1.3.0"
28
28
  spec.add_dependency "json-schema", "~> 2.8.1"
29
29
  spec.add_dependency "oj", "~> 3.11.0"
30
- spec.add_dependency "rack-test", "~> 1.1.0"
31
30
  spec.add_dependency "rspec", "~> 3.10.0"
31
+
32
+ spec.add_development_dependency "factory_bot", "~> 6.1.0"
33
+ spec.add_development_dependency "pry-byebug", "~> 3.9.0"
34
+ spec.add_development_dependency "rack-test", "~> 1.1.0"
32
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagalicious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Howe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-19 00:00:00.000000000 Z
11
+ date: 2021-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -53,33 +53,61 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.11.0
55
55
  - !ruby/object:Gem::Dependency
56
- name: rack-test
56
+ name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 1.1.0
61
+ version: 3.10.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 1.1.0
68
+ version: 3.10.0
69
69
  - !ruby/object:Gem::Dependency
70
- name: rspec
70
+ name: factory_bot
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 3.10.0
76
- type: :runtime
75
+ version: 6.1.0
76
+ type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 3.10.0
82
+ version: 6.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.9.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.9.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-test
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.1.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.1.0
83
111
  description: This gem is almost a straight copy and paste of https://github.com/rswag/rswag/tree/master/rswag-specs
84
112
  with the Rails specific code stripped out so it can be used in Rack applications
85
113
  that don't use Rails.