lutaml-model 0.8.204 → 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: 230c2c513344d8665bb44607cefc80663100ac9707ee8ce63146e5b8ed6cbdbc
4
- data.tar.gz: 2ac13fcbc47d3b7011c3957a6d988d3c996cf10fee05fe04040f82979b89d928
3
+ metadata.gz: eab7d971a6cba2786d724c87b9cf76e6bd5dcfc8b18291748ea9c14d1f0d6e90
4
+ data.tar.gz: 32ca845f618f08d9ae94278188ccad27fa4aec91fc5c00abe48876b552589425
5
5
  SHA512:
6
- metadata.gz: 1d0a5e9ac99b9ef4f68b6eb629ade317097f72d6d54187b3521f7007ccdfda47a4c03d7143040fe86987072b992d7e4741ef62c8b1a683f310b5191c391489bc
7
- data.tar.gz: a0300c85e81232f5a07151c12d69575d0d0caf38c2540268baffcffe57d7d8744802a89030be6fd597f0109f148eaa9fcceddfcee2cb90f04fcade9696cab5ac
6
+ metadata.gz: 928e1d25192b585379158191088e6e198694998141a614b6852e741449e990f95512158852b9ca28b1431a5a7bbcc5e7fd4db95cff873bb0f511a44681125132
7
+ data.tar.gz: a451b8ae1a886fe2a3f859de5c13d92414613e58e161acad16a8658e165901eee8fd72e554c60739085189eb3ae4818b02faa01f9e1066f43366b18ff14b9410
@@ -76,6 +76,22 @@ shapes) and shapes with custom semantics (custom methods,
76
76
  polymorphism, unions, ordered children) hydrate through the
77
77
  interpretive pipeline or a per-subtree fragment of it, unchanged.
78
78
 
79
+ The plan lanes follow the interpretive writer's namespace and
80
+ missing-value contracts. Locally declared elements of a namespace
81
+ whose `elementFormDefault` is not set are in no namespace: under a
82
+ default-namespace root they opt out with `xmlns=""`; an explicitly
83
+ `unqualified` form keeps the default-namespace root and bare locals;
84
+ `render_nil` / `render_empty` policies render their `xsi:nil` or blank
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.
94
+
79
95
  Version floor note: leptris 1.9.320+ retains child-row namespace
80
96
  URIs engine-side and matches plain attribute rows against
81
97
  namespace-qualified wire spellings. On older engines, models whose
@@ -116,15 +116,21 @@ module Lutaml
116
116
  def from(format, data, options = {})
117
117
  resolve_serialization_mapping_option(format, options)
118
118
 
119
- if format == :xml && Lutaml::Model::Config.instance.xml_plan_fast_path
120
- fast = xml_plan_fast_path(data, options)
121
- return fast if fast
122
- end
123
-
124
119
  # The rescue sits on the parse block, not the method, so cache
125
120
  # store failures propagate instead of becoming InvalidFormatError.
121
+ #
122
+ # The XML plan fast path runs INSIDE the cache block and leaves it
123
+ # with `next`: ahead of the block it skipped the lookup, and a
124
+ # method-level `return` skips the store write after the yield —
125
+ # on the production leptris lane the conversion cache was never
126
+ # consulted in either direction.
126
127
  with_conversion_cache(:from, format, data, options) do
127
128
  Instrumentation.instrument(:from, model: name, format: format) do
129
+ if format == :xml && Lutaml::Model::Config.instance.xml_plan_fast_path
130
+ fast = xml_plan_fast_path(data, options)
131
+ next fast if fast
132
+ end
133
+
128
134
  adapter = resolve_adapter(format, options.delete(:adapter))
129
135
 
130
136
  raise Lutaml::Model::FormatAdapterNotSpecifiedError.new(format) if adapter.nil?
@@ -164,7 +170,7 @@ module Lutaml
164
170
  return nil unless defined?(::Leptris::XML::Document)
165
171
  return nil if options.key?(:only) || options.key?(:except) ||
166
172
  options.key?(:mappings) || options.key?(:adapter) ||
167
- options.key?(:_adapter_override) ||
173
+ options.key?(:_adapter_override) || options.key?(:encoding) ||
168
174
  options.key?(:indent) || options.key?(:xml_declaration) ||
169
175
  options.key?(:declaration) || options.key?(:doctype) ||
170
176
  # prefix requests ride the interpretive writer's use_prefix
@@ -188,7 +194,9 @@ module Lutaml
188
194
  # (the #847 xmlns drop is fixed in PlanSerializer); any
189
195
  # unspellable shape answers nil there and falls back
190
196
  # interpretively.
191
- Lutaml::Xml::Leptris::PlanSerializer.call(instance, plan)
197
+ Lutaml::Xml::Leptris::PlanSerializer.call(
198
+ instance, plan, nil, pretty: options[:pretty] ? true : false
199
+ )
192
200
  end
193
201
 
194
202
  # Whole-document native materialization (Phase 5): compile the
@@ -213,6 +221,11 @@ module Lutaml
213
221
  return nil unless adapter&.engine_name == :leptris
214
222
 
215
223
  register = extract_register_id(nil)
224
+ # A type-only model's own error (validate_document) precedes
225
+ # parsing on the interpretive path; the strict parse below
226
+ # would answer a format error first.
227
+ return nil unless root?(register)
228
+
216
229
  plan = Lutaml::Xml::Leptris::PlanCompiler.compile(self, register)
217
230
  return nil unless plan
218
231
 
@@ -225,6 +238,7 @@ module Lutaml
225
238
  root = ::Leptris::XML.parse(data.to_s, **leptris_strict_kwargs).root
226
239
  return nil if root.nil?
227
240
  return nil unless root.name == plan[:tree][:name]
241
+ return nil if Lutaml::Xml::Leptris::PlanHydrator.xsi_nil_below_root?(root)
228
242
 
229
243
  Lutaml::Xml::Leptris::PlanHydrator.call(self, plan,
230
244
  plan[:descriptor].walk(root),
@@ -378,7 +392,7 @@ module Lutaml
378
392
  options[:_adapter_override] = true if adapter_override
379
393
  if format == :xml && Lutaml::Model::Config.instance.xml_plan_fast_path
380
394
  fast = xml_plan_fast_serialize(instance, options)
381
- return fast if fast
395
+ next fast if fast
382
396
  end
383
397
  value = public_send(:"as_#{format}", instance, options)
384
398
  adapter = resolve_adapter(format, adapter_override)
@@ -381,6 +381,8 @@ module Lutaml
381
381
  # Clear centralized mapping and transformation caches
382
382
  # (Single Source of Truth - no longer uses instance variables)
383
383
  TransformationRegistry.instance.clear
384
+ Lutaml::Xml::Leptris::PlanCompiler.invalidate!(self) if
385
+ defined?(Lutaml::Xml::Leptris::PlanCompiler)
384
386
 
385
387
  # Clear Transform cache (uses class identity as key)
386
388
  Transform.invalidate_for(self, register_id)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.204"
5
+ VERSION = "0.8.206"
6
6
  end
7
7
  end
@@ -37,6 +37,9 @@ module Lutaml
37
37
  DEFERRED_ROW_KINDS = %i[raw ordered_deferred].freeze
38
38
  PLANNED_ROW_KINDS = %i[nested ordered_deferred].freeze
39
39
  NESTED_ROW_KINDS = %i[nested ordered_deferred].freeze
40
+ # Element rows whose text the DOM lane spells through a type's
41
+ # own to_xml hook.
42
+ TEXT_ROW_KINDS = %i[scalar collection_native collection_cb].freeze
40
43
  NS_EMITTABLE_ROW_KINDS = %i[scalar collection nested
41
44
  collection_native
42
45
  ordered_deferred].freeze
@@ -57,9 +60,23 @@ module Lutaml
57
60
  # A nil instance runs the static checks only.
58
61
  def compatible?(plan, instance = nil, register = nil)
59
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)
60
68
  return false if namespaced_tree?(plan[:tree]) &&
61
69
  !engine_ns_prefix_safe?
62
70
  return false unless ordered_ns_emittable?(plan, instance)
71
+ # A prefixed-parsed root pins the root's prefix form (format
72
+ # preservation, priority 1), and the ops spelling inherits
73
+ # that prefix down the tree — including over rows the
74
+ # elementFormDefault contract spells :unqualified. The DOM
75
+ # lane spells those bare; the ops lane cannot, so a prefixed
76
+ # source carrying an unqualified element row stays off it.
77
+ return false if instance.is_a?(::Lutaml::Model::Serialize) &&
78
+ prefixed_source?(instance) &&
79
+ unqualified_element_row?(plan[:tree])
63
80
  return false if plan[:ordered] && instance.is_a?(::Lutaml::Model::Serialize) &&
64
81
  instance.element_order.nil?
65
82
  # An UNORDERED instance (no parse-captured element_order)
@@ -80,6 +97,20 @@ module Lutaml
80
97
  subtree_gate_ok?(plan, instance, register)
81
98
  end
82
99
 
100
+ # Whether any named element row in the emission graph is spelled
101
+ # :unqualified — a child row, or one inside a nested plan.
102
+ def unqualified_element_row?(tree)
103
+ return false unless tree.is_a?(::Hash)
104
+
105
+ (tree[:children] || []).any? do |r|
106
+ next false unless NS_EMITTABLE_ROW_KINDS.include?(r[:kind])
107
+ next true if r[:ns] == :unqualified
108
+
109
+ r[:plan] && PLANNED_ROW_KINDS.include?(r[:kind]) &&
110
+ unqualified_element_row?(r[:plan])
111
+ end
112
+ end
113
+
83
114
  # Whether the tree's EXACT-ns rows (tree, attribute rows,
84
115
  # child rows, recursively through nested plans) carry more
85
116
  # than one distinct namespace URI.
@@ -230,16 +261,58 @@ module Lutaml
230
261
  return false if instance.element_order.nil?
231
262
  return false if instance.mutated_after_parse?
232
263
  return false unless instance.element_order.all? do |o|
233
- (o.type == "Element") ||
234
- (o.type == "Text" && o.node_type != :cdata)
264
+ PlanSerializer.order_entry?(o) &&
265
+ ((o.type == "Element") ||
266
+ (o.type == "Text" && o.node_type != :cdata))
235
267
  end
268
+
269
+ # A pristine order can still leave a value unemitted —
270
+ # in-place collection growth never trips the mutation
271
+ # flag. Reconciliation is the DOM lane's; the gate pays
272
+ # the coverage pre-check the DOM walk would run.
273
+ ns_index = PlanSerializer.ns_index_for(plan)
274
+ rows_by_name, name_keys =
275
+ PlanSerializer.ordered_layout_for(plan, nil, ns_index, register)
276
+ return false if PlanSerializer.order_uncovered?(
277
+ instance, plan, rows_by_name, name_keys,
278
+ instance.element_order, register
279
+ )
280
+ end
281
+
282
+ # Attribute values spelling through a type's own to_xml hook
283
+ # are written by the DOM lane's text path.
284
+ plan[:attr_rows].each do |(_rule, attr, _name)|
285
+ return false if PlanSerializer.custom_xml_type?(attr.type(register))
236
286
  end
237
287
 
238
288
  return true unless instance.is_a?(::Lutaml::Model::Serialize)
239
289
 
240
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
241
299
  next unless rule.name
242
300
 
301
+ # A type hooking the XML format through its own to_xml is
302
+ # spelled by the DOM lane's own text path.
303
+ if TEXT_ROW_KINDS.include?(kind) &&
304
+ PlanSerializer.custom_xml_type?(attr.type(register))
305
+ return false
306
+ end
307
+
308
+ # A nil/empty value rendering a policy leaf (xsi:nil,
309
+ # blank) has no ops spelling: the DOM lane mints it.
310
+ if PlanSerializer::PLACEHOLDER_KINDS.include?(kind)
311
+ value = value_of(instance, attr, delegate)
312
+ return false if PlanSerializer.render_value?(rule, value, instance) &&
313
+ PlanSerializer.placeholder_for(rule, value)
314
+ end
315
+
243
316
  if kind == :child_deferred
244
317
  # A VALUED cycle-point row keeps the DOM lane: the
245
318
  # native ordered replay has no ops spelling for it —
@@ -254,10 +327,18 @@ module Lutaml
254
327
  end
255
328
  next unless NESTED_ROW_KINDS.include?(kind)
256
329
 
330
+ declared_type = _type || attr.type(register)
257
331
  value = value_of(instance, attr, delegate)
258
332
  Array(value).each do |item|
259
333
  next unless item
260
334
 
335
+ # A value of another class than the row's declared type
336
+ # serializes through its own mapping (the interpretive
337
+ # dispatch on value.class); the emission graph is built
338
+ # from the declared type's plan — the DOM lane compiles
339
+ # the item's own.
340
+ return false unless item.instance_of?(declared_type)
341
+
261
342
  # leptris#1966: the emit compiles the ITEM CLASS's
262
343
  # plan (nested_plans[[item.class, name]]) — the gate
263
344
  # must gate exactly what emit emits. Unknown classes
@@ -332,6 +413,13 @@ module Lutaml
332
413
  # ops spelling matches it, inherited down the tree.
333
414
  def ns_emittable?(ns_form, prefixed_source = nil)
334
415
  return true if ns_form.nil? || ns_form == :unqualified
416
+ # A :blank row (a local element of a form-not-set parent —
417
+ # in no namespace) follows the enclosing namespace's format:
418
+ # under a prefix-preserved root the ops spelling inherits
419
+ # the prefix, like the DOM lane; under the default-namespace
420
+ # one it must opt out with xmlns="" — the DOM lane's.
421
+ return prefixed_source ? true : false if ns_form == :blank
422
+
335
423
  return false unless ns_form.is_a?(Hash) && ns_form[:prefix] &&
336
424
  !ns_form[:prefix].empty?
337
425
 
@@ -481,7 +569,10 @@ module Lutaml
481
569
  next
482
570
  end
483
571
  name = rule.name ? rule.name.to_s : ""
484
- ns = ns_index.key?(name) ? { ns: ns_index[name] } : {}
572
+ # :blank has no row form: the ops spelling inherits the
573
+ # plan's, exactly as a form-less row did.
574
+ form = ns_index[name]
575
+ ns = form.nil? || form == :blank ? {} : { ns: form }
485
576
  case kind
486
577
  when :scalar
487
578
  out << { name: name, kind: :scalar, **ns }
@@ -560,6 +651,10 @@ module Lutaml
560
651
  text_node_index += 1
561
652
  next if text.nil?
562
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
563
658
 
564
659
  op_slot(buf, len)
565
660
  buf[len][:kind] = :scalar
@@ -623,7 +718,7 @@ module Lutaml
623
718
  buf[len][:plan_index] = my_index
624
719
  buf[len][:row_index] = row_index
625
720
  len += 1
626
- len = if child_plan[:ordered] || !item.element_order.nil?
721
+ len = if PlanSerializer.ordered_walk?(child_plan, item)
627
722
  emit_ordered(buf, len, item, child_plan, register,
628
723
  child_index, graph)
629
724
  else
@@ -765,11 +860,12 @@ module Lutaml
765
860
  len += 1
766
861
  end
767
862
  when :content, :content_deferred
768
- # Mixed-content runs ride SCALAR ops on the content
769
- # row itself — the engine renders CONTENT-kind rows as
770
- # bare escaped text. Unreachable from the non-ordered
771
- # lane (tree_emittable? refuses content rows); the
772
- # 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.
773
869
  Array(value).each do |run|
774
870
  op_slot(buf, len)
775
871
  buf[len][:kind] = :scalar
@@ -798,7 +894,7 @@ module Lutaml
798
894
  buf[len][:plan_index] = my_index
799
895
  buf[len][:row_index] = row_index
800
896
  len += 1
801
- len = if child_plan[:ordered] || !item.element_order.nil?
897
+ len = if PlanSerializer.ordered_walk?(child_plan, item)
802
898
  emit_ordered(buf, len, item, child_plan, register,
803
899
  child_index, graph)
804
900
  else