openapi-ruby 2.6.0 → 3.0.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: 624a9de17a27fc482cf818dde3e816f4a4117f75abe79360acc21ebf6adc3c94
4
- data.tar.gz: f886aa70bf6d15d26a88ed99abda4a7440eb6d3f8304a6759896e28e6dfeb137
3
+ metadata.gz: d33ec2601c1341f8afb455e3b2f0f1daa594aa96ddf3daebb8b6c6675a1f3723
4
+ data.tar.gz: 44827b7a0f446ed51c1de676b8e777437ccfe8408b7166b731f1b55842b96fb2
5
5
  SHA512:
6
- metadata.gz: 54139198ea02942afe69bce8b677bd582ea60e303aece0cbca150b04583a2bc5688f8a56e3dafbf6d2ee4cba87d10396fff54996956d8ad77ab4db109a849fd6
7
- data.tar.gz: 31489283c2805df247155d08ce89575015342429a3d507c5b44549a09baa853646ede82288ca5ecc443d660bcfa93a34036e58fdc872757688471a2e07d804fe
6
+ metadata.gz: 5d52dce6834c33e78cd9b8666291b35c289da0981eba2cadfad5c48b10d63a45c65a8d7e959dc09438ec64b9883e0a15ccea96af1c955892901096e0f1172075
7
+ data.tar.gz: 6c99f505c721c19bc5be989f4d6d0b0e4a5f7dfe794d688286040b89fd5146dc3aa73ef1c2fbe65fc418785644686bf4d65bf23c98185ddc5814e244f8d1b7b0
data/README.md CHANGED
@@ -5,14 +5,14 @@
5
5
  <h1 align="center">openapi_ruby</h1>
6
6
 
7
7
  <p align="center">
8
- A unified OpenAPI 3.1 toolkit for Rails that combines test-driven spec generation, reusable schema components as Ruby classes, and runtime request/response validation middleware. Works with both RSpec and Minitest.
8
+ A unified OpenAPI toolkit for Rails that combines test-driven spec generation, reusable schema components as Ruby classes, and runtime request/response validation middleware. Supports OpenAPI 3.0 and 3.1. Works with both RSpec and Minitest.
9
9
  </p>
10
10
 
11
11
  Replaces [rswag](https://github.com/rswag/rswag), [rswag-schema-components](https://github.com/101skills-gmbh/rswag-schema-components), and [committee](https://github.com/interagent/committee) with a single gem.
12
12
 
13
13
  ## Key Features
14
14
 
15
- - **OpenAPI 3.1** with JSON Schema 2020-12 (via [json_schemer](https://github.com/davishmcclurg/json_schemer))
15
+ - **OpenAPI 3.0 & 3.1** with JSON Schema 2020-12 (via [json_schemer](https://github.com/davishmcclurg/json_schemer))
16
16
  - **Test-framework agnostic** — works with RSpec and Minitest
17
17
  - **Schema components** as Ruby classes with inheritance
18
18
  - **Runtime middleware** for request/response validation with deep type checking
@@ -74,6 +74,45 @@ OpenapiRuby.configure do |config|
74
74
  end
75
75
  ```
76
76
 
77
+ ### OpenAPI Version
78
+
79
+ The default OpenAPI version is 3.1.0. To generate 3.0.x schemas (e.g., when using `nullable: true`):
80
+
81
+ ```ruby
82
+ config.schemas = {
83
+ public_api: {
84
+ openapi_version: "3.0.3",
85
+ info: { title: "My API", version: "v1" },
86
+ servers: [{ url: "/" }]
87
+ }
88
+ }
89
+ ```
90
+
91
+ ### Multiple Schemas with Scopes
92
+
93
+ For projects with multiple APIs, use `component_scope` to partition components:
94
+
95
+ ```ruby
96
+ config.schemas = {
97
+ "internal/v1/schema": {
98
+ info: { title: "Internal API", version: "v1" },
99
+ component_scope: :internal_v1
100
+ },
101
+ "public/v2/schema": {
102
+ info: { title: "Public API", version: "v2" },
103
+ component_scope: :public_v2
104
+ }
105
+ }
106
+
107
+ # Infer scopes from directory structure (e.g., internal/v1/schemas/user.rb → :internal_v1)
108
+ config.component_scope_paths = {
109
+ "internal/v1" => :internal_v1,
110
+ "public/v2" => :public_v2
111
+ }
112
+ ```
113
+
114
+ Components are automatically scoped based on their file path. Use `shared_component` to include a component in all schemas, or `component_scopes :scope1, :scope2` to assign explicitly.
115
+
77
116
  ## Schema Components
78
117
 
79
118
  Define your API schemas as Ruby classes:
@@ -334,13 +373,15 @@ end
334
373
 
335
374
  ## Spec Generation
336
375
 
337
- Generate OpenAPI spec files after running tests:
376
+ Generate OpenAPI spec files without running tests:
338
377
 
339
378
  ```bash
340
379
  rake openapi_ruby:generate
341
380
  ```
342
381
 
343
- Or automatically after the test suite via the `after(:suite)` / `Minitest.after_run` hooks (enabled by default).
382
+ This uses RSpec `--dry-run` (or loads Minitest files) to collect API definitions and write schemas. It auto-detects the test framework, or you can set `FRAMEWORK=rspec` or `FRAMEWORK=minitest`. Custom patterns: `PATTERN="packs/*/spec/**/*_spec.rb"`.
383
+
384
+ Schemas are also generated automatically after running the full test suite via `after(:suite)` / `Minitest.after_run` hooks.
344
385
 
345
386
  ## Runtime Middleware
346
387
 
@@ -191,11 +191,10 @@ module OpenapiRuby
191
191
  end
192
192
 
193
193
  def self.install!
194
- ::Minitest.after_run do
195
- OpenapiRuby::Generator::SchemaWriter.generate_all!
196
- rescue => e
197
- warn "[openapi_ruby] Schema generation failed: #{e.message}"
198
- end
194
+ # Schema writing is handled by the rake task (openapi_ruby:generate),
195
+ # not by test runs. The rake task loads test files to register DSL
196
+ # contexts, then calls SchemaWriter.generate_all! directly.
197
+ # This prevents partial schema overwrites when running a subset of tests.
199
198
  end
200
199
  end
201
200
  end
@@ -278,22 +278,10 @@ module OpenapiRuby
278
278
  config.include ::RSpec::Rails::RequestExampleGroup, type: :openapi
279
279
  end
280
280
 
281
- config.after(:suite) do
282
- OpenapiRuby::Generator::SchemaWriter.generate_all!
283
- rescue => e
284
- warn "[openapi_ruby] Schema generation failed: #{e.message}"
285
- end
286
-
287
- # RSpec's --dry-run mode does not fire after(:suite) hooks.
288
- # Describe/context blocks are still evaluated (registering DSL contexts),
289
- # but the hook that writes schemas never runs. Use at_exit as a fallback.
290
- if config.dry_run?
291
- at_exit do
292
- OpenapiRuby::Generator::SchemaWriter.generate_all!
293
- rescue => e
294
- warn "[openapi_ruby] Schema generation failed: #{e.message}"
295
- end
296
- end
281
+ # Schema writing is handled by the rake task (openapi_ruby:generate),
282
+ # not by test runs. The rake task loads spec files to register DSL
283
+ # contexts, then calls SchemaWriter.generate_all! directly.
284
+ # This prevents partial schema overwrites when running a subset of specs.
297
285
  end
298
286
  end
299
287
  end
@@ -28,7 +28,7 @@ module OpenapiRuby
28
28
  end
29
29
 
30
30
  def set_security(security)
31
- @data["security"] = security if security.any?
31
+ @data["security"] = security
32
32
  end
33
33
 
34
34
  def set_tags(tags)
@@ -34,8 +34,8 @@ module OpenapiRuby
34
34
  def to_openapi
35
35
  result = {}
36
36
 
37
- result["parameters"] = @path_parameters if @path_parameters.any?
38
-
37
+ # Path-level parameters are already copied into each operation (line 27),
38
+ # so we don't output them at the path level to avoid duplicates.
39
39
  @operations.each do |verb, op|
40
40
  result[verb] = op.to_openapi
41
41
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiRuby
4
- VERSION = "2.6.0"
4
+ VERSION = "3.0.0"
5
5
  end
@@ -28,9 +28,27 @@ end
28
28
 
29
29
  def generate_with_rspec
30
30
  pattern = ENV.fetch("PATTERN", "spec/**/*_spec.rb")
31
- command = "bundle exec rspec --pattern '#{pattern}' --dry-run --order defined"
32
31
  puts "Generating OpenAPI schemas (RSpec)..."
33
- system(command) || abort("Schema generation failed")
32
+
33
+ # Load rspec-core first — normally RSpec's runner does this before spec_helper,
34
+ # but since we're loading specs via require we need it explicitly.
35
+ require "rspec/core"
36
+
37
+ # Add spec/ to the load path so require "openapi_helper" / "rails_helper" works,
38
+ # matching what RSpec does when it runs.
39
+ $LOAD_PATH.unshift(File.expand_path("spec")) unless $LOAD_PATH.include?(File.expand_path("spec"))
40
+
41
+ # Load all spec files to trigger DSL context registrations.
42
+ # RSpec's describe/context blocks are evaluated at load time,
43
+ # so requiring the files registers paths and operations without
44
+ # running any tests. Spec files pull in RSpec and the openapi_ruby
45
+ # adapter via their own require chains (e.g. require "openapi_helper").
46
+ pattern.split(",").each do |p|
47
+ Dir.glob(p.strip).each { |f| require File.expand_path(f) }
48
+ end
49
+
50
+ # Generate schemas from the registered contexts
51
+ OpenapiRuby::Generator::SchemaWriter.generate_all!
34
52
  end
35
53
 
36
54
  def generate_with_minitest
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: 2.6.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morten Hartvig
@@ -65,9 +65,9 @@ dependencies:
65
65
  - - ">="
66
66
  - !ruby/object:Gem::Version
67
67
  version: '7.0'
68
- description: A unified OpenAPI 3.1 toolkit for Rails that combines test-driven spec
69
- generation, reusable schema components as Ruby classes, and runtime request/response
70
- validation middleware. Works with both RSpec and Minitest.
68
+ description: A unified OpenAPI toolkit for Rails that combines test-driven spec generation,
69
+ reusable schema components as Ruby classes, and runtime request/response validation
70
+ middleware. Supports OpenAPI 3.0 and 3.1. Works with both RSpec and Minitest.
71
71
  email:
72
72
  - morten@hartvigsen.dev
73
73
  executables: []
@@ -144,6 +144,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  requirements: []
145
145
  rubygems_version: 3.6.9
146
146
  specification_version: 4
147
- summary: OpenAPI 3.1 toolkit for Rails — spec generation, schema components, and runtime
148
- validation
147
+ summary: OpenAPI 3.0/3.1 toolkit for Rails — spec generation, schema components, and
148
+ runtime validation
149
149
  test_files: []