openapi-ruby 3.3.0 → 3.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2596878f73873970637c965935c1f4689cc744f3496467d734bd9a2c816904ee
4
- data.tar.gz: 2c7f2d7b4497175d0c558f2179888529ae1397d724cba0088248049ef5fda518
3
+ metadata.gz: b58ff6e6c8ccbf7c8c02d01f6a79ea02717cf3c709de9d1c8a8f870bb44a84fb
4
+ data.tar.gz: e18b82cac3cd08bee2fcbee279d45e9b705b62ad0015feb8e3e5408cf4af63a5
5
5
  SHA512:
6
- metadata.gz: d54c13cb1de806e0e5f21ed1e17d6287a525b00be8654d2c77e554a6bc6762dc206002797874fa6882872754b2bc6438dbfa590e0f3c03f0211383d1fa2490ff
7
- data.tar.gz: fb01ab7bb2b8f51d7fb9ef0432a261717d5cf1dc90a7c2c17168f3039456d0b5af99c74eb1853d9ebbf1df80dc8410bbaffa5c53af4f3799960a00c4a128d287
6
+ metadata.gz: d9248a97e6b5f513516f5d97d87ef81f68047ce4db54c62503667d20751bf15f99fb4644cde3369d017b388184e92e65f904aa171e546c189134d8d1dc742eea
7
+ data.tar.gz: 4a627ed1b9bc867c24263627a27f01f22d5fc50bfde5ffe9d93e87484c50e68f61e739162c198c808b979d484c9c1a3b80921c14eedd32a9c7b076a702a90cb9
data/README.md CHANGED
@@ -469,10 +469,34 @@ 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=minitest`. Custom patterns: `PATTERN="packs/*/spec/**/*_spec.rb"`.
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 ENV["OPENAPI_RUBY_GENERATING"]
485
+ require "rails/test_help"
486
+ # ...other test-time setup...
487
+ end
488
+ ```
489
+
490
+ ```ruby
491
+ # spec/rails_helper.rb
492
+ unless ENV["OPENAPI_RUBY_GENERATING"]
493
+ require "rspec/rails"
494
+ # ...other spec-time setup...
495
+ end
496
+ ```
497
+
498
+ The rake task 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
+
476
500
  ## Runtime Middleware
477
501
 
478
502
  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 ENV["OPENAPI_RUBY_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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiRuby
4
- VERSION = "3.3.0"
4
+ VERSION = "3.4.0"
5
5
  end
data/lib/openapi_ruby.rb CHANGED
@@ -47,6 +47,7 @@ require_relative "openapi_ruby/testing/request_validator"
47
47
  require_relative "openapi_ruby/testing/assertions"
48
48
  require_relative "openapi_ruby/testing/coverage"
49
49
  require_relative "openapi_ruby/generator/schema_writer"
50
+ require_relative "openapi_ruby/generator/rake_task_support"
50
51
  require_relative "openapi_ruby/middleware/path_matcher"
51
52
  require_relative "openapi_ruby/middleware/coercion"
52
53
  require_relative "openapi_ruby/middleware/error_handler"
@@ -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
- framework = ENV.fetch("FRAMEWORK", detect_test_framework).to_s
7
- pattern = ENV.fetch("PATTERN", default_pattern_for(framework))
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.3.0
4
+ version: 3.4.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