lutaml-model 0.8.203 → 0.8.205

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 (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +5 -0
  3. data/.github/workflows/lml-conformance.yml +71 -0
  4. data/.gitignore +7 -0
  5. data/README.adoc +1 -0
  6. data/docs/_guides/lml-cross-language.adoc +128 -0
  7. data/docs/_guides/native-engines.adoc +10 -0
  8. data/docs/_references/lml-conformance.adoc +78 -0
  9. data/docs/_references/lml-contract.adoc +290 -0
  10. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  11. data/lib/lutaml/model/lml/bundle.rb +171 -0
  12. data/lib/lutaml/model/lml/document.rb +46 -10
  13. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  14. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  15. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  16. data/lib/lutaml/model/lml/exporter.rb +223 -87
  17. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  18. data/lib/lutaml/model/lml/importer.rb +180 -0
  19. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  20. data/lib/lutaml/model/lml/materialization.rb +257 -0
  21. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  22. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  23. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  24. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  25. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  26. data/lib/lutaml/model/lml.rb +61 -0
  27. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  28. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  29. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  30. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  31. data/lib/lutaml/model/sequence.rb +7 -0
  32. data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
  33. data/lib/lutaml/model/serialize/initialization.rb +2 -0
  34. data/lib/lutaml/model/version.rb +1 -1
  35. data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
  36. data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
  37. data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
  38. data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
  39. data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
  40. data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
  41. data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
  42. data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
  43. data/schema/lml/document.schema.json +179 -0
  44. data/scripts/lml/export_choices.rb +57 -0
  45. data/scripts/lml/export_conformance.rb +23 -0
  46. data/scripts/lml/export_mml.rb +253 -0
  47. data/scripts/lml/export_preservation.rb +421 -0
  48. data/scripts/lml/verify_artifacts.rb +31 -0
  49. data/spec/fixtures/lml_artifacts/model.json +1 -0
  50. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  51. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  52. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  53. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  54. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  55. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  56. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  57. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  58. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  59. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  60. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  61. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  62. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  63. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  64. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  65. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  66. data/spec/fixtures/lml_contract/bundle.json +81 -0
  67. data/spec/fixtures/lml_contract/manifest.json +30 -0
  68. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  69. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  70. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  71. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  72. data/spec/fixtures/lml_mml/inventory.json +1 -0
  73. data/spec/fixtures/lml_mml/manifest.json +1 -0
  74. data/spec/fixtures/lml_mml/oracle.json +1 -0
  75. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  76. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  77. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  78. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  79. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  80. data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
  81. data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
  82. data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
  83. data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
  84. data/spec/lutaml/model/lml_document_spec.rb +279 -0
  85. data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
  86. data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
  87. data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
  88. data/spec/lutaml/model/lml_importer_spec.rb +290 -0
  89. data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
  90. data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
  91. data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
  92. data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
  93. data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
  94. data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
  95. data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
  96. data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
  97. data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
  98. data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
  99. data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
  100. data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
  101. data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
  102. data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
  103. data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
  104. data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
  105. data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
  106. data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
  107. data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
  108. data/spec/lutaml/model/sequence_spec.rb +48 -0
  109. data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
  110. data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
  111. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  112. data/spec/support/lml_conformance.rb +292 -0
  113. data/spec/support/lml_corpus.rb +75 -0
  114. data/spec/support/lml_export_fixtures.rb +190 -0
  115. data/spec/support/lml_graph_fixtures.rb +108 -0
  116. data/spec/support/lml_import_fixtures.rb +52 -0
  117. data/spec/support/lml_mapping_fixtures.rb +170 -0
  118. data/spec/support/lml_materialization_fixtures.rb +53 -0
  119. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  120. data/spec/support/lml_order_fixtures.rb +151 -0
  121. metadata +94 -3
  122. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -18,6 +18,7 @@ module Lutaml
18
18
  # Row kinds whose child type/plan resolve once per plan (see
19
19
  # child_row_plans).
20
20
  CHILD_ROW_KINDS = %i[nested ordered_deferred].freeze
21
+ XSI_URI = "http://www.w3.org/2001/XMLSchema-instance"
21
22
 
22
23
  class << self
23
24
  # plan: the compiled plan entry for model_class
@@ -50,15 +51,15 @@ module Lutaml
50
51
  buckets = if node && plan[:needs_nodes]
51
52
  element_buckets(node, plan[:contested_names])
52
53
  end
53
- attr_kwargs = attributes_kwargs(plan, value, node)
54
+ attr_kwargs = attributes_kwargs(plan, value, node, register)
54
55
  child_kwargs, children, delegates, run_children =
55
56
  children_kwargs(model_class, plan, value, buckets, register,
56
- intern)
57
- plan[:collection_defaults].each do |name|
58
- next if attr_kwargs.key?(name) || child_kwargs.key?(name)
59
-
60
- child_kwargs[name] = []
61
- end
57
+ intern, node: node)
58
+ # A collection absent from the document keeps the
59
+ # deserialization seed's lazy sentinel and its default mark
60
+ # — the interpretive parse's state. The reader materializes
61
+ # [], and a later nil write leaves the untouched collection
62
+ # empty rather than nil (the parsed-model mutation contract).
62
63
  # TODO.max-perf/33: the bulk constructor — allocate +
63
64
  # compiled writers, from_hash semantics — measured ~30x on
64
65
  # hydration-heavy corpora versus the generic constructor.
@@ -97,7 +98,7 @@ module Lutaml
97
98
  end
98
99
  interpret_deferred(model_class, plan, value, instance, node,
99
100
  buckets, run_children)
100
- route_delegates(delegates, instance)
101
+ route_delegates(delegates, instance, register)
101
102
  CollectionConsolidation.call(instance, register)
102
103
  instance
103
104
  end
@@ -188,7 +189,8 @@ module Lutaml
188
189
  Lutaml::Model::Config.default_register
189
190
  end
190
191
 
191
- def attributes_kwargs(plan, value, node = nil)
192
+ def attributes_kwargs(plan, value, node = nil, register = nil)
193
+ register ||= Lutaml::Model::Config.default_register
192
194
  kwargs = {}
193
195
  sole_claimants = sole_claimant_names(plan)
194
196
  ns_attr_names = plan[:ns_attr_names]
@@ -219,7 +221,8 @@ module Lutaml
219
221
  if rule.as_list && rule.as_list[:import]
220
222
  v = rule.as_list[:import].call(v)
221
223
  end
222
- kwargs[attr.name.to_sym] = v
224
+ kwargs[attr.name.to_sym] = import_transformed(rule, attr,
225
+ xml_hooked_cast(attr, v, register))
223
226
  end
224
227
  kwargs
225
228
  end
@@ -250,11 +253,10 @@ module Lutaml
250
253
  false
251
254
  end
252
255
 
253
- # A captured-but-empty child marshals as a nil string value.
254
- # The interpretive pipeline classifies the raw "" through the
255
- # rule's value map (:empty key) — the :empty directive (the
256
- # plain-rule default) keeps it as "", :nil binds nil, and any
257
- # other directive means uninitialized, i.e. no binding.
256
+ # An empty capture binds through the rule's value map: a
257
+ # blank element through the :empty key — the :empty directive
258
+ # (the plain-rule default) keeps it as "", :nil binds nil, and
259
+ # any other directive means uninitialized, i.e. no binding.
258
260
  def mapped_empty_value(rule)
259
261
  case rule.value_map(:from)[:empty]
260
262
  when :nil then nil
@@ -263,14 +265,54 @@ module Lutaml
263
265
  end
264
266
  end
265
267
 
266
- def mapped_capture_values(values, rule)
267
- return values unless values.include?(nil)
268
+ # An xsi:nil="true" element binds through the :nil key
269
+ # (Attribute#apply_value_map's contract — the interpretive
270
+ # nil_element? read).
271
+ def mapped_nil_value(rule, attr)
272
+ case rule.value_map(:from)[:nil]
273
+ when :nil then nil
274
+ when :empty then attr.collection? ? [] : ""
275
+ else :skip
276
+ end
277
+ end
278
+
279
+ # An absent element's binding through the value_map :omitted key
280
+ # when it names a typed empty ("" or the empty collection). The
281
+ # default :nil directive leaves the attribute unbound — the
282
+ # constructor's default seeding and a writer-assigned value
283
+ # (#922) own an absent attribute, never an explicit nil.
284
+ def mapped_omitted_value(rule, attr)
285
+ if rule.value_map(:from)[:omitted] == :empty
286
+ attr.collection? ? [] : ""
287
+ else
288
+ :skip
289
+ end
290
+ end
291
+
292
+ def empty_capture_value(rule, attr, marked)
293
+ marked ? mapped_nil_value(rule, attr) : mapped_empty_value(rule)
294
+ end
295
+
296
+ # A collection's captured items. One blank or nil-marked
297
+ # element is an empty collection (the interpretive
298
+ # normalized_value_for_attr rule). A multi-item capture is
299
+ # left alone by the interpretive value map — items bind raw,
300
+ # a blank element staying "" — except that a nil-marked
301
+ # element reads nil (the nil_element? rule).
302
+ def capture_collection_values(values, rule, attr, name, node)
303
+ return values unless attr.collection?
304
+ return [] if values.length == 1 &&
305
+ (values[0].nil? || values[0] == "")
306
+ return values unless values.include?(nil) || values.include?("")
268
307
 
269
- mapped = mapped_empty_value(rule)
308
+ marks = xsi_nil_marks(node)
270
309
  out = []
271
- values.each do |item|
272
- if item.nil?
273
- out << mapped unless mapped == :skip
310
+ values.each_with_index do |item, rank|
311
+ if marked?(marks, name, rank) && (item.nil? || item == "")
312
+ out << nil
313
+ elsif item.nil?
314
+ bound = mapped_empty_value(rule)
315
+ out << bound unless bound == :skip
274
316
  else
275
317
  out << item
276
318
  end
@@ -278,6 +320,73 @@ module Lutaml
278
320
  out
279
321
  end
280
322
 
323
+ # The interpretive reader's two transform lanes
324
+ # (handle_transform_method): a hash/proc transform on the rule or
325
+ # the attribute rides ImportTransformer (transform_dispatch
326
+ # :import); a class transform rides transform_value.
327
+ def import_transformed(rule, attr, value)
328
+ if Lutaml::Model::MappingRule.transform_dispatch(rule, attr) == :import
329
+ return Lutaml::Model::ImportTransformer.call(value, rule, attr,
330
+ context: nil)
331
+ end
332
+
333
+ if rule.transform.is_a?(Class)
334
+ rule.transform_value(attr, value, :from, :xml)
335
+ else
336
+ value
337
+ end
338
+ end
339
+
340
+ # The interpretive reader casts a Value type that hooks the XML
341
+ # format through its own class method (from_xml) via the
342
+ # attribute's format-aware cast (normalize_xml_value); plain
343
+ # scalars stay raw for the constructor's cast.
344
+ def xml_hooked_cast(attr, raw, register)
345
+ return raw if raw.nil?
346
+ return raw unless PlanSerializer.custom_xml_type?(attr.type(register))
347
+
348
+ attr.cast(raw, :xml, register)
349
+ end
350
+
351
+ # Child elements carrying xsi:nil="true", as
352
+ # {wire name => {same-name sibling rank => true}} — the
353
+ # engine walk does not surface the mark, so it is read from
354
+ # the source node on the first empty capture that needs it.
355
+ # Without a source node (a nested model hydrated from walk
356
+ # values alone) nothing is marked.
357
+ def xsi_nil_marks(node)
358
+ return {} unless node
359
+
360
+ marks = {}
361
+ ranks = ::Hash.new(0)
362
+ node.element_children.each do |child|
363
+ rank = ranks[child.name]
364
+ ranks[child.name] = rank + 1
365
+ next unless child["xsi:nil"] == "true"
366
+
367
+ (marks[child.name] ||= {})[rank] = true
368
+ end
369
+ marks
370
+ end
371
+
372
+ def marked?(marks, name, rank)
373
+ !!marks&.dig(name, rank)
374
+ end
375
+
376
+ # Whether a document carries xsi:nil="true" below the root's
377
+ # own children. A nested model hydrated from walk values alone
378
+ # has no source node to read the mark from, so such a
379
+ # document rides the interpretive lane; only a document that
380
+ # declares the instance namespace pays the scan. Both plan
381
+ # entry points (from_xml, PlanWalk) consult it.
382
+ def xsi_nil_below_root?(root)
383
+ return false unless root.namespace_definitions
384
+ .any? { |ns| ns.href == XSI_URI }
385
+
386
+ root.xpath("*//@xsi:nil").any? { |attr| attr.value == "true" }
387
+ end
388
+ public :xsi_nil_below_root?
389
+
281
390
  # Wire-name strings are plan-frozen; materializing them per
282
391
  # hydration allocated two or three strings per attribute row
283
392
  # per instance.
@@ -292,7 +401,7 @@ module Lutaml
292
401
  # parent/root links once the parent exists; delegate values
293
402
  # wait for the instance (their target object must exist).
294
403
  def children_kwargs(_model_class, plan, value, buckets = nil,
295
- register = nil, intern = {})
404
+ register = nil, intern = {}, node: nil)
296
405
  register ||= Lutaml::Model::Config.default_register
297
406
  grouped, tagged, run_children = group_children(value,
298
407
  plan[:row_tags])
@@ -300,6 +409,9 @@ module Lutaml
300
409
  kwargs = {}
301
410
  children = []
302
411
  delegates = []
412
+ # xsi:nil marks of the source node's children — read on the
413
+ # first empty capture that needs them (see xsi_nil_marks).
414
+ marks = nil
303
415
  # Lazy: models without multi-spelling rows never touch it, and
304
416
  # the block-backed Hash is a fresh object per hydrated row.
305
417
  spellings = nil
@@ -324,27 +436,47 @@ module Lutaml
324
436
  # Raw passthrough: the model constructor is the single
325
437
  # cast authority — pre-casting here doubled every cast.
326
438
  # Class transforms still apply before assignment.
327
- if tag && (vals = tagged[tag]) && vals.size > 1
439
+ explicit_nil = false
440
+ captures = tag ? tagged[tag] : grouped[name]
441
+ if captures && captures.size > 1
328
442
  # Interpretive parity: several captures into a
329
- # non-collection attribute arrive as the full array.
330
- v = vals.map(&:string_value)
331
- if v.include?(nil)
332
- v = v.map { |item| item.nil? ? mapped_empty_value(rule) : item }
333
- v.reject! { |item| item == :skip }
443
+ # non-collection attribute arrive as the full array —
444
+ # the over-count reaches `.validate` (Issue #185).
445
+ v = []
446
+ captures.each_with_index do |val, rank|
447
+ raw = val.string_value
448
+ if raw.nil? || raw == ""
449
+ marks ||= xsi_nil_marks(node)
450
+ bound = empty_capture_value(rule, attr,
451
+ marked?(marks, name, rank))
452
+ v << bound unless bound == :skip
453
+ else
454
+ v << xml_hooked_cast(attr, raw, register)
455
+ end
334
456
  end
335
457
  else
336
- first = if tag
337
- tagged[tag]&.first
338
- else
339
- grouped.dig(name, 0)
340
- end
458
+ first = captures&.first
341
459
  v = first&.string_value
342
- v = mapped_empty_value(rule) if v.nil? && first
343
- end
344
- unless v.nil? || v == :skip
345
- v = rule.transform_value(attr, v, :from, :xml) if rule.transform.is_a?(Class)
346
- assign(kwargs, delegates, delegate, rule, attr, v)
460
+ if first && (v.nil? || v == "")
461
+ marks ||= xsi_nil_marks(node)
462
+ explicit_nil = marked?(marks, name, 0)
463
+ v = empty_capture_value(rule, attr, explicit_nil)
464
+ elsif captures.nil? || captures.empty?
465
+ # No capture at all: the attribute's omitted state
466
+ # binds through the value_map :omitted key (the
467
+ # interpretive apply_value_map on the uninitialized
468
+ # sentinel) — nil, the typed empty, or nothing.
469
+ v = mapped_omitted_value(rule, attr)
470
+ end
347
471
  end
472
+ # A bound nil is a state of its own (the nil-marked
473
+ # capture): it is assigned, not left to the default.
474
+ next if v == :skip
475
+ next if v.nil? && !explicit_nil
476
+
477
+ v = import_transformed(rule, attr,
478
+ xml_hooked_cast(attr, v, register))
479
+ assign(kwargs, delegates, delegate, rule, attr, v)
348
480
  when :wildcard_any
349
481
  # interpreted post-instance (interpret_deferred)
350
482
  when :content
@@ -391,7 +523,8 @@ module Lutaml
391
523
  next if elements.empty?
392
524
 
393
525
  results = elements.map do |element|
394
- attr.cast(element, :xml, register)
526
+ attr.cast(element, :xml, register,
527
+ **deferred_cast_options(rule))
395
528
  end
396
529
  unless results.empty?
397
530
  children.concat(results)
@@ -402,10 +535,10 @@ module Lutaml
402
535
  values = grouped[name].to_a.map do |item|
403
536
  item&.string_value
404
537
  end
405
- values = mapped_capture_values(values, rule)
406
- if rule.transform.is_a?(Class)
407
- values = values.map { |v| rule.transform_value(attr, v, :from, :xml) }
408
- end
538
+ values = capture_collection_values(values, rule, attr, name,
539
+ node)
540
+ values = values.map { |item| xml_hooked_cast(attr, item, register) }
541
+ values = import_transformed(rule, attr, values)
409
542
  unless values.empty?
410
543
  assign(kwargs, delegates, delegate, rule, attr, values)
411
544
  end
@@ -415,8 +548,15 @@ module Lutaml
415
548
  else
416
549
  native_collection(value, name)
417
550
  end
418
- values = mapped_capture_values(values, rule) unless values.nil?
419
551
  unless values.nil?
552
+ values = capture_collection_values(values, rule, attr, name,
553
+ node)
554
+ end
555
+ unless values.nil?
556
+ values = values.map do |item|
557
+ xml_hooked_cast(attr, item, register)
558
+ end
559
+ values = import_transformed(rule, attr, values)
420
560
  assign(kwargs, delegates, delegate, rule, attr, values)
421
561
  end
422
562
  when :spelling
@@ -537,8 +677,7 @@ module Lutaml
537
677
 
538
678
  results = deferred_elements(value, wire_name, buckets).map do |element|
539
679
  attr.cast(element, :xml, register,
540
- lutaml_parent: instance,
541
- lutaml_root: instance.lutaml_root || instance)
680
+ **deferred_cast_options(rule, instance))
542
681
  end
543
682
  next if results.empty?
544
683
 
@@ -580,9 +719,7 @@ module Lutaml
580
719
  when :polymorphic
581
720
  results = deferred_elements(value, wire_name, buckets).map do |element|
582
721
  attr.cast(element, :xml, register,
583
- polymorphic: rule.polymorphic,
584
- lutaml_parent: instance,
585
- lutaml_root: instance.lutaml_root || instance)
722
+ **deferred_cast_options(rule, instance))
586
723
  end
587
724
  next if results.empty?
588
725
 
@@ -592,6 +729,20 @@ module Lutaml
592
729
  end
593
730
  end
594
731
 
732
+ # The interpretive cast options for a deferred subtree
733
+ # (value_for_rule's base options): the parent links, and the
734
+ # rule's polymorphic resolution — a child plan that could not
735
+ # compile must not drop it (the subclass identity).
736
+ def deferred_cast_options(rule, instance = nil)
737
+ options = {}
738
+ if instance
739
+ options[:lutaml_parent] = instance
740
+ options[:lutaml_root] = instance.lutaml_root || instance
741
+ end
742
+ options[:polymorphic] = rule.polymorphic if rule.polymorphic
743
+ options
744
+ end
745
+
595
746
  # Wrapper elements for deferred rows: the ORIGINAL source
596
747
  # nodes bridged through moxml's wrapper cache (wrapper
597
748
  # construction only, no parse), falling back to a fragment
@@ -633,14 +784,16 @@ module Lutaml
633
784
  @bridge_context ||= Moxml::Context.new(:leptris)
634
785
  end
635
786
 
636
- def route_delegates(delegates, instance)
637
- delegates.each do |rule, delegate_attr, attr, v|
638
- target = instance.public_send(rule.delegate)
787
+ # Rows carry the delegate NAME (the interpretive
788
+ # MappingRule#deserialize contract: the target attribute's
789
+ # type instantiates an absent target).
790
+ def route_delegates(delegates, instance, register)
791
+ attributes = instance.class.attributes(register)
792
+ delegates.each do |_rule, delegate, attr, v|
793
+ target = instance.public_send(delegate)
639
794
  unless target
640
- # the interpretive path instantiates absent delegate
641
- # targets; mirror it
642
- target = delegate_attr.type(register).new
643
- instance.public_send(:"#{rule.delegate}=", target)
795
+ target = attributes[delegate].type(register).new
796
+ instance.public_send(:"#{delegate}=", target)
644
797
  end
645
798
  target.public_send(:"#{attr.name}=", v)
646
799
  end