apiwork 0.7.0 → 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.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -2
  3. data/app/controllers/apiwork/exports_controller.rb +0 -2
  4. data/lib/apiwork/adapter/base.rb +10 -2
  5. data/lib/apiwork/adapter/capability/base.rb +2 -0
  6. data/lib/apiwork/adapter/capability/contract/scope.rb +0 -1
  7. data/lib/apiwork/adapter/capability/operation/base.rb +1 -0
  8. data/lib/apiwork/adapter/capability/runner.rb +0 -1
  9. data/lib/apiwork/adapter/serializer/resource/base.rb +0 -1
  10. data/lib/apiwork/adapter/serializer/resource/default/contract_builder.rb +1 -2
  11. data/lib/apiwork/adapter/standard/capability/filtering/constants.rb +0 -3
  12. data/lib/apiwork/adapter/standard/capability/filtering/operation/filter.rb +0 -16
  13. data/lib/apiwork/adapter/standard/capability/filtering.rb +3 -0
  14. data/lib/apiwork/adapter/standard/capability/including/operation.rb +0 -1
  15. data/lib/apiwork/adapter/standard/capability/including.rb +1 -0
  16. data/lib/apiwork/adapter/standard/capability/pagination/operation/paginate/cursor.rb +0 -1
  17. data/lib/apiwork/adapter/standard/capability/pagination.rb +2 -0
  18. data/lib/apiwork/adapter/standard/capability/sorting.rb +2 -0
  19. data/lib/apiwork/adapter/standard/capability/writing/contract_builder.rb +0 -4
  20. data/lib/apiwork/adapter/standard/capability/writing/operation.rb +1 -0
  21. data/lib/apiwork/adapter/standard/capability/writing.rb +2 -0
  22. data/lib/apiwork/adapter/standard.rb +3 -0
  23. data/lib/apiwork/adapter/wrapper/collection/default.rb +1 -0
  24. data/lib/apiwork/adapter/wrapper/member/default.rb +2 -0
  25. data/lib/apiwork/api/base.rb +5 -7
  26. data/lib/apiwork/api/object.rb +8 -7
  27. data/lib/apiwork/api/resource.rb +10 -10
  28. data/lib/apiwork/api/router.rb +0 -1
  29. data/lib/apiwork/api/type_registry.rb +1 -0
  30. data/lib/apiwork/configuration/option.rb +0 -1
  31. data/lib/apiwork/configuration/validatable.rb +1 -0
  32. data/lib/apiwork/configuration.rb +1 -0
  33. data/lib/apiwork/contract/action/request.rb +4 -0
  34. data/lib/apiwork/contract/action/response.rb +1 -0
  35. data/lib/apiwork/contract/action.rb +2 -2
  36. data/lib/apiwork/contract/base.rb +3 -3
  37. data/lib/apiwork/contract/object/coercer.rb +0 -2
  38. data/lib/apiwork/contract/object/deserializer.rb +0 -1
  39. data/lib/apiwork/contract/object/transformer.rb +0 -1
  40. data/lib/apiwork/contract/object/validator.rb +13 -19
  41. data/lib/apiwork/contract/object.rb +14 -14
  42. data/lib/apiwork/contract/request_parser.rb +0 -2
  43. data/lib/apiwork/controller.rb +3 -6
  44. data/lib/apiwork/engine.rb +1 -0
  45. data/lib/apiwork/error_code/registry.rb +0 -1
  46. data/lib/apiwork/export/apiwork.rb +1 -0
  47. data/lib/apiwork/export/apiwork_mapper.rb +0 -1
  48. data/lib/apiwork/export/base.rb +2 -3
  49. data/lib/apiwork/export/open_api.rb +4 -11
  50. data/lib/apiwork/export/pipeline/writer.rb +1 -1
  51. data/lib/apiwork/export/pipeline.rb +0 -5
  52. data/lib/apiwork/export/sorbus.rb +2 -0
  53. data/lib/apiwork/export/sorbus_mapper.rb +1 -5
  54. data/lib/apiwork/export/surface_resolver.rb +6 -4
  55. data/lib/apiwork/export/type_analysis.rb +2 -1
  56. data/lib/apiwork/export/type_script.rb +2 -0
  57. data/lib/apiwork/export/type_script_mapper.rb +0 -8
  58. data/lib/apiwork/export/zod.rb +4 -2
  59. data/lib/apiwork/export/zod_mapper.rb +0 -8
  60. data/lib/apiwork/introspection/dump/action.rb +1 -1
  61. data/lib/apiwork/introspection/dump/api.rb +0 -4
  62. data/lib/apiwork/introspection/dump/contract.rb +0 -6
  63. data/lib/apiwork/introspection/dump/param.rb +6 -6
  64. data/lib/apiwork/introspection/dump/resource.rb +1 -0
  65. data/lib/apiwork/introspection/dump/type.rb +5 -5
  66. data/lib/apiwork/object.rb +12 -4
  67. data/lib/apiwork/reference_generator.rb +10 -10
  68. data/lib/apiwork/registry.rb +1 -0
  69. data/lib/apiwork/representation/association.rb +3 -3
  70. data/lib/apiwork/representation/attribute.rb +1 -5
  71. data/lib/apiwork/representation/base.rb +2 -5
  72. data/lib/apiwork/representation/deserializer.rb +0 -1
  73. data/lib/apiwork/representation/inheritance.rb +22 -10
  74. data/lib/apiwork/representation/model_detector.rb +1 -0
  75. data/lib/apiwork/representation/serializer.rb +3 -2
  76. data/lib/apiwork/version.rb +1 -1
  77. data/lib/apiwork.rb +9 -8
  78. metadata +4 -18
@@ -62,7 +62,6 @@ module Apiwork
62
62
  @param = param
63
63
  @path = path
64
64
  @singular = singular
65
-
66
65
  @crud_actions = name ? determine_crud_actions(singular, except:, only:) : []
67
66
  @custom_actions = []
68
67
  @resources = {}
@@ -119,6 +118,7 @@ module Apiwork
119
118
 
120
119
  def find_resource_for_path(resource_path)
121
120
  current = nil
121
+
122
122
  resource_path.split('/').each do |part|
123
123
  next if part.empty?
124
124
 
@@ -129,6 +129,7 @@ module Apiwork
129
129
 
130
130
  current = found
131
131
  end
132
+
132
133
  current
133
134
  end
134
135
 
@@ -213,11 +214,11 @@ module Apiwork
213
214
  param:,
214
215
  path:,
215
216
  }.compact
216
- build_resource(resource_name, options:, singular: false)
217
217
 
218
+ build_resource(resource_name, options:, singular: false)
218
219
  @resource_stack.push(resource_name)
219
-
220
220
  self.concerns(*concerns) if concerns
221
+
221
222
  if block
222
223
  block.arity.positive? ? yield(self) : instance_eval(&block)
223
224
  end
@@ -286,11 +287,11 @@ module Apiwork
286
287
  param:,
287
288
  path:,
288
289
  }.compact
289
- build_resource(resource_name, options:, singular: true)
290
290
 
291
+ build_resource(resource_name, options:, singular: true)
291
292
  @resource_stack.push(resource_name)
292
-
293
293
  self.concerns(*concerns) if concerns
294
+
294
295
  if block
295
296
  block.arity.positive? ? yield(self) : instance_eval(&block)
296
297
  end
@@ -321,9 +322,7 @@ module Apiwork
321
322
  def with_options(options = {}, &block)
322
323
  old_options = @current_options
323
324
  @current_options = merged_options(options)
324
-
325
325
  block.arity.positive? ? yield(self) : instance_eval(&block)
326
-
327
326
  @current_options = old_options
328
327
  end
329
328
 
@@ -489,6 +488,7 @@ module Apiwork
489
488
  resource.instance_exec(options, &block)
490
489
  end
491
490
  end
491
+
492
492
  @concerns[concern_name] = callable
493
493
  end
494
494
 
@@ -518,10 +518,12 @@ module Apiwork
518
518
 
519
519
  def collect_all_representation_classes
520
520
  representation_classes = []
521
+
521
522
  each_resource do |resource|
522
523
  representation_class = resource.representation_class
523
524
  representation_classes << representation_class if representation_class
524
525
  end
526
+
525
527
  representation_classes
526
528
  end
527
529
 
@@ -542,10 +544,8 @@ module Apiwork
542
544
 
543
545
  def build_resource(resource_name, options:, singular:)
544
546
  merged = merged_options(options)
545
-
546
547
  parent_name = @resource_stack.last
547
548
  parent_resource = parent_name ? find_resource(parent_name) : nil
548
-
549
549
  contract = merged.delete(:contract)
550
550
 
551
551
  resource = Resource.new(
@@ -608,6 +608,7 @@ module Apiwork
608
608
 
609
609
  def contract_path_to_class_name(contract_path)
610
610
  namespaces = @api_class.namespaces
611
+
611
612
  parts = if contract_path.start_with?('/')
612
613
  contract_path[1..].split('/')
613
614
  else
@@ -616,7 +617,6 @@ module Apiwork
616
617
 
617
618
  parts = parts.map { |part| part.to_s.camelize }
618
619
  parts[-1] = parts[-1].singularize
619
-
620
620
  "#{parts.join('::')}Contract"
621
621
  end
622
622
 
@@ -12,7 +12,6 @@ module Apiwork
12
12
  def draw
13
13
  api_classes = Registry.values
14
14
  router = self
15
-
16
15
  set = ActionDispatch::Routing::RouteSet.new
17
16
 
18
17
  set.draw do
@@ -87,6 +87,7 @@ module Apiwork
87
87
 
88
88
  def merge(key, block:, deprecated:, description:, example:)
89
89
  definition = @store[key]
90
+
90
91
  @store[key] = definition.merge(
91
92
  block:,
92
93
  deprecated:,
@@ -38,7 +38,6 @@ module Apiwork
38
38
  @default = default
39
39
  @enum = enum
40
40
  @children = children || {}
41
-
42
41
  return unless block && type == :hash
43
42
 
44
43
  block.arity.positive? ? yield(self) : instance_eval(&block)
@@ -13,6 +13,7 @@ module Apiwork
13
13
  when :integer then value.is_a?(Integer)
14
14
  when :hash then value.is_a?(Hash)
15
15
  end
16
+
16
17
  raise ConfigurationError, "#{name} must be #{type}, got #{value.class}" unless valid
17
18
  end
18
19
 
@@ -37,6 +37,7 @@ module Apiwork
37
37
  end
38
38
 
39
39
  value = args.first
40
+
40
41
  if block && option.nested?
41
42
  @storage[name] ||= {}
42
43
  nested = Configuration.new(option, @storage[name])
@@ -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
@@ -98,6 +98,7 @@ module Apiwork
98
98
  )
99
99
  block.arity.positive? ? yield(@body) : @body.instance_eval(&block)
100
100
  end
101
+
101
102
  @body
102
103
  end
103
104
  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|
@@ -78,7 +78,6 @@ module Apiwork
78
78
  return hash unless type_definition
79
79
 
80
80
  representation_class = (type_definition.scope || @shape.contract_class).representation_class
81
-
82
81
  return representation_class.deserialize(hash) if representation_class
83
82
 
84
83
  hash
@@ -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
- value = param_options[:default] if value.nil? && param_options[:default]
80
-
77
+ value = param_options[:default] if value.nil? && param_options.key?(:default)
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
 
@@ -61,7 +63,7 @@ module Apiwork
61
63
  # @param as [Symbol, nil] (nil)
62
64
  # The target attribute name.
63
65
  # @param default [Object] (UNSET)
64
- # The default value. Omit to declare no default. Pass `nil` for an explicit null default.
66
+ # The default value. Omit to declare no default. Pass `nil` for an explicit null default. A param with a default is always optional.
65
67
  # @param deprecated [Boolean] (false)
66
68
  # Whether deprecated. Metadata included in exports.
67
69
  # @param description [String, nil] (nil)
@@ -85,7 +87,7 @@ module Apiwork
85
87
  # @param of [Symbol, Hash, nil] (nil)
86
88
  # The element or value type. Arrays and records only.
87
89
  # @param optional [Boolean] (false)
88
- # Whether the param is optional.
90
+ # Whether the param is optional. Always `true` for params with a `default`.
89
91
  # @param required [Boolean] (false)
90
92
  # Whether the param is required.
91
93
  # @param shape [Contract::Object, Contract::Union, nil] (nil)
@@ -140,9 +142,9 @@ module Apiwork
140
142
 
141
143
  raise ConfigurationError, 'discriminator can only be used with type: :union' if discriminator && type != :union
142
144
 
145
+ optional = normalize_optional(optional, default)
143
146
  visited_types ||= @visited_types
144
147
  visited_types ||= Set.new
145
-
146
148
  resolved_enum = resolve_enum(enum)
147
149
 
148
150
  case type
@@ -184,7 +186,7 @@ module Apiwork
184
186
  # @param as [Symbol, nil] (nil)
185
187
  # The target attribute name.
186
188
  # @param default [Object] (UNSET)
187
- # The default value. Omit to declare no default. Pass `nil` for an explicit null default.
189
+ # The default value. Omit to declare no default. Pass `nil` for an explicit null default. A param with a default is always optional.
188
190
  # @param deprecated [Boolean] (false)
189
191
  # Whether deprecated. Metadata included in exports.
190
192
  # @param description [String, nil] (nil)
@@ -196,7 +198,7 @@ module Apiwork
196
198
  # @param nullable [Boolean] (false)
197
199
  # Whether the value can be `null`.
198
200
  # @param optional [Boolean] (false)
199
- # Whether the param is optional.
201
+ # Whether the param is optional. Always `true` for params with a `default`.
200
202
  # @param required [Boolean] (false)
201
203
  # Whether the param is required.
202
204
  # @yield block for defining element type
@@ -255,7 +257,7 @@ module Apiwork
255
257
  # @param as [Symbol, nil] (nil)
256
258
  # The target attribute name.
257
259
  # @param default [Object] (UNSET)
258
- # The default value. Omit to declare no default. Pass `nil` for an explicit null default.
260
+ # The default value. Omit to declare no default. Pass `nil` for an explicit null default. A param with a default is always optional.
259
261
  # @param deprecated [Boolean] (false)
260
262
  # Whether deprecated. Metadata included in exports.
261
263
  # @param description [String, nil] (nil)
@@ -263,7 +265,7 @@ module Apiwork
263
265
  # @param nullable [Boolean] (false)
264
266
  # Whether the value can be `null`.
265
267
  # @param optional [Boolean] (false)
266
- # Whether the param is optional.
268
+ # Whether the param is optional. Always `true` for params with a `default`.
267
269
  # @param required [Boolean] (false)
268
270
  # Whether the param is required.
269
271
  # @yield block for defining value type
@@ -404,8 +406,8 @@ module Apiwork
404
406
  value:,
405
407
  type: :literal,
406
408
  }.compact
407
- params[:default] = default unless UNSET.equal?(default)
408
409
 
410
+ params[:default] = default unless UNSET.equal?(default)
409
411
  @params[name] = (@params[name] || {}).merge(params)
410
412
  end
411
413
 
@@ -425,8 +427,8 @@ module Apiwork
425
427
  union:,
426
428
  **options,
427
429
  }.compact
428
- params[:default] = default unless UNSET.equal?(default)
429
430
 
431
+ params[:default] = default unless UNSET.equal?(default)
430
432
  @params[name] = (@params[name] || {}).merge(params)
431
433
  end
432
434
 
@@ -491,7 +493,6 @@ module Apiwork
491
493
  options:
492
494
  )
493
495
  scope = type_definition.scope || @contract_class
494
-
495
496
  union = Union.new(scope, discriminator: type_definition.discriminator)
496
497
 
497
498
  type_definition.variants.each do |variant|
@@ -525,8 +526,8 @@ module Apiwork
525
526
  union:,
526
527
  **options,
527
528
  }.compact
528
- params[:default] = default unless UNSET.equal?(default)
529
529
 
530
+ params[:default] = default unless UNSET.equal?(default)
530
531
  @params[name] = (@params[name] || {}).merge(params)
531
532
  end
532
533
 
@@ -568,8 +569,8 @@ module Apiwork
568
569
  type: :object,
569
570
  **options,
570
571
  }.compact
571
- params[:default] = default unless UNSET.equal?(default)
572
572
 
573
+ params[:default] = default unless UNSET.equal?(default)
573
574
  @params[name] = (@params[name] || {}).merge(params)
574
575
  end
575
576
 
@@ -586,10 +587,9 @@ module Apiwork
586
587
  type:,
587
588
  **options,
588
589
  }.compact
589
- params[:default] = default unless UNSET.equal?(default)
590
590
 
591
+ params[:default] = default unless UNSET.equal?(default)
591
592
  @params[name] = (@params[name] || {}).merge(params)
592
-
593
593
  @params[name][:shape] = resolved_shape if resolved_shape
594
594
  end
595
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)), []]
@@ -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
 
@@ -3,6 +3,7 @@
3
3
  module Apiwork
4
4
  class Engine < ::Rails::Engine
5
5
  isolate_namespace Apiwork
6
+
6
7
  engine_name 'apiwork'
7
8
 
8
9
  rake_tasks do