openapi-ruby 3.2.0 → 3.3.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: 2e97365e4c2b566d59db1f4946a2b0e1f45f8ee9b938f2b5c049ac0ac9f56dcc
4
- data.tar.gz: 431709a674d6a028fd8a14f47200761215a6e4576888009b3b2b637fd5a453b9
3
+ metadata.gz: 2596878f73873970637c965935c1f4689cc744f3496467d734bd9a2c816904ee
4
+ data.tar.gz: 2c7f2d7b4497175d0c558f2179888529ae1397d724cba0088248049ef5fda518
5
5
  SHA512:
6
- metadata.gz: 0a670b21d1043c474b2eead1844c47258521df05b247d561a5578de4c8495a6519b575c7b3d2d4fe4fc54c22d5d524e45ee2fab492e99e3366c8eab5bc381ecf
7
- data.tar.gz: e2981bfa965fef6c6db6767d8e36592b8166fa20f675265c9d0147265565250e845093ca4fb3dc3bd3a7d2a2b1c3a7c6cb9d52169a0142bc0be2a90727a50b77
6
+ metadata.gz: d54c13cb1de806e0e5f21ed1e17d6287a525b00be8654d2c77e554a6bc6762dc206002797874fa6882872754b2bc6438dbfa590e0f3c03f0211383d1fa2490ff
7
+ data.tar.gz: fb01ab7bb2b8f51d7fb9ef0432a261717d5cf1dc90a7c2c17168f3039456d0b5af99c74eb1853d9ebbf1df80dc8410bbaffa5c53af4f3799960a00c4a128d287
data/README.md CHANGED
@@ -193,6 +193,31 @@ class Schemas::AdminUser
193
193
  end
194
194
  ```
195
195
 
196
+ ### Class References
197
+
198
+ Instead of writing `$ref` strings manually, you can pass component classes directly anywhere a `$ref` is expected. This gives you typo protection (via `NameError`), IDE navigation, and less boilerplate:
199
+
200
+ ```ruby
201
+ # Instead of:
202
+ schema "$ref" => "#/components/schemas/User"
203
+ schema type: :array, items: { "$ref" => "#/components/schemas/User" }
204
+
205
+ # You can write:
206
+ schema Schemas::User
207
+ schema type: :array, items: Schemas::User
208
+ ```
209
+
210
+ This works in `schema`, `request_body`, and anywhere nested inside hash/array definitions. Non-component classes raise `ArgumentError`.
211
+
212
+ You can also use the explicit `.to_ref` method:
213
+
214
+ ```ruby
215
+ Schemas::User.to_ref
216
+ # => { "$ref" => "#/components/schemas/User" }
217
+ ```
218
+
219
+ Both class refs and string `$ref` hashes are fully supported — use whichever you prefer.
220
+
196
221
  ### Strong Params
197
222
 
198
223
  Schema components can derive Rails strong params permit lists:
@@ -252,7 +277,7 @@ RSpec.describe "Users API", type: :openapi do
252
277
  produces "application/json"
253
278
 
254
279
  response 200, "returns all users" do
255
- schema type: :array, items: { "$ref" => "#/components/schemas/User" }
280
+ schema type: :array, items: Schemas::User
256
281
 
257
282
  run_test! do
258
283
  expect(JSON.parse(response.body).length).to be > 0
@@ -265,19 +290,17 @@ RSpec.describe "Users API", type: :openapi do
265
290
  consumes "application/json"
266
291
 
267
292
  request_body required: true, content: {
268
- "application/json" => {
269
- schema: { "$ref" => "#/components/schemas/UserInput" }
270
- }
293
+ "application/json" => { schema: Schemas::UserInput }
271
294
  }
272
295
 
273
296
  response 201, "user created" do
274
- schema "$ref" => "#/components/schemas/User"
297
+ schema Schemas::User
275
298
  let(:request_body) { { name: "Jane", email: "jane@example.com" } }
276
299
  run_test!
277
300
  end
278
301
 
279
302
  response 422, "validation errors" do
280
- schema "$ref" => "#/components/schemas/ValidationErrors"
303
+ schema Schemas::ValidationErrors
281
304
  let(:request_body) { { name: "" } }
282
305
  run_test!
283
306
  end
@@ -289,7 +312,7 @@ RSpec.describe "Users API", type: :openapi do
289
312
 
290
313
  get "Get a user" do
291
314
  response 200, "user found" do
292
- schema "$ref" => "#/components/schemas/User"
315
+ schema Schemas::User
293
316
  let(:id) { User.create!(name: "Jane", email: "jane@example.com").id }
294
317
  run_test!
295
318
  end
@@ -319,7 +342,7 @@ RSpec.describe "Users API", type: :openapi do
319
342
  produces "application/json"
320
343
 
321
344
  response 200, "returns all users" do
322
- schema type: :array, items: { "$ref" => "#/components/schemas/User" }
345
+ schema type: :array, items: Schemas::User
323
346
  end
324
347
  end
325
348
 
@@ -327,17 +350,15 @@ RSpec.describe "Users API", type: :openapi do
327
350
  consumes "application/json"
328
351
 
329
352
  request_body required: true, content: {
330
- "application/json" => {
331
- schema: { "$ref" => "#/components/schemas/UserInput" }
332
- }
353
+ "application/json" => { schema: Schemas::UserInput }
333
354
  }
334
355
 
335
356
  response 201, "user created" do
336
- schema "$ref" => "#/components/schemas/User"
357
+ schema Schemas::User
337
358
  end
338
359
 
339
360
  response 422, "validation errors" do
340
- schema "$ref" => "#/components/schemas/ValidationErrors"
361
+ schema Schemas::ValidationErrors
341
362
  end
342
363
  end
343
364
  end
@@ -407,7 +428,7 @@ class UsersApiTest < ActionDispatch::IntegrationTest
407
428
  produces "application/json"
408
429
 
409
430
  response 200, "returns all users" do
410
- schema type: :array, items: { "$ref" => "#/components/schemas/User" }
431
+ schema type: :array, items: Schemas::User
411
432
  end
412
433
  end
413
434
 
@@ -415,13 +436,11 @@ class UsersApiTest < ActionDispatch::IntegrationTest
415
436
  consumes "application/json"
416
437
 
417
438
  request_body required: true, content: {
418
- "application/json" => {
419
- schema: { "$ref" => "#/components/schemas/UserInput" }
420
- }
439
+ "application/json" => { schema: Schemas::UserInput }
421
440
  }
422
441
 
423
442
  response 201, "user created" do
424
- schema "$ref" => "#/components/schemas/User"
443
+ schema Schemas::User
425
444
  end
426
445
  end
427
446
  end
@@ -85,6 +85,10 @@ module OpenapiRuby
85
85
  end
86
86
  end
87
87
 
88
+ def to_ref
89
+ OpenapiRuby::Core::RefResolver.ref_object(_component_type, component_name)
90
+ end
91
+
88
92
  def to_openapi
89
93
  definition = _schema_definition.deep_dup
90
94
 
@@ -147,6 +151,12 @@ module OpenapiRuby
147
151
 
148
152
  def deep_stringify(value)
149
153
  case value
154
+ when Class
155
+ if value < OpenapiRuby::Components::Base
156
+ value.to_ref
157
+ else
158
+ raise ArgumentError, "#{value} is not an OpenapiRuby component"
159
+ end
150
160
  when Hash
151
161
  value.each_with_object({}) { |(k, v), h| h[k.to_s] = deep_stringify(v) }
152
162
  when Array
@@ -45,6 +45,12 @@ module OpenapiRuby
45
45
 
46
46
  def deep_stringify(value)
47
47
  case value
48
+ when Class
49
+ if value < OpenapiRuby::Components::Base
50
+ value.to_ref
51
+ else
52
+ raise ArgumentError, "#{value} is not an OpenapiRuby component"
53
+ end
48
54
  when Hash
49
55
  value.each_with_object({}) { |(k, v), h| h[k.to_s] = deep_stringify(v) }
50
56
  when Array
@@ -128,6 +128,12 @@ module OpenapiRuby
128
128
 
129
129
  def deep_stringify(value)
130
130
  case value
131
+ when Class
132
+ if value < OpenapiRuby::Components::Base
133
+ value.to_ref
134
+ else
135
+ raise ArgumentError, "#{value} is not an OpenapiRuby component"
136
+ end
131
137
  when Hash
132
138
  value.each_with_object({}) { |(k, v), h| h[k.to_s] = deep_stringify(v) }
133
139
  when Array
@@ -72,6 +72,12 @@ module OpenapiRuby
72
72
 
73
73
  def deep_stringify(value)
74
74
  case value
75
+ when Class
76
+ if value < OpenapiRuby::Components::Base
77
+ value.to_ref
78
+ else
79
+ raise ArgumentError, "#{value} is not an OpenapiRuby component"
80
+ end
75
81
  when Hash
76
82
  value.each_with_object({}) { |(k, v), h| h[k.to_s] = deep_stringify(v) }
77
83
  when Array
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenapiRuby
4
- VERSION = "3.2.0"
4
+ VERSION = "3.3.0"
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: 3.2.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morten Hartvig