swagalicious 0.1.0 → 0.2.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/README.md +2 -4
- data/lib/core/ext/hash.rb +13 -5
- data/lib/swagalicious.rb +5 -5
- data/lib/swagalicious/configuration.rb +1 -1
- data/lib/swagalicious/example_group_helpers.rb +1 -1
- data/lib/swagalicious/example_helpers.rb +1 -1
- data/lib/swagalicious/extended_schema.rb +1 -1
- data/lib/swagalicious/request_factory.rb +1 -1
- data/lib/swagalicious/response_validator.rb +1 -1
- data/lib/swagalicious/swagger_formatter.rb +52 -48
- data/lib/swagalicious/version.rb +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5067242694dc59e20ef8dd3c3c3a22456cd22b1b905be4805721f73c5239036c
|
4
|
+
data.tar.gz: eac425366e51b211629753cb3ca7eeaa896688beafa44a75de75ab591aac3246
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b89eaa63682085fa323a27b268b2b7255d5ac453040e0c2e0c988d8378c62b986514056eaf062b76737424623c36743fd56ca1f71ee1c6116e1b991bb1082b8
|
7
|
+
data.tar.gz: 9ab0b21f31c7ec3e845ae58be31db0d006855ce717ca1a1f1f596ee480ddad44904d4fa43ea7f47801cd7014e480d0673af177b601b95ec761cc1e66274db8dc
|
data/README.md
CHANGED
@@ -27,13 +27,11 @@ Add the following to your `spec_helper.rb` or add a new `swagger_helper.rb`
|
|
27
27
|
```ruby
|
28
28
|
require 'swagalicious`
|
29
29
|
|
30
|
-
DEFINITIONS = Oj.load(File.read(File.expand_path("docs/definitions.json", __dir__))).freeze
|
31
|
-
|
32
30
|
RSpec.configure do |c|
|
33
31
|
c.swagger_root = "public/swagger_docs" # This is the relative path where the swagger docs will be output
|
34
32
|
c.swagger_docs = {
|
35
33
|
"path/to/swagger_doc.json" => {
|
36
|
-
|
34
|
+
openapi: "3.0.3",
|
37
35
|
basePath: "/api/",
|
38
36
|
version: "v1",
|
39
37
|
info: {
|
@@ -57,7 +55,7 @@ end
|
|
57
55
|
|
58
56
|
## Contributing
|
59
57
|
|
60
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ehowe/swagalicious.
|
61
59
|
|
62
60
|
|
63
61
|
## License
|
data/lib/core/ext/hash.rb
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
class Hash
|
2
|
-
def deep_merge
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
def deep_merge(other_hash, &block)
|
3
|
+
dup.deep_merge!(other_hash)
|
4
|
+
end
|
5
|
+
|
6
|
+
def deep_merge!(other_hash, &block)
|
7
|
+
merge!(other_hash) do |key, this_val, other_val|
|
8
|
+
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
9
|
+
this_val.deep_merge(other_val, &block)
|
10
|
+
elsif block_given?
|
11
|
+
block.call(key, this_val, other_val)
|
12
|
+
else
|
13
|
+
other_val
|
14
|
+
end
|
7
15
|
end
|
8
16
|
end
|
9
17
|
|
data/lib/swagalicious.rb
CHANGED
@@ -2,9 +2,13 @@ require "rspec/core"
|
|
2
2
|
|
3
3
|
require_relative "swagalicious/version"
|
4
4
|
|
5
|
-
|
5
|
+
class Swagalicious
|
6
6
|
class Error < StandardError; end
|
7
7
|
|
8
|
+
def self.config
|
9
|
+
@config ||= Swagalicious::Configuration.new(RSpec.configuration)
|
10
|
+
end
|
11
|
+
|
8
12
|
require_relative "swagalicious/configuration"
|
9
13
|
require_relative "swagalicious/example_group_helpers"
|
10
14
|
require_relative "swagalicious/example_helpers"
|
@@ -23,8 +27,4 @@ module Swagalicious
|
|
23
27
|
c.extend Swagalicious::ExampleGroupHelpers, type: :doc
|
24
28
|
c.include Swagalicious::ExampleHelpers, type: :doc
|
25
29
|
end
|
26
|
-
|
27
|
-
def self.config
|
28
|
-
@config ||= Swagalicious::Configuration.new(RSpec.configuration)
|
29
|
-
end
|
30
30
|
end
|
@@ -2,67 +2,71 @@
|
|
2
2
|
|
3
3
|
require_relative "../core/ext/hash"
|
4
4
|
|
5
|
-
|
5
|
+
class Swagalicious
|
6
6
|
class SwaggerFormatter
|
7
|
-
RSpec::Core::Formatters.register self, :
|
7
|
+
RSpec::Core::Formatters.register self, :stop
|
8
8
|
|
9
|
-
def
|
9
|
+
def config
|
10
|
+
@config ||= Swagalicious.config
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(output, config = nil)
|
10
14
|
@output = output
|
11
15
|
@config = config
|
12
16
|
|
13
17
|
@output.puts "Generating Swagger docs ..."
|
14
18
|
end
|
15
19
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
20
|
+
def merge_metadata_to_document(doc, example)
|
21
|
+
metadata = example.metadata
|
22
|
+
# !metadata[:document] won"t work, since nil means we should generate
|
23
|
+
# docs.
|
24
|
+
return {} if metadata[:document] == false
|
25
|
+
return {} unless metadata.key?(:response)
|
26
|
+
# This is called multiple times per file!
|
27
|
+
# metadata[:operation] is also re-used between examples within file
|
28
|
+
# therefore be careful NOT to modify its content here.
|
29
|
+
upgrade_servers!(doc)
|
30
|
+
upgrade_oauth!(doc)
|
31
|
+
upgrade_response_produces!(doc, metadata)
|
32
|
+
upgrade_request_type!(metadata)
|
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
|
+
doc.deep_merge!(metadata_to_swagger(metadata))
|
35
56
|
end
|
36
57
|
|
37
|
-
def stop(
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
is_hash = value.is_a?(Hash)
|
43
|
-
if is_hash && value.dig(:parameters)
|
44
|
-
schema_param = value.dig(:parameters)&.find { |p| (p[:in] == :body || p[:in] == :formData) && p[:schema] }
|
45
|
-
mime_list = value.dig(:consumes)
|
46
|
-
if value && schema_param && mime_list
|
47
|
-
value[:requestBody] = { content: {} } unless value.dig(:requestBody, :content)
|
48
|
-
mime_list.each do |mime|
|
49
|
-
value[:requestBody][:content][mime] = { schema: schema_param[:schema] }
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
value[:parameters].reject! { |p| p[:in] == :body || p[:in] == :formData }
|
54
|
-
end
|
55
|
-
remove_invalid_operation_keys!(value)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
58
|
+
def stop(notification = nil)
|
59
|
+
config.swagger_docs.each do |url_path, doc|
|
60
|
+
examples = notification.examples.select { |e| e.metadata[:swagger_doc] == url_path }
|
61
|
+
|
62
|
+
merged_doc = examples.each_with_object(doc) { |e, doc| doc = doc.deep_merge!(merge_metadata_to_document(doc, e)) }
|
59
63
|
|
60
|
-
file_path = File.join(
|
64
|
+
file_path = File.join(config.swagger_root, url_path)
|
61
65
|
dirname = File.dirname(file_path)
|
62
66
|
FileUtils.mkdir_p dirname unless File.exist?(dirname)
|
63
67
|
|
64
68
|
File.open(file_path, "w") do |file|
|
65
|
-
file.write(pretty_generate(
|
69
|
+
file.write(pretty_generate(merged_doc))
|
66
70
|
end
|
67
71
|
|
68
72
|
@output.puts "Swagger doc generated at #{file_path}"
|
@@ -72,7 +76,7 @@ module Swagalicious
|
|
72
76
|
private
|
73
77
|
|
74
78
|
def pretty_generate(doc)
|
75
|
-
if
|
79
|
+
if config.swagger_format == :yaml
|
76
80
|
clean_doc = yaml_prepare(doc)
|
77
81
|
YAML.dump(clean_doc)
|
78
82
|
else # config errors are thrown in "def swagger_format", no throw needed here
|
@@ -103,7 +107,7 @@ module Swagalicious
|
|
103
107
|
end
|
104
108
|
|
105
109
|
def doc_version(doc)
|
106
|
-
doc[:openapi] || doc[:swagger] || "3"
|
110
|
+
doc[:openapi] || doc[:swagger] || "3.0.0"
|
107
111
|
end
|
108
112
|
|
109
113
|
def upgrade_response_produces!(swagger_doc, metadata)
|
data/lib/swagalicious/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.
|
1
|
+
class Swagalicious
|
2
|
+
VERSION = "0.2.0"
|
3
3
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eugene Howe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
- !ruby/object:Gem::Version
|
134
134
|
version: '0'
|
135
135
|
requirements: []
|
136
|
-
rubygems_version: 3.
|
136
|
+
rubygems_version: 3.1.4
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: RSwag without Rails
|