openapi-ruby 4.0.1 → 4.0.2

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: d1104f4d110622c6c94bce175fbb538fa07a5621ecc1d45901eac1d4f5ad1907
4
- data.tar.gz: e62100c294bd83d9ddfb8f435bd92af9df3ddfce68fbb0d1e194badb5b799f5a
3
+ metadata.gz: 27487aa88423921e2f0274f7d93a5abd534dc3fca2567692d07c73001b66017f
4
+ data.tar.gz: 3f62cffd0f03d943b7198e6d230b5ad851746d4e7982f65ea891d213fd509490
5
5
  SHA512:
6
- metadata.gz: e04da5e753006dc5342abadf092ac9067ba46b661098eafcf090f9e64a861b6d73a46001ca3b69a21052dd6953cba2beed9fb44238dc186c465ec7fc1ceb1715
7
- data.tar.gz: 63bc2eeff43bbd5efbb82c66eb00239797b7f413ada695cd1b1afe1753872d9237bf419a735fcf48cd785f2c2c58074da5a289ae8761c73df382b7d9a11871b6
6
+ metadata.gz: 580fb57048ffea5a623ba2b7940b83c8bee9a8e7d73db91ccbe566095fcf64b09ae0228fe9a791df6c2a7a9c03f6a14cdb92d65a24cfd7b881bc245715f1e198
7
+ data.tar.gz: be207593a6158ea4b1fd0cc0758d332f5557ba61788b04a32e8eabbc85ae8300060956d20bfad9198952bccd92e0ce41e76fd2a02e298cf4dff17a8a0ec2cd10
data/README.md CHANGED
@@ -469,13 +469,41 @@ rake openapi_ruby:generate
469
469
 
470
470
  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"`.
471
471
 
472
+ Loading a test file normally *is* enough to run it: `rails/test_help` requires `active_support/testing/autorun`, and `rspec/autorun` does the equivalent — both register an `at_exit` hook that runs the suite. The generated script therefore installs `OpenapiRuby::Generator::AutorunSuppressor` before requiring anything of yours, so the hook is never registered. Generation stays a load-only operation no matter how your helpers are wired.
473
+
472
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.
473
475
 
476
+ ### Making generation cheaper (optional)
477
+
478
+ Generation only needs your `path` / `api_path` declarations to register. Everything else a test helper does — connecting to a database, loading fixtures, `maintain_test_schema!` — is dead weight, and on a large suite it dominates the runtime.
479
+
480
+ Guard that setup with `OpenapiRuby.schema_generating?`, which returns `true` only in the rake task's subprocess (it sets `OPENAPI_RUBY_GENERATING=true`):
481
+
482
+ ```ruby
483
+ # test/test_helper.rb
484
+ require "minitest/rails" # keep the spec DSL if your api_path classes use describe/it/let
485
+
486
+ return if OpenapiRuby.schema_generating?
487
+
488
+ require "rails/test_help"
489
+ # ...other test-time setup...
490
+ ```
491
+
492
+ This is an optimization, not a correctness requirement — the suppressor handles the autorun hook either way.
493
+
494
+ One caveat if you do guard: skipping `rails/test_help` also means `fixtures` is undefined, so any test file calling `fixtures :all` in its class body fails to *load*. Point `PATTERN` at just the files carrying `api_path` declarations:
495
+
496
+ ```bash
497
+ PATTERN="test/integration/api/**/*_test.rb" rake openapi_ruby:generate
498
+ ```
499
+
500
+ Suites using FactoryBot rather than fixtures don't hit this.
501
+
474
502
  ### Migrating from RSpec to Minitest (or vice versa)
475
503
 
476
504
  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.
477
505
 
478
- 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:
506
+ Here the guards described above stop being optional: without them both test frameworks wire themselves into Rails' lazy-load hooks in the same process.
479
507
 
480
508
  ```ruby
481
509
  # test/test_helper.rb
@@ -495,7 +523,7 @@ end
495
523
 
496
524
  `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.
497
525
 
498
- 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.
526
+ Once the migration completes and only one test framework remains, the rake task auto-detects that framework. The guard is then no longer *required* but it's still worth keeping for the reasons in "Making generation cheaper" above.
499
527
 
500
528
  ## Runtime Middleware
501
529
 
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenapiRuby
4
+ module Generator
5
+ # Schema generation loads the consumer's spec/test files purely to collect
6
+ # their `path` / `api_path` declarations. Loading them must not also *run*
7
+ # them.
8
+ #
9
+ # Both frameworks install an `at_exit` hook that runs the suite as a side
10
+ # effect of being required, and a consumer's helper almost always pulls one
11
+ # in transitively — `rails/test_help` requires
12
+ # `active_support/testing/autorun`, which calls `Minitest.autorun`; RSpec
13
+ # does the same via `rspec/autorun`. Without this, `rake openapi_ruby:generate`
14
+ # runs the entire suite and fails whenever any unrelated test fails.
15
+ #
16
+ # Installed by the generated script *before* any consumer file is required,
17
+ # so the hook is never registered in the first place.
18
+ module AutorunSuppressor
19
+ module_function
20
+
21
+ def install!
22
+ suppress_minitest!
23
+ suppress_rspec!
24
+ end
25
+
26
+ def suppress_minitest!
27
+ require "minitest"
28
+
29
+ ::Minitest.singleton_class.prepend(MinitestSilencer)
30
+ rescue LoadError
31
+ # Minitest isn't in the bundle — nothing to suppress.
32
+ end
33
+
34
+ def suppress_rspec!
35
+ return unless defined?(::RSpec::Core::Runner)
36
+
37
+ ::RSpec::Core::Runner.disable_autorun!
38
+ end
39
+
40
+ module MinitestSilencer
41
+ # Swallow the `at_exit` registration. Defined as a no-op rather than
42
+ # setting `@@installed_at_exit` so this doesn't depend on Minitest's
43
+ # internals.
44
+ def autorun
45
+ nil
46
+ end
47
+
48
+ # Belt and braces: if something registered the hook before this module
49
+ # was installed, the hook still fires but finds nothing to do.
50
+ def run(*)
51
+ true
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -42,9 +42,19 @@ module OpenapiRuby
42
42
  end
43
43
  end
44
44
 
45
+ # Prepended to every generated script. Loading the consumer's test files
46
+ # must not run them — see AutorunSuppressor.
47
+ def suppress_autorun
48
+ <<~RUBY.chomp
49
+ require "openapi_ruby/generator/autorun_suppressor"
50
+ OpenapiRuby::Generator::AutorunSuppressor.install!
51
+ RUBY
52
+ end
53
+
45
54
  def rspec_script(pattern)
46
55
  <<~RUBY
47
56
  require "rspec/core"
57
+ #{suppress_autorun}
48
58
  $LOAD_PATH.unshift(File.expand_path("spec")) unless $LOAD_PATH.include?(File.expand_path("spec"))
49
59
  #{glob_loads(pattern)}
50
60
  OpenapiRuby::Generator::SchemaWriter.generate_all!
@@ -54,6 +64,7 @@ module OpenapiRuby
54
64
  def minitest_script(pattern)
55
65
  <<~RUBY
56
66
  require "openapi_ruby/minitest"
67
+ #{suppress_autorun}
57
68
  $LOAD_PATH.unshift(File.expand_path("test")) unless $LOAD_PATH.include?(File.expand_path("test"))
58
69
  #{glob_loads(pattern)}
59
70
  OpenapiRuby::Generator::SchemaWriter.generate_all!
@@ -86,6 +97,7 @@ module OpenapiRuby
86
97
  require "rspec/core"
87
98
  require "openapi_ruby/rspec"
88
99
  require "openapi_ruby/minitest"
100
+ #{suppress_autorun}
89
101
 
90
102
  load_with_path = lambda do |dir, glob|
91
103
  path = File.expand_path(dir)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiRuby
4
- VERSION = "4.0.1"
4
+ VERSION = "4.0.2"
5
5
  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: 4.0.1
4
+ version: 4.0.2
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/autorun_suppressor.rb
108
109
  - lib/openapi_ruby/generator/rake_task_support.rb
109
110
  - lib/openapi_ruby/generator/schema_writer.rb
110
111
  - lib/openapi_ruby/middleware/coercion.rb