rspec-openapi 0.3.20 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +15 -20
- data/lib/rspec/openapi/hooks.rb +18 -16
- data/lib/rspec/openapi/schema_file.rb +8 -14
- data/lib/rspec/openapi/version.rb +1 -1
- data/lib/rspec/openapi.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a02d800d2c00b3cfec6a1a860f17eafaed45e51329aaa7407ed1e96892a10ac9
|
4
|
+
data.tar.gz: db0ec61540014497148d218846c01287d2b4f0e1ec4782148d932c5308ccbc75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b357caa7a5b3f497a743ddf7be434ceb141e5de9b93d5995ac66e7d42abce47fddaa65f5f62f0ceffca4e4d42a40405c7b6d8190b4d2557148d633af1c9e289d
|
7
|
+
data.tar.gz: c8166a897c99f4b60747bc4c51f223c20627303fd6276c2e8e9a4908fee6287d43f1fb43135354ff7615f7c0cece8665c1b5a96da234e3598dbe311c1468d712
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
## v0.4.2
|
2
|
+
|
3
|
+
* Allow using Proc as `RSpec::OpenAPI.path`
|
4
|
+
[#35](https://github.com/k0kubun/rspec-openapi/pull/35)
|
5
|
+
|
6
|
+
## v0.4.1
|
7
|
+
|
8
|
+
* Add `RSpec::OpenAPI.example_types` to hook types other than `type: :request`.
|
9
|
+
[#32](https://github.com/k0kubun/rspec-openapi/pull/32)
|
10
|
+
|
11
|
+
## v0.4.0
|
12
|
+
|
13
|
+
* Drop `RSpec::OpenAPI.output` introduced in v0.3.20
|
14
|
+
* Guess whether it's JSON or not by the extension of `RSpec::OpenAPI.path`
|
15
|
+
|
1
16
|
## v0.3.20
|
2
17
|
|
3
18
|
* Add `RSpec::OpenAPI.output` config to output JSON
|
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
|
@@ -108,8 +108,17 @@ 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
|
112
|
-
RSpec::OpenAPI.
|
111
|
+
# Change the output type to JSON
|
112
|
+
RSpec::OpenAPI.path = 'doc/schema.json'
|
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
|
+
}
|
113
122
|
|
114
123
|
# Disable generating `example`
|
115
124
|
RSpec::OpenAPI.enable_example = false
|
@@ -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,23 +172,6 @@ RSpec.describe '/resources', type: :request do
|
|
160
172
|
end
|
161
173
|
```
|
162
174
|
|
163
|
-
## Project status
|
164
|
-
|
165
|
-
Beta
|
166
|
-
|
167
|
-
Basic features are there, and some people are already using this.
|
168
|
-
|
169
|
-
### Other missing features with notes
|
170
|
-
|
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.
|
179
|
-
|
180
175
|
## Links
|
181
176
|
|
182
177
|
Existing RSpec plugins which have OpenAPI integration:
|
data/lib/rspec/openapi/hooks.rb
CHANGED
@@ -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
|
-
|
9
|
-
|
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]
|
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
|
-
|
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
|
-
|
21
|
-
RSpec::OpenAPI::
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
|
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
|
33
|
+
if error_records.any?
|
32
34
|
error_message = <<~EOS
|
33
|
-
RSpec::OpenAPI got errors building #{
|
34
|
-
|
35
|
-
#{
|
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)
|
@@ -21,24 +21,18 @@ class RSpec::OpenAPI::SchemaFile
|
|
21
21
|
# @return [Hash]
|
22
22
|
def read
|
23
23
|
return {} unless File.exist?(@path)
|
24
|
-
|
25
|
-
content = File.read(@path)
|
26
|
-
|
27
|
-
return JSON.load(content) if json?
|
28
|
-
|
29
|
-
YAML.load(content)
|
24
|
+
YAML.load(File.read(@path)) # this can also parse JSON
|
30
25
|
end
|
31
26
|
|
32
27
|
# @param [Hash] spec
|
33
28
|
def write(spec)
|
34
29
|
FileUtils.mkdir_p(File.dirname(@path))
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
30
|
+
output =
|
31
|
+
if json?
|
32
|
+
JSON.pretty_generate(spec)
|
33
|
+
else
|
34
|
+
prepend_comment(YAML.dump(spec))
|
35
|
+
end
|
42
36
|
File.write(@path, output)
|
43
37
|
end
|
44
38
|
|
@@ -53,6 +47,6 @@ class RSpec::OpenAPI::SchemaFile
|
|
53
47
|
end
|
54
48
|
|
55
49
|
def json?
|
56
|
-
|
50
|
+
File.extname(@path) == '.json'
|
57
51
|
end
|
58
52
|
end
|
data/lib/rspec/openapi.rb
CHANGED
@@ -7,11 +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
10
|
@request_headers = []
|
12
11
|
@server_urls = []
|
12
|
+
@example_types = %i[request]
|
13
13
|
|
14
14
|
class << self
|
15
|
-
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
|
16
16
|
end
|
17
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
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takashi Kokubun
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|