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.
@@ -50,15 +50,44 @@ 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
67
+
68
+ # Signature holder: the (attribute count, rule count) a plan was
69
+ # compiled against, keyed like PLAN_CACHE.
70
+ PLAN_SIGNATURES = ::Class.new do
71
+ class << self
72
+ def cache
73
+ @cache ||= {}
74
+ end
75
+ end
76
+ end.cache
60
77
 
61
78
  class << self
79
+ # Drop every compiled plan of a model class: its caches were
80
+ # cleared (Serialization#clear_cache).
81
+ def invalidate!(model_class)
82
+ PLAN_CACHE.each_key.to_a.each do |key|
83
+ next unless key.first.equal?(model_class)
84
+
85
+ PLAN_CACHE.delete(key)
86
+ PLAN_SIGNATURES.delete(key)
87
+ end
88
+ nil
89
+ end
90
+
62
91
  def compile(model_class, register)
63
92
  # Version gate: below MIN_LEPTRIS_VERSION (the single
64
93
  # capability floor) gems take the interpretive path via
@@ -77,7 +106,19 @@ module Lutaml
77
106
  )
78
107
 
79
108
  key = [model_class, register]
80
- return PLAN_CACHE[key] if PLAN_CACHE.key?(key)
109
+ # A late-added attribute or rule (the XMI extension loader
110
+ # re-runs the xml DSL on a defined class) must reach the plan:
111
+ # the compiled shape is checked against the class's current
112
+ # attribute and rule counts.
113
+ signature = plan_signature(model_class, register)
114
+ if PLAN_CACHE.key?(key)
115
+ return PLAN_CACHE[key] if PLAN_SIGNATURES[key] == signature
116
+
117
+ # The class changed under its compiled plan (the XMI
118
+ # extension loader re-runs the xml DSL on a defined class):
119
+ # a late-bound class belongs to the interpretive pipeline.
120
+ return opt_out!(:class_changed)
121
+ end
81
122
 
82
123
  # Cycle guard: a self-referential model (JATS sec-in-sec) must
83
124
  # resolve to nil — the interpretive pipeline owns it. The guard
@@ -94,10 +135,21 @@ module Lutaml
94
135
 
95
136
  stack.push(key)
96
137
  begin
97
- PLAN_CACHE[key] = build(model_class, register)
138
+ PLAN_CACHE[key] = plan = build(model_class, register)
139
+ PLAN_SIGNATURES[key] = signature
98
140
  ensure
99
141
  stack.pop
100
142
  end
143
+ plan
144
+ end
145
+
146
+ def plan_signature(model_class, register)
147
+ return nil unless model_class.is_a?(Class) &&
148
+ model_class.include?(::Lutaml::Model::Serialize)
149
+
150
+ mapping = model_class.mappings_for(:xml, register)
151
+ [model_class.attributes(register).size,
152
+ mapping&.mappings(register)&.size]
101
153
  end
102
154
 
103
155
  private
@@ -138,7 +190,7 @@ module Lutaml
138
190
  rows = []
139
191
  attr_rows = [] # [[rule, attr, row_name]] hydration metadata
140
192
  plan_attrs = [] # [{name:, kind:}] rows for the engine plan
141
- compiled = [] # [rule, attr, kind, spelling, delegate_target]
193
+ compiled = [] # [rule, attr, kind, spelling, delegate_name(, type)]
142
194
  ns_attr_names = [] # exact-URI attribute rows (ambiguity guard)
143
195
  # Local-name claim tallies for attribute rows: a name claimed
144
196
  # by both an exact-URI row and a plain (any-namespace) row
@@ -150,9 +202,26 @@ module Lutaml
150
202
  cdata = false
151
203
  mixed_content = false
152
204
  needs_nodes = false
153
- collection_defaults = [] # collection attrs with element rows
154
205
  tag = 100 # type_tag echo space for callback-routed rows
155
206
  model_ns = plan_namespace(model_class, mapping, register)
207
+ # The interpretive writer's elementFormDefault contract
208
+ # (ElementBuilder.determine_element_namespace +
209
+ # DeclarationPlanner's blank-namespace rule) for a locally
210
+ # declared element with no namespace of its own:
211
+ # - form explicitly :qualified — it inherits the namespace
212
+ # - form explicitly :unqualified — :unqualified: spelled bare
213
+ # whatever the root carries
214
+ # - form not set — :blank: it is in NO namespace of its own;
215
+ # the serializer spells it through the enclosing
216
+ # namespace's format and, under the default-namespace
217
+ # one, opts it out with xmlns="" (BlankScope)
218
+ # Rows whose type or child model declares a namespace keep it.
219
+ ns_class = mapping.namespace_class
220
+ declared_form = if ns_class&.element_form_default_set?
221
+ ns_class.element_form_default
222
+ end
223
+ unqualified_form = declared_form == :unqualified
224
+ blank_form = !ns_class.nil? && declared_form.nil?
156
225
 
157
226
  # Collection rows are NATIVE since leptris 1.9.178 —
158
227
  # collection values echo name and type_tag (the #220 gap is
@@ -170,9 +239,22 @@ module Lutaml
170
239
  else
171
240
  model_class.attributes(register)[rule.to]
172
241
  end
242
+ # Rows carry the delegate NAME: every lane sends it to the
243
+ # instance (value_of). The target attribute above only
244
+ # resolves the child attribute type here; storing it in the
245
+ # row made every delegate: rule crash the leptris lanes.
246
+ delegate_ref = delegate_target && rule.delegate
173
247
  return opt_out!(:attr_nil) if attr.nil?
174
248
  return opt_out!(:derived) if attr.derived?
175
249
 
250
+ # A Hash-typed value has no scalar spelling: the interpretive
251
+ # writer expands it into child elements and the reader
252
+ # rebuilds it (a scalar row serialized its to_s and crashed
253
+ # Hash.cast on the way back).
254
+ attr_type = attr.type(register)
255
+ return opt_out!(:hash_typed) if attr_type.is_a?(Class) &&
256
+ attr_type <= ::Lutaml::Model::Type::Hash
257
+
176
258
  # Partition rows (TODO 34 step 2) ride native predicates
177
259
  # only in the plain-capture shape; any other when_attribute
178
260
  # shape stays interpretive (the suite pins nested-target
@@ -188,13 +270,6 @@ module Lutaml
188
270
  return opt_out!(:non_plain_partition) unless plain_partition
189
271
  end
190
272
 
191
- # Interpretive hydration materializes every mapped
192
- # collection, present or not; the fast path mirrors with
193
- # constructor-time empty arrays.
194
- unless rule.attribute? || !attr.collection?
195
- collection_defaults << attr.name.to_sym
196
- end
197
-
198
273
  # Fragment deferrals lose ancestor namespace context —
199
274
  # ns-qualified models keep those rules interpretive.
200
275
  fragment_needed = rule.has_custom_method_for_deserialization? ||
@@ -212,50 +287,55 @@ module Lutaml
212
287
  mixed_content = true
213
288
  compiled << [rule, attr,
214
289
  attr.collection? ? :content : :content_deferred,
215
- nil, delegate_target]
290
+ nil, delegate_ref]
216
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)
217
298
  elsif rule.raw_mapping? || rule.raw == :element
218
- compiled << [rule, attr, :raw, nil, delegate_target]
299
+ compiled << [rule, attr, :raw, nil, delegate_ref]
219
300
  rows << { name: rule.name.to_s, kind: :raw }
220
301
  elsif rule.has_custom_method_for_deserialization?
221
302
  needs_nodes = true
222
- compiled << [rule, attr, :custom_method, nil, delegate_target]
303
+ compiled << [rule, attr, :custom_method, nil, delegate_ref]
223
304
  rows << { name: rule.name.to_s, kind: :raw }
224
- elsif rule.polymorphic_mapping? || attr.polymorphic? || attr.union?
305
+ elsif polymorphic_shape?(rule, attr)
225
306
  type = attr.type(register)
226
307
  return opt_out!(:non_serializable_poly) unless serializable_type?(type) || attr.union?
227
308
 
228
309
  needs_nodes = true
229
- compiled << [rule, attr, :polymorphic, nil, delegate_target]
310
+ compiled << [rule, attr, :polymorphic, nil, delegate_ref]
230
311
  rows << { name: rule.name.to_s, kind: :raw }
231
312
  else
232
313
  type = attr.type(register)
314
+ return opt_out!(:nested_collection) if nested_collection_attribute?(attr, type)
315
+
233
316
  if serializable_type?(type)
234
317
  child = compile(type, register)
235
- if child.nil? &&
236
- !Lutaml::Model::Config.instance.xml_plan_ordered_path
237
- child_mapping = type.mappings_for(:xml, register)
238
- if child_mapping&.ordered? ||
239
- child_mapping&.mixed_content?
240
- # A child refused ONLY by the ordered opt-in gate
241
- # cascades: the bridged-island deferral drops its
242
- # mixed children (customxml schemaRefs), so the
243
- # parent compiles to nil — main's whole-model
244
- # interpretive shape.
245
- return opt_out!(:ordered_opt_in_cascade)
246
- end
318
+ if ordered_cascade_refusal?(type, register, child)
319
+ return opt_out!(:ordered_opt_in_cascade)
247
320
  end
321
+
248
322
  needs_nodes = true if child.nil? || child[:ordered]
249
323
  compiled_entry, row = child_row_for(rule, attr, type, child,
250
- delegate_target,
251
- model_ns)
324
+ delegate_ref,
325
+ model_ns,
326
+ local_element_form(
327
+ child && child[:tree][:ns],
328
+ model_ns, rule,
329
+ unqualified_form,
330
+ blank_form
331
+ ))
252
332
  compiled << compiled_entry
253
333
  needs_nodes ||= child && child[:needs_nodes]
254
334
  rows << row
255
335
  elsif rule.multiple_mappings?
256
336
  rule.name.each do |spelling|
257
337
  compiled << [rule, attr, :spelling, spelling.to_s,
258
- delegate_target]
338
+ delegate_ref]
259
339
  rows << { name: spelling.to_s, kind: :callback,
260
340
  type_tag: (tag += 1) }
261
341
  end
@@ -263,22 +343,12 @@ module Lutaml
263
343
  return opt_out!(:non_scalar) unless scalar_type?(attr, register)
264
344
 
265
345
  row = { name: rule.name.to_s }
266
- if rule.namespace_set?
267
- row[:ns] = child_ns(rule, model_ns)
268
- else
269
- # The interpretive writer qualifies an element row
270
- # by its attribute's type namespace when the rule
271
- # declares none (resolve step 4). Rows without the
272
- # type form inherit the tree namespace and land
273
- # under the wrong default xmlns (uniword
274
- # CoreProperties: <dc:title> spelled <title>).
275
- type_ns_form = element_type_ns_form(rule, attr,
276
- register, model_ns)
277
- return opt_out!(:elem_type_ns_symbol) if type_ns_form == :bail_symbol
278
- return opt_out!(:elem_type_ns_aliases) if type_ns_form == :bail_aliases
279
-
280
- row[:ns] = type_ns_form if type_ns_form
281
- end
346
+ ns = scalar_row_ns(rule, attr, register, model_ns,
347
+ unqualified_form, blank_form)
348
+ return opt_out!(:elem_type_ns_symbol) if ns == :bail_symbol
349
+ return opt_out!(:elem_type_ns_aliases) if ns == :bail_aliases
350
+
351
+ row[:ns] = ns if ns
282
352
  # lutaml-model#88: same-name rows partitioned by the
283
353
  # rule's discriminator ride the engine's exclusive
284
354
  # predicate match (leptris 1.9.221+, #1272).
@@ -286,11 +356,11 @@ module Lutaml
286
356
 
287
357
  if attr.collection?
288
358
  compiled << [rule, attr, :collection_native, nil,
289
- delegate_target]
359
+ delegate_ref]
290
360
  rows << row.merge(kind: :collection)
291
361
  else
292
362
  cdata ||= rule.cdata
293
- compiled << [rule, attr, :scalar, nil, delegate_target]
363
+ compiled << [rule, attr, :scalar, nil, delegate_ref]
294
364
  rows << row.merge(kind: :scalar)
295
365
  end
296
366
  end
@@ -391,17 +461,35 @@ module Lutaml
391
461
  # Lazy: the leptris require only belongs on the
392
462
  # engines-enabled path.
393
463
  require "leptris/xml/descriptor"
394
- descriptor = ::Leptris::XML::Descriptor.build(**tree)
464
+ # :blank is the serializer's no-namespace form. The engine
465
+ # captures such a row by name at any qualification (no ns
466
+ # form — main's behavior for a locally declared element):
467
+ # the source may spell it bare or with the parent's
468
+ # prefix, and the instance's own format preservation
469
+ # decides the spelling on the way out.
470
+ descriptor = ::Leptris::XML::Descriptor.build(
471
+ **tree, children: rows.map do |r|
472
+ r[:ns] == :blank ? r.except(:ns) : r
473
+ end
474
+ )
395
475
  rescue StandardError
396
476
  return nil
397
477
  end
398
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
+
399
486
  { descriptor: descriptor, tree: tree, rows: compiled,
400
487
  attr_rows: attr_rows, mapping: mapping,
401
488
  row_tags: row_tags, namespaced: namespaced,
402
489
  ordered: mapping.ordered? || mapping.mixed_content?,
490
+ sequence: sequence,
403
491
  needs_nodes: needs_nodes,
404
- collection_defaults: collection_defaults,
492
+ needs_buckets: compiled.any? { |row| BUCKET_ROW_KINDS.include?(row[2]) },
405
493
  contested_names: contested.empty? ? nil : contested,
406
494
  ns_attr_names: ns_attr_names.empty? ? nil : ns_attr_names,
407
495
  wildcard_tag: (wildcard_tag if defined?(wildcard_tag)) }
@@ -531,22 +619,75 @@ module Lutaml
531
619
  # rows; nothing is lost under a namespaced parent.
532
620
  # - otherwise: :nested with the child's tree.
533
621
  # Returns [[compiled tuple], tree row].
534
- def child_row_for(rule, attr, type, child, delegate_target,
535
- model_ns)
622
+ def child_row_for(rule, attr, type, child, delegate_ref,
623
+ model_ns, local_form = nil)
536
624
  if child.nil?
537
- [[rule, attr, :child_deferred, nil, delegate_target, type],
625
+ [[rule, attr, :child_deferred, nil, delegate_ref, type],
538
626
  { name: rule.name.to_s, kind: :raw }]
539
627
  elsif child[:ordered]
540
- [[rule, attr, :ordered_deferred, nil, delegate_target, type],
628
+ [[rule, attr, :ordered_deferred, nil, delegate_ref, type],
541
629
  { name: rule.name.to_s, kind: :raw }]
542
630
  else
543
631
  row = { name: rule.name.to_s, kind: :nested,
544
632
  plan: child[:tree] }
545
- row[:ns] = child_ns(rule, model_ns) if rule.namespace_set?
546
- [[rule, attr, :nested, nil, delegate_target, type], row]
633
+ if rule.namespace_set?
634
+ row[:ns] = child_ns(rule, model_ns)
635
+ elsif local_form
636
+ row[:ns] = local_form
637
+ end
638
+ [[rule, attr, :nested, nil, delegate_ref, type], row]
547
639
  end
548
640
  end
549
641
 
642
+ # The ns form of a scalar element row: the rule's own
643
+ # namespace, else the attribute type's (the interpretive
644
+ # resolve step 4 — nil when it equals the model's own), else
645
+ # the local element's form per the parent mapping's
646
+ # elementFormDefault. A locally declared scalar under an
647
+ # explicitly :unqualified parent in its own namespace is
648
+ # spelled bare (apply_unqualified_form_default); a type with
649
+ # no namespace at all follows the parent's form. Returns the
650
+ # opt-out clause when the type form is not row-expressible.
651
+ def scalar_row_ns(rule, attr, register, model_ns, unqualified_form,
652
+ blank_form)
653
+ return child_ns(rule, model_ns) if rule.namespace_set?
654
+
655
+ type_ns_form = element_type_ns_form(rule, attr, register, model_ns)
656
+ return type_ns_form if type_ns_form
657
+ return unless unqualified_row?(rule)
658
+
659
+ local_element_form(nil, model_ns, rule, unqualified_form,
660
+ blank_form &&
661
+ attr.type_namespace_class(register).nil?)
662
+ end
663
+
664
+ # The ns form of a locally declared element (no rule
665
+ # namespace), per the parent mapping's elementFormDefault: the
666
+ # element's own namespace (a nested model's) is kept unless the
667
+ # explicit :unqualified form spells it bare; without one it
668
+ # follows the parent's form — :unqualified, or :blank (no
669
+ # namespace) when the form is not set.
670
+ def local_element_form(own_ns, model_ns, rule, unqualified_form,
671
+ blank_form)
672
+ return unless unqualified_row?(rule)
673
+
674
+ # The rule's own form: :unqualified always spells bare
675
+ # (determine_element_namespace priority 3), whatever the
676
+ # parent's form declares.
677
+ return :unqualified if rule.form == :unqualified
678
+
679
+ own_uri = own_ns.is_a?(::Hash) ? own_ns[:exact] : nil
680
+ if own_uri
681
+ return :unqualified if unqualified_form &&
682
+ own_uri == model_ns&.[](:exact)
683
+ elsif unqualified_form
684
+ return :unqualified
685
+ elsif blank_form
686
+ return :blank
687
+ end
688
+ nil
689
+ end
690
+
550
691
  def compilable_mapping?(mapping)
551
692
  # Mapping's uniform interface: root_mappings defaults to
552
693
  # false on the base (KeyValue overrides with its own).
@@ -636,6 +777,13 @@ module Lutaml
636
777
  form
637
778
  end
638
779
 
780
+ # A locally declared element the writer would leave to its
781
+ # parent's form: no rule namespace, and no :qualified form
782
+ # override forcing inheritance.
783
+ def unqualified_row?(rule)
784
+ !rule.namespace_set? && rule.form != :qualified
785
+ end
786
+
639
787
  def content_rows(rows)
640
788
  rows.count { |r| r[:kind] == :content }
641
789
  end
@@ -653,6 +801,47 @@ module Lutaml
653
801
  prefix: ns_class.prefix_default&.to_s }
654
802
  end
655
803
 
804
+ # Whether a row resolves its child class at parse time — the
805
+ # interpretive cast's own inputs: the rule's polymorphic option
806
+ # (a class map or `true`), its legacy polymorphic_map, the
807
+ # attribute's polymorphic class, or a union. Such a row defers
808
+ # its subtree to the interpretive cast.
809
+ def polymorphic_shape?(rule, attr)
810
+ option = rule.polymorphic
811
+ rule.polymorphic_mapping? ||
812
+ (option.is_a?(::Hash) ? !option.empty? : !!option) ||
813
+ attr.polymorphic? || attr.union?
814
+ end
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
+
656
845
  def scalar_type?(attr, register)
657
846
  type = attr.type(register)
658
847
  type.is_a?(Class) && type < ::Lutaml::Model::Type::Value &&