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.
- checksums.yaml +4 -4
- data/README.md +7 -2
- 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 +8 -7
- 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 +13 -19
- data/lib/apiwork/contract/object.rb +14 -14
- 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/object.rb +12 -4
- 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 +1 -5
- 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 -18
|
@@ -76,12 +76,12 @@ module Apiwork
|
|
|
76
76
|
return {} unless type_definition.params
|
|
77
77
|
|
|
78
78
|
result = {}
|
|
79
|
-
|
|
80
79
|
expand_merged_types(type_definition, result)
|
|
81
80
|
|
|
82
81
|
type_definition.params.sort_by { |name, _| name.to_s }.each do |name, param_options|
|
|
83
82
|
result[name] = build_param(name, param_options, type_definition.scope)
|
|
84
83
|
end
|
|
84
|
+
|
|
85
85
|
result
|
|
86
86
|
end
|
|
87
87
|
|
|
@@ -135,6 +135,7 @@ module Apiwork
|
|
|
135
135
|
value: options[:type] == :literal ? options[:value] : nil,
|
|
136
136
|
variants: build_nested_variants(options[:shape]),
|
|
137
137
|
}
|
|
138
|
+
|
|
138
139
|
result[:default] = options[:default] if options.key?(:default)
|
|
139
140
|
result
|
|
140
141
|
end
|
|
@@ -171,9 +172,11 @@ module Apiwork
|
|
|
171
172
|
return {} unless shape.respond_to?(:params)
|
|
172
173
|
|
|
173
174
|
result = {}
|
|
175
|
+
|
|
174
176
|
shape.params.sort_by { |name, _| name.to_s }.each do |name, param_options|
|
|
175
177
|
result[name] = build_param(name, param_options, nil)
|
|
176
178
|
end
|
|
179
|
+
|
|
177
180
|
result
|
|
178
181
|
end
|
|
179
182
|
|
|
@@ -240,6 +243,7 @@ module Apiwork
|
|
|
240
243
|
value: nil,
|
|
241
244
|
variants: union_shape ? union_shape.variants.map { |variant| build_variant(variant, scope) } : [],
|
|
242
245
|
}
|
|
246
|
+
|
|
243
247
|
result[:of] = resolve_of({ of: of.inner }, scope) if of.type == :array && of.inner
|
|
244
248
|
result
|
|
245
249
|
end
|
|
@@ -301,14 +305,12 @@ module Apiwork
|
|
|
301
305
|
def i18n_attribute_description(attribute)
|
|
302
306
|
representation_name = attribute.representation_class_name
|
|
303
307
|
attribute_name = attribute.name
|
|
304
|
-
|
|
305
308
|
@api_class.translate(:representations, representation_name, :attributes, attribute_name, :description)
|
|
306
309
|
end
|
|
307
310
|
|
|
308
311
|
def i18n_association_description(association)
|
|
309
312
|
representation_name = association.representation_class_name
|
|
310
313
|
association_name = association.name
|
|
311
|
-
|
|
312
314
|
@api_class.translate(:representations, representation_name, :associations, association_name, :description)
|
|
313
315
|
end
|
|
314
316
|
|
|
@@ -333,9 +335,7 @@ module Apiwork
|
|
|
333
335
|
def resolve_scoped_type_name(type_name, scope)
|
|
334
336
|
return nil unless type_name.is_a?(Symbol)
|
|
335
337
|
return nil unless @api_class
|
|
336
|
-
|
|
337
338
|
return type_name if @api_class.type_registry.key?(type_name) || @api_class.enum_registry.key?(type_name)
|
|
338
|
-
|
|
339
339
|
return nil unless scope
|
|
340
340
|
|
|
341
341
|
scoped_name = @api_class.scoped_type_name(scope, type_name)
|
data/lib/apiwork/object.rb
CHANGED
|
@@ -157,8 +157,8 @@ module Apiwork
|
|
|
157
157
|
# Whether the param is required.
|
|
158
158
|
# @return [void]
|
|
159
159
|
#
|
|
160
|
-
# @example Optional string
|
|
161
|
-
# string? :nickname
|
|
160
|
+
# @example Optional string
|
|
161
|
+
# string? :nickname
|
|
162
162
|
def string?(
|
|
163
163
|
name,
|
|
164
164
|
as: nil,
|
|
@@ -284,7 +284,7 @@ module Apiwork
|
|
|
284
284
|
# @return [void]
|
|
285
285
|
#
|
|
286
286
|
# @example Optional page number
|
|
287
|
-
# integer? :page, min: 1
|
|
287
|
+
# integer? :page, min: 1
|
|
288
288
|
def integer?(
|
|
289
289
|
name,
|
|
290
290
|
as: nil,
|
|
@@ -619,7 +619,7 @@ module Apiwork
|
|
|
619
619
|
# @return [void]
|
|
620
620
|
#
|
|
621
621
|
# @example Optional notification flag
|
|
622
|
-
# boolean? :notify
|
|
622
|
+
# boolean? :notify
|
|
623
623
|
def boolean?(
|
|
624
624
|
name,
|
|
625
625
|
as: nil,
|
|
@@ -1864,5 +1864,13 @@ module Apiwork
|
|
|
1864
1864
|
def param(name, type: nil, **options, &block)
|
|
1865
1865
|
raise NotImplementedError, "#{self.class} must implement #param"
|
|
1866
1866
|
end
|
|
1867
|
+
|
|
1868
|
+
private
|
|
1869
|
+
|
|
1870
|
+
def normalize_optional(optional, default)
|
|
1871
|
+
return true unless UNSET.equal?(default)
|
|
1872
|
+
|
|
1873
|
+
optional
|
|
1874
|
+
end
|
|
1867
1875
|
end
|
|
1868
1876
|
end
|
|
@@ -9,6 +9,7 @@ module Apiwork
|
|
|
9
9
|
GEM_ROOT = File.expand_path('../..', __dir__)
|
|
10
10
|
OUTPUT_DIR = File.join(GEM_ROOT, 'docs/reference')
|
|
11
11
|
GITHUB_URL = 'https://github.com/skiftle/apiwork/blob/main'
|
|
12
|
+
|
|
12
13
|
RUBY_PRIMITIVES = %w[
|
|
13
14
|
String Integer Float Boolean Symbol Hash Array Object
|
|
14
15
|
TrueClass FalseClass NilClass Numeric Proc
|
|
@@ -22,7 +23,6 @@ module Apiwork
|
|
|
22
23
|
|
|
23
24
|
def generate
|
|
24
25
|
require 'yard'
|
|
25
|
-
|
|
26
26
|
parse_source
|
|
27
27
|
modules = extract_modules
|
|
28
28
|
write_files(modules)
|
|
@@ -82,7 +82,6 @@ module Apiwork
|
|
|
82
82
|
|
|
83
83
|
has_own_docstring = !yard_object.docstring.to_s.strip.empty?
|
|
84
84
|
return true if has_own_docstring && api_tag
|
|
85
|
-
|
|
86
85
|
return false unless yard_object.file
|
|
87
86
|
|
|
88
87
|
lines = File.readlines(yard_object.file)
|
|
@@ -93,8 +92,8 @@ module Apiwork
|
|
|
93
92
|
else
|
|
94
93
|
[yard_object.line - 5, 0].max
|
|
95
94
|
end
|
|
96
|
-
end_line = yard_object.line
|
|
97
95
|
|
|
96
|
+
end_line = yard_object.line
|
|
98
97
|
preceding_lines = lines[start_line...end_line].join
|
|
99
98
|
preceding_lines.include?('@api public')
|
|
100
99
|
end
|
|
@@ -167,6 +166,7 @@ module Apiwork
|
|
|
167
166
|
|
|
168
167
|
def build_signature(method)
|
|
169
168
|
params = method.parameters.reject { |name, _default| name.to_s.start_with?('internal') }
|
|
169
|
+
|
|
170
170
|
params = params.map do |name, default|
|
|
171
171
|
if name.to_s.end_with?(':')
|
|
172
172
|
default ? "#{name} #{default}" : name.to_s
|
|
@@ -216,6 +216,7 @@ module Apiwork
|
|
|
216
216
|
return { default: nil, description: nil, values: nil, values_type: nil } if text.blank?
|
|
217
217
|
|
|
218
218
|
match = text.match(/\A(?:\(([^)]+)\))?\s*(?:\[(?:(\w+):\s*)?([^\]]+)\])?\s*(.*)\z/m)
|
|
219
|
+
|
|
219
220
|
{
|
|
220
221
|
default: match[1],
|
|
221
222
|
description: match[4].presence,
|
|
@@ -257,8 +258,8 @@ module Apiwork
|
|
|
257
258
|
cleanup_old_files
|
|
258
259
|
@modules = modules
|
|
259
260
|
@modules_with_children = build_modules_with_children(modules)
|
|
260
|
-
|
|
261
261
|
write_root_index
|
|
262
|
+
|
|
262
263
|
modules.each.with_index(1) do |mod, order|
|
|
263
264
|
filepath = module_filepath(mod[:path])
|
|
264
265
|
FileUtils.mkdir_p(File.dirname(filepath))
|
|
@@ -288,7 +289,6 @@ module Apiwork
|
|
|
288
289
|
end
|
|
289
290
|
|
|
290
291
|
parts << "#{linkify_yard_refs(constant[:docstring])}\n" if constant[:docstring].present?
|
|
291
|
-
|
|
292
292
|
parts.join("\n")
|
|
293
293
|
end
|
|
294
294
|
|
|
@@ -376,6 +376,7 @@ module Apiwork
|
|
|
376
376
|
link = folder ? "./#{child_path}/" : "./#{child_path}"
|
|
377
377
|
parts << "- [#{child}](#{link})"
|
|
378
378
|
end
|
|
379
|
+
|
|
379
380
|
parts << ''
|
|
380
381
|
end
|
|
381
382
|
|
|
@@ -391,6 +392,7 @@ module Apiwork
|
|
|
391
392
|
|
|
392
393
|
def build_modules_with_children(modules)
|
|
393
394
|
paths = modules.map { |m| m[:path] }.to_set
|
|
395
|
+
|
|
394
396
|
paths.select do |path|
|
|
395
397
|
paths.any? { |other| other != path && other.start_with?("#{path}::") }
|
|
396
398
|
end.to_set
|
|
@@ -433,6 +435,7 @@ module Apiwork
|
|
|
433
435
|
link_path = yard_ref_to_path(ref)
|
|
434
436
|
"[#{ref}](#{link_path})"
|
|
435
437
|
end
|
|
438
|
+
|
|
436
439
|
escape_html(result)
|
|
437
440
|
end
|
|
438
441
|
|
|
@@ -501,6 +504,7 @@ module Apiwork
|
|
|
501
504
|
|
|
502
505
|
def build_type_path_lookup
|
|
503
506
|
lookup = {}
|
|
507
|
+
|
|
504
508
|
public_api_yard_objects.each do |yard_object|
|
|
505
509
|
full_path = yard_object.path.delete_prefix('Apiwork::')
|
|
506
510
|
parts = full_path.split('::')
|
|
@@ -541,7 +545,6 @@ module Apiwork
|
|
|
541
545
|
resolved = type_path_lookup[class_name] || class_name
|
|
542
546
|
without_apiwork = resolved.delete_prefix('Apiwork::')
|
|
543
547
|
parts = without_apiwork.split('::')
|
|
544
|
-
|
|
545
548
|
return '/reference/' if parts.empty?
|
|
546
549
|
|
|
547
550
|
file_parts = parts.map { |part| dasherize(part) }
|
|
@@ -585,10 +588,9 @@ module Apiwork
|
|
|
585
588
|
end
|
|
586
589
|
|
|
587
590
|
parts << "#{linkify_yard_refs(mod[:docstring])}\n" if mod[:docstring].present?
|
|
588
|
-
|
|
589
591
|
render_examples(parts, mod[:examples]) if mod[:examples].any?
|
|
590
|
-
|
|
591
592
|
children = find_direct_children(mod[:path])
|
|
593
|
+
|
|
592
594
|
if children.any?
|
|
593
595
|
parts << "## Modules\n"
|
|
594
596
|
render_child_links(parts, mod[:path], children)
|
|
@@ -613,7 +615,6 @@ module Apiwork
|
|
|
613
615
|
|
|
614
616
|
def render_method(method, prefix)
|
|
615
617
|
parts = []
|
|
616
|
-
|
|
617
618
|
parts << "### #{prefix}#{escape_brackets(method[:name])}\n"
|
|
618
619
|
parts << "`#{prefix}#{method[:signature]}`\n"
|
|
619
620
|
|
|
@@ -673,7 +674,6 @@ module Apiwork
|
|
|
673
674
|
end
|
|
674
675
|
|
|
675
676
|
render_examples(parts, method[:examples]) if method[:examples].any?
|
|
676
|
-
|
|
677
677
|
parts << "---\n"
|
|
678
678
|
parts.join("\n")
|
|
679
679
|
end
|
data/lib/apiwork/registry.rb
CHANGED
|
@@ -82,7 +82,6 @@ module Apiwork
|
|
|
82
82
|
@representation_class = representation
|
|
83
83
|
validate_representation!
|
|
84
84
|
@polymorphic = normalize_polymorphic(polymorphic)
|
|
85
|
-
|
|
86
85
|
@filterable = filterable
|
|
87
86
|
@sortable = sortable
|
|
88
87
|
@include = include
|
|
@@ -92,9 +91,7 @@ module Apiwork
|
|
|
92
91
|
@description = description
|
|
93
92
|
@example = example
|
|
94
93
|
@deprecated = deprecated
|
|
95
|
-
|
|
96
94
|
detect_polymorphic_discriminator! if @polymorphic
|
|
97
|
-
|
|
98
95
|
validate_include_option!
|
|
99
96
|
validate_association_exists!
|
|
100
97
|
validate_polymorphic!
|
|
@@ -322,6 +319,7 @@ module Apiwork
|
|
|
322
319
|
|
|
323
320
|
detail = "Invalid include option ':#{@include}' for association '#{@name}'. " \
|
|
324
321
|
'Must be :always or :optional'
|
|
322
|
+
|
|
325
323
|
error = ConfigurationError.new(
|
|
326
324
|
detail:,
|
|
327
325
|
code: :invalid_include_option,
|
|
@@ -340,6 +338,7 @@ module Apiwork
|
|
|
340
338
|
return if reflection
|
|
341
339
|
|
|
342
340
|
detail = "Undefined association '#{@name}' in #{@owner_representation_class.name}: no association on model"
|
|
341
|
+
|
|
343
342
|
error = ConfigurationError.new(
|
|
344
343
|
detail:,
|
|
345
344
|
code: :invalid_association,
|
|
@@ -354,6 +353,7 @@ module Apiwork
|
|
|
354
353
|
return unless writable?
|
|
355
354
|
|
|
356
355
|
nested_attribute_method = "#{@name}_attributes="
|
|
356
|
+
|
|
357
357
|
unless @model_class.instance_methods.include?(nested_attribute_method.to_sym)
|
|
358
358
|
detail = "#{@model_class.name} doesn't accept nested attributes for #{@name}. " \
|
|
359
359
|
"Add: accepts_nested_attributes_for :#{@name}"
|
|
@@ -170,7 +170,6 @@ module Apiwork
|
|
|
170
170
|
optional = false if optional.nil?
|
|
171
171
|
nullable = false if nullable.nil?
|
|
172
172
|
default = '' if UNSET.equal?(default) && empty
|
|
173
|
-
|
|
174
173
|
@filterable = filterable
|
|
175
174
|
@preload = preload
|
|
176
175
|
@sortable = sortable
|
|
@@ -191,7 +190,6 @@ module Apiwork
|
|
|
191
190
|
@write_only = write_only
|
|
192
191
|
@default_set = !UNSET.equal?(default)
|
|
193
192
|
@default = @default_set ? default : nil
|
|
194
|
-
|
|
195
193
|
validate_min_max_range!
|
|
196
194
|
validate_format!
|
|
197
195
|
validate_empty!
|
|
@@ -350,8 +348,7 @@ module Apiwork
|
|
|
350
348
|
|
|
351
349
|
column = column_for(name)
|
|
352
350
|
return false unless column
|
|
353
|
-
|
|
354
|
-
return true if column.default.present?
|
|
351
|
+
return true unless column.default.nil?
|
|
355
352
|
|
|
356
353
|
column.null
|
|
357
354
|
end
|
|
@@ -366,7 +363,6 @@ module Apiwork
|
|
|
366
363
|
|
|
367
364
|
default = @model_class.column_defaults[name.to_s]
|
|
368
365
|
return default unless default.nil?
|
|
369
|
-
|
|
370
366
|
return '' if empty && nullable && optional
|
|
371
367
|
return nil if nullable && optional
|
|
372
368
|
|
|
@@ -67,7 +67,6 @@ module Apiwork
|
|
|
67
67
|
#
|
|
68
68
|
# @return [Representation::Inheritance, nil]
|
|
69
69
|
class_attribute :inheritance, default: nil, instance_accessor: false
|
|
70
|
-
|
|
71
70
|
class_attribute :_adapter_config, default: {}, instance_accessor: false
|
|
72
71
|
class_attribute :type_definitions, default: {}, instance_accessor: false
|
|
73
72
|
|
|
@@ -128,10 +127,12 @@ module Apiwork
|
|
|
128
127
|
"model must be an ActiveRecord model class, got #{value.class}. " \
|
|
129
128
|
"Use: model Post (not 'Post' or :post)"
|
|
130
129
|
end
|
|
130
|
+
|
|
131
131
|
unless value < ActiveRecord::Base
|
|
132
132
|
raise ConfigurationError,
|
|
133
133
|
"model must be an ActiveRecord model class, got #{value}"
|
|
134
134
|
end
|
|
135
|
+
|
|
135
136
|
@model_class = value
|
|
136
137
|
end
|
|
137
138
|
|
|
@@ -862,7 +863,6 @@ module Apiwork
|
|
|
862
863
|
target_class = subclass? ? superclass : self
|
|
863
864
|
target_inheritance = target_class.inheritance
|
|
864
865
|
target_model = target_class.model_class
|
|
865
|
-
|
|
866
866
|
return nil unless target_inheritance&.subclasses&.any?
|
|
867
867
|
return nil unless target_model.respond_to?(:inheritance_column)
|
|
868
868
|
|
|
@@ -872,7 +872,6 @@ module Apiwork
|
|
|
872
872
|
def serialize_record(record, context: {}, include: nil)
|
|
873
873
|
subclass_representation_class = inheritance.resolve(record) if inheritance&.subclasses&.any?
|
|
874
874
|
representation_class = subclass_representation_class || self
|
|
875
|
-
|
|
876
875
|
representation_class.new(record, context:, include:).as_json
|
|
877
876
|
end
|
|
878
877
|
|
|
@@ -896,9 +895,7 @@ module Apiwork
|
|
|
896
895
|
return unless @model_class
|
|
897
896
|
|
|
898
897
|
model_detector = ModelDetector.new(self)
|
|
899
|
-
|
|
900
898
|
auto_configure_inheritance if model_detector.sti_base?(@model_class) && inheritance.nil?
|
|
901
|
-
|
|
902
899
|
return unless model_detector.sti_subclass?(@model_class)
|
|
903
900
|
return unless model_detector.superclass_is_sti_base?(@model_class)
|
|
904
901
|
return if subclass?
|
|
@@ -18,13 +18,7 @@ module Apiwork
|
|
|
18
18
|
# The base class for this inheritance.
|
|
19
19
|
#
|
|
20
20
|
# @return [Class<Representation::Base>]
|
|
21
|
-
|
|
22
|
-
# @api public
|
|
23
|
-
# The subclasses for this inheritance.
|
|
24
|
-
#
|
|
25
|
-
# @return [Array<Class<Representation::Base>>]
|
|
26
|
-
attr_reader :base_class,
|
|
27
|
-
:subclasses
|
|
21
|
+
attr_reader :base_class
|
|
28
22
|
|
|
29
23
|
def initialize(base_class)
|
|
30
24
|
@base_class = base_class
|
|
@@ -47,7 +41,7 @@ module Apiwork
|
|
|
47
41
|
# @return [Class<Representation::Base>, nil]
|
|
48
42
|
def resolve(record)
|
|
49
43
|
type_value = record.public_send(column)
|
|
50
|
-
|
|
44
|
+
subclasses.find { |klass| klass.model_class.sti_name == type_value }
|
|
51
45
|
end
|
|
52
46
|
|
|
53
47
|
# @api public
|
|
@@ -55,7 +49,7 @@ module Apiwork
|
|
|
55
49
|
#
|
|
56
50
|
# @return [Boolean]
|
|
57
51
|
def transform?
|
|
58
|
-
|
|
52
|
+
subclasses.any? { |klass| klass.sti_name != klass.model_class.sti_name }
|
|
59
53
|
end
|
|
60
54
|
|
|
61
55
|
# @api public
|
|
@@ -63,16 +57,34 @@ module Apiwork
|
|
|
63
57
|
#
|
|
64
58
|
# @return [Hash{String => String}]
|
|
65
59
|
def mapping
|
|
66
|
-
|
|
60
|
+
subclasses.to_h { |klass| [klass.sti_name, klass.model_class.sti_name] }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# @api public
|
|
64
|
+
# The subclasses for this inheritance.
|
|
65
|
+
#
|
|
66
|
+
# @return [Array<Class<Representation::Base>>]
|
|
67
|
+
def subclasses
|
|
68
|
+
Apiwork.eager_load_representations!
|
|
69
|
+
detect_subclasses
|
|
70
|
+
@subclasses
|
|
67
71
|
end
|
|
68
72
|
|
|
69
73
|
def register(representation_class)
|
|
70
74
|
@subclasses << representation_class
|
|
75
|
+
@subclasses.sort_by!(&:name)
|
|
71
76
|
end
|
|
72
77
|
|
|
73
78
|
def subclass?(representation_class)
|
|
79
|
+
detect_subclasses
|
|
74
80
|
@subclasses.include?(representation_class)
|
|
75
81
|
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def detect_subclasses
|
|
86
|
+
@base_class.subclasses.each(&:model_class)
|
|
87
|
+
end
|
|
76
88
|
end
|
|
77
89
|
end
|
|
78
90
|
end
|
|
@@ -17,7 +17,6 @@ module Apiwork
|
|
|
17
17
|
|
|
18
18
|
def serialize
|
|
19
19
|
fields = {}
|
|
20
|
-
|
|
21
20
|
add_discriminator_field(fields) if @representation_class.subclass?
|
|
22
21
|
|
|
23
22
|
@representation_class.attributes.each do |name, attribute|
|
|
@@ -51,12 +50,14 @@ module Apiwork
|
|
|
51
50
|
return value if value.nil?
|
|
52
51
|
|
|
53
52
|
association = @representation_class.polymorphic_association_for_type_column(attribute_name)
|
|
53
|
+
|
|
54
54
|
if association
|
|
55
55
|
found_class = association.find_representation_for_type(value)
|
|
56
56
|
return found_class.polymorphic_name if found_class
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
inheritance = @representation_class.inheritance_for_column(attribute_name)
|
|
60
|
+
|
|
60
61
|
if inheritance
|
|
61
62
|
klass = inheritance.subclasses.find { |subclass| subclass.model_class.sti_name == value }
|
|
62
63
|
return klass.sti_name if klass
|
|
@@ -85,8 +86,8 @@ module Apiwork
|
|
|
85
86
|
if target_representation_class.inheritance&.subclasses&.any?
|
|
86
87
|
subclass_representation_class = target_representation_class.inheritance.resolve(record)
|
|
87
88
|
end
|
|
88
|
-
representation_class = subclass_representation_class || target_representation_class
|
|
89
89
|
|
|
90
|
+
representation_class = subclass_representation_class || target_representation_class
|
|
90
91
|
representation_class.new(record, context: @representation.context, include: nested_includes).as_json
|
|
91
92
|
end
|
|
92
93
|
|
data/lib/apiwork/version.rb
CHANGED
data/lib/apiwork.rb
CHANGED
|
@@ -13,18 +13,23 @@ module Apiwork
|
|
|
13
13
|
routes.call(env)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
def prepare!
|
|
16
|
+
def prepare!
|
|
17
17
|
API.clear!
|
|
18
18
|
Adapter.clear!
|
|
19
19
|
ErrorCode.clear!
|
|
20
20
|
Export.clear!
|
|
21
|
-
|
|
22
21
|
Adapter.register_defaults!
|
|
23
22
|
ErrorCode.register_defaults!
|
|
24
23
|
Export.register_defaults!
|
|
25
|
-
|
|
26
24
|
load_api_definitions!
|
|
27
|
-
|
|
25
|
+
@representations_eager_loaded = false
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def eager_load_representations!
|
|
29
|
+
return if @representations_eager_loaded
|
|
30
|
+
|
|
31
|
+
Rails.autoloaders.main.eager_load_dir(Rails.root.join('app/representations'))
|
|
32
|
+
@representations_eager_loaded = true
|
|
28
33
|
end
|
|
29
34
|
|
|
30
35
|
private
|
|
@@ -39,10 +44,6 @@ module Apiwork
|
|
|
39
44
|
API::Router.draw
|
|
40
45
|
end
|
|
41
46
|
|
|
42
|
-
def eager_load_representations!
|
|
43
|
-
Dir[Rails.root.join('app/representations/**/*.rb')].sort.each(&method(:require_dependency))
|
|
44
|
-
end
|
|
45
|
-
|
|
46
47
|
def load_api_definitions!
|
|
47
48
|
Dir[Rails.root.join('config/apis/**/*.rb')].sort.each(&method(:load))
|
|
48
49
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: apiwork
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.7.
|
|
4
|
+
version: 0.7.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- skiftle
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -24,20 +24,6 @@ dependencies:
|
|
|
24
24
|
- - ">="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '8.0'
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
28
|
-
name: bundler-audit
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - "~>"
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '0.9'
|
|
34
|
-
type: :development
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - "~>"
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '0.9'
|
|
41
27
|
- !ruby/object:Gem::Dependency
|
|
42
28
|
name: lefthook
|
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -100,14 +86,14 @@ dependencies:
|
|
|
100
86
|
requirements:
|
|
101
87
|
- - "~>"
|
|
102
88
|
- !ruby/object:Gem::Version
|
|
103
|
-
version: '0.
|
|
89
|
+
version: '0.2'
|
|
104
90
|
type: :development
|
|
105
91
|
prerelease: false
|
|
106
92
|
version_requirements: !ruby/object:Gem::Requirement
|
|
107
93
|
requirements:
|
|
108
94
|
- - "~>"
|
|
109
95
|
- !ruby/object:Gem::Version
|
|
110
|
-
version: '0.
|
|
96
|
+
version: '0.2'
|
|
111
97
|
- !ruby/object:Gem::Dependency
|
|
112
98
|
name: sqlite3
|
|
113
99
|
requirement: !ruby/object:Gem::Requirement
|