lutaml-model 0.8.205 → 0.8.206

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04adc7ed9ee1f1ddae6380fd82e5efac2ba007eec5dd096b2af1fc421a36416b
4
- data.tar.gz: f9c02256da6c8abce558566e4ad70382dd3abed1b58a0c01426b84044f54b2d5
3
+ metadata.gz: eab7d971a6cba2786d724c87b9cf76e6bd5dcfc8b18291748ea9c14d1f0d6e90
4
+ data.tar.gz: 32ca845f618f08d9ae94278188ccad27fa4aec91fc5c00abe48876b552589425
5
5
  SHA512:
6
- metadata.gz: 8d99a3063c1eca8a7117d5e5e3dcca73244d9d763e85868a8584f27fe87f0043e08ef946066f9520aa8300b37b5e605db808003d76a97d9401f6af0d4385f3a9
7
- data.tar.gz: bedfecb6db1fa494a314f7ecc95ec3f8ead2d1d412d3f74de930a102121cfad943b2a9a3e9884768bea3b1432d174284c8d0e1b55d5d5c441bd89edf284e4f38
6
+ metadata.gz: 928e1d25192b585379158191088e6e198694998141a614b6852e741449e990f95512158852b9ca28b1431a5a7bbcc5e7fd4db95cff873bb0f511a44681125132
7
+ data.tar.gz: a451b8ae1a886fe2a3f859de5c13d92414613e58e161acad16a8658e165901eee8fd72e554c60739085189eb3ae4818b02faa01f9e1066f43366b18ff14b9410
@@ -82,9 +82,15 @@ whose `elementFormDefault` is not set are in no namespace: under a
82
82
  default-namespace root they opt out with `xmlns=""`; an explicitly
83
83
  `unqualified` form keeps the default-namespace root and bare locals;
84
84
  `render_nil` / `render_empty` policies render their `xsi:nil` or blank
85
- element exactly as the standard pipeline does. One shape stays
86
- interpretive: an `xsi:nil` mark on a nested element (below the root's
87
- own children), which the engine walk does not surface.
85
+ element exactly as the standard pipeline does. A model declaring a
86
+ `sequence` records its parse-built element order on the fast path too,
87
+ so `validate` reports an out-of-order member there as it does on the
88
+ standard pipeline. Two shapes ride the standard pipeline by design —
89
+ `raw: :content` captures (an element's inner XML) and a singular
90
+ attribute typed as a `Collection`, whose own root element rides under
91
+ the mapped one — and one stays interpretive for an engine reason: an
92
+ `xsi:nil` mark on a nested element (below the root's own children),
93
+ which the engine walk does not surface.
88
94
 
89
95
  Version floor note: leptris 1.9.320+ retains child-row namespace
90
96
  URIs engine-side and matches plain attribute rows against
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.205"
5
+ VERSION = "0.8.206"
6
6
  end
7
7
  end
@@ -60,6 +60,11 @@ module Lutaml
60
60
  # A nil instance runs the static checks only.
61
61
  def compatible?(plan, instance = nil, register = nil)
62
62
  return false if plan.nil?
63
+ # A tracked element_order on an unordered plan (#977) is the
64
+ # DOM lane's ordered walk; the flat ops layout emits mapping
65
+ # order.
66
+ return false if instance && !plan[:ordered] &&
67
+ PlanSerializer.ordered_walk?(plan, instance)
63
68
  return false if namespaced_tree?(plan[:tree]) &&
64
69
  !engine_ns_prefix_safe?
65
70
  return false unless ordered_ns_emittable?(plan, instance)
@@ -283,6 +288,14 @@ module Lutaml
283
288
  return true unless instance.is_a?(::Lutaml::Model::Serialize)
284
289
 
285
290
  plan[:rows].each do |(rule, attr, kind, _sp, delegate, _type)|
291
+ # Content runs of an unordered plan interleave with the
292
+ # element children (the interpretive apply_mixed_content);
293
+ # the flat ops layout emits mapping order.
294
+ if !plan[:ordered] && attr &&
295
+ PlanSerializer::CONTENT_KINDS.include?(kind) &&
296
+ value_of(instance, attr, delegate).is_a?(::Array)
297
+ return false
298
+ end
286
299
  next unless rule.name
287
300
 
288
301
  # A type hooking the XML format through its own to_xml is
@@ -638,6 +651,10 @@ module Lutaml
638
651
  text_node_index += 1
639
652
  next if text.nil?
640
653
  next if content_attr.nil? && text.strip.empty?
654
+ # Text with no content row to ride (mixed content whose
655
+ # text is unmapped) has no ops spelling: the DOM lane
656
+ # emits it as a bare text node.
657
+ return nil unless content_entry
641
658
 
642
659
  op_slot(buf, len)
643
660
  buf[len][:kind] = :scalar
@@ -701,7 +718,7 @@ module Lutaml
701
718
  buf[len][:plan_index] = my_index
702
719
  buf[len][:row_index] = row_index
703
720
  len += 1
704
- len = if child_plan[:ordered] || !item.element_order.nil?
721
+ len = if PlanSerializer.ordered_walk?(child_plan, item)
705
722
  emit_ordered(buf, len, item, child_plan, register,
706
723
  child_index, graph)
707
724
  else
@@ -843,11 +860,12 @@ module Lutaml
843
860
  len += 1
844
861
  end
845
862
  when :content, :content_deferred
846
- # Mixed-content runs ride SCALAR ops on the content
847
- # row itself — the engine renders CONTENT-kind rows as
848
- # bare escaped text. Unreachable from the non-ordered
849
- # lane (tree_emittable? refuses content rows); the
850
- # ordered lane's unordered leaves land here.
863
+ # Content text rides SCALAR ops on the content row
864
+ # itself — the engine renders CONTENT-kind rows as bare
865
+ # escaped text. A run array on an unordered plan never
866
+ # lands here (the gate refuses it: the runs interleave
867
+ # with the element children); the ordered lane's
868
+ # unordered leaves do.
851
869
  Array(value).each do |run|
852
870
  op_slot(buf, len)
853
871
  buf[len][:kind] = :scalar
@@ -876,7 +894,7 @@ module Lutaml
876
894
  buf[len][:plan_index] = my_index
877
895
  buf[len][:row_index] = row_index
878
896
  len += 1
879
- len = if child_plan[:ordered] || !item.element_order.nil?
897
+ len = if PlanSerializer.ordered_walk?(child_plan, item)
880
898
  emit_ordered(buf, len, item, child_plan, register,
881
899
  child_index, graph)
882
900
  else
@@ -50,13 +50,20 @@ module Lutaml
50
50
 
51
51
  # Name-matched element row kinds eligible for the :unqualified
52
52
  # ns form (leptris#1560). Text rows (content) match no element
53
- # name and wildcard rows carry their own :any.
54
- ROW_KINDS_NS_UNQUALIFIED = %i[scalar collection nested callback
55
- raw].freeze
53
+ # name and wildcard rows carry their own :any. Raw captures are
54
+ # absent on purpose: the interpretive matcher binds a raw rule
55
+ # by local name whatever the prefix ("capture any element with
56
+ # that name"), so a raw row keeps the any-qualification form.
57
+ ROW_KINDS_NS_UNQUALIFIED = %i[scalar collection nested callback].freeze
56
58
  # Row kinds whose hydration can dispatch per source node —
57
59
  # the only safe claimants of a contested (multi-row) name.
58
60
  PER_NODE_ROW_KINDS = %i[nested ordered_deferred
59
61
  child_deferred].freeze
62
+ # Row kinds that read the per-name source-node buckets; a plan
63
+ # keeping its node for another reason (a sequence's element_order
64
+ # record) builds no buckets without one of these.
65
+ BUCKET_ROW_KINDS = %i[nested ordered_deferred child_deferred
66
+ custom_method polymorphic].freeze
60
67
 
61
68
  # Signature holder: the (attribute count, rule count) a plan was
62
69
  # compiled against, keyed like PLAN_CACHE.
@@ -282,6 +289,12 @@ module Lutaml
282
289
  attr.collection? ? :content : :content_deferred,
283
290
  nil, delegate_ref]
284
291
  rows << { name: "__content__#{compiled.size}", kind: :content }
292
+ elsif raw_content_rule?(rule, attr)
293
+ # raw: :content captures the element's INNER XML: no row
294
+ # kind spells it (a scalar row captures the text only,
295
+ # an :element row the whole fragment). The interpretive
296
+ # reader owns the shape.
297
+ return opt_out!(:content_raw)
285
298
  elsif rule.raw_mapping? || rule.raw == :element
286
299
  compiled << [rule, attr, :raw, nil, delegate_ref]
287
300
  rows << { name: rule.name.to_s, kind: :raw }
@@ -298,21 +311,14 @@ module Lutaml
298
311
  rows << { name: rule.name.to_s, kind: :raw }
299
312
  else
300
313
  type = attr.type(register)
314
+ return opt_out!(:nested_collection) if nested_collection_attribute?(attr, type)
315
+
301
316
  if serializable_type?(type)
302
317
  child = compile(type, register)
303
- if child.nil? &&
304
- !Lutaml::Model::Config.instance.xml_plan_ordered_path
305
- child_mapping = type.mappings_for(:xml, register)
306
- if child_mapping&.ordered? ||
307
- child_mapping&.mixed_content?
308
- # A child refused ONLY by the ordered opt-in gate
309
- # cascades: the bridged-island deferral drops its
310
- # mixed children (customxml schemaRefs), so the
311
- # parent compiles to nil — main's whole-model
312
- # interpretive shape.
313
- return opt_out!(:ordered_opt_in_cascade)
314
- end
318
+ if ordered_cascade_refusal?(type, register, child)
319
+ return opt_out!(:ordered_opt_in_cascade)
315
320
  end
321
+
316
322
  needs_nodes = true if child.nil? || child[:ordered]
317
323
  compiled_entry, row = child_row_for(rule, attr, type, child,
318
324
  delegate_ref,
@@ -470,11 +476,20 @@ module Lutaml
470
476
  return nil
471
477
  end
472
478
 
479
+ # Sequence validation reads the parse-built element_order
480
+ # (validate → order_names): a model declaring a sequence
481
+ # records it from its source node, nested instances
482
+ # included, like the ordered shapes.
483
+ sequence = !mapping.element_sequence.empty?
484
+ needs_nodes = true if sequence
485
+
473
486
  { descriptor: descriptor, tree: tree, rows: compiled,
474
487
  attr_rows: attr_rows, mapping: mapping,
475
488
  row_tags: row_tags, namespaced: namespaced,
476
489
  ordered: mapping.ordered? || mapping.mixed_content?,
490
+ sequence: sequence,
477
491
  needs_nodes: needs_nodes,
492
+ needs_buckets: compiled.any? { |row| BUCKET_ROW_KINDS.include?(row[2]) },
478
493
  contested_names: contested.empty? ? nil : contested,
479
494
  ns_attr_names: ns_attr_names.empty? ? nil : ns_attr_names,
480
495
  wildcard_tag: (wildcard_tag if defined?(wildcard_tag)) }
@@ -798,6 +813,35 @@ module Lutaml
798
813
  attr.polymorphic? || attr.union?
799
814
  end
800
815
 
816
+ # The unified raw mode (the interpretive rule compiler's
817
+ # resolve_raw_mode): the rule's own, else the attribute's
818
+ # raw flag. :content captures the inner XML.
819
+ def raw_content_rule?(rule, attr)
820
+ mode = rule.raw || (attr.raw? ? :content : nil)
821
+ mode == :content
822
+ end
823
+
824
+ # A singular attribute typed as a Collection carries the
825
+ # collection's own root under the mapped element (the
826
+ # interpretive nested_collection_attribute unwrap) — a
827
+ # wrapper-plus-root shape no row spells.
828
+ def nested_collection_attribute?(attr, type)
829
+ attr.singular? && type.is_a?(Class) &&
830
+ type <= ::Lutaml::Model::Collection
831
+ end
832
+
833
+ # A child refused ONLY by the ordered opt-in gate cascades: the
834
+ # bridged-island deferral drops its mixed children (customxml
835
+ # schemaRefs), so the parent compiles to nil — main's
836
+ # whole-model interpretive shape.
837
+ def ordered_cascade_refusal?(type, register, child)
838
+ return false unless child.nil?
839
+ return false if Lutaml::Model::Config.instance.xml_plan_ordered_path
840
+
841
+ child_mapping = type.mappings_for(:xml, register)
842
+ child_mapping&.ordered? || child_mapping&.mixed_content?
843
+ end
844
+
801
845
  def scalar_type?(attr, register)
802
846
  type = attr.type(register)
803
847
  type.is_a?(Class) && type < ::Lutaml::Model::Type::Value &&
@@ -48,11 +48,12 @@ module Lutaml
48
48
  # duplicate name/URI strings inside the summaries. One
49
49
  # shared table per parse collapses them to distinct strings.
50
50
  intern ||= {}
51
- buckets = if node && plan[:needs_nodes]
51
+ buckets = if node && plan[:needs_nodes] && plan[:needs_buckets]
52
52
  element_buckets(node, plan[:contested_names])
53
53
  end
54
54
  attr_kwargs = attributes_kwargs(plan, value, node, register)
55
- child_kwargs, children, delegates, run_children =
55
+ child_kwargs, children, delegates, run_children,
56
+ element_children =
56
57
  children_kwargs(model_class, plan, value, buckets, register,
57
58
  intern, node: node)
58
59
  # A collection absent from the document keeps the
@@ -76,8 +77,11 @@ module Lutaml
76
77
  instance.xml_namespace_prefix = node.prefix
77
78
  end
78
79
  # #909: order summaries are per-instance pinned records —
79
- # read-only consumers skip them via the config.
80
- if node && plan[:ordered] &&
80
+ # read-only consumers skip them via the config. Ordered and
81
+ # sequence models record them: the ordered walk replays
82
+ # them, and sequence validation reads the element names in
83
+ # document order (without the record it silently passes).
84
+ if node && (plan[:ordered] || plan[:sequence]) &&
81
85
  Lutaml::Model::Config.instance.element_order_tracking
82
86
  instance.element_order = PlanOrder.build(node, intern)
83
87
  end
@@ -97,7 +101,8 @@ module Lutaml
97
101
  child.lutaml_root ||= instance.lutaml_root || instance
98
102
  end
99
103
  interpret_deferred(model_class, plan, value, instance, node,
100
- buckets, run_children)
104
+ buckets, run_children,
105
+ element_children: element_children)
101
106
  route_delegates(delegates, instance, register)
102
107
  CollectionConsolidation.call(instance, register)
103
108
  instance
@@ -330,11 +335,10 @@ module Lutaml
330
335
  context: nil)
331
336
  end
332
337
 
333
- if rule.transform.is_a?(Class)
334
- rule.transform_value(attr, value, :from, :xml)
335
- else
336
- value
337
- end
338
+ # The interpretive transform_value: class transformers on
339
+ # the rule or the attribute, in order (its own fast path
340
+ # returns the value when there are none).
341
+ rule.transform_value(attr, value, :from, :xml)
338
342
  end
339
343
 
340
344
  # The interpretive reader casts a Value type that hooks the XML
@@ -612,7 +616,10 @@ module Lutaml
612
616
  attr.collection? ? values : values.first)
613
617
  end
614
618
  end
615
- [kwargs, children, delegates, run_children]
619
+ # grouped holds the captured element rows: non-empty means
620
+ # the source element has element children (the content
621
+ # shape below reads it).
622
+ [kwargs, children, delegates, run_children, !grouped.empty?]
616
623
  end
617
624
 
618
625
  # Deferred islands: raw subtrees become wrapper elements via a
@@ -623,7 +630,8 @@ module Lutaml
623
630
  # node (no fragment parse — the reparse dominated deferred
624
631
  # hydration). Ordered children only land here without a node.
625
632
  def interpret_deferred(model_class, plan, value, instance, node = nil,
626
- buckets = nil, run_children = nil)
633
+ buckets = nil, run_children = nil,
634
+ element_children: false)
627
635
  # Array#each + top counter: no per-node enumerator (see
628
636
  # children_kwargs).
629
637
  rows = plan[:rows]
@@ -654,10 +662,13 @@ module Lutaml
654
662
  next
655
663
  end
656
664
 
657
- # Non-collection content attrs hold the joined text
658
- # (the interpretive path assigns element text, the runs
659
- # concatenated).
660
- instance.public_send(:"#{attr.name}=", runs.join)
665
+ # A singular content attribute takes the interpretive
666
+ # reader's shape: the text runs when the element has
667
+ # non-text children (validate flags a singular target
668
+ # holding them), the joined text otherwise.
669
+ instance.public_send(:"#{attr.name}=",
670
+ content_text(node, runs,
671
+ element_children))
661
672
  when :raw
662
673
  raws = raw_strings(value, wire_name)
663
674
  next if raws.empty?
@@ -668,7 +679,16 @@ module Lutaml
668
679
  elements = deferred_elements(value, wire_name, buckets)
669
680
  next if elements.empty?
670
681
 
671
- args = attr.collection? ? elements : elements.first
682
+ # The interpretive reader's custom-method contract: a
683
+ # singular attribute receives its one element, or the
684
+ # whole array on an over-count (#185); the rule's
685
+ # transform runs first, on that same value.
686
+ args = if attr.collection? || elements.size > 1
687
+ elements
688
+ else
689
+ elements.first
690
+ end
691
+ args = import_transformed(rule, attr, args)
672
692
  rule.deserialize(instance, args,
673
693
  model_class.attributes(register),
674
694
  model_class)
@@ -825,6 +845,21 @@ module Lutaml
825
845
  end
826
846
  end
827
847
 
848
+ # The interpretive XmlElement#text shape for a singular
849
+ # content attribute: the text runs when the source element has
850
+ # non-text children (mixed content), the joined text otherwise.
851
+ # Mapped element children show in the walk's own capture; an
852
+ # unmapped one only on the source node, consulted when the
853
+ # hydration already holds it (the buckets chain is not kept
854
+ # for this — 85 singular content attributes ride every NISO
855
+ # JATS paragraph).
856
+ def content_text(node, runs, element_children)
857
+ return runs if element_children
858
+ return runs.join unless node
859
+
860
+ node.first_element_child.nil? ? runs.join : runs
861
+ end
862
+
828
863
  # Native collection rows echo their producing row's name
829
864
  # (leptris 1.9.178).
830
865
  def native_collection(value, name)
@@ -97,10 +97,10 @@ module Lutaml
97
97
  # (namespaced spellings are instance-dependent; ordered,
98
98
  # wildcard, raw, and callback shapes are not
99
99
  # ops-expressible).
100
- # element_order is the order contract: a model carrying it —
101
- # parse-captured or programmatically tracked (#977) — must
102
- # serialize through the ordered walk, not the flat
103
- # schema-order native ops layout.
100
+ # element_order is the order contract: a model carrying a
101
+ # tracked one (#977) serializes through the ordered walk
102
+ # below, not the flat schema-order native ops layout — the
103
+ # native gate refuses those instances.
104
104
  return NativeEmission.emit(instance, plan, register) if
105
105
  NativeEmission.compatible?(plan, instance, register)
106
106
 
@@ -125,7 +125,7 @@ module Lutaml
125
125
  root.set_attribute("xsi:schemaLocation",
126
126
  instance.raw_schema_location)
127
127
  end
128
- built = if plan[:ordered] || !instance.element_order.nil?
128
+ built = if ordered_walk?(plan, instance)
129
129
  build_ordered(root, instance, plan, doc, register, ns_ctx,
130
130
  scope: BlankScope.new(ns_ctx.default_namespace,
131
131
  false, pretty))
@@ -433,7 +433,7 @@ module Lutaml
433
433
  scope, ns_ctx),
434
434
  )
435
435
  ns_ctx&.collect(child_plan[:tree])
436
- if child_plan[:ordered] || !item.element_order.nil?
436
+ if ordered_walk?(child_plan, item)
437
437
  build_ordered(child, item, child_plan,
438
438
  doc, reg, ns_ctx, form,
439
439
  scope: child_scope) || return
@@ -452,8 +452,14 @@ module Lutaml
452
452
  # deferred rows ride the fast lane untouched.
453
453
  return nil unless Array(value).empty?
454
454
  when :content, :content_deferred
455
- Array(value).each do |run|
456
- append_text(element, run.to_s, doc, cdata: rule.cdata)
455
+ if value.is_a?(::Array)
456
+ # The interpretive apply_mixed_content: the runs
457
+ # interleave with the element children emitted so far
458
+ # — the first before the first element, each next one
459
+ # after an element, a trailing run appended.
460
+ interleave_runs(element, value, doc, cdata: rule.cdata)
461
+ else
462
+ append_text(element, value.to_s, doc, cdata: rule.cdata)
457
463
  end
458
464
  end
459
465
  end
@@ -685,7 +691,7 @@ module Lutaml
685
691
  scope,
686
692
  qualify!(child, child_ns, blank, scope, ns_ctx),
687
693
  )
688
- if child_plan[:ordered] || !item.element_order.nil?
694
+ if ordered_walk?(child_plan, item)
689
695
  return nil unless build_ordered(child, item, child_plan,
690
696
  doc, reg, ns_ctx, child_ns,
691
697
  scope: child_scope)
@@ -694,7 +700,18 @@ module Lutaml
694
700
  scope: child_scope) || return
695
701
  end
696
702
  when :raw
697
- element.add_child(value.to_s) unless value.nil?
703
+ # One captured fragment per order entry — the
704
+ # interpretive walk's per-value raw injection. A
705
+ # collection's fragments were injected whole at the
706
+ # first entry (the rest arriving as a Ruby array
707
+ # spelling).
708
+ items = value.is_a?(::Array) ? value : [value]
709
+ index = element_indices[dispatch_key]
710
+ next unless index < items.length
711
+
712
+ element_indices[dispatch_key] += 1
713
+ raw = items[index]
714
+ element.add_child(raw.to_s) unless raw.nil? || raw.to_s.empty?
698
715
  when :child_deferred
699
716
  # Valued cycle-point children: interpretive lane (see
700
717
  # build).
@@ -748,6 +765,22 @@ module Lutaml
748
765
  RECONCILER.order_entry?(object)
749
766
  end
750
767
 
768
+ # Whether an instance serializes through the ordered walk: an
769
+ # ordered/mixed plan, or an instance carrying a programmatically
770
+ # tracked element_order (the order-tracker pattern, lutaml-model
771
+ # #977) whatever its mapping declares. A parse-built record on
772
+ # an unordered plan does not reroute — sequence validation reads
773
+ # it, and the interpretive writer emits mapping order there. The
774
+ # plan lane's parse-built record is PlanOrder.build's frozen
775
+ # array; a tracked one is mutable (entries are appended).
776
+ def ordered_walk?(plan, instance)
777
+ return true if plan[:ordered]
778
+ return false unless instance.is_a?(::Lutaml::Model::Serialize)
779
+
780
+ order = instance.element_order
781
+ !order.nil? && !order.empty? && !order.frozen?
782
+ end
783
+
751
784
  # [attribute name, serialized name] -> CompiledRule for one
752
785
  # transformation, identity-keyed by its frozen rule array.
753
786
  def compiled_rule_index_for(klass, reg)
@@ -858,7 +891,7 @@ module Lutaml
858
891
  child_scope = subtree_scope(
859
892
  scope, qualify!(child, child_ns, blank, scope, ns_ctx)
860
893
  )
861
- if child_plan[:ordered] || !item.element_order.nil?
894
+ if ordered_walk?(child_plan, item)
862
895
  return nil unless build_ordered(child, item, child_plan,
863
896
  doc, reg, ns_ctx, child_ns,
864
897
  scope: child_scope)
@@ -1079,7 +1112,7 @@ module Lutaml
1079
1112
  # Shared with NativeEmission's gate: the order-entry predicate,
1080
1113
  # the render-policy placeholder and the coverage pre-check
1081
1114
  # decide what the ops lane cannot serve.
1082
- public :order_entry?, :placeholder_for, :order_uncovered?
1115
+ public :order_entry?, :ordered_walk?, :placeholder_for, :order_uncovered?
1083
1116
 
1084
1117
  # Whether a row's value reaches the output at all — the
1085
1118
  # interpretive should_skip_value? contract (RenderPolicy): an
@@ -1112,7 +1145,7 @@ module Lutaml
1112
1145
  # FFI::Pointer wrapper per fresh node, the last per-row
1113
1146
  # Ruby-side allocation on the DOM build.
1114
1147
  def append_text(parent, text, doc, cdata: false)
1115
- node = cdata ? doc.create_cdata(text) : doc.create_text_node(text)
1148
+ node = text_node(doc, text, cdata)
1116
1149
  status = ::Leptris::XML::FFI.leptris_element_add_child_addr(
1117
1150
  parent.c_address, node.c_address
1118
1151
  )
@@ -1124,6 +1157,27 @@ module Lutaml
1124
1157
  node
1125
1158
  end
1126
1159
 
1160
+ def text_node(doc, text, cdata)
1161
+ cdata ? doc.create_cdata(text) : doc.create_text_node(text)
1162
+ end
1163
+
1164
+ # The interpretive apply_mixed_content over the children emitted
1165
+ # so far: the runs interleave with them — the first before the
1166
+ # first child, each next one after a child, a trailing run
1167
+ # appended (the #1164 tail).
1168
+ def interleave_runs(element, runs, doc, cdata: false)
1169
+ existing = element.children
1170
+ nodes = runs.map do |run|
1171
+ run.nil? || run.to_s.empty? ? nil : text_node(doc, run.to_s, cdata)
1172
+ end
1173
+ interleaved = [nodes[0]]
1174
+ existing.each_with_index do |child, index|
1175
+ interleaved << child << nodes[index + 1]
1176
+ end
1177
+ interleaved.concat(nodes[(existing.size + 1)..] || [])
1178
+ element.children = interleaved.compact
1179
+ end
1180
+
1127
1181
  # Whether a Value type hooks the XML format through its own
1128
1182
  # methods (instance to_xml, class from_xml) instead of a
1129
1183
  # registered format serializer. The interpretive writer calls
@@ -334,15 +334,24 @@ RSpec.describe Lutaml::Model::Cli do
334
334
 
335
335
  after { FileUtils.rm_rf(temp_dir) }
336
336
 
337
- it "handles malformed XML gracefully" do
337
+ it "handles malformed XML according to the engine's parse contract" do
338
338
  cli.options = options
339
339
 
340
- # The XML parser is forgiving and creates empty models for malformed XML
341
- expect do
342
- cli.compare(malformed_xml, files[:target_xml_path])
343
- end.to output(
344
- a_string_including("Differences between", "Similarity score:"),
345
- ).to_stdout
340
+ # The leptris plan lane parses strictly (malformed XML raises on
341
+ # every lane, 17a4658d): the CLI reports the parse failure. A
342
+ # recovering engine (Nokogiri) hands it a best-effort tree and
343
+ # the comparison runs on that.
344
+ if Lutaml::Xml.moxml_engine == :leptris
345
+ expect do
346
+ cli.compare(malformed_xml, files[:target_xml_path])
347
+ end.to raise_error(StandardError, /Error parsing file .*malformed\.xml/)
348
+ else
349
+ expect do
350
+ cli.compare(malformed_xml, files[:target_xml_path])
351
+ end.to output(
352
+ a_string_including("Differences between", "Similarity score:"),
353
+ ).to_stdout
354
+ end
346
355
  end
347
356
 
348
357
  it "raises error when parsing malformed YAML" do
@@ -266,9 +266,14 @@ RSpec.describe "Group" do
266
266
  let(:mapper) { GroupSpec::CeramicCollection }
267
267
 
268
268
  it "raises error if type-only class used directly for parsing" do
269
+ # A well-formed document: the model's own error must be the one
270
+ # raised, not an engine's verdict on the input (a strict lane
271
+ # rejects a two-root fragment before the model is consulted).
269
272
  xml = <<~XML
270
- <type>Data</type>
271
- <name>Smith</name>
273
+ <ceramic>
274
+ <type>Data</type>
275
+ <name>Smith</name>
276
+ </ceramic>
272
277
  XML
273
278
 
274
279
  expect { GroupSpec::Ceramic.from_xml(xml) }.to raise_error(
@@ -218,22 +218,23 @@ RSpec.describe "ValueMap" do
218
218
 
219
219
  let(:parsed) { ValueMapSpec::WithValueMaps.from_xml(xml) }
220
220
 
221
- let(:expected) do
222
- ValueMapSpec::WithValueMaps.new(
223
- {
224
- nil_as_nil: nil,
225
- nil_as_empty: "",
226
- empty_as_empty: "",
227
- empty_as_nil: nil,
228
- omitted_as_nil: nil,
229
- omitted_as_empty: "",
230
- },
231
- { omitted: :omitted },
232
- )
233
- end
234
-
235
221
  it "sets correct values when deserializing" do
236
- expect(parsed).to eq(expected)
222
+ # The bound attributes carry their value_map's binding.
223
+ expect(parsed.nil_as_nil).to be_nil
224
+ expect(parsed.nil_as_empty).to eq("")
225
+ expect(parsed.empty_as_empty).to eq("")
226
+ expect(parsed.empty_as_nil).to be_nil
227
+ expect(parsed.omitted_as_nil).to be_nil
228
+ expect(parsed.omitted_as_empty).to eq("")
229
+
230
+ # :omitted binds no value — whether the element was absent,
231
+ # nil-marked, or empty. (How an unbound attribute is held is the
232
+ # constructor's business: the deserialization seed keeps the
233
+ # uninitialized sentinel, the plan lane's seed a nil.)
234
+ unbound = ->(value) { value.nil? || Lutaml::Model::Utils.uninitialized?(value) }
235
+ %i[omitted_as_omitted nil_as_omitted empty_as_omitted].each do |name|
236
+ expect(unbound.call(parsed.public_send(name))).to be(true), "#{name} should bind no value"
237
+ end
237
238
  end
238
239
  end
239
240
  end
@@ -373,6 +373,145 @@ module ProductionLaneParitySpec
373
373
  map_element "type", to: :type
374
374
  end
375
375
  end
376
+
377
+ class RawCapture < Lutaml::Model::Serializable
378
+ attribute :svg_data, :string
379
+
380
+ xml do
381
+ element "container"
382
+ map_element "svg", to: :svg_data, raw: :element
383
+ end
384
+ end
385
+
386
+ class MixedRawCollection < Lutaml::Model::Serializable
387
+ attribute :name, :string
388
+ attribute :fragments, :string, collection: true
389
+
390
+ xml do
391
+ element "container"
392
+ mixed_content
393
+ map_element "name", to: :name
394
+ map_element "fragment", to: :fragments, raw: :element
395
+ end
396
+ end
397
+
398
+ class InnerContent < Lutaml::Model::Serializable
399
+ attribute :street, :string
400
+
401
+ xml do
402
+ element "address"
403
+ map_element "street", to: :street, raw: :content
404
+ end
405
+ end
406
+
407
+ class NestedTitle < Lutaml::Model::Serializable
408
+ attribute :content, :string
409
+
410
+ xml do
411
+ element "artifact"
412
+ map_element "content", to: :content
413
+ end
414
+ end
415
+
416
+ class NestedTitles < Lutaml::Model::Collection
417
+ instances :items, NestedTitle
418
+
419
+ xml do
420
+ element "title-group"
421
+ map_element "artifact", to: :items
422
+ end
423
+ end
424
+
425
+ class NestedBibitem < Lutaml::Model::Serializable
426
+ attribute :titles, NestedTitles
427
+
428
+ xml do
429
+ element "bibitem"
430
+ map_element "titles", to: :titles
431
+ end
432
+ end
433
+
434
+ class Sequenced < Lutaml::Model::Serializable
435
+ attribute :title, :string
436
+ attribute :note, :string
437
+ attribute :size, :string
438
+
439
+ xml do
440
+ element "sequenced"
441
+ map_element :title, to: :title
442
+ sequence do
443
+ map_element :note, to: :note
444
+ map_element :size, to: :size
445
+ end
446
+ end
447
+ end
448
+
449
+ class SequencedHolder < Lutaml::Model::Serializable
450
+ attribute :items, Sequenced, collection: true
451
+
452
+ xml do
453
+ element "holder"
454
+ map_element "sequenced", to: :items
455
+ end
456
+ end
457
+
458
+ class CustomSingular < Lutaml::Model::Serializable
459
+ attribute :label, :string
460
+
461
+ xml do
462
+ element "custom"
463
+ map_element "label", to: :label,
464
+ with: { from: :label_from, to: :label_to }
465
+ end
466
+
467
+ def self.received
468
+ @received ||= []
469
+ end
470
+
471
+ def label_from(model, node)
472
+ self.class.received << node
473
+ texts = Array(node).map(&:text)
474
+ model.label = texts.size > 1 ? texts : texts.first
475
+ end
476
+
477
+ def label_to(model, parent, doc); end
478
+ end
479
+
480
+ class SingularContent < Lutaml::Model::Serializable
481
+ attribute :bold, :string, collection: true
482
+ attribute :content, :string
483
+
484
+ xml do
485
+ element "r"
486
+ map_element "bold", to: :bold
487
+ map_content to: :content
488
+ end
489
+ end
490
+
491
+ class UnmappedMixedText < Lutaml::Model::Serializable
492
+ attribute :singular, :string
493
+ attribute :items, :string, collection: true
494
+
495
+ xml do
496
+ element "container"
497
+ mixed_content
498
+ map_element "singular", to: :singular
499
+ map_element "items", to: :items
500
+ end
501
+ end
502
+
503
+ # An unordered mapping whose instances may carry a tracked
504
+ # element_order (the order-tracker pattern, lutaml-model#977).
505
+ class Tracked < Lutaml::Model::Serializable
506
+ attribute :singular, :string
507
+ attribute :items, :string, collection: true
508
+
509
+ xml do
510
+ element "container"
511
+ map_element "singular", to: :singular
512
+ map_element "items", to: :items
513
+ end
514
+ end
376
515
  end
377
516
 
378
517
  RSpec.describe "production-lane parity" do
@@ -601,6 +740,136 @@ RSpec.describe "production-lane parity" do
601
740
  .to raise_error(Lutaml::Model::TypeOnlyMappingError)
602
741
  end
603
742
 
743
+ describe "raw captures" do
744
+ it "captures an explicitly prefixed element by local name" do
745
+ doc = ProductionLaneParitySpec::RawCapture.from_xml(
746
+ '<container xmlns:s="http://www.w3.org/2000/svg"><s:svg><s:rect/></s:svg></container>',
747
+ )
748
+ expect(doc.svg_data).to include("<s:svg", "<s:rect")
749
+ end
750
+
751
+ it "injects one captured fragment per order entry of a mixed collection" do
752
+ xml = <<~XML
753
+ <container>
754
+ before
755
+ <name>multi</name>
756
+ <fragment id="1">first</fragment>
757
+ <fragment id="2"><nested>second</nested></fragment>
758
+ after
759
+ </container>
760
+ XML
761
+ doc = ProductionLaneParitySpec::MixedRawCollection.from_xml(xml)
762
+ output = doc.to_xml
763
+
764
+ expect(output).not_to include("<fragment><fragment")
765
+ expect(output).to include('id="1"', "first", 'id="2"', "<nested>second</nested>")
766
+ expect(ProductionLaneParitySpec::MixedRawCollection.from_xml(output).fragments.length)
767
+ .to eq(2)
768
+ end
769
+
770
+ it "captures the inner XML for raw: :content" do
771
+ doc = ProductionLaneParitySpec::InnerContent.from_xml(
772
+ "<address><street><b>123</b> Main St</street></address>",
773
+ )
774
+ expect(doc.street).to include("<b>123</b>", "Main St")
775
+ expect(doc.street).not_to include("<street>")
776
+ expect(ProductionLaneParitySpec::InnerContent.from_xml(doc.to_xml).street)
777
+ .to include("<b>123</b>")
778
+ end
779
+ end
780
+
781
+ describe "sequence validation and element order" do
782
+ it "flags an out-of-order member on the parsed root" do
783
+ doc = ProductionLaneParitySpec::Sequenced.from_xml(
784
+ "<sequenced><size>S</size><note>N</note><title>T</title></sequenced>",
785
+ )
786
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::IncorrectSequenceError))
787
+
788
+ ordered = ProductionLaneParitySpec::Sequenced.from_xml(
789
+ "<sequenced><title>T</title><note>N</note><size>S</size></sequenced>",
790
+ )
791
+ expect(ordered.validate).to be_empty
792
+ end
793
+
794
+ it "flags an out-of-order member on a nested member" do
795
+ doc = ProductionLaneParitySpec::SequencedHolder.from_xml(
796
+ "<holder><sequenced><title>T</title><size>S</size><note>N</note></sequenced></holder>",
797
+ )
798
+ expect(doc.items.first.validate)
799
+ .to include(an_instance_of(Lutaml::Model::IncorrectSequenceError))
800
+ end
801
+
802
+ it "serializes a parsed sequence model in mapping order" do
803
+ doc = ProductionLaneParitySpec::Sequenced.from_xml(
804
+ "<sequenced><note>N</note><size>S</size><title>T</title></sequenced>",
805
+ )
806
+ expect(doc.to_xml).to be_xml_equivalent_to(
807
+ "<sequenced><title>T</title><note>N</note><size>S</size></sequenced>",
808
+ )
809
+ end
810
+
811
+ it "serializes a tracked element_order on an unordered plan through the ordered walk" do
812
+ model = ProductionLaneParitySpec::Tracked.new(items: %w[a b], singular: "s")
813
+ model.element_order = [
814
+ Lutaml::Xml::Element.new("Element", "items", node_type: :element),
815
+ Lutaml::Xml::Element.new("Element", "singular", node_type: :element),
816
+ Lutaml::Xml::Element.new("Element", "items", node_type: :element),
817
+ ]
818
+ xml = model.to_xml
819
+ expect(xml.index("<items>a</items>")).to be < xml.index("<singular>s</singular>")
820
+ expect(xml.index("<singular>s</singular>")).to be < xml.index("<items>b</items>")
821
+ end
822
+ end
823
+
824
+ it "hands a singular attribute's over-count to the custom method as a whole array" do
825
+ klass = ProductionLaneParitySpec::CustomSingular
826
+ klass.received.clear
827
+ doc = klass.from_xml("<custom><label>a</label><label>b</label></custom>")
828
+
829
+ expect(klass.received.size).to eq(1)
830
+ expect(klass.received.first).to be_a(Array)
831
+ expect(doc.label).to eq(%w[a b])
832
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::CollectionTrueMissingError))
833
+ end
834
+
835
+ describe "singular content" do
836
+ it "binds the text runs when the element has element children, and flags the over-count" do
837
+ source = "<r>one <bold>b</bold> two <bold>c</bold> three</r>"
838
+ doc = ProductionLaneParitySpec::SingularContent.from_xml(source)
839
+ expect(doc.content).to eq(["one ", " two ", " three"])
840
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::CollectionTrueMissingError))
841
+ expect(doc.to_xml).to include("one <bold>b</bold> two <bold>c</bold> three")
842
+
843
+ plain = ProductionLaneParitySpec::SingularContent.from_xml("<r>plain</r>")
844
+ expect(plain.content).to eq("plain")
845
+ end
846
+ end
847
+
848
+ it "round-trips mixed content whose text is unmapped" do
849
+ source = "<container>before <items>a</items> mid <singular>x</singular> end</container>"
850
+ parsed = ProductionLaneParitySpec::UnmappedMixedText.from_xml(source)
851
+
852
+ expect(parsed.to_xml).to include("before <items>a</items> mid <singular>x</singular> end")
853
+ end
854
+
855
+ it "unwraps a collection's own root under the mapped element" do
856
+ xml = <<~XML
857
+ <bibitem>
858
+ <titles>
859
+ <title-group>
860
+ <artifact><content>Title One</content></artifact>
861
+ <artifact><content>Title Two</content></artifact>
862
+ </title-group>
863
+ </titles>
864
+ </bibitem>
865
+ XML
866
+ doc = ProductionLaneParitySpec::NestedBibitem.from_xml(xml)
867
+
868
+ expect(doc.titles).to be_a(ProductionLaneParitySpec::NestedTitles)
869
+ expect(doc.titles.items.map(&:content)).to eq(["Title One", "Title Two"])
870
+ expect(doc.to_xml).to be_xml_equivalent_to(xml)
871
+ end
872
+
604
873
  describe "conversion caching" do
605
874
  let(:store) { Lutaml::Store::BasicStore.new(adapter: { type: :memory }) }
606
875
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.205
4
+ version: 0.8.206
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.