swagalicious 0.1.0 → 0.2.0

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: 941a558739b2079027c117ccea67ffc90c7a21f682811ab817a739ef0ea93d82
4
- data.tar.gz: 275cce80dedbfcaa5b5e76d01499fe18babcc7e30a948806ad95df1cea46d475
3
+ metadata.gz: 5067242694dc59e20ef8dd3c3c3a22456cd22b1b905be4805721f73c5239036c
4
+ data.tar.gz: eac425366e51b211629753cb3ca7eeaa896688beafa44a75de75ab591aac3246
5
5
  SHA512:
6
- metadata.gz: 2e8f90a288217b8e27e85411194a73a7f5d1bbe7bd8881a1f086c11319b8ee9a832143bc9c610a8398fb11051484be70f828f3a1625d5c2180d5e1d1194e5121
7
- data.tar.gz: 181ff6d1a9dcab5b13499f648b75299f4ab6321c2ea4d65956a94d4bd8b62f90d8e049c771629ed457ca828635f7f90ab35f9bba630b437d0ebb11136d0cdb19
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
- swagger: "3.0",
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/eugene@xtreme-computers.net/swagalicious.
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ehowe/swagalicious.
61
59
 
62
60
 
63
61
  ## License
@@ -1,9 +1,17 @@
1
1
  class Hash
2
- def deep_merge!(other_hash)
3
- self.merge(other_hash) do |key, oldval, newval|
4
- oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
5
- newval = newval.to_hash if newval.respond_to?(:to_hash)
6
- oldval.class.to_s == "Hash" && newval.class.to_s == "Hash" ? oldval.deep_merge(newval) : newval
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
 
@@ -2,9 +2,13 @@ require "rspec/core"
2
2
 
3
3
  require_relative "swagalicious/version"
4
4
 
5
- module Swagalicious
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
@@ -1,4 +1,4 @@
1
- module Swagalicious
1
+ class Swagalicious
2
2
  class ConfigurationError < StandardError; end
3
3
 
4
4
  class Configuration
@@ -1,4 +1,4 @@
1
- module Swagalicious
1
+ class Swagalicious
2
2
  module ExampleGroupHelpers
3
3
  def path(template, metadata={}, &block)
4
4
  metadata[:path_item] = { template: template }
@@ -5,7 +5,7 @@ require "oj"
5
5
 
6
6
  require_relative "response_validator"
7
7
 
8
- module Swagalicious
8
+ class Swagalicious
9
9
  module ExampleHelpers
10
10
  include Rack::Test::Methods
11
11
 
@@ -1,6 +1,6 @@
1
1
  require "json-schema"
2
2
 
3
- module Swagalicious
3
+ class Swagalicious
4
4
  class ExtendedSchema < JSON::Schema::Draft4
5
5
  def initialize
6
6
  super
@@ -1,7 +1,7 @@
1
1
  require "faraday"
2
2
  require "faraday/adapter/rack"
3
3
 
4
- module Swagalicious
4
+ class Swagalicious
5
5
  class RequestFactory
6
6
  def initialize(config = ::Swagalicious.config)
7
7
  @config = config
@@ -1,6 +1,6 @@
1
1
  require "json"
2
2
 
3
- module Swagalicious
3
+ class Swagalicious
4
4
  class UnexpectedResponse < StandardError; end
5
5
 
6
6
  class ResponseValidator
@@ -2,67 +2,71 @@
2
2
 
3
3
  require_relative "../core/ext/hash"
4
4
 
5
- module Swagalicious
5
+ class Swagalicious
6
6
  class SwaggerFormatter
7
- RSpec::Core::Formatters.register self, :example_group_finished, :stop
7
+ RSpec::Core::Formatters.register self, :stop
8
8
 
9
- def initialize(output, config = Swagalicious.config)
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 example_group_finished(notification)
17
- metadata = notification.group.metadata
18
-
19
- # !metadata[:document] won"t work, since nil means we should generate
20
- # docs.
21
- return if metadata[:document] == false
22
- return unless metadata.key?(:response)
23
-
24
- swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])
25
-
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_request_type!(metadata)
30
- upgrade_servers!(swagger_doc)
31
- upgrade_oauth!(swagger_doc)
32
- upgrade_response_produces!(swagger_doc, metadata)
33
-
34
- swagger_doc.deep_merge!(metadata_to_swagger(metadata))
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(_notification = nil)
38
- @config.swagger_docs.each do |url_path, doc|
39
- unless doc_version(doc).start_with?("2")
40
- doc[:paths]&.each_pair do |_k, v|
41
- v.each_pair do |_verb, value|
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(@config.swagger_root, url_path)
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(doc))
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 @config.swagger_format == :yaml
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)
@@ -1,3 +1,3 @@
1
- module Swagalicious
2
- VERSION = "0.1.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.1.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-09-10 00:00:00.000000000 Z
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.0.6
136
+ rubygems_version: 3.1.4
137
137
  signing_key:
138
138
  specification_version: 4
139
139
  summary: RSwag without Rails