verquest 0.6.4 → 0.7.1
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 +4 -4
- data/CHANGELOG.md +57 -1
- data/README.md +325 -45
- data/Rakefile +5 -0
- data/docs/rails.md +169 -0
- data/lib/verquest/base/condition_class_methods.rb +211 -0
- data/lib/verquest/base/private_class_methods.rb +84 -20
- data/lib/verquest/base/public_class_methods.rb +51 -1
- data/lib/verquest/base.rb +1 -0
- data/lib/verquest/conditions/branch.rb +111 -0
- data/lib/verquest/conditions/condition.rb +189 -0
- data/lib/verquest/conditions/conjunction.rb +39 -0
- data/lib/verquest/conditions/fragment_merge.rb +52 -0
- data/lib/verquest/conditions/property_merge.rb +47 -0
- data/lib/verquest/conditions/rule_set.rb +74 -0
- data/lib/verquest/conditions/rules/absent.rb +41 -0
- data/lib/verquest/conditions/rules/alternatives.rb +28 -0
- data/lib/verquest/conditions/rules/any.rb +43 -0
- data/lib/verquest/conditions/rules/equals.rb +22 -0
- data/lib/verquest/conditions/rules/names_rule.rb +23 -0
- data/lib/verquest/conditions/rules/none.rb +40 -0
- data/lib/verquest/conditions/rules/present.rb +38 -0
- data/lib/verquest/conditions/rules/property_rule.rb +66 -0
- data/lib/verquest/conditions/rules/satisfies.rb +22 -0
- data/lib/verquest/conditions/rules/value_in.rb +22 -0
- data/lib/verquest/conditions/rules.rb +17 -0
- data/lib/verquest/conditions/variant_shape.rb +39 -0
- data/lib/verquest/conditions.rb +36 -0
- data/lib/verquest/configuration.rb +24 -6
- data/lib/verquest/gem_version.rb +1 -1
- data/lib/verquest/helper_methods/conditional_properties.rb +126 -0
- data/lib/verquest/helper_methods/unnamed_one_of.rb +35 -0
- data/lib/verquest/properties/array.rb +15 -1
- data/lib/verquest/properties/base.rb +104 -10
- data/lib/verquest/properties/collection.rb +63 -19
- data/lib/verquest/properties/const.rb +2 -1
- data/lib/verquest/properties/object.rb +38 -4
- data/lib/verquest/properties/one_of/discriminated_schema.rb +84 -0
- data/lib/verquest/properties/one_of.rb +87 -45
- data/lib/verquest/properties/reference.rb +61 -17
- data/lib/verquest/query/coercer.rb +165 -0
- data/lib/verquest/query/definition_check.rb +91 -0
- data/lib/verquest/query/parameters.rb +79 -0
- data/lib/verquest/query.rb +25 -0
- data/lib/verquest/supported_versions.rb +73 -0
- data/lib/verquest/transformer/collection.rb +32 -0
- data/lib/verquest/transformer/const_as_enum.rb +47 -0
- data/lib/verquest/transformer/layout.rb +83 -0
- data/lib/verquest/transformer/mapping_layout.rb +166 -0
- data/lib/verquest/transformer/one_of.rb +111 -0
- data/lib/verquest/transformer/path.rb +188 -0
- data/lib/verquest/transformer/plan.rb +183 -0
- data/lib/verquest/transformer/scope.rb +64 -0
- data/lib/verquest/transformer.rb +43 -880
- data/lib/verquest/version.rb +152 -79
- data/lib/verquest/versions.rb +7 -0
- data/lib/verquest.rb +7 -0
- metadata +44 -7
|
@@ -30,6 +30,9 @@ module Verquest
|
|
|
30
30
|
# @example oneOf without discriminator (pure JSON Schema validation)
|
|
31
31
|
# one_of = Verquest::Properties::OneOf.new(name: :value)
|
|
32
32
|
# # Validates that exactly one schema matches
|
|
33
|
+
#
|
|
34
|
+
# With a discriminator, the validation schema keeps the discriminator (see DiscriminatedSchema),
|
|
35
|
+
# so OpenAPI 3.0 and 3.1 validators validate only the variant the discriminator selects.
|
|
33
36
|
class OneOf < Base
|
|
34
37
|
# @return [String, nil] The discriminator property name for schema selection
|
|
35
38
|
attr_reader :discriminator
|
|
@@ -57,12 +60,15 @@ module Verquest
|
|
|
57
60
|
# Object properties define inline schemas directly within the oneOf.
|
|
58
61
|
#
|
|
59
62
|
# @param schema [Verquest::Properties::Reference, Verquest::Properties::Object] The schema to add
|
|
60
|
-
# @raise [ArgumentError] If schema is neither a Reference nor an Object
|
|
63
|
+
# @raise [ArgumentError] If schema is neither a Reference nor an Object, or references a schema-only class
|
|
61
64
|
# @return [Verquest::Properties::Base] The added schema
|
|
62
65
|
def add(schema)
|
|
63
66
|
unless schema.is_a?(Verquest::Properties::Reference) || schema.is_a?(Verquest::Properties::Object)
|
|
64
67
|
raise ArgumentError, "Must be a Reference or Object property"
|
|
65
68
|
end
|
|
69
|
+
if schema.is_a?(Verquest::Properties::Reference) && schema.schema_only?
|
|
70
|
+
raise ArgumentError, "one_of variants must reference Verquest::Base subclasses"
|
|
71
|
+
end
|
|
66
72
|
|
|
67
73
|
schemas[schema.name] = schema
|
|
68
74
|
end
|
|
@@ -113,8 +119,33 @@ module Verquest
|
|
|
113
119
|
|
|
114
120
|
build_variant_mappings(mapping, source_prefix, target_prefix, version)
|
|
115
121
|
store_discriminator_path(mapping, source_prefix)
|
|
116
|
-
store_variant_schemas(mapping, version) unless discriminator
|
|
117
|
-
store_nullable_metadata(mapping, target_prefix:) if nullable
|
|
122
|
+
store_variant_schemas(mapping, source_prefix, version) unless discriminator
|
|
123
|
+
store_nullable_metadata(mapping, source_prefix:, target_prefix:) if nullable
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Adds this oneOf to a transformer layout
|
|
127
|
+
#
|
|
128
|
+
# Each variant gets its own layout, so it can hold oneOfs and collections.
|
|
129
|
+
# Like in the mapping, variant properties sit directly under the oneOf
|
|
130
|
+
# path, not under the variant name.
|
|
131
|
+
#
|
|
132
|
+
# @param key_prefix [Array<String>] Prefix for the source key
|
|
133
|
+
# @param value_prefix [Array<String>] Prefix for the target value
|
|
134
|
+
# @param layout [Verquest::Transformer::Layout] The layout to add to
|
|
135
|
+
# @param version [String, nil] The version to create the layout for
|
|
136
|
+
# @return [void]
|
|
137
|
+
def layout(key_prefix:, value_prefix:, layout:, version:)
|
|
138
|
+
freeze_schemas
|
|
139
|
+
source_prefix = compute_source_prefix(key_prefix)
|
|
140
|
+
target_prefix = compute_target_prefix(value_prefix)
|
|
141
|
+
|
|
142
|
+
layout.add_one_of(Transformer::Layout::OneOfLayout.new(
|
|
143
|
+
path: source_prefix.join("/"),
|
|
144
|
+
discriminator: discriminator && join_path(source_prefix, discriminator),
|
|
145
|
+
variants: schemas.each_value.to_h { |schema| [schema.name, variant_layout(schema, source_prefix, target_prefix, layout, version)] },
|
|
146
|
+
variant_schemas: (variant_schemas(version:) unless discriminator),
|
|
147
|
+
null_target: (target_prefix.join("/") if nullable && !source_prefix.empty?)
|
|
148
|
+
))
|
|
118
149
|
end
|
|
119
150
|
|
|
120
151
|
# Returns validation schemas for all variants
|
|
@@ -191,7 +222,7 @@ module Verquest
|
|
|
191
222
|
# Builds variant mappings for each schema option
|
|
192
223
|
#
|
|
193
224
|
# Handles both Reference and Object schemas:
|
|
194
|
-
# - Reference:
|
|
225
|
+
# - Reference: maps the referenced request's properties under the oneOf path
|
|
195
226
|
# - Object: builds mapping from child properties directly
|
|
196
227
|
#
|
|
197
228
|
# @param mapping [Hash] The mapping hash to populate
|
|
@@ -211,6 +242,9 @@ module Verquest
|
|
|
211
242
|
|
|
212
243
|
# Builds mapping for a Reference variant
|
|
213
244
|
#
|
|
245
|
+
# Maps the referenced request's properties under the oneOf path, so its
|
|
246
|
+
# oneOfs land in the variant's `_oneOfs` with full paths.
|
|
247
|
+
#
|
|
214
248
|
# @param mapping [Hash] The mapping hash to populate
|
|
215
249
|
# @param schema [Verquest::Properties::Reference] The reference schema
|
|
216
250
|
# @param source_prefix [Array<String>] Source path prefix
|
|
@@ -218,15 +252,10 @@ module Verquest
|
|
|
218
252
|
# @param version [String, nil] The version for schema resolution
|
|
219
253
|
# @return [void]
|
|
220
254
|
def build_reference_variant_mapping(mapping, schema, source_prefix, target_prefix, version)
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
variant_target_prefix = compute_reference_target_prefix(reference_map, target_prefix)
|
|
255
|
+
variant_target_prefix = compute_reference_target_prefix(schema.send(:map), target_prefix)
|
|
256
|
+
variant_mapping = mapping[schema.name] = {}
|
|
224
257
|
|
|
225
|
-
|
|
226
|
-
reference_mapping,
|
|
227
|
-
source_prefix,
|
|
228
|
-
variant_target_prefix
|
|
229
|
-
)
|
|
258
|
+
add_request_mapping(schema.send(:from), key_prefix: source_prefix, value_prefix: variant_target_prefix, mapping: variant_mapping, version:)
|
|
230
259
|
end
|
|
231
260
|
|
|
232
261
|
# Builds mapping for an inline Object variant
|
|
@@ -242,34 +271,34 @@ module Verquest
|
|
|
242
271
|
# @param version [String, nil] The version for schema resolution
|
|
243
272
|
# @return [void]
|
|
244
273
|
def build_object_variant_mapping(mapping, schema, source_prefix, target_prefix, version)
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
variant_target_prefix = compute_reference_target_prefix(object_map, target_prefix)
|
|
274
|
+
variant_target_prefix = compute_reference_target_prefix(schema.send(:map), target_prefix)
|
|
275
|
+
variant_mapping = mapping[schema.name] = {}
|
|
248
276
|
|
|
249
|
-
# Build mapping from object's child properties
|
|
250
277
|
# Properties are mapped directly under source_prefix (not source_prefix + object_name)
|
|
251
|
-
# to match how Reference variants work
|
|
252
|
-
schema.
|
|
253
|
-
property
|
|
254
|
-
key_prefix: source_prefix,
|
|
255
|
-
value_prefix: variant_target_prefix,
|
|
256
|
-
mapping: object_mapping,
|
|
257
|
-
version: version
|
|
258
|
-
)
|
|
278
|
+
# to match how Reference variants work; a nested oneOf lands in the variant's _oneOfs
|
|
279
|
+
schema.all_properties.each_value do |property|
|
|
280
|
+
add_child_mapping(property, key_prefix: source_prefix, value_prefix: variant_target_prefix, mapping: variant_mapping, version:)
|
|
259
281
|
end
|
|
260
|
-
|
|
261
|
-
mapping[schema.name] = object_mapping
|
|
262
282
|
end
|
|
263
283
|
|
|
264
|
-
#
|
|
284
|
+
# Adds a variant's properties to its layout, directly under the oneOf path
|
|
265
285
|
#
|
|
266
|
-
# @param
|
|
267
|
-
# @param source_prefix [Array<String>]
|
|
268
|
-
# @param target_prefix [Array<String>]
|
|
269
|
-
# @
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
286
|
+
# @param schema [Verquest::Properties::Reference, Verquest::Properties::Object] The variant
|
|
287
|
+
# @param source_prefix [Array<String>] The oneOf source path
|
|
288
|
+
# @param target_prefix [Array<String>] The oneOf target path
|
|
289
|
+
# @param layout [Verquest::Transformer::Layout] The layout holding the oneOf
|
|
290
|
+
# @param version [String, nil] The version for schema resolution
|
|
291
|
+
# @return [Verquest::Transformer::Layout] The variant layout
|
|
292
|
+
def variant_layout(schema, source_prefix, target_prefix, layout, version)
|
|
293
|
+
variant_target_prefix = compute_reference_target_prefix(schema.send(:map), target_prefix)
|
|
294
|
+
layout.variant.tap do |variant|
|
|
295
|
+
if schema.is_a?(Verquest::Properties::Reference)
|
|
296
|
+
add_request_layout(schema.send(:from), key_prefix: source_prefix, value_prefix: variant_target_prefix, layout: variant, version:)
|
|
297
|
+
else
|
|
298
|
+
schema.all_properties.each_value do |property|
|
|
299
|
+
property.layout(key_prefix: source_prefix, value_prefix: variant_target_prefix, layout: variant, version:)
|
|
300
|
+
end
|
|
301
|
+
end
|
|
273
302
|
end
|
|
274
303
|
end
|
|
275
304
|
|
|
@@ -295,11 +324,13 @@ module Verquest
|
|
|
295
324
|
# schemas to determine which variant matches the input data.
|
|
296
325
|
#
|
|
297
326
|
# @param mapping [Hash] The mapping hash to update
|
|
327
|
+
# @param source_prefix [Array<String>] The source path prefix, ending with the oneOf name
|
|
298
328
|
# @param version [String, nil] The version for schema resolution
|
|
299
329
|
# @return [void]
|
|
300
|
-
def store_variant_schemas(mapping, version)
|
|
330
|
+
def store_variant_schemas(mapping, source_prefix, version)
|
|
301
331
|
mapping["_variant_schemas"] = variant_schemas(version: version)
|
|
302
|
-
|
|
332
|
+
# An unnamed oneOf is only at the root when nothing references it
|
|
333
|
+
mapping["_variant_path"] = source_prefix.join("/") unless source_prefix.empty?
|
|
303
334
|
end
|
|
304
335
|
|
|
305
336
|
# Stores nullable metadata in the mapping
|
|
@@ -308,13 +339,14 @@ module Verquest
|
|
|
308
339
|
# without attempting variant resolution.
|
|
309
340
|
#
|
|
310
341
|
# @param mapping [Hash] The mapping hash to update
|
|
342
|
+
# @param source_prefix [Array<String>] The source path prefix, ending with the oneOf name
|
|
311
343
|
# @param target_prefix [Array<String>] The target path prefix for the oneOf property
|
|
312
344
|
# @return [void]
|
|
313
|
-
def store_nullable_metadata(mapping, target_prefix:)
|
|
345
|
+
def store_nullable_metadata(mapping, source_prefix:, target_prefix:)
|
|
314
346
|
mapping["_nullable"] = true
|
|
315
|
-
return if
|
|
347
|
+
return if source_prefix.empty?
|
|
316
348
|
|
|
317
|
-
mapping["_nullable_path"] =
|
|
349
|
+
mapping["_nullable_path"] = source_prefix.join("/")
|
|
318
350
|
mapping["_nullable_target_path"] = target_prefix.join("/")
|
|
319
351
|
end
|
|
320
352
|
|
|
@@ -369,14 +401,16 @@ module Verquest
|
|
|
369
401
|
|
|
370
402
|
# Builds the validation schema structure with inline definitions
|
|
371
403
|
#
|
|
372
|
-
#
|
|
373
|
-
#
|
|
374
|
-
#
|
|
404
|
+
# Without a discriminator, the variants are inlined in the oneOf array.
|
|
405
|
+
# With a discriminator, DiscriminatedSchema builds a self-contained schema
|
|
406
|
+
# that keeps the discriminator.
|
|
375
407
|
#
|
|
376
408
|
# @param version [String, nil] The version to generate validation schema for
|
|
377
|
-
# @return [Hash] Validation schema with oneOf array
|
|
409
|
+
# @return [Hash] Validation schema with oneOf array
|
|
378
410
|
def build_validation_schema(version:)
|
|
379
|
-
{schema_keyword => collect_inline_schemas(version)}
|
|
411
|
+
return {schema_keyword => collect_inline_schemas(version)} unless discriminator
|
|
412
|
+
|
|
413
|
+
DiscriminatedSchema.new(definitions: collect_inline_definitions(version), discriminator: discriminator).to_h
|
|
380
414
|
end
|
|
381
415
|
|
|
382
416
|
# Collects $ref schema references for all variants
|
|
@@ -391,7 +425,15 @@ module Verquest
|
|
|
391
425
|
# @param version [String, nil] The version for schema resolution
|
|
392
426
|
# @return [Array<Hash>] Array of inline schema definitions
|
|
393
427
|
def collect_inline_schemas(version)
|
|
394
|
-
|
|
428
|
+
collect_inline_definitions(version).values
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
# Collects inline schema definitions keyed by variant name
|
|
432
|
+
#
|
|
433
|
+
# @param version [String, nil] The version for schema resolution
|
|
434
|
+
# @return [Hash<String, Hash>] Variant name => inline schema definition
|
|
435
|
+
def collect_inline_definitions(version)
|
|
436
|
+
schemas.transform_values { |schema| schema.to_validation_schema(version: version)[schema.name] }
|
|
395
437
|
end
|
|
396
438
|
|
|
397
439
|
# Adds discriminator information to the schema if present
|
|
@@ -56,37 +56,81 @@ module Verquest
|
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
# Create mapping for this reference property
|
|
59
|
-
#
|
|
59
|
+
#
|
|
60
|
+
# The referenced request's properties map themselves under this
|
|
61
|
+
# reference's prefixes. A reference to a single property maps that
|
|
62
|
+
# property as if it were declared here, with this reference's name and map:.
|
|
60
63
|
#
|
|
61
64
|
# @param key_prefix [Array<String>] Prefix for the source key
|
|
62
65
|
# @param value_prefix [Array<String>] Prefix for the target value
|
|
63
66
|
# @param mapping [Hash] The mapping hash to be updated
|
|
64
67
|
# @param version [String] The version to create mapping for
|
|
65
68
|
# @return [void]
|
|
69
|
+
# @raise [Verquest::PropertyNotFoundError] If the referenced property doesn't exist
|
|
66
70
|
def mapping(key_prefix:, value_prefix:, mapping:, version:)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
71
|
+
return mapping[(key_prefix + [name]).join("/")] = mapping_value_key(value_prefix:) if schema_only?
|
|
72
|
+
return add_request_mapping(from, key_prefix: key_prefix + [name], value_prefix: mapping_value_prefix(value_prefix:), mapping:, version:) unless property
|
|
73
|
+
|
|
74
|
+
referenced = from.resolve(version)
|
|
75
|
+
add_child_mapping(referenced.fetch_property(property).renamed(name:, map:), key_prefix:, value_prefix:, mapping:, version: referenced.name)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Adds the referenced request's properties, or the referenced property, to a transformer layout
|
|
79
|
+
#
|
|
80
|
+
# @param key_prefix [Array<String>] Prefix for the source key
|
|
81
|
+
# @param value_prefix [Array<String>] Prefix for the target value
|
|
82
|
+
# @param layout [Verquest::Transformer::Layout] The layout to add to
|
|
83
|
+
# @param version [String, nil] The version to create the layout for
|
|
84
|
+
# @return [void]
|
|
85
|
+
# @raise [Verquest::PropertyNotFoundError] If the referenced property doesn't exist
|
|
86
|
+
def layout(key_prefix:, value_prefix:, layout:, version:)
|
|
87
|
+
return super if schema_only?
|
|
88
|
+
return add_request_layout(from, key_prefix: key_prefix + [name], value_prefix: mapping_value_prefix(value_prefix:), layout:, version:) unless property
|
|
89
|
+
|
|
90
|
+
referenced = from.resolve(version)
|
|
91
|
+
referenced.fetch_property(property).renamed(name:, map:).layout(key_prefix:, value_prefix:, layout:, version: referenced.name)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Returns the query parameter options of the referenced request, or of
|
|
95
|
+
# the referenced property renamed to this reference
|
|
96
|
+
#
|
|
97
|
+
# This reference's own parameter examples replace the referenced ones.
|
|
98
|
+
#
|
|
99
|
+
# @param prefix [Array<String>] Path of the parent property
|
|
100
|
+
# @param version [String] The version to resolve references with
|
|
101
|
+
# @return [Hash{Array<String> => Hash}] The options of each property path
|
|
102
|
+
# @raise [Verquest::PropertyNotFoundError] If the referenced property doesn't exist
|
|
103
|
+
def query_options(prefix:, version:)
|
|
104
|
+
return super if schema_only?
|
|
105
|
+
|
|
106
|
+
referenced = from.resolve(version)
|
|
107
|
+
return referenced_property_options(referenced, prefix:) if property
|
|
108
|
+
|
|
109
|
+
referenced.all_properties.each_value.with_object(super) do |child, options|
|
|
110
|
+
options.merge!(child.query_options(prefix: prefix + [name], version: referenced.name))
|
|
82
111
|
end
|
|
112
|
+
end
|
|
83
113
|
|
|
84
|
-
|
|
114
|
+
# Whether the referenced class only provides a schema (not a Verquest::Base subclass),
|
|
115
|
+
# so its value passes through unchanged
|
|
116
|
+
#
|
|
117
|
+
# @return [Boolean]
|
|
118
|
+
# @api private
|
|
119
|
+
def schema_only?
|
|
120
|
+
!(from.is_a?(Class) && from <= Verquest::Base)
|
|
85
121
|
end
|
|
86
122
|
|
|
87
123
|
private
|
|
88
124
|
|
|
89
125
|
attr_reader :from, :property
|
|
126
|
+
|
|
127
|
+
# @return [Hash{Array<String> => Hash}] The options of the referenced property under this reference's name
|
|
128
|
+
def referenced_property_options(referenced, prefix:)
|
|
129
|
+
copy = referenced.fetch_property(property).renamed(name:, map:)
|
|
130
|
+
copy.parameter_examples = parameter_examples if parameter_examples
|
|
131
|
+
|
|
132
|
+
copy.query_options(prefix:, version: referenced.name)
|
|
133
|
+
end
|
|
90
134
|
end
|
|
91
135
|
end
|
|
92
136
|
end
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
module Query
|
|
5
|
+
# Converts parsed query parameters to the types their schema expects
|
|
6
|
+
#
|
|
7
|
+
# Query strings carry only strings, so `"2"` has to become `2` and `"a,b"`
|
|
8
|
+
# has to become `["a", "b"]` before the params are validated. The coercer
|
|
9
|
+
# walks the version's validation schema next to the params. A value no rule
|
|
10
|
+
# converts is left unchanged, so the validation reports it.
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# schema = {"type" => "object", "properties" => {"page" => {"type" => "integer"}}}
|
|
14
|
+
# Verquest::Query::Coercer.new(schema:, explodes: {}).call({"page" => "2"})
|
|
15
|
+
# # => {"page" => 2}
|
|
16
|
+
class Coercer
|
|
17
|
+
# Returned by a rule that doesn't apply to a value
|
|
18
|
+
NO_MATCH = ::Object.new.freeze
|
|
19
|
+
|
|
20
|
+
# The JSON type of each class an enum or const value can have
|
|
21
|
+
JSON_TYPES = {
|
|
22
|
+
::Integer => "integer",
|
|
23
|
+
::Float => "number",
|
|
24
|
+
::String => "string",
|
|
25
|
+
::TrueClass => "boolean",
|
|
26
|
+
::FalseClass => "boolean",
|
|
27
|
+
::NilClass => "null"
|
|
28
|
+
}.freeze
|
|
29
|
+
|
|
30
|
+
# Strings converted to an Integer
|
|
31
|
+
INTEGER_PATTERN = /\A-?\d+\z/
|
|
32
|
+
|
|
33
|
+
# Strings converted to an Integer or a Float
|
|
34
|
+
NUMBER_PATTERN = /\A-?\d+(\.\d+)?\z/
|
|
35
|
+
|
|
36
|
+
# Strings converted to a boolean
|
|
37
|
+
BOOLEANS = {"true" => true, "false" => false}.freeze
|
|
38
|
+
|
|
39
|
+
# @param schema [Hash] The version's validation schema
|
|
40
|
+
# @param explodes [Hash{Array<String> => Boolean}] The explode value of each array property, by path
|
|
41
|
+
def initialize(schema:, explodes:)
|
|
42
|
+
@schema = schema
|
|
43
|
+
@explodes = explodes
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Converts the params
|
|
47
|
+
#
|
|
48
|
+
# @param params [Hash] The parsed query parameters, with string or symbol keys
|
|
49
|
+
# @return [Hash] A copy of the params with converted values
|
|
50
|
+
def call(params)
|
|
51
|
+
coerce_properties(params, schema, [])
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
attr_reader :schema, :explodes
|
|
57
|
+
|
|
58
|
+
# @return [Hash] A copy of the hash with each known property converted
|
|
59
|
+
def coerce_properties(hash, object_schema, path)
|
|
60
|
+
properties = object_schema.fetch("properties", {})
|
|
61
|
+
additional = object_schema["additionalProperties"]
|
|
62
|
+
|
|
63
|
+
hash.each_with_object(hash.dup) do |(key, value), result|
|
|
64
|
+
property_schema = properties.fetch(key.to_s) { additional if additional.is_a?(Hash) }
|
|
65
|
+
next unless property_schema
|
|
66
|
+
|
|
67
|
+
coerced = coerce(value, property_schema, path + [key.to_s])
|
|
68
|
+
result[key] = coerced unless coerced.equal?(NO_MATCH)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Tries the schema's types, then its anyOf and oneOf variants
|
|
73
|
+
#
|
|
74
|
+
# @return [Object] The converted value or NO_MATCH
|
|
75
|
+
def coerce(value, value_schema, path)
|
|
76
|
+
types(value_schema).each do |type|
|
|
77
|
+
coerced = coerce_type(type, value, value_schema, path)
|
|
78
|
+
return coerced unless coerced.equal?(NO_MATCH)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
value_schema.values_at("anyOf", "oneOf").compact.flatten.each do |variant|
|
|
82
|
+
coerced = coerce(value, variant, path)
|
|
83
|
+
return coerced unless coerced.equal?(NO_MATCH)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
NO_MATCH
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# @return [Array<String>] The schema's types, inferred from enum and const values without a type
|
|
90
|
+
def types(value_schema)
|
|
91
|
+
if value_schema.key?("type")
|
|
92
|
+
Array(value_schema["type"])
|
|
93
|
+
elsif value_schema.key?("enum")
|
|
94
|
+
value_schema["enum"].filter_map { |value| JSON_TYPES[value.class] }.uniq
|
|
95
|
+
elsif value_schema.key?("const")
|
|
96
|
+
[JSON_TYPES[value_schema["const"].class]].compact
|
|
97
|
+
else
|
|
98
|
+
[]
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# @return [Object] The value converted to the type or NO_MATCH
|
|
103
|
+
def coerce_type(type, value, value_schema, path)
|
|
104
|
+
case type
|
|
105
|
+
when "integer" then coerce_integer(value)
|
|
106
|
+
when "number" then coerce_number(value)
|
|
107
|
+
when "boolean" then coerce_boolean(value)
|
|
108
|
+
when "null" then (value.nil? || value == "") ? nil : NO_MATCH
|
|
109
|
+
when "string" then value.is_a?(::String) ? value : NO_MATCH
|
|
110
|
+
when "array" then coerce_array(value, value_schema, path)
|
|
111
|
+
when "object" then value.is_a?(::Hash) ? coerce_properties(value, value_schema, path) : NO_MATCH
|
|
112
|
+
else NO_MATCH
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# @return [Integer, Object] The integer or NO_MATCH
|
|
117
|
+
def coerce_integer(value)
|
|
118
|
+
return value if value.is_a?(::Integer)
|
|
119
|
+
return NO_MATCH unless value.is_a?(::String) && INTEGER_PATTERN.match?(value)
|
|
120
|
+
|
|
121
|
+
Integer(value, 10)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# @return [Numeric, Object] The number or NO_MATCH
|
|
125
|
+
def coerce_number(value)
|
|
126
|
+
return value if value.is_a?(::Numeric)
|
|
127
|
+
return NO_MATCH unless value.is_a?(::String) && NUMBER_PATTERN.match?(value)
|
|
128
|
+
|
|
129
|
+
value.include?(".") ? Float(value) : Integer(value, 10)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# @return [Boolean, Object] The boolean or NO_MATCH
|
|
133
|
+
def coerce_boolean(value)
|
|
134
|
+
return value if value == true || value == false
|
|
135
|
+
|
|
136
|
+
BOOLEANS.fetch(value, NO_MATCH)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Splits a string by the array's explode value and converts each item;
|
|
140
|
+
# an item no rule converts is kept as it is
|
|
141
|
+
def coerce_array(value, array_schema, path)
|
|
142
|
+
items = split(value, path)
|
|
143
|
+
return items if items.equal?(NO_MATCH) || !array_schema.key?("items")
|
|
144
|
+
|
|
145
|
+
items.map do |item|
|
|
146
|
+
coerced = coerce(item, array_schema["items"], path)
|
|
147
|
+
coerced.equal?(NO_MATCH) ? item : coerced
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# @return [Array, Object] The items of the value or NO_MATCH
|
|
152
|
+
def split(value, path)
|
|
153
|
+
return value if value.is_a?(::Array)
|
|
154
|
+
return NO_MATCH unless value.is_a?(::String)
|
|
155
|
+
|
|
156
|
+
explode?(path) ? [value] : value.split(",")
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# @return [Boolean] Whether the array at the path is sent as repeated keys
|
|
160
|
+
def explode?(path)
|
|
161
|
+
explodes.fetch(path) { Verquest.configuration.query_array_explode }
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
module Query
|
|
5
|
+
# Rejects query and path versions with shapes their location can't express
|
|
6
|
+
#
|
|
7
|
+
# Query parameters are scalars, arrays of scalars, or one level of objects
|
|
8
|
+
# sent as `deepObject` (`filter[status]=open`). Path parameters are required
|
|
9
|
+
# scalars. Anything else, and oneOf properties, raise when the version is
|
|
10
|
+
# prepared.
|
|
11
|
+
class DefinitionCheck
|
|
12
|
+
# @param version [Verquest::Version] The query or path version to check
|
|
13
|
+
def initialize(version)
|
|
14
|
+
@version = version
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @return [void]
|
|
18
|
+
# @raise [Verquest::DefinitionError] If the version uses an unsupported shape
|
|
19
|
+
def call
|
|
20
|
+
raise DefinitionError, "Version '#{version.name}' has a root-level one_of, which #{version.location} parameters can't express" if version.combination?
|
|
21
|
+
|
|
22
|
+
version.all_properties.each_value do |property|
|
|
23
|
+
reject([property.name], "a one_of") if property.is_a?(Properties::OneOf)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
version.validation_schema.fetch("properties", {}).each do |name, property_schema|
|
|
27
|
+
(version.location == :path) ? check_path(property_schema, name) : check(property_schema, [name], nested: false)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
attr_reader :version
|
|
34
|
+
|
|
35
|
+
# Rejects path parameters that aren't required scalars
|
|
36
|
+
#
|
|
37
|
+
# @return [void]
|
|
38
|
+
def check_path(property_schema, name)
|
|
39
|
+
types = variants(property_schema).flat_map { |variant| Array(variant["type"]) }
|
|
40
|
+
|
|
41
|
+
reject([name], "an array") if types.include?("array")
|
|
42
|
+
reject([name], "an object") if types.include?("object")
|
|
43
|
+
return if version.validation_schema.fetch("required", []).include?(name)
|
|
44
|
+
|
|
45
|
+
raise DefinitionError, "Property '#{name}' in version '#{version.name}' is optional, but path parameters are always required"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Checks the objects and arrays of a property schema and its variants
|
|
49
|
+
#
|
|
50
|
+
# @return [void]
|
|
51
|
+
def check(property_schema, path, nested:)
|
|
52
|
+
variants(property_schema).each do |variant|
|
|
53
|
+
types = Array(variant["type"])
|
|
54
|
+
|
|
55
|
+
check_object(variant, path, nested:) if types.include?("object")
|
|
56
|
+
check_items(variant["items"], path) if types.include?("array") && variant.key?("items")
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Rejects a nested object, then checks the object's properties
|
|
61
|
+
#
|
|
62
|
+
# @return [void]
|
|
63
|
+
def check_object(object_schema, path, nested:)
|
|
64
|
+
reject(path, "a nested object") if nested
|
|
65
|
+
|
|
66
|
+
object_schema.fetch("properties", {}).each do |name, child_schema|
|
|
67
|
+
check(child_schema, path + [name], nested: true)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Rejects array items that are objects or arrays
|
|
72
|
+
#
|
|
73
|
+
# @return [void]
|
|
74
|
+
def check_items(items_schema, path)
|
|
75
|
+
item_types = variants(items_schema).flat_map { |variant| Array(variant["type"]) }
|
|
76
|
+
|
|
77
|
+
reject(path, "an array of objects or arrays") if item_types.intersect?(%w[object array])
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# @return [Array<Hash>] The schema and its anyOf and oneOf variants
|
|
81
|
+
def variants(schema)
|
|
82
|
+
[schema, *schema.values_at("anyOf", "oneOf").compact.flatten]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @raise [Verquest::DefinitionError] Always
|
|
86
|
+
def reject(path, shape)
|
|
87
|
+
raise DefinitionError, "Property '#{path.join("/")}' in version '#{version.name}' is #{shape}, which #{version.location} parameters can't express"
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Verquest
|
|
4
|
+
module Query
|
|
5
|
+
# Builds OpenAPI parameter objects from a query or path version's validation schema
|
|
6
|
+
#
|
|
7
|
+
# Each property becomes one `in: :query` (or `in: :path`) parameter. Keys are symbols,
|
|
8
|
+
# including inside `schema`, because rswag reads them that way
|
|
9
|
+
# (`param[:in]`, `param[:style]`, `param[:schema][:type]`).
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# schema = {"type" => "object", "required" => [], "properties" => {"page" => {"type" => "integer"}}}
|
|
13
|
+
# Verquest::Query::Parameters.new(schema:, options: {}).call
|
|
14
|
+
# # => [{name: "page", in: :query, required: false, schema: {type: "integer"}}]
|
|
15
|
+
class Parameters
|
|
16
|
+
# Schema keys moved to the parameter itself
|
|
17
|
+
DOCUMENTATION_KEYS = %w[description deprecated].freeze
|
|
18
|
+
|
|
19
|
+
# @param schema [Hash] The version's validation schema
|
|
20
|
+
# @param options [Hash{Array<String> => Hash}] The `explode:` and `examples:` of each property path
|
|
21
|
+
# @param location [Symbol] `:query` or `:path`, written as `in:`
|
|
22
|
+
def initialize(schema:, options:, location: :query)
|
|
23
|
+
@schema = schema
|
|
24
|
+
@options = options
|
|
25
|
+
@location = location
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @return [Array<Hash>] The parameter objects, in property order
|
|
29
|
+
def call
|
|
30
|
+
required = schema.fetch("required", [])
|
|
31
|
+
|
|
32
|
+
schema.fetch("properties", {}).map do |name, property_schema|
|
|
33
|
+
parameter(name, property_schema, required: required.include?(name))
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
attr_reader :schema, :options, :location
|
|
40
|
+
|
|
41
|
+
# @return [Hash] The parameter object of one property
|
|
42
|
+
def parameter(name, property_schema, required:)
|
|
43
|
+
{name:, in: location, required:}
|
|
44
|
+
.merge(property_schema.slice(*DOCUMENTATION_KEYS).transform_keys(&:to_sym))
|
|
45
|
+
.merge(serialization(name, property_schema))
|
|
46
|
+
.merge(examples(name))
|
|
47
|
+
.merge(schema: symbolize(property_schema.except(*DOCUMENTATION_KEYS)))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @return [Hash] The style and explode of an array or object parameter
|
|
51
|
+
def serialization(name, property_schema)
|
|
52
|
+
types = [property_schema, *property_schema.fetch("anyOf", [])].flat_map { |variant| Array(variant["type"]) }
|
|
53
|
+
|
|
54
|
+
if types.include?("object")
|
|
55
|
+
{style: :deepObject, explode: true}
|
|
56
|
+
elsif types.include?("array")
|
|
57
|
+
{style: :form, explode: options.fetch([name], {}).fetch(:explode) { Verquest.configuration.query_array_explode }}
|
|
58
|
+
else
|
|
59
|
+
{}
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# @return [Hash] The named Example Objects of the parameter, if it has any
|
|
64
|
+
def examples(name)
|
|
65
|
+
examples = options.dig([name], :examples)
|
|
66
|
+
examples ? {examples: symbolize(examples)} : {}
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @return [Object] The value with symbol keys in every hash
|
|
70
|
+
def symbolize(value)
|
|
71
|
+
case value
|
|
72
|
+
when ::Hash then value.to_h { |key, child| [key.to_sym, symbolize(child)] }
|
|
73
|
+
when ::Array then value.map { |child| symbolize(child) }
|
|
74
|
+
else value
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|