rspec-openapi 0.4.0 → 0.4.3

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: 71067082bcf502d2e38341d6c357ee596851cefa9ff4e95b4ac34249261782df
4
- data.tar.gz: '03279a96e39df1126d9fac2858903382654d2daa3121d415cf163b0bb1117fd1'
3
+ metadata.gz: 3bf318171323fbd3fa8d664a4f34ec1fe5af39a4d39ab5ee7b020b3871a31cb4
4
+ data.tar.gz: 610173247f3b9c99c7f5d2461bad49ae1d9000d7c8f63dfc4ac884472830c005
5
5
  SHA512:
6
- metadata.gz: 8a3344f5ee11410e189661e1ec4a6e39ab9775e2554a9a7c92818bfee71de670ec606539ccb71f60da70b727b1e8d65366507640496947e181e2bbe25251dbe2
7
- data.tar.gz: e332d6b082d4d6215535e90b750afaf2228310f989916f7b4d1b770c972ef055f329b0c84f37dba070719b4bbee7fcb47ec7d3f0c422af66b4bd712493b51e46
6
+ metadata.gz: 4b8c0402fe94dbcc43353c210acb1c9c4b182331020670bb8955dc4abf17a18732852fc4b191a3d1ded1d3613ef5e0059aa79e09084eb23bd9ce7ca5beec9e25
7
+ data.tar.gz: 6c4cd49edbaea41dfc1c2b17c89ec85dac8e1063612ff29f3c63dccde9dacd7a19eb276253363f1091a19976e0e6a20d40dc32f5deae36fa291e17f054b22eaf
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## v0.4.3
2
+
3
+ * Allow customizing `schema`, `description`, and `tags` through `:openapi` metadata
4
+ [#36](https://github.com/k0kubun/rspec-openapi/pull/36)
5
+
6
+ ## v0.4.2
7
+
8
+ * Allow using Proc as `RSpec::OpenAPI.path`
9
+ [#35](https://github.com/k0kubun/rspec-openapi/pull/35)
10
+
11
+ ## v0.4.1
12
+
13
+ * Add `RSpec::OpenAPI.example_types` to hook types other than `type: :request`.
14
+ [#32](https://github.com/k0kubun/rspec-openapi/pull/32)
15
+
1
16
  ## v0.4.0
2
17
 
3
18
  * Drop `RSpec::OpenAPI.output` introduced in v0.3.20
data/README.md CHANGED
@@ -24,7 +24,7 @@ gem 'rspec-openapi', group: :test
24
24
  Run rspec with OPENAPI=1 to generate `doc/openapi.yaml` for your request specs.
25
25
 
26
26
  ```bash
27
- $ OPENAPI=1 rspec
27
+ $ OPENAPI=1 bundle exec rspec
28
28
  ```
29
29
 
30
30
  ### Example
@@ -111,6 +111,15 @@ RSpec::OpenAPI.path = 'doc/schema.yaml'
111
111
  # Change the output type to JSON
112
112
  RSpec::OpenAPI.path = 'doc/schema.json'
113
113
 
114
+ # Or generate multiple partial schema files, given an RSpec example
115
+ RSpec::OpenAPI.path = -> (example) {
116
+ case example.file_path
117
+ when %r[spec/requests/api/v1/] then 'doc/openapi/v1.yaml'
118
+ when %r[spec/requests/api/v2/] then 'doc/openapi/v2.yaml'
119
+ else 'doc/openapi.yaml'
120
+ end
121
+ }
122
+
114
123
  # Disable generating `example`
115
124
  RSpec::OpenAPI.enable_example = false
116
125
 
@@ -135,6 +144,9 @@ EOS
135
144
 
136
145
  # Generate a custom description, given an RSpec example
137
146
  RSpec::OpenAPI.description_builder = -> (example) { example.description }
147
+
148
+ # Change the example type(s) that will generate schema
149
+ RSpec::OpenAPI.example_types = %i[request]
138
150
  ```
139
151
 
140
152
  ### How can I add information which can't be generated from RSpec?
@@ -160,22 +172,21 @@ RSpec.describe '/resources', type: :request do
160
172
  end
161
173
  ```
162
174
 
163
- ## Project status
164
-
165
- Beta
175
+ ## Customizations
166
176
 
167
- Basic features are there, and some people are already using this.
177
+ Some examples' attributes can be overwritten via RSpec metadata options. Example:
168
178
 
169
- ### Other missing features with notes
179
+ ```rb
180
+ describe 'GET /api/v1/posts', openapi: {
181
+ summary: 'list all posts',
182
+ description: 'list all posts ordered by pub_date',
183
+ tags: %w[v1 posts],
184
+ } do
185
+ # ...
186
+ end
187
+ ```
170
188
 
171
- * Delete obsoleted endpoints
172
- * Give up, or at least make the feature optional?
173
- * Running all to detect obsoleted endpoints is sometimes not realistic anyway.
174
- * Intelligent merges
175
- * To maintain both automated changes and manual edits, the schema merge needs to be intelligent.
176
- * We'll just deep-reverse-merge schema for now, but if there's a $ref for example, modifications
177
- there should be rerouted to the referenced object.
178
- * A type could be an array of all possible types when merged.
189
+ **NOTE**: `description` key will override also the one provided by `RSpec::OpenAPI.description_builder` method.
179
190
 
180
191
  ## Links
181
192
 
@@ -5,34 +5,36 @@ require 'rspec/openapi/schema_builder'
5
5
  require 'rspec/openapi/schema_file'
6
6
  require 'rspec/openapi/schema_merger'
7
7
 
8
- records = []
9
- records_errors = []
8
+ path_records = Hash.new { |h, k| h[k] = [] }
9
+ error_records = {}
10
10
 
11
11
  RSpec.configuration.after(:each) do |example|
12
- if example.metadata[:type] == :request && example.metadata[:openapi] != false
12
+ if RSpec::OpenAPI.example_types.include?(example.metadata[:type]) && example.metadata[:openapi] != false
13
+ path = RSpec::OpenAPI.path.yield_self { |path| path.is_a?(Proc) ? path.call(example) : path }
13
14
  record = RSpec::OpenAPI::RecordBuilder.build(self, example: example)
14
- records << record if record
15
+ path_records[path] << record if record
15
16
  end
16
17
  end
17
18
 
18
19
  RSpec.configuration.after(:suite) do
19
20
  title = File.basename(Dir.pwd)
20
- RSpec::OpenAPI::SchemaFile.new(RSpec::OpenAPI.path).edit do |spec|
21
- RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::DefaultSchema.build(title))
22
- records.each do |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]
21
+ path_records.each do |path, records|
22
+ RSpec::OpenAPI::SchemaFile.new(path).edit do |spec|
23
+ RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::DefaultSchema.build(title))
24
+ records.each do |record|
25
+ begin
26
+ RSpec::OpenAPI::SchemaMerger.reverse_merge!(spec, RSpec::OpenAPI::SchemaBuilder.build(record))
27
+ rescue StandardError, NotImplementedError => e # e.g. SchemaBuilder raises a NotImplementedError
28
+ error_records[e] = record # Avoid failing the build
29
+ end
28
30
  end
29
31
  end
30
32
  end
31
- if records_errors.any?
33
+ if error_records.any?
32
34
  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")}
35
+ RSpec::OpenAPI got errors building #{error_records.size} requests
36
+
37
+ #{error_records.map {|e, record| "#{e.inspect}: #{record.inspect}" }.join("\n")}
36
38
  EOS
37
39
  colorizer = ::RSpec::Core::Formatters::ConsoleCodes
38
40
  RSpec.configuration.reporter.message colorizer.wrap(error_message, :failure)
@@ -40,6 +40,8 @@ class << RSpec::OpenAPI::RecordBuilder = Object.new
40
40
  headers_arr << [header, header_value] if header_value
41
41
  end
42
42
 
43
+ metadata_options = example.metadata[:openapi] || {}
44
+
43
45
  RSpec::OpenAPI::Record.new(
44
46
  method: request.request_method,
45
47
  path: path,
@@ -48,9 +50,9 @@ class << RSpec::OpenAPI::RecordBuilder = Object.new
48
50
  request_params: raw_request_params(request),
49
51
  request_content_type: request.media_type,
50
52
  request_headers: request_headers,
51
- summary: summary,
52
- tags: tags,
53
- description: RSpec::OpenAPI.description_builder.call(example),
53
+ summary: metadata_options[:summary] || summary,
54
+ tags: metadata_options[:tags] || tags,
55
+ description: metadata_options[:description] || RSpec::OpenAPI.description_builder.call(example),
54
56
  status: response.status,
55
57
  response_body: response_body,
56
58
  response_content_type: response.media_type,
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module OpenAPI
3
- VERSION = '0.4.0'
3
+ VERSION = '0.4.3'
4
4
  end
5
5
  end
data/lib/rspec/openapi.rb CHANGED
@@ -9,8 +9,9 @@ module RSpec::OpenAPI
9
9
  @application_version = '1.0.0'
10
10
  @request_headers = []
11
11
  @server_urls = []
12
+ @example_types = %i[request]
12
13
 
13
14
  class << self
14
- attr_accessor :path, :comment, :enable_example, :description_builder, :application_version, :request_headers, :server_urls
15
+ attr_accessor :path, :comment, :enable_example, :description_builder, :application_version, :request_headers, :server_urls, :example_types
15
16
  end
16
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.4.0
4
+ version: 0.4.3
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-10-11 00:00:00.000000000 Z
11
+ date: 2022-03-17 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.1.2
93
+ rubygems_version: 3.3.7
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Generate OpenAPI schema from RSpec request specs