rspec-openapi 0.3.15 → 0.3.19

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: 76a7b33ccfea2d2fbaf9cb45b49171c46c0b6b9df44280ac6d6bf0f864aedb60
4
- data.tar.gz: d52a3651c1dc945711c378d39e53867f518c9b8aed2d6bf7502c00aade767856
3
+ metadata.gz: bc42fc95d88e3523412bf9efbee6066b2789637a5b0213550f9d16711c1c9a94
4
+ data.tar.gz: fcaa99b78c4cc3404b9ee5b8cb9906fb3872b8c06ceb8ceb5d38f775015ae6ed
5
5
  SHA512:
6
- metadata.gz: f8eac8961aa0b71a48f3af48c118dad88b8e4e90767dfb37c87d0902422a9ed6bea2c43cf87c713c209be0d7284f114cd455d49069b8521d3d34ad6ffcd901eb
7
- data.tar.gz: d2a984317e8b93f8f2f29540c2a34af9fa89b46aadbdd300cc2247212d338530aff7d53aae75b27cabbd1d21ea3a30698051e1cd836509bf36c202f5c538eec9
6
+ metadata.gz: aa9865db15800d17549ee640ed88b2d5f32289e4ac61a30db43cf0adf7021889123cedb96558032c0c8b25c03d5390897fa716352edb98c7a5c091b64210b924
7
+ data.tar.gz: 5da282aa5811e4a61f24f4c853200c3e8e786704d737eff325651eca75cef49c06fb96d1d7c9547afa821e98e807fa8cf1509c672b934a296e57927f584c3e14
data/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ ## v0.3.19
2
+
3
+ * Add `server_urls` and `request_headers` configs
4
+ [#30](https://github.com/k0kubun/rspec-openapi/pull/30)
5
+
6
+ ## v0.3.18
7
+
8
+ * Support nested query parameters
9
+ [#29](https://github.com/k0kubun/rspec-openapi/pull/29)
10
+
11
+ ## v0.3.17
12
+
13
+ * Rescue NotImplementedError in the after suite hook as well
14
+ [#28](https://github.com/k0kubun/rspec-openapi/pull/28)
15
+
16
+ ## v0.3.16
17
+
18
+ * Use `media_type` instead of `content_type` for Rails 6.1
19
+ [#26](https://github.com/k0kubun/rspec-openapi/pull/26)
20
+ * Avoid crashing the after suite hook when it fails to build schema
21
+ [#27](https://github.com/k0kubun/rspec-openapi/pull/27)
22
+
1
23
  ## v0.3.15
2
24
 
3
25
  * Fix an error when Content-Disposition header is inline
data/README.md CHANGED
@@ -114,6 +114,14 @@ RSpec::OpenAPI.enable_example = false
114
114
  # Change `info.version`
115
115
  RSpec::OpenAPI.application_version = '1.0.0'
116
116
 
117
+ # Set `headers` - generate parameters with headers for a request
118
+ RSpec::OpenAPI.request_headers = %w[X-Authorization-Token]
119
+
120
+ # Set `server_urls` - generate servers of a schema file
121
+ RSpec::OpenAPI.server_urls = %w[
122
+ http://localhost:3000
123
+ ]
124
+
117
125
  # Generate a comment on top of a schema file
118
126
  RSpec::OpenAPI.comment = <<~EOS
119
127
  This file is auto-generated by rspec-openapi https://github.com/k0kubun/rspec-openapi
@@ -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
@@ -6,6 +6,7 @@ require 'rspec/openapi/schema_file'
6
6
  require 'rspec/openapi/schema_merger'
7
7
 
8
8
  records = []
9
+ records_errors = []
9
10
 
10
11
  RSpec.configuration.after(:each) do |example|
11
12
  if example.metadata[:type] == :request && example.metadata[:openapi] != false
@@ -19,7 +20,21 @@ RSpec.configuration.after(:suite) do
19
20
  RSpec::OpenAPI::SchemaFile.new(RSpec::OpenAPI.path).edit do |spec|
20
21
  RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::DefaultSchema.build(title))
21
22
  records.each do |record|
22
- RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::SchemaBuilder.build(record))
23
+ begin
24
+ RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::SchemaBuilder.build(record))
25
+ rescue StandardError, NotImplementedError => e # e.g. SchemaBuilder raises a NotImplementedError
26
+ # NOTE: Don't fail the build
27
+ records_errors << [e, record]
28
+ end
23
29
  end
24
30
  end
31
+ if records_errors.any?
32
+ error_message = <<~EOS
33
+ RSpec::OpenAPI got errors building #{records_errors.size} requests
34
+
35
+ #{records_errors.map {|e, record| "#{e.inspect}: #{record.inspect}" }.join("\n")}
36
+ EOS
37
+ colorizer = ::RSpec::Core::Formatters::ConsoleCodes
38
+ RSpec.configuration.reporter.message colorizer.wrap(error_message, :failure)
39
+ end
25
40
  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,19 +34,26 @@ 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,
40
46
  path_params: raw_path_params(request),
41
47
  query_params: request.query_parameters,
42
48
  request_params: raw_request_params(request),
43
- request_content_type: request.content_type,
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),
47
54
  status: response.status,
48
55
  response_body: response_body,
49
- response_content_type: response.content_type,
56
+ response_content_type: response.media_type,
50
57
  response_content_disposition: response.header["Content-Disposition"],
51
58
  ).freeze
52
59
  end
@@ -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?
@@ -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.15'
3
+ VERSION = '0.3.19'
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.15
4
+ version: 0.3.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-24 00:00:00.000000000 Z
11
+ date: 2021-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  - !ruby/object:Gem::Version
91
91
  version: '0'
92
92
  requirements: []
93
- rubygems_version: 3.2.3
93
+ rubygems_version: 3.1.2
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Generate OpenAPI schema from RSpec request specs