rspec-openapi 0.3.17 → 0.4.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: 880f187fffd674c1abc266390cb70acedd726d09e947042286ce01b93b42f1f1
4
- data.tar.gz: f602dbac30374bd4efa06806f505b8a867172b6ea493bf6d3e3ce8b156ce8455
3
+ metadata.gz: 71067082bcf502d2e38341d6c357ee596851cefa9ff4e95b4ac34249261782df
4
+ data.tar.gz: '03279a96e39df1126d9fac2858903382654d2daa3121d415cf163b0bb1117fd1'
5
5
  SHA512:
6
- metadata.gz: 1a8b1611dea768d2bf7fa3c884d666d2635fec945dccda28ce8314e6557fe33b1c6315c8994953639f92ed181c61323a4c0a169670c2e47808aa9348f88a88a3
7
- data.tar.gz: 82602c1729a8299fb128bd3f31b9449369e4eef7744bf60da1a54f1de148b9eb36b1e7b2e85b423695e36e3b6a3c1de7fa2710b5bae2e194ba23371b719f29ac
6
+ metadata.gz: 8a3344f5ee11410e189661e1ec4a6e39ab9775e2554a9a7c92818bfee71de670ec606539ccb71f60da70b727b1e8d65366507640496947e181e2bbe25251dbe2
7
+ data.tar.gz: e332d6b082d4d6215535e90b750afaf2228310f989916f7b4d1b770c972ef055f329b0c84f37dba070719b4bbee7fcb47ec7d3f0c422af66b4bd712493b51e46
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## v0.4.0
2
+
3
+ * Drop `RSpec::OpenAPI.output` introduced in v0.3.20
4
+ * Guess whether it's JSON or not by the extension of `RSpec::OpenAPI.path`
5
+
6
+ ## v0.3.20
7
+
8
+ * Add `RSpec::OpenAPI.output` config to output JSON
9
+ [#31](https://github.com/k0kubun/rspec-openapi/pull/31)
10
+
11
+ ## v0.3.19
12
+
13
+ * Add `server_urls` and `request_headers` configs
14
+ [#30](https://github.com/k0kubun/rspec-openapi/pull/30)
15
+
16
+ ## v0.3.18
17
+
18
+ * Support nested query parameters
19
+ [#29](https://github.com/k0kubun/rspec-openapi/pull/29)
20
+
1
21
  ## v0.3.17
2
22
 
3
23
  * Rescue NotImplementedError in the after suite hook as well
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 to JSON
112
+ RSpec::OpenAPI.path = 'doc/schema.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
@@ -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,19 @@ 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
+ YAML.load(File.read(@path)) # this can also parse JSON
24
25
  end
25
26
 
26
27
  # @param [Hash] spec
27
28
  def write(spec)
28
29
  FileUtils.mkdir_p(File.dirname(@path))
29
- File.write(@path, prepend_comment(YAML.dump(spec)))
30
+ output =
31
+ if json?
32
+ JSON.pretty_generate(spec)
33
+ else
34
+ prepend_comment(YAML.dump(spec))
35
+ end
36
+ File.write(@path, output)
30
37
  end
31
38
 
32
39
  def prepend_comment(content)
@@ -38,4 +45,8 @@ class RSpec::OpenAPI::SchemaFile
38
45
  end
39
46
  "#{comment.gsub(/^/, '# ').gsub(/^# \n/, "#\n")}#{content}"
40
47
  end
48
+
49
+ def json?
50
+ File.extname(@path) == '.json'
51
+ end
41
52
  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.17'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
data/lib/rspec/openapi.rb CHANGED
@@ -7,8 +7,10 @@ module RSpec::OpenAPI
7
7
  @enable_example = true
8
8
  @description_builder = -> (example) { example.description }
9
9
  @application_version = '1.0.0'
10
+ @request_headers = []
11
+ @server_urls = []
10
12
 
11
13
  class << self
12
- attr_accessor :path, :comment, :enable_example, :description_builder, :application_version
14
+ attr_accessor :path, :comment, :enable_example, :description_builder, :application_version, :request_headers, :server_urls
13
15
  end
14
16
  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.17
4
+ version: 0.4.0
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-15 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.1.2
94
+ signing_key:
95
95
  specification_version: 4
96
96
  summary: Generate OpenAPI schema from RSpec request specs
97
97
  test_files: []