verquest 0.10.0 → 0.12.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: ba6bc529dbf828ec473edcf82a1cedefe5d3b1ac4b581a159e5421ab3dc9f751
4
- data.tar.gz: 7a1e7463fcd6987fcaaed5fab9b757acff2e7176f7a5a490e6de5c6197e84b8b
3
+ metadata.gz: 3e0fcd7f6d608af2e076189ca2484861a076d2e24e19fb61a76795924a53b22c
4
+ data.tar.gz: 59fd91a6e47c36499a212bffca49bd71fa3375bf333b91d0b162a5e54a9564d7
5
5
  SHA512:
6
- metadata.gz: 67030d45303503d3e2ea2a8258c7c74081c66582458e228a965cd0335431a1a713ad6617d5dd6a7242ce84ff0e79c9890a7d27f8b704a4e295e857ae9414cdc7
7
- data.tar.gz: 5f503d96382ca5c77ba193cfd5cabf2e494fa3273b770b1f73a38b07e6e96151983b571cb35def51378278060a1874104c67d9efa91dd6f379a99c55ba2eb8db
6
+ metadata.gz: d81a27ce31546a76107dac399cbc7baf57e02dad44eef25b00482c9b9023440ce88921e830d74ce9b991ea78d566e31a75258d1f5def238ae71a8151b34fc4e9
7
+ data.tar.gz: bd463d27e048cd7d513106c6b22352bad04f5c0ef333398a38cbf26c748826a7c64778096e238f10f4b1840fbb60ccd3330e39534e6c91a439fe6abab49e5a02
data/.rubocop.yml CHANGED
@@ -10,5 +10,5 @@ plugins:
10
10
  - rubocop-rake
11
11
 
12
12
  AllCops:
13
- TargetRubyVersion: 3.1
13
+ TargetRubyVersion: 3.3
14
14
  NewCops: enable # Enables new cops from plugins, as the main cops are managed by standard (the inherit_gem above)
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.12.0] - 2026-11-17
4
+
5
+ ### Added
6
+ - A named `one_of` takes schema options: annotations (`description`, `title`, `examples`, `example`, `deprecated`, `readOnly`, `writeOnly`, `externalDocs`, `$comment`), `default` and `x-` extensions. They go on the property schema next to `oneOf`, or next to the outer `anyOf` of a nullable `one_of`. The validation schema takes only `default`, so a nullable `one_of` can default to null. A default that doesn't match the `one_of`, like null on one that isn't nullable, raises `Verquest::DefinitionError`, as does a default on a `one_of` with `required: [:other]`, which would make every request without the `one_of` need `other`. Other options raise `Verquest::DefinitionError`, as do schema options on an unnamed `one_of`.
7
+
8
+ ### Changed
9
+ - A named `one_of` inside `with_options` takes that block's annotation defaults (`description`, `title`, `deprecated`, `readOnly`, `writeOnly`, `externalDocs`), which changes the generated schemas. Other defaults, like `example`, `additional_properties` or `discriminator`, still don't apply to it. Its variants and their properties no longer take the annotations it takes, so they don't repeat the `one_of`'s description; they still take `examples`, `example` and `$comment`.
10
+ - Under `json_schema_version = :openapi30`, documents write `examples` as `example` (its first item, unless the schema has an `example`) and `$comment` as the `x-comment` extension, as OpenAPI 3.0 has neither. A schema with both a keyword and the extension it becomes, like `$comment` and `x-comment`, raises `Verquest::DefinitionError`.
11
+ - Verquest needs Ruby 3.3 or later. Ruby 3.2 reached its end of life on 2026-04-01.
12
+
13
+ ### Fixed
14
+ - An inserted default is a deep copy, so changing its nested Hashes, Arrays or Strings in the processed params no longer changes the default for later requests.
15
+ - A root `one_of` schema takes its version's `description` and the annotations and `x-` extensions of its `schema_options`, which it used to drop. Other schema options on its version, except `additional_properties`, raise `Verquest::DefinitionError`.
16
+ - Schema option keys of `x-` extensions and `$` keywords keep their names, so `"x-my_ext":` is no longer written as `x-myExt`.
17
+
18
+ ## [0.11.0] - 2026-11-10
19
+
20
+ ### Added
21
+ - `case_insensitive: true` on `field`, `enum` and `array` (and `filter`) in query and path requests reads a value matching a declared string enum value case-insensitively as that value, in every version, without a lenient correction. The schema is unchanged. It raises `Verquest::DefinitionError` in a body request, with `with_options`, with a value other than `true`, `false` or `nil`, without string enum values (also on `const`, `object` and `collection`), or with values that differ only by case.
22
+
3
23
  ## [0.10.0] - 2026-11-09
4
24
 
5
25
  ### Added
data/README.md CHANGED
@@ -358,7 +358,7 @@ OpenAPI 3.0 has no `null` type, so with `json_schema_version = :openapi30` docum
358
358
  - merged into a nullable constant (with `null` in its `enum`) or a `one_of` without a discriminator;
359
359
  - next to an `allOf` around a nullable reference (`{"allOf" => [{"$ref" => "..."}], "nullable" => true}`) or a nullable `one_of` with a discriminator, which keeps null outside the discriminated `oneOf`, as versponse does.
360
360
 
361
- OpenAPI 3.0 also has no `dependentRequired` (from `required: [:other]`), so documents write it as the `x-dependentRequired` extension. Validation still enforces it.
361
+ OpenAPI 3.0 also has no `dependentRequired` (from `required: [:other]`), so documents write it as the `x-dependentRequired` extension. Validation still enforces it. Nor does it have `examples` or `$comment`: documents write the first example as `example`, unless the schema has one, and `$comment` as the `x-comment` extension. A schema with both `$comment` and `x-comment`, or `dependentRequired` and `x-dependentRequired`, raises `Verquest::DefinitionError`.
362
362
 
363
363
  Mixed types become an `anyOf` of single types, each nullable when the property is. Strict OpenAPI 3.0.3 tools honour `nullable` only next to `type`, so they may still treat a nullable reference or `one_of` as not nullable. Validation schemas keep the forms above, which validators honour.
364
364
 
@@ -560,9 +560,10 @@ OrderRequest.process({"order_id" => "o1", "payment" => {"method" => "card", "car
560
560
  "discriminator" => {"propertyName" => "method", "mapping" => {"card" => "#/components/schemas/CardPaymentComponent", "bank" => "#/components/schemas/BankPaymentComponent"}}}
561
561
  ```
562
562
 
563
- - Without `name:`, the whole request is one of the variants (`one_of discriminator: :type do ... end` as the only property of a version).
563
+ - Without `name:`, the whole request is one of the variants (`one_of discriminator: :type do ... end` as the only property of a version). Its schema takes the version's `description` and the annotations and `x-` extensions of its `schema_options`; other schema options, except `additional_properties`, raise `Verquest::DefinitionError`.
564
564
  - Without `discriminator:`, the value must match exactly one variant's schema. Validation rejects it otherwise; with validation off, mapping raises `Verquest::MappingError`.
565
565
  - `one_of` works inside objects, collections and other variants, and with `nullable: true` and `map:`.
566
+ - A named `one_of` takes schema options: annotations (`description:`, `title:`, `examples:`, `example:`, `deprecated:`, `read_only:`, `write_only:`, `external_docs:`, `"$comment":`), `default:` and `x-` extensions (`"x-internal": true`). They go on the property schema next to `oneOf`, or next to the outer `anyOf` of a nullable `one_of`; the validation schema takes only `default:`, which must match the `one_of` (null only when it is `nullable: true`) and can't go with `required: [:other]`. From `with_options`, it takes only `description`, `title`, `deprecated`, `read_only`, `write_only` and `external_docs`, and its variants take none of those, only the block's `examples`, `example` and `$comment`. Other options, like `additional_properties:`, `min_properties:` or `parameter_examples:`, raise `Verquest::DefinitionError`. So do schema options on an unnamed `one_of`; describe its version or the enclosing property instead.
566
567
 
567
568
  #### oneOf discriminators in validation
568
569
 
@@ -861,6 +862,15 @@ array :status, type: :string, explode: false, item_schema_options: {enum: %w[ope
861
862
  # => {name: "status", ..., examples: {single: {value: ["open"]}, multiple: {value: ["open", "won"]}}, schema: {...}}
862
863
  ```
863
864
 
865
+ `case_insensitive: true` on a `field` or `enum`, or an `array` of enum items, reads a value matching a declared string value case-insensitively as that value, in every version and without a [lenient](#lenient-requests) correction. The schema lists only the declared values, so mention it in `description:` if clients should know:
866
+
867
+ ```ruby
868
+ field :sort_direction, type: :string, enum: %w[ASC DESC], case_insensitive: true, description: "`ASC` or `DESC`, case-insensitive"
869
+ # ?sort_direction=desc => {"sort_direction" => "DESC"}
870
+ ```
871
+
872
+ It raises `Verquest::DefinitionError` in a body request, with `with_options`, with a value other than `true`, `false` or `nil`, on a property without string enum values (such as a `const` or an `object`), or when two values differ only by case. A `reference` to the property keeps the option in a query or path class; a body class referencing it matches exactly.
873
+
864
874
  A property reference to another query class reuses its definition, e.g. a shared `expand` parameter: `reference :expand, from: ExpandQuery, property: :expand`.
865
875
 
866
876
  #### Lenient requests
@@ -876,7 +886,7 @@ end
876
886
  | Rule | Value sent | Correction |
877
887
  |------|------------|------------|
878
888
  | `blank` | `page=` | Removed, where strict validation would reject it (not for required properties, arrays, properties that accept null, or strings without constraints, also as an anyOf or oneOf variant) |
879
- | `enum_case` | `sort_direction=desc` | `"DESC"`, when exactly one enum value matches case-insensitively |
889
+ | `enum_case` | `sort_direction=desc` | `"DESC"`, when exactly one enum value matches case-insensitively (properties with `case_insensitive: true` match without a correction) |
880
890
  | `maximum` | `limit=200` | Capped at the `maximum`, `100` |
881
891
  | `datetime_offset` | `2025-01-12 15:30:00`, `...+0100` | `T` between the date and the time, and `Z` added or a colon added to the offset (`+01:00`) |
882
892
  | `invalid` | `page=0` | Removed when it still fails validation, except a filter or a required property, which is still rejected |
@@ -14,12 +14,13 @@ module Verquest
14
14
  #
15
15
  # Transforms all keys in the given hash from snake_case (e.g., "max_length")
16
16
  # to camelCase (e.g., "maxLength") format, which is commonly used in JSON Schema.
17
+ # Keys of x- extensions and $ keywords are kept as they are, as their names are exact.
17
18
  # The transformation happens in place, modifying the original hash.
18
19
  #
19
20
  # @param hash [Hash] The hash containing snake_case keys
20
21
  # @return [Hash] The same hash with keys transformed to camelCase
21
22
  def camelize(hash)
22
- hash.transform_keys! { |key| snake_to_camel(key.to_s).to_sym }
23
+ hash.transform_keys! { |key| key.to_s.start_with?("x-", "$") ? key.to_sym : snake_to_camel(key.to_s).to_sym }
23
24
  end
24
25
 
25
26
  # Converts a snake_case string to camelCase
@@ -227,7 +227,10 @@ module Verquest
227
227
  # @param options [Hash] The options to set during the block execution
228
228
  # @yield Block to be executed with the temporary options
229
229
  # @return [void]
230
+ # @raise [Verquest::DefinitionError] If options include case_insensitive
230
231
  def with_options(**options, &block)
232
+ raise DefinitionError, "case_insensitive can't be set with with_options, set it on each property" if options.key?(:case_insensitive)
233
+
231
234
  camelize(options)
232
235
 
233
236
  original_options = default_options
@@ -247,8 +250,9 @@ module Verquest
247
250
  # @param nullable [Boolean] Whether the field can be null
248
251
  # @param schema_options [Hash] Additional schema options for the field
249
252
  # @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
253
+ # @param case_insensitive [Boolean, nil] Whether a query value matches the enum case-insensitively (query and path requests only)
250
254
  # @return [void]
251
- def field(name, type: nil, map: nil, required: nil, nullable: nil, parameter_examples: nil, **schema_options)
255
+ def field(name, type: nil, map: nil, required: nil, nullable: nil, parameter_examples: nil, case_insensitive: nil, **schema_options)
252
256
  camelize(schema_options)
253
257
 
254
258
  type = default_options.fetch(:type, type)
@@ -257,7 +261,7 @@ module Verquest
257
261
  schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
258
262
 
259
263
  field = Properties::Field.new(name:, type:, map:, required:, nullable:, **schema_options)
260
- add_property(field, parameter_examples)
264
+ add_property(field, parameter_examples, case_insensitive)
261
265
  end
262
266
 
263
267
  # Defines a new enum property for the current version scope
@@ -269,8 +273,9 @@ module Verquest
269
273
  # @param nullable [Boolean] Whether the enum can be null
270
274
  # @param schema_options [Hash] Additional schema options for the enum
271
275
  # @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
276
+ # @param case_insensitive [Boolean, nil] Whether a query value matches the enum case-insensitively (query and path requests only)
272
277
  # @return [void]
273
- def enum(name, values:, map: nil, required: nil, nullable: nil, parameter_examples: nil, **schema_options)
278
+ def enum(name, values:, map: nil, required: nil, nullable: nil, parameter_examples: nil, case_insensitive: nil, **schema_options)
274
279
  camelize(schema_options)
275
280
 
276
281
  required = default_options.fetch(:required, false) if required.nil?
@@ -278,7 +283,7 @@ module Verquest
278
283
  schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
279
284
 
280
285
  enum_property = Properties::Enum.new(name:, values:, map:, required:, nullable:, **schema_options)
281
- add_property(enum_property, parameter_examples)
286
+ add_property(enum_property, parameter_examples, case_insensitive)
282
287
  end
283
288
 
284
289
  # Defines a new constant property for the current version scope
@@ -290,14 +295,15 @@ module Verquest
290
295
  # @param nullable [Boolean] Whether the constant can be null
291
296
  # @param schema_options [Hash] Additional schema options for the constant
292
297
  # @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
298
+ # @param case_insensitive [Boolean, nil] Not supported (a constant has no enum); `true` raises Verquest::DefinitionError
293
299
  # @return [void]
294
- def const(name, value:, map: nil, required: nil, nullable: nil, parameter_examples: nil, **schema_options)
300
+ def const(name, value:, map: nil, required: nil, nullable: nil, parameter_examples: nil, case_insensitive: nil, **schema_options)
295
301
  camelize(schema_options)
296
302
  required = default_options.fetch(:required, false) if required.nil?
297
303
  nullable = default_options.fetch(:nullable, false) if nullable.nil?
298
304
 
299
305
  const = Properties::Const.new(name:, value:, map:, required:, nullable:, **schema_options)
300
- add_property(const, parameter_examples)
306
+ add_property(const, parameter_examples, case_insensitive)
301
307
  end
302
308
 
303
309
  # Defines a new object for the current version scope
@@ -308,9 +314,10 @@ module Verquest
308
314
  # @param nullable [Boolean] Whether the object can be null
309
315
  # @param schema_options [Hash] Additional schema options for the object
310
316
  # @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
317
+ # @param case_insensitive [Boolean, nil] Not supported (an object has no enum); `true` raises Verquest::DefinitionError
311
318
  # @yield Block executed in the context of the new object definition
312
319
  # @return [void]
313
- def object(name, map: nil, required: nil, nullable: nil, parameter_examples: nil, **schema_options, &block)
320
+ def object(name, map: nil, required: nil, nullable: nil, parameter_examples: nil, case_insensitive: nil, **schema_options, &block)
314
321
  unless block_given?
315
322
  raise ArgumentError, "a block must be given to define the object"
316
323
  end
@@ -321,7 +328,7 @@ module Verquest
321
328
  schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
322
329
 
323
330
  object = Properties::Object.new(name:, map:, required:, nullable:, **schema_options)
324
- add_property(object, parameter_examples)
331
+ add_property(object, parameter_examples, case_insensitive)
325
332
 
326
333
  if block_given?
327
334
  previous_scope = current_scope
@@ -341,10 +348,11 @@ module Verquest
341
348
  # @param nullable [Boolean] Whether the collection can be null
342
349
  # @param map [String, nil] An optional mapping to another collection
343
350
  # @param schema_options [Hash] Additional schema options for the collection
351
+ # @param case_insensitive [Boolean, nil] Not supported (a collection has no enum); `true` raises Verquest::DefinitionError
344
352
  # @yield Block executed in the context of the new collection definition
345
353
  # @return [void]
346
354
  # @raise [ArgumentError] without item: or a block, or when item does not respond to to_ref
347
- def collection(name, item: nil, required: nil, nullable: nil, map: nil, **schema_options, &block)
355
+ def collection(name, item: nil, required: nil, nullable: nil, map: nil, case_insensitive: nil, **schema_options, &block)
348
356
  if item.nil? && !block_given?
349
357
  raise ArgumentError, "item must be provided or a block must be given to define the collection"
350
358
  elsif !item.nil? && !item.respond_to?(:to_ref)
@@ -357,7 +365,7 @@ module Verquest
357
365
  schema_options = default_options.except(:required, :nullable).merge(schema_options)
358
366
 
359
367
  collection = Properties::Collection.new(name:, item:, required:, nullable:, map:, **schema_options)
360
- current_scope.add(collection)
368
+ add_property(collection, nil, case_insensitive)
361
369
 
362
370
  if block_given?
363
371
  previous_scope = current_scope
@@ -398,8 +406,9 @@ module Verquest
398
406
  # @param item_schema_options [Hash] Additional JSON schema options for the array items (merged with custom type options)
399
407
  # @param schema_options [Hash] Additional schema options for the array property
400
408
  # @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter (query requests only)
409
+ # @param case_insensitive [Boolean, nil] Whether a query item matches the items' enum case-insensitively (query and path requests only)
401
410
  # @return [void]
402
- def array(name, type:, required: nil, nullable: nil, map: nil, explode: nil, parameter_examples: nil, item_schema_options: {}, **schema_options)
411
+ def array(name, type:, required: nil, nullable: nil, map: nil, explode: nil, parameter_examples: nil, case_insensitive: nil, item_schema_options: {}, **schema_options)
403
412
  camelize(schema_options)
404
413
 
405
414
  type = default_options.fetch(:type, type)
@@ -408,7 +417,7 @@ module Verquest
408
417
  schema_options = default_options.except(:type, :required, :nullable).merge(schema_options)
409
418
 
410
419
  array = Properties::Array.new(name:, type:, required:, nullable:, map:, explode:, item_schema_options:, **schema_options)
411
- add_property(array, parameter_examples)
420
+ add_property(array, parameter_examples, case_insensitive)
412
421
  end
413
422
 
414
423
  # Defines a property and the block that filters by its processed value
@@ -453,23 +462,57 @@ module Verquest
453
462
  # @param required [Boolean, Array<Symbol>, nil] Whether this property is required
454
463
  # @param nullable [Boolean] Whether this property can be null
455
464
  # @param map [String, nil] An optional mapping to another property
465
+ # @param schema_options [Hash] Additional schema options for a named oneOf property: annotations, like a description,
466
+ # a default and x- extensions. Only annotations without examples come from the with_options defaults
467
+ # (see Verquest::Properties::OneOf::DEFAULT_ANNOTATION_KEYWORDS), and the variants of a named oneOf
468
+ # don't take the with_options annotations
456
469
  # @yield Block defining the schema options using reference declarations
457
470
  # @return [void]
458
- def one_of(name: nil, discriminator: nil, required: nil, nullable: nil, map: nil, &block)
471
+ # @raise [Verquest::DefinitionError] If schema options are given without a name, or aren't annotations,
472
+ # a default or x- extensions (see Verquest::Properties::OneOf.schema_option?)
473
+ def one_of(name: nil, discriminator: nil, required: nil, nullable: nil, map: nil, **schema_options, &block)
474
+ previous_scope = current_scope
475
+ previous_options = default_options
476
+ camelize(schema_options)
459
477
  required = default_options.fetch(:required, false) if required.nil?
460
478
  nullable = default_options.fetch(:nullable, false) if nullable.nil?
479
+ schema_options = one_of_schema_options(name, schema_options)
461
480
 
462
- one_of = Properties::OneOf.new(name:, discriminator:, required:, nullable:, map:)
481
+ one_of = Properties::OneOf.new(name:, discriminator:, required:, nullable:, map:, **schema_options)
463
482
  current_scope.add(one_of)
464
483
 
465
484
  if block_given?
466
- previous_scope = current_scope
467
485
  @current_scope = one_of
486
+ # A named oneOf holds the with_options annotations it takes, so its variants don't repeat them
487
+ @default_options = default_options.except(*Properties::OneOf::DEFAULT_ANNOTATION_OPTIONS) if name
468
488
 
469
489
  instance_exec(&block)
470
490
  end
471
491
  ensure
472
- @current_scope = previous_scope if block_given?
492
+ if block_given?
493
+ @current_scope = previous_scope
494
+ @default_options = previous_options
495
+ end
496
+ end
497
+
498
+ # Schema options of a oneOf, with the annotation defaults of with_options for a named one
499
+ #
500
+ # A named oneOf takes only annotations without examples from the with_options
501
+ # defaults, and Verquest::Properties::OneOf checks the options it's given.
502
+ # An unnamed oneOf stands for its scope and has no property schema to hold
503
+ # options, so it ignores the defaults and rejects its own options.
504
+ #
505
+ # @param name [Symbol, nil] The oneOf property name
506
+ # @param schema_options [Hash] The camelized schema options given to the oneOf
507
+ # @return [Hash] The schema options for the oneOf property
508
+ # @raise [Verquest::DefinitionError] If schema options are given without a name
509
+ def one_of_schema_options(name, schema_options)
510
+ return default_options.slice(*Properties::OneOf::DEFAULT_ANNOTATION_OPTIONS).merge(schema_options) if name
511
+ return schema_options if schema_options.empty?
512
+
513
+ alternative = current_scope.is_a?(Version) ? "describe the version" : "put the options on the enclosing property"
514
+ raise DefinitionError, "An unnamed one_of in version '#{@defining_version&.name}' can't take schema options " \
515
+ "(#{schema_options.keys.join(", ")}), give it a name: or #{alternative}"
473
516
  end
474
517
 
475
518
  # Errors for root keys that only another version declares
@@ -530,9 +573,18 @@ module Verquest
530
573
  #
531
574
  # @param property [Verquest::Properties::Base] The property to add
532
575
  # @param parameter_examples [Hash, nil] Named OpenAPI Example Objects for the query parameter
576
+ # @param case_insensitive [Boolean, nil] Whether a query value matches the enum case-insensitively
533
577
  # @return [void]
534
- def add_property(property, parameter_examples)
578
+ # @raise [Verquest::DefinitionError] If case_insensitive isn't true, false or nil, or is set in a body request
579
+ def add_property(property, parameter_examples, case_insensitive = nil)
580
+ label = "Property '#{property.name}' in version '#{@defining_version&.name}'"
581
+ unless [true, false, nil].include?(case_insensitive)
582
+ raise DefinitionError, "#{label} has case_insensitive: #{case_insensitive.inspect}, which must be true, false or nil"
583
+ end
584
+ raise DefinitionError, "#{label} is case_insensitive, which only query and path parameters support" if case_insensitive && location == :body
585
+
535
586
  property.parameter_examples = parameter_examples
587
+ property.case_insensitive = case_insensitive
536
588
  current_scope.add(property)
537
589
  end
538
590
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Verquest
4
- GEM_VERSION = "0.10.0"
4
+ GEM_VERSION = "0.12.0"
5
5
  end
@@ -10,7 +10,9 @@ module Verquest
10
10
  # - an `anyOf` with a single null alternative becomes `nullable: true`, with an `allOf` around a `$ref`
11
11
  # (OpenAPI 3.0 ignores keywords next to `$ref`) or a discriminated `oneOf` (which would reject null),
12
12
  # and `null` added to an `enum`
13
- # - `dependentRequired` becomes the `x-dependentRequired` extension
13
+ # - `dependentRequired` becomes the `x-dependentRequired` extension, and `$comment` the `x-comment` extension
14
+ # (a schema with both a keyword and its extension raises Verquest::DefinitionError)
15
+ # - `examples` becomes `example`, its first item, unless the schema has an `example`
14
16
  #
15
17
  # Only schema positions are visited, so a property named "type" is never read as the keyword.
16
18
  #
@@ -34,7 +36,7 @@ module Verquest
34
36
  ].freeze
35
37
 
36
38
  # Keywords OpenAPI 3.0 lacks, written as extensions
37
- RENAMED = {"dependentRequired" => "x-dependentRequired"}.freeze
39
+ RENAMED = {"dependentRequired" => "x-dependentRequired", "$comment" => "x-comment"}.freeze
38
40
 
39
41
  module_function
40
42
 
@@ -44,7 +46,34 @@ module Verquest
44
46
  return schema unless schema.is_a?(::Hash)
45
47
 
46
48
  # The null alternative goes first, so a merged alternative's type array meets nullable in type_union
47
- type_union(nullable_any_of(schema).to_h { |keyword, value| [RENAMED.fetch(keyword, keyword), child(keyword, value)] })
49
+ schema = single_example(nullable_any_of(schema))
50
+ check_renamed(schema)
51
+ type_union(schema.to_h { |keyword, value| [RENAMED.fetch(keyword, keyword), child(keyword, value)] })
52
+ end
53
+
54
+ # Raises when a schema has a keyword and the extension it's written as, so one would overwrite the other
55
+ #
56
+ # @param schema [Hash] A schema
57
+ # @return [void]
58
+ # @raise [Verquest::DefinitionError] If the schema has both
59
+ def check_renamed(schema)
60
+ RENAMED.each do |keyword, extension|
61
+ next unless schema.key?(keyword) && schema.key?(extension)
62
+
63
+ raise DefinitionError, "A schema has both #{keyword} and #{extension}, which OpenAPI 3.0 documents write #{keyword} as"
64
+ end
65
+ end
66
+
67
+ # OpenAPI 3.0 has a single `example` and no `examples`
68
+ #
69
+ # @param schema [Hash] A schema
70
+ # @return [Hash] The schema without examples, with the first one as its example unless it has one
71
+ def single_example(schema)
72
+ examples = schema["examples"]
73
+ return schema unless examples.is_a?(::Array)
74
+ return schema.except("examples") if schema.key?("example") || examples.empty?
75
+
76
+ schema.except("examples").merge("example" => examples.first)
48
77
  end
49
78
 
50
79
  # @param keyword [String] The schema keyword
@@ -123,6 +152,6 @@ module Verquest
123
152
  schema.key?("enum") ? schema.merge("enum" => schema["enum"] | [nil]) : schema
124
153
  end
125
154
 
126
- private_class_method :child, :type_union, :any_of, :nullable_any_of, :nullable_alternative, :nullable_enum
155
+ private_class_method :check_renamed, :single_example, :child, :type_union, :any_of, :nullable_any_of, :nullable_alternative, :nullable_enum
127
156
  end
128
157
  end
@@ -83,13 +83,13 @@ module Verquest
83
83
  mapping[(key_prefix + [name]).join("/")] = mapping_value_key(value_prefix:)
84
84
  end
85
85
 
86
- # Returns this array's explode value and parameter examples
86
+ # Returns this array's explode value, parameter examples and case_insensitive
87
87
  #
88
88
  # @param prefix [Array<String>] Path of the parent property
89
89
  # @param version [String] The version to resolve references with
90
90
  # @return [Hash{Array<String> => Hash}] The options of this array, by path
91
91
  def query_options(prefix:, version:)
92
- {prefix + [name] => {explode:, examples: parameter_examples}.compact}
92
+ {prefix + [name] => {explode:}.compact.merge(super.fetch(prefix + [name], {}))}
93
93
  end
94
94
 
95
95
  private
@@ -23,7 +23,9 @@ module Verquest
23
23
  # @return [String, nil] The mapping path for this property
24
24
  # @!attribute [rw] parameter_examples
25
25
  # @return [Hash, nil] Named OpenAPI Example Objects for this property's query parameter, kept out of the JSON schemas
26
- attr_accessor :name, :required, :map, :parameter_examples
26
+ # @!attribute [rw] case_insensitive
27
+ # @return [Boolean, nil] Whether a query value matches the enum case-insensitively
28
+ attr_accessor :name, :required, :map, :parameter_examples, :case_insensitive
27
29
 
28
30
  # Adds a child property to this property
29
31
  # @abstract
@@ -76,14 +78,16 @@ module Verquest
76
78
 
77
79
  # Returns the query parameter options under this property, by path
78
80
  #
79
- # Options are `explode:` for arrays and `examples:` for properties with
80
- # parameter examples. Paths without options are left out.
81
+ # Options are `explode:` for arrays, `examples:` for properties with
82
+ # parameter examples, and `case_insensitive:` for properties matching
83
+ # their enum case-insensitively. Paths without options are left out.
81
84
  #
82
85
  # @param prefix [Array<String>] Path of the parent property
83
86
  # @param version [String] The version to resolve references with
84
87
  # @return [Hash{Array<String> => Hash}] The options of each property path
85
88
  def query_options(prefix:, version:)
86
- parameter_examples ? {prefix + [name] => {examples: parameter_examples}} : {}
89
+ options = {examples: parameter_examples, case_insensitive: (true if case_insensitive)}.compact
90
+ options.empty? ? {} : {prefix + [name] => options}
87
91
  end
88
92
 
89
93
  # Returns a copy of this property under another name and map:
@@ -34,6 +34,32 @@ module Verquest
34
34
  # With a discriminator, the validation schema keeps the discriminator (see DiscriminatedSchema),
35
35
  # so OpenAPI 3.0 and 3.1 validators validate only the variant the discriminator selects.
36
36
  class OneOf < Base
37
+ # Annotation keywords a named oneOf takes, which go only into the documentation schema
38
+ ANNOTATION_KEYWORDS = %w[description title examples example deprecated readOnly writeOnly externalDocs $comment].freeze
39
+
40
+ # Annotation keywords a named oneOf takes from the with_options defaults. Examples and
41
+ # $comment are left out, as they're written for the sibling properties and may not fit
42
+ # the oneOf, so they go to its variants instead.
43
+ DEFAULT_ANNOTATION_KEYWORDS = (ANNOTATION_KEYWORDS - %w[examples example $comment]).freeze
44
+
45
+ # DEFAULT_ANNOTATION_KEYWORDS as the with_options keys the DSL slices
46
+ DEFAULT_ANNOTATION_OPTIONS = DEFAULT_ANNOTATION_KEYWORDS.map(&:to_sym).freeze
47
+
48
+ # Keywords a named oneOf takes besides annotations, which also go into the validation schema
49
+ VALIDATION_KEYWORDS = %w[default].freeze
50
+
51
+ # Whether a named oneOf takes the schema option
52
+ #
53
+ # A named oneOf takes annotations, a default and x- extensions. Any other keyword
54
+ # would constrain the value next to the variants, or isn't a schema keyword.
55
+ #
56
+ # @param key [String, Symbol] The camelized schema option key
57
+ # @return [Boolean] true if the oneOf takes the option
58
+ def self.schema_option?(key)
59
+ key = key.to_s
60
+ ANNOTATION_KEYWORDS.include?(key) || VALIDATION_KEYWORDS.include?(key) || key.start_with?("x-")
61
+ end
62
+
37
63
  # @return [String, nil] The discriminator property name for schema selection
38
64
  attr_reader :discriminator
39
65
 
@@ -45,13 +71,21 @@ module Verquest
45
71
  # @param required [Boolean, Array<Symbol>] Whether this property is required, or array of dependency names
46
72
  # @param nullable [Boolean] Whether this property can be null
47
73
  # @param map [String, nil] The mapping path for this property
48
- def initialize(name: nil, discriminator: nil, required: false, nullable: false, map: nil)
74
+ # @param schema_options [Hash] Additional JSON schema options for a named oneOf property, like a description
75
+ # (see .schema_option?)
76
+ # @raise [Verquest::DefinitionError] If a schema option isn't an annotation, a default or an x- extension,
77
+ # or the oneOf has a default and requires other properties
78
+ def initialize(name: nil, discriminator: nil, required: false, nullable: false, map: nil, **schema_options)
49
79
  @name = name&.to_s
50
80
  @required = required
51
81
  @nullable = nullable
52
82
  @map = map
53
83
  @discriminator = discriminator&.to_s
54
84
  @schemas = {}
85
+ @schema_options = schema_options.transform_keys(&:to_s)
86
+ @checked_default_schemas = {}
87
+ check_schema_options
88
+ check_default_dependencies
55
89
  end
56
90
 
57
91
  # Add a schema option to this oneOf
@@ -78,19 +112,24 @@ module Verquest
78
112
  # @return [Hash] The schema definition with oneOf array and optional discriminator
79
113
  def to_schema
80
114
  freeze_schemas
81
- wrap_schema(nullable_schema(build_schema_with_refs))
115
+ wrap_schema(nullable_schema(build_schema_with_refs).merge(schema_options))
82
116
  end
83
117
 
84
118
  # Generate validation schema for this oneOf property
85
119
  #
86
120
  # Unlike to_schema which uses $ref, the validation schema includes
87
- # the full inline schema definitions for each option.
121
+ # the full inline schema definitions for each option. Of the schema
122
+ # options, it keeps only the default, as annotations and extensions
123
+ # don't take part in validation.
88
124
  #
89
125
  # @param version [String, nil] The version to generate validation schema for
90
126
  # @return [Hash] The validation schema with inline schema definitions
127
+ # @raise [Verquest::DefinitionError] If the default doesn't match the schema
91
128
  def to_validation_schema(version: nil)
92
129
  freeze_schemas
93
- wrap_schema(nullable_schema(build_validation_schema(version: version)))
130
+ schema = nullable_schema(build_validation_schema(version: version))
131
+ check_default(schema)
132
+ wrap_schema(schema.merge(schema_options.slice(*VALIDATION_KEYWORDS)))
94
133
  end
95
134
 
96
135
  # Create mapping for this oneOf property
@@ -163,7 +202,48 @@ module Verquest
163
202
 
164
203
  private
165
204
 
166
- attr_reader :schemas
205
+ attr_reader :schemas, :schema_options
206
+
207
+ # Raises for schema options a oneOf doesn't take (see .schema_option?)
208
+ #
209
+ # @return [void]
210
+ # @raise [Verquest::DefinitionError] If a schema option isn't an annotation, a default or an x- extension
211
+ def check_schema_options
212
+ rejected = schema_options.keys.reject { |key| self.class.schema_option?(key) }
213
+ return if rejected.empty?
214
+
215
+ raise DefinitionError, "one_of '#{name}' can't take #{rejected.join(", ")}, " \
216
+ "it takes only #{(ANNOTATION_KEYWORDS + VALIDATION_KEYWORDS).join(", ")} and x- extensions"
217
+ end
218
+
219
+ # Raises when the oneOf has a default and requires other properties, as inserting
220
+ # the default would make every request without the oneOf need them
221
+ #
222
+ # @return [void]
223
+ # @raise [Verquest::DefinitionError] If the oneOf has a default and requires other properties
224
+ def check_default_dependencies
225
+ return unless schema_options.key?("default") && required.is_a?(::Array)
226
+
227
+ raise DefinitionError, "one_of '#{name}' has a default and requires #{required.join(", ")}, " \
228
+ "so every request without #{name} would need #{required.join(", ")}"
229
+ end
230
+
231
+ # Raises when the default doesn't match the oneOf, so inserting it wouldn't fail the request
232
+ #
233
+ # Each schema is checked once, as versions that inherit the oneOf share it.
234
+ #
235
+ # @param schema [Hash] The validation schema without the default
236
+ # @return [void]
237
+ # @raise [Verquest::DefinitionError] If the default doesn't match the schema
238
+ def check_default(schema)
239
+ return unless schema_options.key?("default")
240
+
241
+ default = schema_options["default"]
242
+ key = [schema, Verquest.configuration.json_schema_uri]
243
+ return if @checked_default_schemas[key] ||= JSONSchemer.schema(schema, meta_schema: key[1]).valid?(default)
244
+
245
+ raise DefinitionError, "one_of '#{name}' has a default, #{default.inspect}, that doesn't match its schema"
246
+ end
167
247
 
168
248
  # Freezes the schemas hash to prevent further modifications
169
249
  # This is called on first read access to ensure immutability after setup
@@ -38,9 +38,11 @@ module Verquest
38
38
 
39
39
  # @param schema [Hash] The version's validation schema
40
40
  # @param explodes [Hash{Array<String> => Boolean}] The explode value of each array property, by path
41
- def initialize(schema:, explodes:)
41
+ # @param case_insensitive [Set<Array<String>>] Paths of properties matching their enum case-insensitively
42
+ def initialize(schema:, explodes:, case_insensitive: Set.new)
42
43
  @schema = schema
43
44
  @explodes = explodes
45
+ @case_insensitive = case_insensitive
44
46
  end
45
47
 
46
48
  # Converts the params
@@ -53,7 +55,7 @@ module Verquest
53
55
 
54
56
  private
55
57
 
56
- attr_reader :schema, :explodes
58
+ attr_reader :schema, :explodes, :case_insensitive
57
59
 
58
60
  # @return [Hash] A copy of the hash with each known property converted
59
61
  def coerce_properties(hash, object_schema, path)
@@ -141,14 +143,22 @@ module Verquest
141
143
  BOOLEANS.fetch(value, NO_MATCH)
142
144
  end
143
145
 
144
- # A string outside the schema's enum tries the next type, so "" can still map to nil
146
+ # A string outside the schema's enum tries the next type, so "" can still map to nil.
147
+ # On a case-insensitive path, a string matching an enum value case-insensitively is that value.
145
148
  #
146
- # @return [String, Object] The string or NO_MATCH
147
- def coerce_string(value, value_schema, _path)
149
+ # @return [String, Object] The string, the declared value or NO_MATCH
150
+ def coerce_string(value, value_schema, path)
148
151
  return NO_MATCH unless value.is_a?(::String)
149
- return NO_MATCH if value_schema.key?("enum") && !value_schema["enum"].include?(value)
152
+ return value unless value_schema.key?("enum")
153
+ return value if value_schema["enum"].include?(value)
154
+ return NO_MATCH unless case_insensitive?(path)
150
155
 
151
- value
156
+ enum_matches(value, value_schema).first || NO_MATCH
157
+ end
158
+
159
+ # @return [Array<String>] The schema's string enum values matching the value case-insensitively
160
+ def enum_matches(value, value_schema)
161
+ value_schema["enum"].select { |declared| declared.is_a?(::String) && declared.casecmp?(value) }
152
162
  end
153
163
 
154
164
  # Splits a string by the array's explode value and converts each item;
@@ -175,6 +185,13 @@ module Verquest
175
185
  def explode?(path)
176
186
  explodes.fetch(path) { Verquest.configuration.query_array_explode }
177
187
  end
188
+
189
+ # The path of an array item ends with its index, so an item is case-insensitive when its array is
190
+ #
191
+ # @return [Boolean] Whether the value at the path matches its enum case-insensitively
192
+ def case_insensitive?(path)
193
+ case_insensitive.include?(path) || case_insensitive.include?(path[...-1])
194
+ end
178
195
  end
179
196
  end
180
197
  end
@@ -7,11 +7,14 @@ module Verquest
7
7
  # Query parameters are scalars, arrays of scalars, or one level of objects
8
8
  # sent as `deepObject` (`filter[status]=open`). Path parameters are required
9
9
  # scalars. Anything else, and oneOf properties, raise when the version is
10
- # prepared.
10
+ # prepared, as does `case_insensitive` on a property without string enum
11
+ # values, or with values equal case-insensitively.
11
12
  class DefinitionCheck
12
13
  # @param version [Verquest::Version] The query or path version to check
13
- def initialize(version)
14
+ # @param query_options [Hash{Array<String> => Hash}] The version's query options, by path
15
+ def initialize(version, query_options: {})
14
16
  @version = version
17
+ @query_options = query_options
15
18
  end
16
19
 
17
20
  # @return [void]
@@ -26,11 +29,13 @@ module Verquest
26
29
  version.validation_schema.fetch("properties", {}).each do |name, property_schema|
27
30
  (version.location == :path) ? check_path(property_schema, name) : check(property_schema, [name], nested: false)
28
31
  end
32
+
33
+ query_options.each { |path, options| check_case_insensitive(path) if options[:case_insensitive] }
29
34
  end
30
35
 
31
36
  private
32
37
 
33
- attr_reader :version
38
+ attr_reader :version, :query_options
34
39
 
35
40
  # Rejects path parameters that aren't required scalars
36
41
  #
@@ -77,6 +82,36 @@ module Verquest
77
82
  reject(path, "an array of objects or arrays") if item_types.intersect?(%w[object array])
78
83
  end
79
84
 
85
+ # Rejects case_insensitive on a property without string enum values (for an array, its items'),
86
+ # or with two values equal case-insensitively
87
+ #
88
+ # @return [void]
89
+ # @raise [Verquest::DefinitionError] If the property can't match case-insensitively
90
+ def check_case_insensitive(path)
91
+ label = "Property '#{path.join("/")}' in version '#{version.name}' is case_insensitive"
92
+ strings = enum_strings(property_schemas(path))
93
+ raise DefinitionError, "#{label}, which needs string enum values" if strings.empty?
94
+
95
+ first, second = strings.group_by { |value| value.downcase(:fold) }.values.find { |same| same.size > 1 }
96
+ raise DefinitionError, "#{label}, but its enum values #{first.inspect} and #{second.inspect} differ only by case" if first
97
+ end
98
+
99
+ # @return [Array<Hash>] The schemas at the path, through anyOf and oneOf variants, with an array's items in place of the array
100
+ def property_schemas(path)
101
+ schemas = path.reduce([version.validation_schema]) do |current, name|
102
+ current.flat_map { |schema| variants(schema) }.filter_map { |schema| schema.dig("properties", name) }
103
+ end
104
+
105
+ schemas.flat_map { |schema| variants(schema) }.map do |schema|
106
+ (Array(schema["type"]).include?("array") && schema.key?("items")) ? schema["items"] : schema
107
+ end
108
+ end
109
+
110
+ # @return [Array<String>] The distinct string enum values of the schemas and their variants
111
+ def enum_strings(schemas)
112
+ schemas.flat_map { |schema| variants(schema) }.flat_map { |schema| Array(schema["enum"]) }.grep(::String).uniq
113
+ end
114
+
80
115
  # @return [Array<Hash>] The schema and its anyOf and oneOf variants
81
116
  def variants(schema)
82
117
  [schema, *schema.values_at("anyOf", "oneOf").compact.flatten]
@@ -18,8 +18,9 @@ module Verquest
18
18
  # @param explodes [Hash{Array<String> => Boolean}] The explode value of each array property, by path
19
19
  # @param required [Array<String>] Root properties that are never removed (see {Verquest::Version#required_property_names})
20
20
  # @param on_correction [#call, nil] Called with each {Verquest::Correction}
21
- def initialize(schema:, explodes:, required:, on_correction:)
22
- super(schema:, explodes:)
21
+ # @param case_insensitive [Set<Array<String>>] Paths of properties matching their enum case-insensitively
22
+ def initialize(schema:, explodes:, required:, on_correction:, case_insensitive: Set.new)
23
+ super(schema:, explodes:, case_insensitive:)
23
24
  @required = required
24
25
  @on_correction = on_correction
25
26
  end
@@ -96,14 +97,14 @@ module Verquest
96
97
  coerced = super
97
98
  return coerced unless value.is_a?(::String)
98
99
  return enum_case(value, value_schema, path) if coerced.equal?(NO_MATCH) && value_schema.key?("enum")
99
- return with_offset(value, path) if value_schema["format"] == "date-time"
100
+ return with_offset(coerced, path) if value_schema["format"] == "date-time"
100
101
 
101
102
  coerced
102
103
  end
103
104
 
104
105
  # @return [String, Object] The only declared value matching case-insensitively or NO_MATCH
105
106
  def enum_case(value, value_schema, path)
106
- matches = value_schema["enum"].select { |declared| declared.is_a?(::String) && declared.casecmp?(value) }
107
+ matches = enum_matches(value, value_schema)
107
108
  return NO_MATCH unless matches.one?
108
109
 
109
110
  correct(path, :enum_case, value, matches.first)
@@ -17,7 +17,7 @@ module Verquest
17
17
  DOCUMENTATION_KEYS = %w[description deprecated].freeze
18
18
 
19
19
  # @param schema [Hash] The version's validation schema
20
- # @param options [Hash{Array<String> => Hash}] The `explode:` and `examples:` of each property path
20
+ # @param options [Hash{Array<String> => Hash}] The `explode:` and `examples:` of each property path; other options, such as `case_insensitive:`, are ignored
21
21
  # @param location [Symbol] `:query` or `:path`, written as `in:`
22
22
  def initialize(schema:, options:, location: :query)
23
23
  @schema = schema
@@ -275,7 +275,8 @@ module Verquest
275
275
  return params unless parameters?
276
276
  return query_coercer.call(params) unless lenient
277
277
 
278
- Query::LenientCoercer.new(schema: validation_schema, explodes: query_explodes, required: required_property_names, on_correction:).call(params)
278
+ Query::LenientCoercer.new(schema: validation_schema, explodes: query_explodes, required: required_property_names,
279
+ on_correction:, case_insensitive: query_case_insensitive).call(params)
279
280
  end
280
281
 
281
282
  # Returns the root properties the validation schema requires, always (`required`), when another
@@ -364,6 +365,9 @@ module Verquest
364
365
  # @return [Hash{Array<String> => Boolean}, nil] The explode value of each array property of a query or path version, by path
365
366
  attr_reader :query_explodes
366
367
 
368
+ # @return [Set<Array<String>>, nil] Paths of query or path properties matching their enum case-insensitively
369
+ attr_reader :query_case_insensitive
370
+
367
371
  # @return [Hash{Array<String> => Hash}, nil] The parameter options of each property path of a query or path version
368
372
  attr_reader :query_options
369
373
 
@@ -372,13 +376,14 @@ module Verquest
372
376
  # @return [void]
373
377
  # @raise [Verquest::DefinitionError] If the version uses a shape its location can't express
374
378
  def prepare_parameters
375
- Query::DefinitionCheck.new(self).call
376
-
377
379
  @query_options = all_properties.each_value.with_object({}) do |property, options|
378
380
  options.merge!(property.query_options(prefix: [], version: name))
379
381
  end.freeze
382
+ Query::DefinitionCheck.new(self, query_options: @query_options).call
383
+
380
384
  @query_explodes = query_options.filter_map { |path, options| [path, options[:explode]] if options.key?(:explode) }.to_h.freeze
381
- @query_coercer = Query::Coercer.new(schema: validation_schema, explodes: @query_explodes)
385
+ @query_case_insensitive = query_options.filter_map { |path, options| path if options[:case_insensitive] }.to_set.freeze
386
+ @query_coercer = Query::Coercer.new(schema: validation_schema, explodes: @query_explodes, case_insensitive: @query_case_insensitive)
382
387
  end
383
388
 
384
389
  # Returns the compiled validation schema for the current configuration
@@ -393,7 +398,39 @@ module Verquest
393
398
  key = [Verquest.configuration.json_schema_uri, insert_property_defaults_setting]
394
399
 
395
400
  @validation_schemers_lock.synchronize do
396
- @validation_schemers[key] ||= JSONSchemer.schema(validation_schema, meta_schema: key[0], insert_property_defaults: key[1])
401
+ @validation_schemers[key] ||= JSONSchemer.schema(
402
+ validation_schema, meta_schema: key[0], insert_property_defaults: key[1], property_default_resolver: property_default_resolver(key[1])
403
+ )
404
+ end
405
+ end
406
+
407
+ # Returns a resolver that inserts a deep copy of the default
408
+ #
409
+ # json_schemer inserts a shallow copy, so the params would share nested Hashes and Arrays
410
+ # with the schema, and changing them would change the default for later requests.
411
+ #
412
+ # @param setting [Boolean, Symbol] The insert_property_defaults value to compile with
413
+ # @return [Proc, nil] The resolver, or nil when defaults aren't inserted
414
+ def property_default_resolver(setting)
415
+ return unless setting
416
+
417
+ symbol = setting == :symbol
418
+ resolver = symbol ? JSONSchemer::Schema::SYMBOL_PROPERTY_DEFAULT_RESOLVER : JSONSchemer::Schema::DEFAULT_PROPERTY_DEFAULT_RESOLVER
419
+ proc do |instance, property, results|
420
+ resolver.call(instance, property, results).tap do |inserted|
421
+ key = symbol ? property.to_sym : property
422
+ instance[key] = copy_default(instance[key]) if inserted
423
+ end
424
+ end
425
+ end
426
+
427
+ # @param value [Object] An inserted default
428
+ # @return [Object] A copy of the value with copies of its Hashes, Arrays and Strings
429
+ def copy_default(value)
430
+ case value
431
+ when Hash then value.transform_values { |item| copy_default(item) }
432
+ when Array then value.map { |item| copy_default(item) }
433
+ else value.dup
397
434
  end
398
435
  end
399
436
 
@@ -427,14 +464,30 @@ module Verquest
427
464
  # Generates the JSON schema for combination schemas (oneOf at root level)
428
465
  #
429
466
  # For combination schemas, the schema is delegated directly to the oneOf
430
- # property since it represents the entire request structure.
467
+ # property since it represents the entire request structure. It takes the
468
+ # version's description and the annotations and x- extensions of its schema
469
+ # options. additionalProperties is left out, as the variants set their own.
431
470
  #
432
471
  # @return [Hash] The schema from the oneOf property
433
472
  # @raise [ArgumentError] If the version also has conditions
473
+ # @raise [Verquest::DefinitionError] If the version has other schema options
434
474
  def prepare_combination_schema
435
475
  raise ArgumentError, "Conditions can not be declared next to a root-level one_of" if conditions.any?
436
476
 
437
- @schema = document_schema(properties.values.first.to_schema)
477
+ annotations = combination_annotations
478
+ annotations["description"] = description if description
479
+ @schema = document_schema(properties.values.first.to_schema.merge(annotations))
480
+ end
481
+
482
+ # @return [Hash] The annotations and x- extensions of the schema options
483
+ # @raise [Verquest::DefinitionError] If the version has other schema options
484
+ def combination_annotations
485
+ options = schema_options.except("additionalProperties")
486
+ rejected = options.keys.reject { |key| key != "default" && Properties::OneOf.schema_option?(key) }
487
+ return options if rejected.empty?
488
+
489
+ raise DefinitionError, "The root one_of of version '#{name}' can't take schema options (#{rejected.join(", ")}), " \
490
+ "it takes only annotations and x- extensions"
438
491
  end
439
492
 
440
493
  # @return [Hash] The schema the meta-schema checks: the document under OpenAPI 3.0, the validation schema otherwise
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verquest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Petr Hlavicka
@@ -132,7 +132,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
132
  requirements:
133
133
  - - ">="
134
134
  - !ruby/object:Gem::Version
135
- version: '3.2'
135
+ version: '3.3'
136
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  requirements:
138
138
  - - ">="