apiwork 0.7.1 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/apiwork/exports_controller.rb +0 -2
- data/lib/apiwork/adapter/base.rb +10 -2
- data/lib/apiwork/adapter/capability/base.rb +2 -0
- data/lib/apiwork/adapter/capability/contract/scope.rb +0 -1
- data/lib/apiwork/adapter/capability/operation/base.rb +1 -0
- data/lib/apiwork/adapter/capability/runner.rb +0 -1
- data/lib/apiwork/adapter/serializer/resource/base.rb +0 -1
- data/lib/apiwork/adapter/serializer/resource/default/contract_builder.rb +1 -2
- data/lib/apiwork/adapter/standard/capability/filtering/constants.rb +0 -3
- data/lib/apiwork/adapter/standard/capability/filtering/operation/filter.rb +0 -16
- data/lib/apiwork/adapter/standard/capability/filtering.rb +3 -0
- data/lib/apiwork/adapter/standard/capability/including/operation.rb +0 -1
- data/lib/apiwork/adapter/standard/capability/including.rb +1 -0
- data/lib/apiwork/adapter/standard/capability/pagination/operation/paginate/cursor.rb +0 -1
- data/lib/apiwork/adapter/standard/capability/pagination.rb +2 -0
- data/lib/apiwork/adapter/standard/capability/sorting.rb +2 -0
- data/lib/apiwork/adapter/standard/capability/writing/contract_builder.rb +0 -4
- data/lib/apiwork/adapter/standard/capability/writing/operation.rb +1 -0
- data/lib/apiwork/adapter/standard/capability/writing.rb +2 -0
- data/lib/apiwork/adapter/standard.rb +3 -0
- data/lib/apiwork/adapter/wrapper/collection/default.rb +1 -0
- data/lib/apiwork/adapter/wrapper/member/default.rb +2 -0
- data/lib/apiwork/api/base.rb +5 -7
- data/lib/apiwork/api/object.rb +1 -1
- data/lib/apiwork/api/resource.rb +10 -10
- data/lib/apiwork/api/router.rb +0 -1
- data/lib/apiwork/api/type_registry.rb +1 -0
- data/lib/apiwork/configuration/option.rb +0 -1
- data/lib/apiwork/configuration/validatable.rb +1 -0
- data/lib/apiwork/configuration.rb +1 -0
- data/lib/apiwork/contract/action/request.rb +4 -0
- data/lib/apiwork/contract/action/response.rb +1 -0
- data/lib/apiwork/contract/action.rb +2 -2
- data/lib/apiwork/contract/base.rb +3 -3
- data/lib/apiwork/contract/object/coercer.rb +0 -2
- data/lib/apiwork/contract/object/deserializer.rb +0 -1
- data/lib/apiwork/contract/object/transformer.rb +0 -1
- data/lib/apiwork/contract/object/validator.rb +12 -18
- data/lib/apiwork/contract/object.rb +7 -9
- data/lib/apiwork/contract/request_parser.rb +0 -2
- data/lib/apiwork/controller.rb +3 -6
- data/lib/apiwork/engine.rb +1 -0
- data/lib/apiwork/error_code/registry.rb +0 -1
- data/lib/apiwork/export/apiwork.rb +1 -0
- data/lib/apiwork/export/apiwork_mapper.rb +0 -1
- data/lib/apiwork/export/base.rb +2 -3
- data/lib/apiwork/export/open_api.rb +4 -11
- data/lib/apiwork/export/pipeline/writer.rb +1 -1
- data/lib/apiwork/export/pipeline.rb +0 -5
- data/lib/apiwork/export/sorbus.rb +2 -0
- data/lib/apiwork/export/sorbus_mapper.rb +1 -5
- data/lib/apiwork/export/surface_resolver.rb +6 -4
- data/lib/apiwork/export/type_analysis.rb +2 -1
- data/lib/apiwork/export/type_script.rb +2 -0
- data/lib/apiwork/export/type_script_mapper.rb +0 -8
- data/lib/apiwork/export/zod.rb +4 -2
- data/lib/apiwork/export/zod_mapper.rb +0 -8
- data/lib/apiwork/introspection/dump/action.rb +1 -1
- data/lib/apiwork/introspection/dump/api.rb +0 -4
- data/lib/apiwork/introspection/dump/contract.rb +0 -6
- data/lib/apiwork/introspection/dump/param.rb +6 -6
- data/lib/apiwork/introspection/dump/resource.rb +1 -0
- data/lib/apiwork/introspection/dump/type.rb +5 -5
- data/lib/apiwork/reference_generator.rb +10 -10
- data/lib/apiwork/registry.rb +1 -0
- data/lib/apiwork/representation/association.rb +3 -3
- data/lib/apiwork/representation/attribute.rb +0 -4
- data/lib/apiwork/representation/base.rb +2 -5
- data/lib/apiwork/representation/deserializer.rb +0 -1
- data/lib/apiwork/representation/inheritance.rb +22 -10
- data/lib/apiwork/representation/model_detector.rb +1 -0
- data/lib/apiwork/representation/serializer.rb +3 -2
- data/lib/apiwork/version.rb +1 -1
- data/lib/apiwork.rb +9 -8
- metadata +4 -4
|
@@ -62,9 +62,11 @@ module Apiwork
|
|
|
62
62
|
# end
|
|
63
63
|
def query(&block)
|
|
64
64
|
@query ||= Object.new(@contract_class, action_name: @action_name)
|
|
65
|
+
|
|
65
66
|
if block
|
|
66
67
|
block.arity.positive? ? yield(@query) : @query.instance_eval(&block)
|
|
67
68
|
end
|
|
69
|
+
|
|
68
70
|
@query
|
|
69
71
|
end
|
|
70
72
|
|
|
@@ -90,9 +92,11 @@ module Apiwork
|
|
|
90
92
|
# end
|
|
91
93
|
def body(&block)
|
|
92
94
|
@body ||= Object.new(@contract_class, action_name: @action_name)
|
|
95
|
+
|
|
93
96
|
if block
|
|
94
97
|
block.arity.positive? ? yield(@body) : @body.instance_eval(&block)
|
|
95
98
|
end
|
|
99
|
+
|
|
96
100
|
@body
|
|
97
101
|
end
|
|
98
102
|
end
|
|
@@ -138,6 +138,7 @@ module Apiwork
|
|
|
138
138
|
# end
|
|
139
139
|
def raises(*error_code_keys)
|
|
140
140
|
error_code_keys = error_code_keys.flatten
|
|
141
|
+
|
|
141
142
|
error_code_keys.each do |error_code_key|
|
|
142
143
|
unless error_code_key.is_a?(Symbol)
|
|
143
144
|
hint = error_code_key.is_a?(Integer) ? " Use :#{ErrorCode.key_for_status(error_code_key)} instead." : ''
|
|
@@ -150,6 +151,7 @@ module Apiwork
|
|
|
150
151
|
"Unknown error code :#{error_code_key}. Register it with: " \
|
|
151
152
|
"Apiwork::ErrorCode.register :#{error_code_key}, status: <status>"
|
|
152
153
|
end
|
|
154
|
+
|
|
153
155
|
@raises |= error_code_keys
|
|
154
156
|
end
|
|
155
157
|
|
|
@@ -189,7 +191,6 @@ module Apiwork
|
|
|
189
191
|
# end
|
|
190
192
|
def request(replace: false, &block)
|
|
191
193
|
@reset_request = replace if replace
|
|
192
|
-
|
|
193
194
|
@request ||= Request.new(contract_class, name)
|
|
194
195
|
|
|
195
196
|
if block
|
|
@@ -236,7 +237,6 @@ module Apiwork
|
|
|
236
237
|
# end
|
|
237
238
|
def response(replace: false, &block)
|
|
238
239
|
@reset_response = replace if replace
|
|
239
|
-
|
|
240
240
|
@response ||= Response.new(contract_class, name)
|
|
241
241
|
|
|
242
242
|
if block
|
|
@@ -143,6 +143,7 @@ module Apiwork
|
|
|
143
143
|
"representation must be a Representation class, got #{klass.class}. " \
|
|
144
144
|
"Use: representation InvoiceRepresentation (not 'InvoiceRepresentation' or :invoice)"
|
|
145
145
|
end
|
|
146
|
+
|
|
146
147
|
unless klass < Representation::Base
|
|
147
148
|
raise ConfigurationError,
|
|
148
149
|
'representation must be a Representation class (subclass of Apiwork::Representation::Base), ' \
|
|
@@ -407,11 +408,11 @@ module Apiwork
|
|
|
407
408
|
end
|
|
408
409
|
|
|
409
410
|
imports[as] = klass
|
|
410
|
-
|
|
411
411
|
return if klass.building?
|
|
412
412
|
return unless klass.representation? && klass.api_class
|
|
413
413
|
|
|
414
414
|
klass.building = true
|
|
415
|
+
|
|
415
416
|
begin
|
|
416
417
|
klass.api_class.ensure_contract_built!(klass)
|
|
417
418
|
ensure
|
|
@@ -481,6 +482,7 @@ module Apiwork
|
|
|
481
482
|
if block_given?
|
|
482
483
|
block.arity.positive? ? yield(action) : action.instance_eval(&block)
|
|
483
484
|
end
|
|
485
|
+
|
|
484
486
|
actions[name] = action
|
|
485
487
|
end
|
|
486
488
|
|
|
@@ -526,7 +528,6 @@ module Apiwork
|
|
|
526
528
|
|
|
527
529
|
contract_name = representation_class.name.sub(/Representation\z/, 'Contract')
|
|
528
530
|
contract_class = contract_name.safe_constantize
|
|
529
|
-
|
|
530
531
|
return contract_class if contract_class.is_a?(Class) && contract_class < Contract::Base
|
|
531
532
|
|
|
532
533
|
synthetic_contracts[representation_class] ||= build_synthetic_contract(representation_class, api_class)
|
|
@@ -576,7 +577,6 @@ module Apiwork
|
|
|
576
577
|
|
|
577
578
|
def action_for(action_name)
|
|
578
579
|
api_class.ensure_contract_built!(self)
|
|
579
|
-
|
|
580
580
|
action_name = action_name.to_sym
|
|
581
581
|
actions[action_name]
|
|
582
582
|
end
|
|
@@ -82,7 +82,6 @@ module Apiwork
|
|
|
82
82
|
|
|
83
83
|
def coerce_value(value, param_options)
|
|
84
84
|
type = param_options[:type]
|
|
85
|
-
|
|
86
85
|
return coerce_union(value, param_options[:union]) if type == :union
|
|
87
86
|
return coerce_array(value, param_options) if type == :array && value.is_a?(Array)
|
|
88
87
|
return coerce_record(value, param_options) if type == :record && value.is_a?(Hash)
|
|
@@ -102,7 +101,6 @@ module Apiwork
|
|
|
102
101
|
of = param_options[:of]
|
|
103
102
|
of_type = of&.type
|
|
104
103
|
of_shape = of&.shape
|
|
105
|
-
|
|
106
104
|
custom_shape = resolve_custom_shape(of_type) if of_type && !PRIMITIVES.key?(of_type)
|
|
107
105
|
|
|
108
106
|
array.map do |item|
|
|
@@ -97,7 +97,6 @@ module Apiwork
|
|
|
97
97
|
|
|
98
98
|
def build_type_shape(type_definition, contract_class)
|
|
99
99
|
scope = type_definition.scope || contract_class
|
|
100
|
-
|
|
101
100
|
type_shape = Object.new(scope, action_name: @shape.action_name)
|
|
102
101
|
type_shape.copy_type_definition_params(type_definition, type_shape)
|
|
103
102
|
type_shape
|
|
@@ -36,7 +36,6 @@ module Apiwork
|
|
|
36
36
|
issues = []
|
|
37
37
|
params = {}
|
|
38
38
|
data = data.deep_symbolize_keys if data.is_a?(Hash)
|
|
39
|
-
|
|
40
39
|
return max_depth_error(current_depth, max_depth, path) if current_depth > max_depth
|
|
41
40
|
|
|
42
41
|
@shape.params.each do |name, param_options|
|
|
@@ -54,7 +53,6 @@ module Apiwork
|
|
|
54
53
|
end
|
|
55
54
|
|
|
56
55
|
issues.concat(check_unknown_params(data, path))
|
|
57
|
-
|
|
58
56
|
Result.new(issues:, params:)
|
|
59
57
|
end
|
|
60
58
|
|
|
@@ -67,21 +65,19 @@ module Apiwork
|
|
|
67
65
|
path:,
|
|
68
66
|
meta: { max:, depth: current_depth },
|
|
69
67
|
)]
|
|
68
|
+
|
|
70
69
|
Result.new(issues:, params: {})
|
|
71
70
|
end
|
|
72
71
|
|
|
73
72
|
def validate_param(name, value, param_options, data, path, current_depth:, max_depth:)
|
|
74
73
|
field_path = path + [name]
|
|
75
|
-
|
|
76
74
|
required_error = validate_required(name, value, param_options, data, field_path)
|
|
77
75
|
return [[required_error], NOT_SET] if required_error
|
|
78
76
|
|
|
79
77
|
value = param_options[:default] if value.nil? && param_options.key?(:default)
|
|
80
|
-
|
|
81
78
|
nullable_error = validate_nullable(name, value, param_options, data, field_path)
|
|
82
79
|
return [[nullable_error], NOT_SET] if nullable_error
|
|
83
|
-
|
|
84
|
-
return [[], NOT_SET] if value.nil?
|
|
80
|
+
return [[], data.key?(name) ? nil : NOT_SET] if value.nil?
|
|
85
81
|
|
|
86
82
|
enum_error = validate_enum_value(name, value, param_options[:enum], field_path)
|
|
87
83
|
return [[enum_error], NOT_SET] if enum_error
|
|
@@ -128,7 +124,6 @@ module Apiwork
|
|
|
128
124
|
def validate_required(name, value, param_options, data, field_path)
|
|
129
125
|
return nil if param_options[:optional]
|
|
130
126
|
return nil if param_options[:nullable] && data.key?(name) && value.nil?
|
|
131
|
-
|
|
132
127
|
return nil unless value.nil?
|
|
133
128
|
|
|
134
129
|
if param_options[:enum].present?
|
|
@@ -193,6 +188,7 @@ module Apiwork
|
|
|
193
188
|
current_depth:,
|
|
194
189
|
max_depth:,
|
|
195
190
|
)
|
|
191
|
+
|
|
196
192
|
union_error ? [[union_error], NOT_SET] : [[], union_value]
|
|
197
193
|
end
|
|
198
194
|
|
|
@@ -210,12 +206,14 @@ module Apiwork
|
|
|
210
206
|
|
|
211
207
|
def validate_shape_object(value, nested_shape, field_path, max_depth, current_depth)
|
|
212
208
|
validator = Validator.new(normalize_shape(nested_shape))
|
|
209
|
+
|
|
213
210
|
shape_result = validator.validate(
|
|
214
211
|
value,
|
|
215
212
|
max_depth:,
|
|
216
213
|
current_depth: current_depth + 1,
|
|
217
214
|
path: field_path,
|
|
218
215
|
)
|
|
216
|
+
|
|
219
217
|
shape_result.invalid? ? [shape_result.issues, NOT_SET] : [[], shape_result.params]
|
|
220
218
|
end
|
|
221
219
|
|
|
@@ -229,13 +227,13 @@ module Apiwork
|
|
|
229
227
|
param_options:,
|
|
230
228
|
},
|
|
231
229
|
)
|
|
230
|
+
|
|
232
231
|
array_issues.empty? ? [[], array_values] : [array_issues, NOT_SET]
|
|
233
232
|
end
|
|
234
233
|
|
|
235
234
|
def validate_record_param(value, param_options, field_path, max_depth, current_depth)
|
|
236
235
|
issues = []
|
|
237
236
|
validated = {}
|
|
238
|
-
|
|
239
237
|
of = param_options[:of]
|
|
240
238
|
of_type = of&.type
|
|
241
239
|
of_shape = of&.shape
|
|
@@ -269,6 +267,7 @@ module Apiwork
|
|
|
269
267
|
|
|
270
268
|
def check_unknown_params(data, path)
|
|
271
269
|
extra_keys = data.keys - @shape.params.keys
|
|
270
|
+
|
|
272
271
|
extra_keys.map do |key|
|
|
273
272
|
Issue.new(
|
|
274
273
|
:field_unknown,
|
|
@@ -284,10 +283,8 @@ module Apiwork
|
|
|
284
283
|
field_path = options[:field_path]
|
|
285
284
|
max_depth = options[:max_depth]
|
|
286
285
|
current_depth = options[:current_depth]
|
|
287
|
-
|
|
288
286
|
issues = []
|
|
289
287
|
values = []
|
|
290
|
-
|
|
291
288
|
max = param_options[:max]
|
|
292
289
|
min = param_options[:min]
|
|
293
290
|
|
|
@@ -347,7 +344,6 @@ module Apiwork
|
|
|
347
344
|
def validate_array_item_with_type(item, index, param_options, item_path, current_depth, max_depth)
|
|
348
345
|
of = param_options[:of]
|
|
349
346
|
type_name = of&.type
|
|
350
|
-
|
|
351
347
|
type_definition = @shape.contract_class.resolve_custom_type(type_name)
|
|
352
348
|
|
|
353
349
|
if type_definition
|
|
@@ -385,7 +381,6 @@ module Apiwork
|
|
|
385
381
|
|
|
386
382
|
def validate_array_item_with_object_type(item, type_definition, item_path, current_depth, max_depth)
|
|
387
383
|
result = validate_with_type_definition(type_definition, item, item_path, current_depth:, max_depth:)
|
|
388
|
-
|
|
389
384
|
result.invalid? ? { issues: result.issues, value: nil } : { issues: [], value: result.params }
|
|
390
385
|
end
|
|
391
386
|
|
|
@@ -479,6 +474,7 @@ module Apiwork
|
|
|
479
474
|
return [most_specific_error, nil] if most_specific_error
|
|
480
475
|
|
|
481
476
|
expected_types = variants.map { |variant| variant[:type] }
|
|
477
|
+
|
|
482
478
|
error = Issue.new(
|
|
483
479
|
:type_invalid,
|
|
484
480
|
translate_detail(:type_invalid),
|
|
@@ -495,8 +491,8 @@ module Apiwork
|
|
|
495
491
|
|
|
496
492
|
def validate_discriminated_variant(name, value, variants, discriminator, path, current_depth:, max_depth:)
|
|
497
493
|
discriminator_value = value[discriminator]
|
|
498
|
-
|
|
499
494
|
normalized_discriminator = normalize_discriminator_value(discriminator_value)
|
|
495
|
+
|
|
500
496
|
matching_variant = variants.find do |variant|
|
|
501
497
|
normalize_discriminator_value(variant[:tag]) == normalized_discriminator
|
|
502
498
|
end
|
|
@@ -529,7 +525,6 @@ module Apiwork
|
|
|
529
525
|
)
|
|
530
526
|
|
|
531
527
|
validated_value = validated_value.merge(discriminator => discriminator_value) if validated_value.is_a?(Hash)
|
|
532
|
-
|
|
533
528
|
[error, validated_value]
|
|
534
529
|
end
|
|
535
530
|
|
|
@@ -545,8 +540,8 @@ module Apiwork
|
|
|
545
540
|
variant_type = variant[:type]
|
|
546
541
|
variant_of = variant[:of]
|
|
547
542
|
variant_shape = variant[:shape]
|
|
548
|
-
|
|
549
543
|
type_definition = @shape.contract_class.resolve_custom_type(variant_type)
|
|
544
|
+
|
|
550
545
|
if type_definition
|
|
551
546
|
return [build_type_invalid_error(name, value, variant_type, path), nil] unless value.is_a?(Hash)
|
|
552
547
|
|
|
@@ -642,11 +637,9 @@ module Apiwork
|
|
|
642
637
|
|
|
643
638
|
def validate_with_type_definition(type_definition, value, path, current_depth:, exclude_param: nil, max_depth:)
|
|
644
639
|
scope = type_definition.scope || @shape.contract_class
|
|
645
|
-
|
|
646
640
|
type_shape = Object.new(scope, action_name: @shape.action_name)
|
|
647
641
|
type_shape.copy_type_definition_params(type_definition, type_shape)
|
|
648
642
|
type_shape.params.delete(exclude_param) if exclude_param
|
|
649
|
-
|
|
650
643
|
Validator.new(type_shape).validate(value, max_depth:, path:, current_depth: current_depth + 1)
|
|
651
644
|
end
|
|
652
645
|
|
|
@@ -700,7 +693,6 @@ module Apiwork
|
|
|
700
693
|
|
|
701
694
|
def validate_string_length(name, value, param_options, field_path)
|
|
702
695
|
return nil unless value.is_a?(String)
|
|
703
|
-
|
|
704
696
|
return nil if value.empty?
|
|
705
697
|
|
|
706
698
|
min_length = param_options[:min]
|
|
@@ -745,9 +737,11 @@ module Apiwork
|
|
|
745
737
|
return shape if shape.is_a?(Contract::Object)
|
|
746
738
|
|
|
747
739
|
contract_shape = Object.new(@shape.contract_class, action_name: @shape.action_name)
|
|
740
|
+
|
|
748
741
|
shape.params.each do |name, param_options|
|
|
749
742
|
contract_shape.params[name] = param_options
|
|
750
743
|
end
|
|
744
|
+
|
|
751
745
|
contract_shape
|
|
752
746
|
end
|
|
753
747
|
|
|
@@ -29,6 +29,7 @@ module Apiwork
|
|
|
29
29
|
return @params if @merged.empty?
|
|
30
30
|
|
|
31
31
|
expanded = @params.dup
|
|
32
|
+
|
|
32
33
|
@merged.each do |type_name|
|
|
33
34
|
merged_type = @contract_class.api_class&.type_registry&.[](type_name)
|
|
34
35
|
next unless merged_type&.params
|
|
@@ -37,6 +38,7 @@ module Apiwork
|
|
|
37
38
|
expanded[name] ||= param_options
|
|
38
39
|
end
|
|
39
40
|
end
|
|
41
|
+
|
|
40
42
|
expanded
|
|
41
43
|
end
|
|
42
44
|
|
|
@@ -141,10 +143,8 @@ module Apiwork
|
|
|
141
143
|
raise ConfigurationError, 'discriminator can only be used with type: :union' if discriminator && type != :union
|
|
142
144
|
|
|
143
145
|
optional = normalize_optional(optional, default)
|
|
144
|
-
|
|
145
146
|
visited_types ||= @visited_types
|
|
146
147
|
visited_types ||= Set.new
|
|
147
|
-
|
|
148
148
|
resolved_enum = resolve_enum(enum)
|
|
149
149
|
|
|
150
150
|
case type
|
|
@@ -406,8 +406,8 @@ module Apiwork
|
|
|
406
406
|
value:,
|
|
407
407
|
type: :literal,
|
|
408
408
|
}.compact
|
|
409
|
-
params[:default] = default unless UNSET.equal?(default)
|
|
410
409
|
|
|
410
|
+
params[:default] = default unless UNSET.equal?(default)
|
|
411
411
|
@params[name] = (@params[name] || {}).merge(params)
|
|
412
412
|
end
|
|
413
413
|
|
|
@@ -427,8 +427,8 @@ module Apiwork
|
|
|
427
427
|
union:,
|
|
428
428
|
**options,
|
|
429
429
|
}.compact
|
|
430
|
-
params[:default] = default unless UNSET.equal?(default)
|
|
431
430
|
|
|
431
|
+
params[:default] = default unless UNSET.equal?(default)
|
|
432
432
|
@params[name] = (@params[name] || {}).merge(params)
|
|
433
433
|
end
|
|
434
434
|
|
|
@@ -493,7 +493,6 @@ module Apiwork
|
|
|
493
493
|
options:
|
|
494
494
|
)
|
|
495
495
|
scope = type_definition.scope || @contract_class
|
|
496
|
-
|
|
497
496
|
union = Union.new(scope, discriminator: type_definition.discriminator)
|
|
498
497
|
|
|
499
498
|
type_definition.variants.each do |variant|
|
|
@@ -527,8 +526,8 @@ module Apiwork
|
|
|
527
526
|
union:,
|
|
528
527
|
**options,
|
|
529
528
|
}.compact
|
|
530
|
-
params[:default] = default unless UNSET.equal?(default)
|
|
531
529
|
|
|
530
|
+
params[:default] = default unless UNSET.equal?(default)
|
|
532
531
|
@params[name] = (@params[name] || {}).merge(params)
|
|
533
532
|
end
|
|
534
533
|
|
|
@@ -570,8 +569,8 @@ module Apiwork
|
|
|
570
569
|
type: :object,
|
|
571
570
|
**options,
|
|
572
571
|
}.compact
|
|
573
|
-
params[:default] = default unless UNSET.equal?(default)
|
|
574
572
|
|
|
573
|
+
params[:default] = default unless UNSET.equal?(default)
|
|
575
574
|
@params[name] = (@params[name] || {}).merge(params)
|
|
576
575
|
end
|
|
577
576
|
|
|
@@ -588,10 +587,9 @@ module Apiwork
|
|
|
588
587
|
type:,
|
|
589
588
|
**options,
|
|
590
589
|
}.compact
|
|
591
|
-
params[:default] = default unless UNSET.equal?(default)
|
|
592
590
|
|
|
591
|
+
params[:default] = default unless UNSET.equal?(default)
|
|
593
592
|
@params[name] = (@params[name] || {}).merge(params)
|
|
594
|
-
|
|
595
593
|
@params[name][:shape] = resolved_shape if resolved_shape
|
|
596
594
|
end
|
|
597
595
|
|
|
@@ -16,7 +16,6 @@ module Apiwork
|
|
|
16
16
|
|
|
17
17
|
def parse(request, coerce: false)
|
|
18
18
|
request = coerce_request(request) if coerce
|
|
19
|
-
|
|
20
19
|
parsed_query, query_issues = parse_part(request.query, :query)
|
|
21
20
|
parsed_body, body_issues = parse_part(request.body, :body)
|
|
22
21
|
|
|
@@ -40,7 +39,6 @@ module Apiwork
|
|
|
40
39
|
return [data, []] unless shape
|
|
41
40
|
|
|
42
41
|
validated = shape.validate(data)
|
|
43
|
-
|
|
44
42
|
return [{}, validated.issues] if validated.invalid?
|
|
45
43
|
|
|
46
44
|
[shape.transform(shape.deserialize(validated.params)), []]
|
data/lib/apiwork/controller.rb
CHANGED
|
@@ -122,13 +122,14 @@ module Apiwork
|
|
|
122
122
|
# expose invoice, status: :created
|
|
123
123
|
# end
|
|
124
124
|
def expose(data, meta: {}, status: nil)
|
|
125
|
+
representation_class = contract_class.representation_class
|
|
126
|
+
|
|
125
127
|
if contract_class.actions[action_name.to_sym]&.response&.no_content?
|
|
128
|
+
adapter.process_no_content(data, representation_class, contract.request) if representation_class
|
|
126
129
|
head :no_content
|
|
127
130
|
return
|
|
128
131
|
end
|
|
129
132
|
|
|
130
|
-
representation_class = contract_class.representation_class
|
|
131
|
-
|
|
132
133
|
body = if representation_class
|
|
133
134
|
action = resource.actions[action_name.to_sym]
|
|
134
135
|
if action.collection?
|
|
@@ -149,7 +150,6 @@ module Apiwork
|
|
|
149
150
|
end
|
|
150
151
|
|
|
151
152
|
response = api_class.prepare_response(response)
|
|
152
|
-
|
|
153
153
|
render json: response.body, status: status || (action_name.to_sym == :create ? :created : :ok)
|
|
154
154
|
end
|
|
155
155
|
|
|
@@ -246,10 +246,8 @@ module Apiwork
|
|
|
246
246
|
def render_error(error)
|
|
247
247
|
representation_class = resource ? contract_class.representation_class : nil
|
|
248
248
|
body = adapter.process_error(error, representation_class, context:)
|
|
249
|
-
|
|
250
249
|
response = Response.new(body:)
|
|
251
250
|
response = api_class.prepare_error_response(response)
|
|
252
|
-
|
|
253
251
|
render json: response.body, status: error.status
|
|
254
252
|
end
|
|
255
253
|
|
|
@@ -288,7 +286,6 @@ module Apiwork
|
|
|
288
286
|
def raise_contract_not_found_error
|
|
289
287
|
resource_base = resource.name.to_s.singularize
|
|
290
288
|
namespaces = api_class.namespaces
|
|
291
|
-
|
|
292
289
|
contract_name = [*namespaces.map { |namespace| namespace.to_s.camelize }, "#{resource_base.camelize}Contract"].join('::')
|
|
293
290
|
contract_path = ['app/contracts', *namespaces, "#{resource_base}_contract.rb"].join('/')
|
|
294
291
|
|
data/lib/apiwork/engine.rb
CHANGED
|
@@ -7,7 +7,6 @@ module Apiwork
|
|
|
7
7
|
def register(key, attach_path: false, status:)
|
|
8
8
|
key = normalize_key(key)
|
|
9
9
|
status = Integer(status)
|
|
10
|
-
|
|
11
10
|
raise ArgumentError, "Status must be 400-599, got #{status}" unless (400..599).cover?(status)
|
|
12
11
|
|
|
13
12
|
store[key] = Definition.new(attach_path:, key:, status:)
|
|
@@ -80,7 +80,6 @@ module Apiwork
|
|
|
80
80
|
type_hashes = types.transform_values(&:to_h)
|
|
81
81
|
sorted = TypeAnalysis.topological_sort_types(type_hashes)
|
|
82
82
|
recursive = TypeAnalysis.cycle_breaking_types(type_hashes)
|
|
83
|
-
|
|
84
83
|
sorted.map { |name, _| serialize_type(name, types[name], recursive: recursive.include?(name)) }
|
|
85
84
|
end
|
|
86
85
|
|
data/lib/apiwork/export/base.rb
CHANGED
|
@@ -95,7 +95,6 @@ module Apiwork
|
|
|
95
95
|
|
|
96
96
|
def generate(api_base_path, format: nil, **options)
|
|
97
97
|
format ||= :json
|
|
98
|
-
|
|
99
98
|
raise ArgumentError, "#{export_name} export does not support #{format} format" if hash_output? && !supports_format?(format)
|
|
100
99
|
|
|
101
100
|
export = new(api_base_path, **options)
|
|
@@ -158,12 +157,14 @@ module Apiwork
|
|
|
158
157
|
return nil unless value.is_a?(Hash)
|
|
159
158
|
|
|
160
159
|
nested = {}
|
|
160
|
+
|
|
161
161
|
option.children.each do |child_name, child_option|
|
|
162
162
|
child_value = value[child_name] || value[child_name.to_s]
|
|
163
163
|
next if child_value.nil?
|
|
164
164
|
|
|
165
165
|
nested[child_name] = child_option.cast(child_value)
|
|
166
166
|
end
|
|
167
|
+
|
|
167
168
|
nested
|
|
168
169
|
end
|
|
169
170
|
|
|
@@ -216,7 +217,6 @@ module Apiwork
|
|
|
216
217
|
@options = self.class.default_options.merge(api_config).merge(all_options)
|
|
217
218
|
@options[:key_format] ||= @api_class.key_format || :keep
|
|
218
219
|
validate_options!
|
|
219
|
-
|
|
220
220
|
@api = @api_class.introspect(locale: @options[:locale])
|
|
221
221
|
end
|
|
222
222
|
|
|
@@ -250,7 +250,6 @@ module Apiwork
|
|
|
250
250
|
# @see #key_format
|
|
251
251
|
def transform_key(key)
|
|
252
252
|
key_string = key.to_s
|
|
253
|
-
|
|
254
253
|
return key_string if key_string.match?(/\A[A-Z]+\z/)
|
|
255
254
|
|
|
256
255
|
case key_format
|
|
@@ -6,6 +6,7 @@ module Apiwork
|
|
|
6
6
|
KEY_ORDER = %i[openapi info servers paths components].freeze
|
|
7
7
|
|
|
8
8
|
export_name :openapi
|
|
9
|
+
|
|
9
10
|
output :hash
|
|
10
11
|
|
|
11
12
|
option :version, default: '3.1.0', enum: %w[3.1.0], type: :string
|
|
@@ -26,6 +27,7 @@ module Apiwork
|
|
|
26
27
|
return { title: api_base_path, version: '1.0.0' } unless api.info
|
|
27
28
|
|
|
28
29
|
info = api.info
|
|
30
|
+
|
|
29
31
|
{
|
|
30
32
|
contact: build_contact(info.contact),
|
|
31
33
|
description: info.description,
|
|
@@ -100,8 +102,8 @@ module Apiwork
|
|
|
100
102
|
}
|
|
101
103
|
|
|
102
104
|
path_params = extract_path_parameters(action.path)
|
|
103
|
-
|
|
104
105
|
request = action.request
|
|
106
|
+
|
|
105
107
|
if request
|
|
106
108
|
query_params = request.query? ? build_query_parameters(request.query) : []
|
|
107
109
|
all_params = path_params + query_params
|
|
@@ -116,7 +118,6 @@ module Apiwork
|
|
|
116
118
|
end
|
|
117
119
|
|
|
118
120
|
operation[:responses] = build_responses(action_name, action.response, raises: action.raises)
|
|
119
|
-
|
|
120
121
|
operation.compact
|
|
121
122
|
end
|
|
122
123
|
|
|
@@ -203,7 +204,6 @@ module Apiwork
|
|
|
203
204
|
|
|
204
205
|
def build_responses(action_name, response, raises: [])
|
|
205
206
|
responses = {}
|
|
206
|
-
|
|
207
207
|
response_description = response.description || ''
|
|
208
208
|
|
|
209
209
|
if response.no_content?
|
|
@@ -317,15 +317,11 @@ module Apiwork
|
|
|
317
317
|
end
|
|
318
318
|
|
|
319
319
|
schema = map_param(param)
|
|
320
|
-
|
|
321
320
|
schema[:description] = param.description if param.description
|
|
322
321
|
schema[:example] = param.example if param.concrete? && param.example
|
|
323
322
|
schema[:deprecated] = true if param.deprecated?
|
|
324
|
-
|
|
325
323
|
schema[:format] = param.format.to_s if param.formattable? && param.format
|
|
326
|
-
|
|
327
324
|
schema[:default] = param.default if param.respond_to?(:default) && param.default?
|
|
328
|
-
|
|
329
325
|
apply_nullable(schema, param.nullable?)
|
|
330
326
|
end
|
|
331
327
|
|
|
@@ -372,9 +368,7 @@ module Apiwork
|
|
|
372
368
|
|
|
373
369
|
def map_array(param)
|
|
374
370
|
items_param = param.of
|
|
375
|
-
|
|
376
371
|
return { items: map_inline_object(param.shape), type: 'array' } if items_param.nil? && param.shape.any?
|
|
377
|
-
|
|
378
372
|
return { items: {}, type: 'array' } unless items_param
|
|
379
373
|
|
|
380
374
|
{
|
|
@@ -446,6 +440,7 @@ module Apiwork
|
|
|
446
440
|
end
|
|
447
441
|
|
|
448
442
|
mapping = {}
|
|
443
|
+
|
|
449
444
|
variants.each do |variant|
|
|
450
445
|
tag = variant.tag
|
|
451
446
|
next unless tag
|
|
@@ -483,11 +478,9 @@ module Apiwork
|
|
|
483
478
|
return {} if param.unknown?
|
|
484
479
|
|
|
485
480
|
type_value = openapi_type(param.type)
|
|
486
|
-
|
|
487
481
|
return {} if type_value.nil?
|
|
488
482
|
|
|
489
483
|
result = { type: type_value }
|
|
490
|
-
|
|
491
484
|
format_value = openapi_format(param.type)
|
|
492
485
|
result[:format] = format_value if format_value
|
|
493
486
|
|
|
@@ -17,10 +17,8 @@ module Apiwork
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
api_classes = api_base_path ? [find_api_class(api_base_path)] : API.values
|
|
20
|
-
|
|
21
20
|
start_time = Time.zone.now
|
|
22
21
|
count = 0
|
|
23
|
-
|
|
24
22
|
Rails.logger.debug 'Generating artifacts...'
|
|
25
23
|
|
|
26
24
|
api_classes.each do |api_class|
|
|
@@ -40,7 +38,6 @@ module Apiwork
|
|
|
40
38
|
end
|
|
41
39
|
|
|
42
40
|
Rails.logger.debug "\nGenerated #{count} file#{count == 1 ? '' : 's'} in #{(Time.zone.now - start_time).round(2)}s"
|
|
43
|
-
|
|
44
41
|
count
|
|
45
42
|
end
|
|
46
43
|
|
|
@@ -58,11 +55,9 @@ module Apiwork
|
|
|
58
55
|
|
|
59
56
|
def generate_file(api_class:, export_name:, format:, key_format:, locale:, output:, version:)
|
|
60
57
|
api_base_path = api_class.base_path
|
|
61
|
-
|
|
62
58
|
options = { format:, key_format:, locale:, version: }.compact
|
|
63
59
|
options_label = options.any? ? " (#{options.map { |key, value| "#{key}: #{value}" }.join(', ')})" : ''
|
|
64
60
|
Rails.logger.debug " ✓ #{api_base_path} to #{export_name}#{options_label}"
|
|
65
|
-
|
|
66
61
|
content = generate(export_name, api_base_path, format:, key_format:, locale:, version:)
|
|
67
62
|
export_class = Registry.find!(export_name)
|
|
68
63
|
extension = export_class.file_extension_for(format:)
|