rspec-openapi 0.3.16 → 0.3.20

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: 799f75b4bb5607dff7ab4a556b5d27d72b587af88636e7b510533724a1ede97a
4
- data.tar.gz: 71aedf1e1610f23106dd778a2b9d9f4677c73c280127ff61300afa024068bef8
3
+ metadata.gz: d0ff0f4854e68684d59b37788218a797c61cdff744808237ca08818493a393eb
4
+ data.tar.gz: 0730c2487ccb75a2fd3bec8052136cb75b9410d42daea1e3a6c9826d6854df8d
5
5
  SHA512:
6
- metadata.gz: e65865ca2d26e66f6fa560fe3fc157a491786ba7243d267dcdd4f22e3e5c28c29895384a32b8b19c095317a0447453ad292bfe91ff4059395f8ee08f0763f738
7
- data.tar.gz: fda7f3678e952359a356069353d0756890edd7da50e4cd422489db091f9223439e1385d7939f27bd55d4c4afc7e6760bd55805275f932345273e6d5528a2b036
6
+ metadata.gz: dadb96bc250a1c615176c5ada6e5dd6a41f1dec2a96099ef5181ac83e77d9c008168c0219c0a92d59bfc7a5ca0388138bcdd2641d8c5eb767d42a38a2cbd4096
7
+ data.tar.gz: 276bf5d35e634db31d6cdcaac519dac4972cbf0c93014c9c45d59a97bb0d2c20ee969c843f54e879eca8bab29cea0192cf36aab9c3ba5b0c2ee803692b0a5816
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## v0.3.20
2
+
3
+ * Add `RSpec::OpenAPI.output` config to output JSON
4
+ [#31](https://github.com/k0kubun/rspec-openapi/pull/31)
5
+
6
+ ## v0.3.19
7
+
8
+ * Add `server_urls` and `request_headers` configs
9
+ [#30](https://github.com/k0kubun/rspec-openapi/pull/30)
10
+
11
+ ## v0.3.18
12
+
13
+ * Support nested query parameters
14
+ [#29](https://github.com/k0kubun/rspec-openapi/pull/29)
15
+
16
+ ## v0.3.17
17
+
18
+ * Rescue NotImplementedError in the after suite hook as well
19
+ [#28](https://github.com/k0kubun/rspec-openapi/pull/28)
20
+
1
21
  ## v0.3.16
2
22
 
3
23
  * Use `media_type` instead of `content_type` for Rails 6.1
data/README.md CHANGED
@@ -108,12 +108,23 @@ The following configurations are optional.
108
108
  # Change the path to generate schema from `doc/openapi.yaml`
109
109
  RSpec::OpenAPI.path = 'doc/schema.yaml'
110
110
 
111
+ # Change the output type yaml or json
112
+ RSpec::OpenAPI.output = :json
113
+
111
114
  # Disable generating `example`
112
115
  RSpec::OpenAPI.enable_example = false
113
116
 
114
117
  # Change `info.version`
115
118
  RSpec::OpenAPI.application_version = '1.0.0'
116
119
 
120
+ # Set `headers` - generate parameters with headers for a request
121
+ RSpec::OpenAPI.request_headers = %w[X-Authorization-Token]
122
+
123
+ # Set `server_urls` - generate servers of a schema file
124
+ RSpec::OpenAPI.server_urls = %w[
125
+ http://localhost:3000
126
+ ]
127
+
117
128
  # Generate a comment on top of a schema file
118
129
  RSpec::OpenAPI.comment = <<~EOS
119
130
  This file is auto-generated by rspec-openapi https://github.com/k0kubun/rspec-openapi
@@ -155,10 +166,6 @@ Beta
155
166
 
156
167
  Basic features are there, and some people are already using this.
157
168
 
158
- ### Current limitation
159
-
160
- * Generating a JSON file is not supported yet
161
-
162
169
  ### Other missing features with notes
163
170
 
164
171
  * Delete obsoleted endpoints
@@ -6,6 +6,7 @@ class << RSpec::OpenAPI::DefaultSchema = Object.new
6
6
  title: title,
7
7
  version: RSpec::OpenAPI.application_version,
8
8
  },
9
+ servers: RSpec::OpenAPI.server_urls.map { |url| { url: url } } || [],
9
10
  paths: {},
10
11
  }.freeze
11
12
  end
@@ -22,7 +22,7 @@ RSpec.configuration.after(:suite) do
22
22
  records.each do |record|
23
23
  begin
24
24
  RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::SchemaBuilder.build(record))
25
- rescue => e # e.g. SchemaBuilder raises a NotImplementedError
25
+ rescue StandardError, NotImplementedError => e # e.g. SchemaBuilder raises a NotImplementedError
26
26
  # NOTE: Don't fail the build
27
27
  records_errors << [e, record]
28
28
  end
@@ -5,6 +5,7 @@ RSpec::OpenAPI::Record = Struct.new(
5
5
  :query_params, # @param [Hash] - {:query=>"string"}
6
6
  :request_params, # @param [Hash] - {:request=>"body"}
7
7
  :request_content_type, # @param [String] - "application/json"
8
+ :request_headers, # @param [Array] - [["header_key1", "header_value1"], ["header_key2", "header_value2"]]
8
9
  :summary, # @param [String] - "v1/statuses #show"
9
10
  :tags, # @param [Array] - ["Status"]
10
11
  :description, # @param [String] - "returns a status"
@@ -34,6 +34,12 @@ class << RSpec::OpenAPI::RecordBuilder = Object.new
34
34
  nil
35
35
  end
36
36
 
37
+ request_headers = RSpec::OpenAPI.request_headers.each_with_object([]) do |header, headers_arr|
38
+ header_key = header.gsub(/-/, '_').upcase
39
+ header_value = request.get_header(['HTTP', header_key].join('_')) || request.get_header(header_key)
40
+ headers_arr << [header, header_value] if header_value
41
+ end
42
+
37
43
  RSpec::OpenAPI::Record.new(
38
44
  method: request.request_method,
39
45
  path: path,
@@ -41,6 +47,7 @@ class << RSpec::OpenAPI::RecordBuilder = Object.new
41
47
  query_params: request.query_parameters,
42
48
  request_params: raw_request_params(request),
43
49
  request_content_type: request.media_type,
50
+ request_headers: request_headers,
44
51
  summary: summary,
45
52
  tags: tags,
46
53
  description: RSpec::OpenAPI.description_builder.call(example),
@@ -50,7 +50,7 @@ class << RSpec::OpenAPI::SchemaBuilder = Object.new
50
50
 
51
51
  record.path_params.each do |key, value|
52
52
  parameters << {
53
- name: key.to_s,
53
+ name: build_parameter_name(key, value),
54
54
  in: 'path',
55
55
  required: true,
56
56
  schema: build_property(try_cast(value)),
@@ -60,17 +60,37 @@ class << RSpec::OpenAPI::SchemaBuilder = Object.new
60
60
 
61
61
  record.query_params.each do |key, value|
62
62
  parameters << {
63
- name: key.to_s,
63
+ name: build_parameter_name(key, value),
64
64
  in: 'query',
65
65
  schema: build_property(try_cast(value)),
66
66
  example: (try_cast(value) if example_enabled?),
67
67
  }.compact
68
68
  end
69
69
 
70
+ record.request_headers.each do |key, value|
71
+ parameters << {
72
+ name: build_parameter_name(key, value),
73
+ in: 'header',
74
+ required: true,
75
+ schema: build_property(try_cast(value)),
76
+ example: (try_cast(value) if example_enabled?),
77
+ }.compact
78
+ end
79
+
70
80
  return nil if parameters.empty?
71
81
  parameters
72
82
  end
73
83
 
84
+ def build_parameter_name(key, value)
85
+ key = key.to_s
86
+ if value.is_a?(Hash) && (value_keys = value.keys).size == 1
87
+ value_key = value_keys.first
88
+ build_parameter_name("#{key}[#{value_key}]", value[value_key])
89
+ else
90
+ key
91
+ end
92
+ end
93
+
74
94
  def build_request_body(record)
75
95
  return nil if record.request_content_type.nil?
76
96
  return nil if record.request_params.empty?
@@ -1,5 +1,6 @@
1
1
  require 'fileutils'
2
2
  require 'yaml'
3
+ require 'json'
3
4
 
4
5
  # TODO: Support JSON
5
6
  class RSpec::OpenAPI::SchemaFile
@@ -20,13 +21,25 @@ class RSpec::OpenAPI::SchemaFile
20
21
  # @return [Hash]
21
22
  def read
22
23
  return {} unless File.exist?(@path)
23
- YAML.load(File.read(@path))
24
+
25
+ content = File.read(@path)
26
+
27
+ return JSON.load(content) if json?
28
+
29
+ YAML.load(content)
24
30
  end
25
31
 
26
32
  # @param [Hash] spec
27
33
  def write(spec)
28
34
  FileUtils.mkdir_p(File.dirname(@path))
29
- File.write(@path, prepend_comment(YAML.dump(spec)))
35
+
36
+ output = if json?
37
+ JSON.pretty_generate(spec)
38
+ else
39
+ prepend_comment(YAML.dump(spec))
40
+ end
41
+
42
+ File.write(@path, output)
30
43
  end
31
44
 
32
45
  def prepend_comment(content)
@@ -38,4 +51,8 @@ class RSpec::OpenAPI::SchemaFile
38
51
  end
39
52
  "#{comment.gsub(/^/, '# ').gsub(/^# \n/, "#\n")}#{content}"
40
53
  end
54
+
55
+ def json?
56
+ RSpec::OpenAPI.output.to_s == 'json'
57
+ end
41
58
  end
@@ -30,6 +30,13 @@ class << RSpec::OpenAPI::SchemaMerger = Object.new
30
30
  deep_reverse_merge!(base[key], value)
31
31
  elsif !base.key?(key)
32
32
  base[key] = value
33
+ elsif base[key].is_a?(Array) && value.is_a?(Array)
34
+ if key == "parameters"
35
+ # merge arrays
36
+ base[key] |= value
37
+ end
38
+ else
39
+ # no-op
33
40
  end
34
41
  end
35
42
  base
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module OpenAPI
3
- VERSION = '0.3.16'
3
+ VERSION = '0.3.20'
4
4
  end
5
5
  end
data/lib/rspec/openapi.rb CHANGED
@@ -7,8 +7,11 @@ module RSpec::OpenAPI
7
7
  @enable_example = true
8
8
  @description_builder = -> (example) { example.description }
9
9
  @application_version = '1.0.0'
10
+ @output = :yaml
11
+ @request_headers = []
12
+ @server_urls = []
10
13
 
11
14
  class << self
12
- attr_accessor :path, :comment, :enable_example, :description_builder, :application_version
15
+ attr_accessor :path, :comment, :enable_example, :description_builder, :application_version, :request_headers, :server_urls, :output
13
16
  end
14
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-openapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.16
4
+ version: 0.3.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-12 00:00:00.000000000 Z
11
+ date: 2021-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -75,7 +75,7 @@ metadata:
75
75
  homepage_uri: https://github.com/k0kubun/rspec-openapi
76
76
  source_code_uri: https://github.com/k0kubun/rspec-openapi
77
77
  changelog_uri: https://github.com/k0kubun/rspec-openapi/blob/master/CHANGELOG.md
78
- post_install_message:
78
+ post_install_message:
79
79
  rdoc_options: []
80
80
  require_paths:
81
81
  - lib
@@ -90,8 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  - !ruby/object:Gem::Version
91
91
  version: '0'
92
92
  requirements: []
93
- rubygems_version: 3.1.6
94
- signing_key:
93
+ rubygems_version: 3.2.22
94
+ signing_key:
95
95
  specification_version: 4
96
96
  summary: Generate OpenAPI schema from RSpec request specs
97
97
  test_files: []