openapi-ruby 3.3.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +27 -1
- data/lib/openapi_ruby/generator/rake_task_support.rb +89 -0
- data/lib/openapi_ruby/version.rb +1 -1
- data/lib/openapi_ruby.rb +17 -0
- data/lib/tasks/openapi_ruby.rake +6 -40
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4fd91990e73c6bcd5fae00558bb63afd1c4999de976b16d542aa495ddf2ad6a4
|
|
4
|
+
data.tar.gz: 19391cec8179b314284b51afa6adc977849f75aa431a7ec54df70f29b9877e45
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e9384c41d176a5cdf12a8164a148e8ff0a853aa15c73b06efb4f32bc96d40aaa18d4667c93ab6db863bf12de8eac783308a232a99dad66f755b3e1141ad456e5
|
|
7
|
+
data.tar.gz: c6d8b92d8473ac2f2582632c93d1f5d72fc41645b3c34a0b88192530d0bd1808aef836ac99b8864a92a88cb0587a89f1b0cf867811190e2758fdb86900e25552
|
data/README.md
CHANGED
|
@@ -469,10 +469,36 @@ Generate OpenAPI spec files without running tests:
|
|
|
469
469
|
rake openapi_ruby:generate
|
|
470
470
|
```
|
|
471
471
|
|
|
472
|
-
This loads spec/test files to collect API definitions and writes schemas without running any tests. It auto-detects the test framework, or you can set `FRAMEWORK=rspec` or `FRAMEWORK=
|
|
472
|
+
This loads spec/test files to collect API definitions and writes schemas without running any tests. It auto-detects the test framework, or you can set `FRAMEWORK=rspec`, `FRAMEWORK=minitest`, or `FRAMEWORK=hybrid`. Custom patterns: `PATTERN="packs/*/spec/**/*_spec.rb"`.
|
|
473
473
|
|
|
474
474
|
Schemas are **only** written by the rake task — running tests (`bundle exec rspec`, `rails test`) does not generate or overwrite schema files. This prevents partial schema overwrites when running a subset of specs.
|
|
475
475
|
|
|
476
|
+
### Migrating from RSpec to Minitest (or vice versa)
|
|
477
|
+
|
|
478
|
+
When both `spec/spec_helper.rb` and `test/test_helper.rb` are present, the rake task auto-selects `FRAMEWORK=hybrid` — it requires both adapters and loads both glob patterns (`spec/**/*_spec.rb,test/**/*_test.rb`) into one process. Style 1 `path(...)` and Style 2 `api_path(...)` definitions register into the same `MetadataStore`, so a single schema file holds paths contributed by either DSL.
|
|
479
|
+
|
|
480
|
+
Two things to set up on the consumer side so the two test frameworks don't both wire themselves into Rails' lazy-load hooks during schema generation:
|
|
481
|
+
|
|
482
|
+
```ruby
|
|
483
|
+
# test/test_helper.rb
|
|
484
|
+
unless OpenapiRuby.schema_generating?
|
|
485
|
+
require "rails/test_help"
|
|
486
|
+
# ...other test-time setup...
|
|
487
|
+
end
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
```ruby
|
|
491
|
+
# spec/rails_helper.rb
|
|
492
|
+
unless OpenapiRuby.schema_generating?
|
|
493
|
+
require "rspec/rails"
|
|
494
|
+
# ...other spec-time setup...
|
|
495
|
+
end
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
`OpenapiRuby.schema_generating?` returns `true` when the rake task launched the current process (it sets `OPENAPI_RUBY_GENERATING=true` in the subprocess). With the guards in place, neither test framework boots its full Rails integration during generation — only the DSL needs to be live for `api_path` / `path` to register.
|
|
499
|
+
|
|
500
|
+
The guards are only needed while both frameworks are live. Once the migration completes and only one test framework remains, the rake task auto-detects that framework and the guard becomes dead code that can be removed.
|
|
501
|
+
|
|
476
502
|
## Runtime Middleware
|
|
477
503
|
|
|
478
504
|
Validate requests and responses against your OpenAPI spec at runtime:
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenapiRuby
|
|
4
|
+
module Generator
|
|
5
|
+
# Helpers backing the `openapi_ruby:generate` rake task. Extracted
|
|
6
|
+
# so the framework detection / script generation logic is testable
|
|
7
|
+
# without booting Rake.
|
|
8
|
+
module RakeTaskSupport
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def detect_test_framework
|
|
12
|
+
rspec = File.exist?("spec/spec_helper.rb") || File.exist?("spec/rails_helper.rb")
|
|
13
|
+
minitest = File.exist?("test/test_helper.rb")
|
|
14
|
+
|
|
15
|
+
if rspec && minitest
|
|
16
|
+
"hybrid"
|
|
17
|
+
elsif rspec
|
|
18
|
+
"rspec"
|
|
19
|
+
elsif minitest
|
|
20
|
+
"minitest"
|
|
21
|
+
else
|
|
22
|
+
raise ArgumentError,
|
|
23
|
+
"Could not detect test framework. Set FRAMEWORK=rspec, FRAMEWORK=minitest, or FRAMEWORK=hybrid."
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def default_pattern_for(framework)
|
|
28
|
+
case framework
|
|
29
|
+
when "rspec" then "spec/**/*_spec.rb"
|
|
30
|
+
when "minitest" then "test/**/*_test.rb"
|
|
31
|
+
when "hybrid" then "spec/**/*_spec.rb,test/**/*_test.rb"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def generate_script(framework, pattern)
|
|
36
|
+
case framework
|
|
37
|
+
when "rspec" then rspec_script(pattern)
|
|
38
|
+
when "minitest" then minitest_script(pattern)
|
|
39
|
+
when "hybrid" then hybrid_script(pattern)
|
|
40
|
+
else
|
|
41
|
+
raise ArgumentError, "Unknown test framework '#{framework}'."
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def rspec_script(pattern)
|
|
46
|
+
<<~RUBY
|
|
47
|
+
require "rspec/core"
|
|
48
|
+
$LOAD_PATH.unshift(File.expand_path("spec")) unless $LOAD_PATH.include?(File.expand_path("spec"))
|
|
49
|
+
#{glob_loads(pattern)}
|
|
50
|
+
OpenapiRuby::Generator::SchemaWriter.generate_all!
|
|
51
|
+
RUBY
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def minitest_script(pattern)
|
|
55
|
+
<<~RUBY
|
|
56
|
+
require "openapi_ruby/minitest"
|
|
57
|
+
#{glob_loads(pattern)}
|
|
58
|
+
OpenapiRuby::Generator::SchemaWriter.generate_all!
|
|
59
|
+
RUBY
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Loads both adapters and both file globs in one process. Useful
|
|
63
|
+
# during a phased RSpec → Minitest migration where the suite
|
|
64
|
+
# holds both DSL styles. Consumers should guard
|
|
65
|
+
# `require "rails/test_help"` and `require "rspec/rails"` in
|
|
66
|
+
# their test helpers with `unless OpenapiRuby.schema_generating?`
|
|
67
|
+
# so the two test frameworks don't both register Rails lazy
|
|
68
|
+
# hooks in the same process — only the DSL needs to be live for
|
|
69
|
+
# schema generation.
|
|
70
|
+
def hybrid_script(pattern)
|
|
71
|
+
<<~RUBY
|
|
72
|
+
require "rspec/core"
|
|
73
|
+
require "openapi_ruby/rspec"
|
|
74
|
+
require "openapi_ruby/minitest"
|
|
75
|
+
$LOAD_PATH.unshift(File.expand_path("spec")) unless $LOAD_PATH.include?(File.expand_path("spec"))
|
|
76
|
+
$LOAD_PATH.unshift(File.expand_path("test")) unless $LOAD_PATH.include?(File.expand_path("test"))
|
|
77
|
+
#{glob_loads(pattern)}
|
|
78
|
+
OpenapiRuby::Generator::SchemaWriter.generate_all!
|
|
79
|
+
RUBY
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def glob_loads(pattern)
|
|
83
|
+
pattern.split(",").map do |p|
|
|
84
|
+
%[Dir.glob(#{p.strip.inspect}).sort.each { |f| require File.expand_path(f) }]
|
|
85
|
+
end.join("\n")
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
data/lib/openapi_ruby/version.rb
CHANGED
data/lib/openapi_ruby.rb
CHANGED
|
@@ -27,6 +27,22 @@ module OpenapiRuby
|
|
|
27
27
|
def reset_configuration!
|
|
28
28
|
@configuration = Configuration.new
|
|
29
29
|
end
|
|
30
|
+
|
|
31
|
+
# True when the current process was started by `openapi_ruby:generate`
|
|
32
|
+
# (the rake task sets OPENAPI_RUBY_GENERATING=true in the subprocess).
|
|
33
|
+
#
|
|
34
|
+
# Useful in consumer test helpers to guard test-framework requires
|
|
35
|
+
# that conflict when both `rspec/rails` and `rails/test_help` load
|
|
36
|
+
# in the same process (the FRAMEWORK=hybrid case):
|
|
37
|
+
#
|
|
38
|
+
# # test/test_helper.rb
|
|
39
|
+
# unless OpenapiRuby.schema_generating?
|
|
40
|
+
# require "rails/test_help"
|
|
41
|
+
# # ...other test-time setup...
|
|
42
|
+
# end
|
|
43
|
+
def schema_generating?
|
|
44
|
+
ENV["OPENAPI_RUBY_GENERATING"] == "true"
|
|
45
|
+
end
|
|
30
46
|
end
|
|
31
47
|
end
|
|
32
48
|
|
|
@@ -47,6 +63,7 @@ require_relative "openapi_ruby/testing/request_validator"
|
|
|
47
63
|
require_relative "openapi_ruby/testing/assertions"
|
|
48
64
|
require_relative "openapi_ruby/testing/coverage"
|
|
49
65
|
require_relative "openapi_ruby/generator/schema_writer"
|
|
66
|
+
require_relative "openapi_ruby/generator/rake_task_support"
|
|
50
67
|
require_relative "openapi_ruby/middleware/path_matcher"
|
|
51
68
|
require_relative "openapi_ruby/middleware/coercion"
|
|
52
69
|
require_relative "openapi_ruby/middleware/error_handler"
|
data/lib/tasks/openapi_ruby.rake
CHANGED
|
@@ -1,55 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "openapi_ruby/generator/rake_task_support"
|
|
4
|
+
|
|
3
5
|
namespace :openapi_ruby do
|
|
4
6
|
desc "Generate OpenAPI schema files from spec definitions and components"
|
|
5
7
|
task :generate do
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
support = OpenapiRuby::Generator::RakeTaskSupport
|
|
9
|
+
framework = ENV.fetch("FRAMEWORK") { support.detect_test_framework }.to_s
|
|
10
|
+
pattern = ENV.fetch("PATTERN") { support.default_pattern_for(framework) }
|
|
8
11
|
|
|
9
12
|
# Spawn a subprocess so RAILS_ENV defaults to "test" cleanly,
|
|
10
13
|
# just like rswag did with RSpec::Core::RakeTask.
|
|
11
14
|
env = {"RAILS_ENV" => ENV.fetch("RAILS_ENV", "test"), "OPENAPI_RUBY_GENERATING" => "true"}
|
|
12
|
-
script = generate_script(framework, pattern)
|
|
15
|
+
script = support.generate_script(framework, pattern)
|
|
13
16
|
command = "bundle exec ruby -e #{Shellwords.escape(script)}"
|
|
14
17
|
|
|
15
18
|
puts "Generating OpenAPI schemas (#{framework})..."
|
|
16
19
|
system(env, command) || abort("Schema generation failed")
|
|
17
20
|
end
|
|
18
21
|
end
|
|
19
|
-
|
|
20
|
-
def detect_test_framework
|
|
21
|
-
if File.exist?("spec/spec_helper.rb") || File.exist?("spec/rails_helper.rb")
|
|
22
|
-
"rspec"
|
|
23
|
-
elsif File.exist?("test/test_helper.rb")
|
|
24
|
-
"minitest"
|
|
25
|
-
else
|
|
26
|
-
abort "Could not detect test framework. Set FRAMEWORK=rspec or FRAMEWORK=minitest."
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def default_pattern_for(framework)
|
|
31
|
-
case framework
|
|
32
|
-
when "rspec" then "spec/**/*_spec.rb"
|
|
33
|
-
when "minitest" then "test/**/*_test.rb"
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def generate_script(framework, pattern)
|
|
38
|
-
case framework
|
|
39
|
-
when "rspec"
|
|
40
|
-
<<~RUBY
|
|
41
|
-
require "rspec/core"
|
|
42
|
-
$LOAD_PATH.unshift(File.expand_path("spec")) unless $LOAD_PATH.include?(File.expand_path("spec"))
|
|
43
|
-
#{pattern.split(",").map { |p| %[Dir.glob(#{p.strip.inspect}).sort.each { |f| require File.expand_path(f) }] }.join("\n")}
|
|
44
|
-
OpenapiRuby::Generator::SchemaWriter.generate_all!
|
|
45
|
-
RUBY
|
|
46
|
-
when "minitest"
|
|
47
|
-
<<~RUBY
|
|
48
|
-
require "openapi_ruby/minitest"
|
|
49
|
-
#{pattern.split(",").map { |p| %[Dir.glob(#{p.strip.inspect}).sort.each { |f| require File.expand_path(f) }] }.join("\n")}
|
|
50
|
-
OpenapiRuby::Generator::SchemaWriter.generate_all!
|
|
51
|
-
RUBY
|
|
52
|
-
else
|
|
53
|
-
abort "Unknown test framework '#{framework}'."
|
|
54
|
-
end
|
|
55
|
-
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: openapi-ruby
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Morten Hartvig
|
|
@@ -105,6 +105,7 @@ files:
|
|
|
105
105
|
- lib/openapi_ruby/dsl/response_context.rb
|
|
106
106
|
- lib/openapi_ruby/engine.rb
|
|
107
107
|
- lib/openapi_ruby/errors.rb
|
|
108
|
+
- lib/openapi_ruby/generator/rake_task_support.rb
|
|
108
109
|
- lib/openapi_ruby/generator/schema_writer.rb
|
|
109
110
|
- lib/openapi_ruby/middleware/coercion.rb
|
|
110
111
|
- lib/openapi_ruby/middleware/error_handler.rb
|