swagalicious 0.3.0 → 0.4.3

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: 76b550126d23800f0a2612840c99e9204b82b1007a77f9aa3ba0469565e98385
4
- data.tar.gz: 30fd88c6c6e4316247cea40688b5649e42d9971c54fe98b301f90d24d56418c5
3
+ metadata.gz: 07172e8767c218c6fa083315bd673c0a44e2b58e1a6895498494dc94c254dbd6
4
+ data.tar.gz: 89f1919937bcbd9bf1d19c8908d3627740436628ff12445bc9749ec547a44c3c
5
5
  SHA512:
6
- metadata.gz: 2a8b447b7903389bcaa24bcda9896723e79b3ff32b438874bc8bc604ba9420ed3888d5010c1074b2a0a56f5b2386b889ce111d6f0751ad98eab3a07ca03856cc
7
- data.tar.gz: 1932ca415378e1197cf7ec88b43a7bd9284d1d8e61bb0575f8c4ff8fa6b2a60851de15166e190d70c4dd0021db4d1009cf248b564fe5183f3e22af4c8cf4864b
6
+ metadata.gz: fc08b4023572d0da49587efdcadd66ca490df1474adfb6e522f07f326c9214f2d26d3ae751774a1335c7a21349efe178c8fe5ba989b8ae440a3b8c012179c7d4
7
+ data.tar.gz: 6399f42ccf024d8add0ef4b181eba92f4bd520b634c194a22cc5337216989e35cb1dca1cafa166e3c192a15a0059de86f015045d6537d40219b5f284f27c0690
data/Gemfile.lock CHANGED
@@ -1,51 +1,81 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swagalicious (0.2.0)
5
- faraday (~> 1.0.1)
4
+ swagalicious (0.4.3)
5
+ faraday (~> 1.3.0)
6
6
  json-schema (~> 2.8.1)
7
- oj (~> 3.10.14)
8
- rack-test (~> 1.1.0)
9
- rspec (~> 3.9.0)
7
+ oj (~> 3.11.0)
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)
17
- faraday (1.0.1)
25
+ factory_bot (6.1.0)
26
+ activesupport (>= 5.0.0)
27
+ faraday (1.3.0)
28
+ faraday-net_http (~> 1.0)
18
29
  multipart-post (>= 1.2, < 3)
30
+ ruby2_keywords
31
+ faraday-net_http (1.0.1)
32
+ i18n (1.8.7)
33
+ concurrent-ruby (~> 1.0)
19
34
  json-schema (2.8.1)
20
35
  addressable (>= 2.4)
36
+ method_source (1.0.0)
37
+ minitest (5.14.3)
21
38
  multipart-post (2.1.1)
22
- oj (3.10.18)
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)
23
46
  public_suffix (4.0.6)
24
47
  rack (2.2.3)
25
48
  rack-test (1.1.0)
26
49
  rack (>= 1.0, < 3)
27
50
  rake (12.3.3)
28
- rspec (3.9.0)
29
- rspec-core (~> 3.9.0)
30
- rspec-expectations (~> 3.9.0)
31
- rspec-mocks (~> 3.9.0)
32
- rspec-core (3.9.2)
33
- rspec-support (~> 3.9.3)
34
- rspec-expectations (3.9.2)
51
+ rspec (3.10.0)
52
+ rspec-core (~> 3.10.0)
53
+ rspec-expectations (~> 3.10.0)
54
+ rspec-mocks (~> 3.10.0)
55
+ rspec-core (3.10.1)
56
+ rspec-support (~> 3.10.0)
57
+ rspec-expectations (3.10.1)
35
58
  diff-lcs (>= 1.2.0, < 2.0)
36
- rspec-support (~> 3.9.0)
37
- rspec-mocks (3.9.1)
59
+ rspec-support (~> 3.10.0)
60
+ rspec-mocks (3.10.1)
38
61
  diff-lcs (>= 1.2.0, < 2.0)
39
- rspec-support (~> 3.9.0)
40
- rspec-support (3.9.3)
62
+ rspec-support (~> 3.10.0)
63
+ rspec-support (3.10.1)
64
+ ruby2_keywords (0.0.4)
65
+ tzinfo (2.0.4)
66
+ concurrent-ruby (~> 1.0)
67
+ zeitwerk (2.4.2)
41
68
 
42
69
  PLATFORMS
43
70
  ruby
44
71
 
45
72
  DEPENDENCIES
73
+ factory_bot (~> 6.1.0)
74
+ pry-byebug (~> 3.9.0)
75
+ rack-test (~> 1.1.0)
46
76
  rake (~> 12.0)
47
77
  rspec (~> 3.0)
48
78
  swagalicious!
49
79
 
50
80
  BUNDLED WITH
51
- 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
@@ -49,14 +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
- # TODO: What if there is more than 1 produces?
57
- mime_type = metadata[:operation][:produces].first
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] ||= {}
58
67
 
59
- metadata[:response][:body] ||= { mime_type => { examples: { mime_type => @body } } }
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
60
74
 
61
75
  # Validates response matches the proper schema
62
76
  Swagalicious::ResponseValidator.new.validate!(metadata, response)
@@ -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)
@@ -1,3 +1,3 @@
1
1
  class Swagalicious
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.3"
3
3
  end
data/swagalicious.gemspec CHANGED
@@ -24,9 +24,12 @@ Gem::Specification.new do |spec|
24
24
  end
25
25
  spec.require_paths = ["lib"]
26
26
 
27
- spec.add_dependency "faraday", "~> 1.0.1"
27
+ spec.add_dependency "faraday", "~> 1.3.0"
28
28
  spec.add_dependency "json-schema", "~> 2.8.1"
29
- spec.add_dependency "oj", "~> 3.10.14"
30
- spec.add_dependency "rack-test", "~> 1.1.0"
31
- spec.add_dependency "rspec", "~> 3.9.0"
29
+ spec.add_dependency "oj", "~> 3.11.0"
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.0
4
+ version: 0.4.3
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-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.1
19
+ version: 1.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.1
26
+ version: 1.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json-schema
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,42 +44,70 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 3.10.14
47
+ version: 3.11.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 3.10.14
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
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 6.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 6.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - "~>"
74
88
  - !ruby/object:Gem::Version
75
89
  version: 3.9.0
76
- type: :runtime
90
+ type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
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.