lutaml-model 0.8.204 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 230c2c513344d8665bb44607cefc80663100ac9707ee8ce63146e5b8ed6cbdbc
4
- data.tar.gz: 2ac13fcbc47d3b7011c3957a6d988d3c996cf10fee05fe04040f82979b89d928
3
+ metadata.gz: 04adc7ed9ee1f1ddae6380fd82e5efac2ba007eec5dd096b2af1fc421a36416b
4
+ data.tar.gz: f9c02256da6c8abce558566e4ad70382dd3abed1b58a0c01426b84044f54b2d5
5
5
  SHA512:
6
- metadata.gz: 1d0a5e9ac99b9ef4f68b6eb629ade317097f72d6d54187b3521f7007ccdfda47a4c03d7143040fe86987072b992d7e4741ef62c8b1a683f310b5191c391489bc
7
- data.tar.gz: a0300c85e81232f5a07151c12d69575d0d0caf38c2540268baffcffe57d7d8744802a89030be6fd597f0109f148eaa9fcceddfcee2cb90f04fcade9696cab5ac
6
+ metadata.gz: 8d99a3063c1eca8a7117d5e5e3dcca73244d9d763e85868a8584f27fe87f0043e08ef946066f9520aa8300b37b5e605db808003d76a97d9401f6af0d4385f3a9
7
+ data.tar.gz: bedfecb6db1fa494a314f7ecc95ec3f8ead2d1d412d3f74de930a102121cfad943b2a9a3e9884768bea3b1432d174284c8d0e1b55d5d5c441bd89edf284e4f38
@@ -76,6 +76,16 @@ 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. 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.
88
+
79
89
  Version floor note: leptris 1.9.320+ retains child-row namespace
80
90
  URIs engine-side and matches plain attribute rows against
81
91
  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.205"
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
@@ -60,6 +63,15 @@ module Lutaml
60
63
  return false if namespaced_tree?(plan[:tree]) &&
61
64
  !engine_ns_prefix_safe?
62
65
  return false unless ordered_ns_emittable?(plan, instance)
66
+ # A prefixed-parsed root pins the root's prefix form (format
67
+ # preservation, priority 1), and the ops spelling inherits
68
+ # that prefix down the tree — including over rows the
69
+ # elementFormDefault contract spells :unqualified. The DOM
70
+ # lane spells those bare; the ops lane cannot, so a prefixed
71
+ # source carrying an unqualified element row stays off it.
72
+ return false if instance.is_a?(::Lutaml::Model::Serialize) &&
73
+ prefixed_source?(instance) &&
74
+ unqualified_element_row?(plan[:tree])
63
75
  return false if plan[:ordered] && instance.is_a?(::Lutaml::Model::Serialize) &&
64
76
  instance.element_order.nil?
65
77
  # An UNORDERED instance (no parse-captured element_order)
@@ -80,6 +92,20 @@ module Lutaml
80
92
  subtree_gate_ok?(plan, instance, register)
81
93
  end
82
94
 
95
+ # Whether any named element row in the emission graph is spelled
96
+ # :unqualified — a child row, or one inside a nested plan.
97
+ def unqualified_element_row?(tree)
98
+ return false unless tree.is_a?(::Hash)
99
+
100
+ (tree[:children] || []).any? do |r|
101
+ next false unless NS_EMITTABLE_ROW_KINDS.include?(r[:kind])
102
+ next true if r[:ns] == :unqualified
103
+
104
+ r[:plan] && PLANNED_ROW_KINDS.include?(r[:kind]) &&
105
+ unqualified_element_row?(r[:plan])
106
+ end
107
+ end
108
+
83
109
  # Whether the tree's EXACT-ns rows (tree, attribute rows,
84
110
  # child rows, recursively through nested plans) carry more
85
111
  # than one distinct namespace URI.
@@ -230,9 +256,28 @@ module Lutaml
230
256
  return false if instance.element_order.nil?
231
257
  return false if instance.mutated_after_parse?
232
258
  return false unless instance.element_order.all? do |o|
233
- (o.type == "Element") ||
234
- (o.type == "Text" && o.node_type != :cdata)
259
+ PlanSerializer.order_entry?(o) &&
260
+ ((o.type == "Element") ||
261
+ (o.type == "Text" && o.node_type != :cdata))
235
262
  end
263
+
264
+ # A pristine order can still leave a value unemitted —
265
+ # in-place collection growth never trips the mutation
266
+ # flag. Reconciliation is the DOM lane's; the gate pays
267
+ # the coverage pre-check the DOM walk would run.
268
+ ns_index = PlanSerializer.ns_index_for(plan)
269
+ rows_by_name, name_keys =
270
+ PlanSerializer.ordered_layout_for(plan, nil, ns_index, register)
271
+ return false if PlanSerializer.order_uncovered?(
272
+ instance, plan, rows_by_name, name_keys,
273
+ instance.element_order, register
274
+ )
275
+ end
276
+
277
+ # Attribute values spelling through a type's own to_xml hook
278
+ # are written by the DOM lane's text path.
279
+ plan[:attr_rows].each do |(_rule, attr, _name)|
280
+ return false if PlanSerializer.custom_xml_type?(attr.type(register))
236
281
  end
237
282
 
238
283
  return true unless instance.is_a?(::Lutaml::Model::Serialize)
@@ -240,6 +285,21 @@ module Lutaml
240
285
  plan[:rows].each do |(rule, attr, kind, _sp, delegate, _type)|
241
286
  next unless rule.name
242
287
 
288
+ # A type hooking the XML format through its own to_xml is
289
+ # spelled by the DOM lane's own text path.
290
+ if TEXT_ROW_KINDS.include?(kind) &&
291
+ PlanSerializer.custom_xml_type?(attr.type(register))
292
+ return false
293
+ end
294
+
295
+ # A nil/empty value rendering a policy leaf (xsi:nil,
296
+ # blank) has no ops spelling: the DOM lane mints it.
297
+ if PlanSerializer::PLACEHOLDER_KINDS.include?(kind)
298
+ value = value_of(instance, attr, delegate)
299
+ return false if PlanSerializer.render_value?(rule, value, instance) &&
300
+ PlanSerializer.placeholder_for(rule, value)
301
+ end
302
+
243
303
  if kind == :child_deferred
244
304
  # A VALUED cycle-point row keeps the DOM lane: the
245
305
  # native ordered replay has no ops spelling for it —
@@ -254,10 +314,18 @@ module Lutaml
254
314
  end
255
315
  next unless NESTED_ROW_KINDS.include?(kind)
256
316
 
317
+ declared_type = _type || attr.type(register)
257
318
  value = value_of(instance, attr, delegate)
258
319
  Array(value).each do |item|
259
320
  next unless item
260
321
 
322
+ # A value of another class than the row's declared type
323
+ # serializes through its own mapping (the interpretive
324
+ # dispatch on value.class); the emission graph is built
325
+ # from the declared type's plan — the DOM lane compiles
326
+ # the item's own.
327
+ return false unless item.instance_of?(declared_type)
328
+
261
329
  # leptris#1966: the emit compiles the ITEM CLASS's
262
330
  # plan (nested_plans[[item.class, name]]) — the gate
263
331
  # must gate exactly what emit emits. Unknown classes
@@ -332,6 +400,13 @@ module Lutaml
332
400
  # ops spelling matches it, inherited down the tree.
333
401
  def ns_emittable?(ns_form, prefixed_source = nil)
334
402
  return true if ns_form.nil? || ns_form == :unqualified
403
+ # A :blank row (a local element of a form-not-set parent —
404
+ # in no namespace) follows the enclosing namespace's format:
405
+ # under a prefix-preserved root the ops spelling inherits
406
+ # the prefix, like the DOM lane; under the default-namespace
407
+ # one it must opt out with xmlns="" — the DOM lane's.
408
+ return prefixed_source ? true : false if ns_form == :blank
409
+
335
410
  return false unless ns_form.is_a?(Hash) && ns_form[:prefix] &&
336
411
  !ns_form[:prefix].empty?
337
412
 
@@ -481,7 +556,10 @@ module Lutaml
481
556
  next
482
557
  end
483
558
  name = rule.name ? rule.name.to_s : ""
484
- ns = ns_index.key?(name) ? { ns: ns_index[name] } : {}
559
+ # :blank has no row form: the ops spelling inherits the
560
+ # plan's, exactly as a form-less row did.
561
+ form = ns_index[name]
562
+ ns = form.nil? || form == :blank ? {} : { ns: form }
485
563
  case kind
486
564
  when :scalar
487
565
  out << { name: name, kind: :scalar, **ns }
@@ -58,7 +58,29 @@ module Lutaml
58
58
  PER_NODE_ROW_KINDS = %i[nested ordered_deferred
59
59
  child_deferred].freeze
60
60
 
61
+ # Signature holder: the (attribute count, rule count) a plan was
62
+ # compiled against, keyed like PLAN_CACHE.
63
+ PLAN_SIGNATURES = ::Class.new do
64
+ class << self
65
+ def cache
66
+ @cache ||= {}
67
+ end
68
+ end
69
+ end.cache
70
+
61
71
  class << self
72
+ # Drop every compiled plan of a model class: its caches were
73
+ # cleared (Serialization#clear_cache).
74
+ def invalidate!(model_class)
75
+ PLAN_CACHE.each_key.to_a.each do |key|
76
+ next unless key.first.equal?(model_class)
77
+
78
+ PLAN_CACHE.delete(key)
79
+ PLAN_SIGNATURES.delete(key)
80
+ end
81
+ nil
82
+ end
83
+
62
84
  def compile(model_class, register)
63
85
  # Version gate: below MIN_LEPTRIS_VERSION (the single
64
86
  # capability floor) gems take the interpretive path via
@@ -77,7 +99,19 @@ module Lutaml
77
99
  )
78
100
 
79
101
  key = [model_class, register]
80
- return PLAN_CACHE[key] if PLAN_CACHE.key?(key)
102
+ # A late-added attribute or rule (the XMI extension loader
103
+ # re-runs the xml DSL on a defined class) must reach the plan:
104
+ # the compiled shape is checked against the class's current
105
+ # attribute and rule counts.
106
+ signature = plan_signature(model_class, register)
107
+ if PLAN_CACHE.key?(key)
108
+ return PLAN_CACHE[key] if PLAN_SIGNATURES[key] == signature
109
+
110
+ # The class changed under its compiled plan (the XMI
111
+ # extension loader re-runs the xml DSL on a defined class):
112
+ # a late-bound class belongs to the interpretive pipeline.
113
+ return opt_out!(:class_changed)
114
+ end
81
115
 
82
116
  # Cycle guard: a self-referential model (JATS sec-in-sec) must
83
117
  # resolve to nil — the interpretive pipeline owns it. The guard
@@ -94,10 +128,21 @@ module Lutaml
94
128
 
95
129
  stack.push(key)
96
130
  begin
97
- PLAN_CACHE[key] = build(model_class, register)
131
+ PLAN_CACHE[key] = plan = build(model_class, register)
132
+ PLAN_SIGNATURES[key] = signature
98
133
  ensure
99
134
  stack.pop
100
135
  end
136
+ plan
137
+ end
138
+
139
+ def plan_signature(model_class, register)
140
+ return nil unless model_class.is_a?(Class) &&
141
+ model_class.include?(::Lutaml::Model::Serialize)
142
+
143
+ mapping = model_class.mappings_for(:xml, register)
144
+ [model_class.attributes(register).size,
145
+ mapping&.mappings(register)&.size]
101
146
  end
102
147
 
103
148
  private
@@ -138,7 +183,7 @@ module Lutaml
138
183
  rows = []
139
184
  attr_rows = [] # [[rule, attr, row_name]] hydration metadata
140
185
  plan_attrs = [] # [{name:, kind:}] rows for the engine plan
141
- compiled = [] # [rule, attr, kind, spelling, delegate_target]
186
+ compiled = [] # [rule, attr, kind, spelling, delegate_name(, type)]
142
187
  ns_attr_names = [] # exact-URI attribute rows (ambiguity guard)
143
188
  # Local-name claim tallies for attribute rows: a name claimed
144
189
  # by both an exact-URI row and a plain (any-namespace) row
@@ -150,9 +195,26 @@ module Lutaml
150
195
  cdata = false
151
196
  mixed_content = false
152
197
  needs_nodes = false
153
- collection_defaults = [] # collection attrs with element rows
154
198
  tag = 100 # type_tag echo space for callback-routed rows
155
199
  model_ns = plan_namespace(model_class, mapping, register)
200
+ # The interpretive writer's elementFormDefault contract
201
+ # (ElementBuilder.determine_element_namespace +
202
+ # DeclarationPlanner's blank-namespace rule) for a locally
203
+ # declared element with no namespace of its own:
204
+ # - form explicitly :qualified — it inherits the namespace
205
+ # - form explicitly :unqualified — :unqualified: spelled bare
206
+ # whatever the root carries
207
+ # - form not set — :blank: it is in NO namespace of its own;
208
+ # the serializer spells it through the enclosing
209
+ # namespace's format and, under the default-namespace
210
+ # one, opts it out with xmlns="" (BlankScope)
211
+ # Rows whose type or child model declares a namespace keep it.
212
+ ns_class = mapping.namespace_class
213
+ declared_form = if ns_class&.element_form_default_set?
214
+ ns_class.element_form_default
215
+ end
216
+ unqualified_form = declared_form == :unqualified
217
+ blank_form = !ns_class.nil? && declared_form.nil?
156
218
 
157
219
  # Collection rows are NATIVE since leptris 1.9.178 —
158
220
  # collection values echo name and type_tag (the #220 gap is
@@ -170,9 +232,22 @@ module Lutaml
170
232
  else
171
233
  model_class.attributes(register)[rule.to]
172
234
  end
235
+ # Rows carry the delegate NAME: every lane sends it to the
236
+ # instance (value_of). The target attribute above only
237
+ # resolves the child attribute type here; storing it in the
238
+ # row made every delegate: rule crash the leptris lanes.
239
+ delegate_ref = delegate_target && rule.delegate
173
240
  return opt_out!(:attr_nil) if attr.nil?
174
241
  return opt_out!(:derived) if attr.derived?
175
242
 
243
+ # A Hash-typed value has no scalar spelling: the interpretive
244
+ # writer expands it into child elements and the reader
245
+ # rebuilds it (a scalar row serialized its to_s and crashed
246
+ # Hash.cast on the way back).
247
+ attr_type = attr.type(register)
248
+ return opt_out!(:hash_typed) if attr_type.is_a?(Class) &&
249
+ attr_type <= ::Lutaml::Model::Type::Hash
250
+
176
251
  # Partition rows (TODO 34 step 2) ride native predicates
177
252
  # only in the plain-capture shape; any other when_attribute
178
253
  # shape stays interpretive (the suite pins nested-target
@@ -188,13 +263,6 @@ module Lutaml
188
263
  return opt_out!(:non_plain_partition) unless plain_partition
189
264
  end
190
265
 
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
266
  # Fragment deferrals lose ancestor namespace context —
199
267
  # ns-qualified models keep those rules interpretive.
200
268
  fragment_needed = rule.has_custom_method_for_deserialization? ||
@@ -212,21 +280,21 @@ module Lutaml
212
280
  mixed_content = true
213
281
  compiled << [rule, attr,
214
282
  attr.collection? ? :content : :content_deferred,
215
- nil, delegate_target]
283
+ nil, delegate_ref]
216
284
  rows << { name: "__content__#{compiled.size}", kind: :content }
217
285
  elsif rule.raw_mapping? || rule.raw == :element
218
- compiled << [rule, attr, :raw, nil, delegate_target]
286
+ compiled << [rule, attr, :raw, nil, delegate_ref]
219
287
  rows << { name: rule.name.to_s, kind: :raw }
220
288
  elsif rule.has_custom_method_for_deserialization?
221
289
  needs_nodes = true
222
- compiled << [rule, attr, :custom_method, nil, delegate_target]
290
+ compiled << [rule, attr, :custom_method, nil, delegate_ref]
223
291
  rows << { name: rule.name.to_s, kind: :raw }
224
- elsif rule.polymorphic_mapping? || attr.polymorphic? || attr.union?
292
+ elsif polymorphic_shape?(rule, attr)
225
293
  type = attr.type(register)
226
294
  return opt_out!(:non_serializable_poly) unless serializable_type?(type) || attr.union?
227
295
 
228
296
  needs_nodes = true
229
- compiled << [rule, attr, :polymorphic, nil, delegate_target]
297
+ compiled << [rule, attr, :polymorphic, nil, delegate_ref]
230
298
  rows << { name: rule.name.to_s, kind: :raw }
231
299
  else
232
300
  type = attr.type(register)
@@ -247,15 +315,21 @@ module Lutaml
247
315
  end
248
316
  needs_nodes = true if child.nil? || child[:ordered]
249
317
  compiled_entry, row = child_row_for(rule, attr, type, child,
250
- delegate_target,
251
- model_ns)
318
+ delegate_ref,
319
+ model_ns,
320
+ local_element_form(
321
+ child && child[:tree][:ns],
322
+ model_ns, rule,
323
+ unqualified_form,
324
+ blank_form
325
+ ))
252
326
  compiled << compiled_entry
253
327
  needs_nodes ||= child && child[:needs_nodes]
254
328
  rows << row
255
329
  elsif rule.multiple_mappings?
256
330
  rule.name.each do |spelling|
257
331
  compiled << [rule, attr, :spelling, spelling.to_s,
258
- delegate_target]
332
+ delegate_ref]
259
333
  rows << { name: spelling.to_s, kind: :callback,
260
334
  type_tag: (tag += 1) }
261
335
  end
@@ -263,22 +337,12 @@ module Lutaml
263
337
  return opt_out!(:non_scalar) unless scalar_type?(attr, register)
264
338
 
265
339
  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
340
+ ns = scalar_row_ns(rule, attr, register, model_ns,
341
+ unqualified_form, blank_form)
342
+ return opt_out!(:elem_type_ns_symbol) if ns == :bail_symbol
343
+ return opt_out!(:elem_type_ns_aliases) if ns == :bail_aliases
344
+
345
+ row[:ns] = ns if ns
282
346
  # lutaml-model#88: same-name rows partitioned by the
283
347
  # rule's discriminator ride the engine's exclusive
284
348
  # predicate match (leptris 1.9.221+, #1272).
@@ -286,11 +350,11 @@ module Lutaml
286
350
 
287
351
  if attr.collection?
288
352
  compiled << [rule, attr, :collection_native, nil,
289
- delegate_target]
353
+ delegate_ref]
290
354
  rows << row.merge(kind: :collection)
291
355
  else
292
356
  cdata ||= rule.cdata
293
- compiled << [rule, attr, :scalar, nil, delegate_target]
357
+ compiled << [rule, attr, :scalar, nil, delegate_ref]
294
358
  rows << row.merge(kind: :scalar)
295
359
  end
296
360
  end
@@ -391,7 +455,17 @@ module Lutaml
391
455
  # Lazy: the leptris require only belongs on the
392
456
  # engines-enabled path.
393
457
  require "leptris/xml/descriptor"
394
- descriptor = ::Leptris::XML::Descriptor.build(**tree)
458
+ # :blank is the serializer's no-namespace form. The engine
459
+ # captures such a row by name at any qualification (no ns
460
+ # form — main's behavior for a locally declared element):
461
+ # the source may spell it bare or with the parent's
462
+ # prefix, and the instance's own format preservation
463
+ # decides the spelling on the way out.
464
+ descriptor = ::Leptris::XML::Descriptor.build(
465
+ **tree, children: rows.map do |r|
466
+ r[:ns] == :blank ? r.except(:ns) : r
467
+ end
468
+ )
395
469
  rescue StandardError
396
470
  return nil
397
471
  end
@@ -401,7 +475,6 @@ module Lutaml
401
475
  row_tags: row_tags, namespaced: namespaced,
402
476
  ordered: mapping.ordered? || mapping.mixed_content?,
403
477
  needs_nodes: needs_nodes,
404
- collection_defaults: collection_defaults,
405
478
  contested_names: contested.empty? ? nil : contested,
406
479
  ns_attr_names: ns_attr_names.empty? ? nil : ns_attr_names,
407
480
  wildcard_tag: (wildcard_tag if defined?(wildcard_tag)) }
@@ -531,20 +604,73 @@ module Lutaml
531
604
  # rows; nothing is lost under a namespaced parent.
532
605
  # - otherwise: :nested with the child's tree.
533
606
  # Returns [[compiled tuple], tree row].
534
- def child_row_for(rule, attr, type, child, delegate_target,
535
- model_ns)
607
+ def child_row_for(rule, attr, type, child, delegate_ref,
608
+ model_ns, local_form = nil)
536
609
  if child.nil?
537
- [[rule, attr, :child_deferred, nil, delegate_target, type],
610
+ [[rule, attr, :child_deferred, nil, delegate_ref, type],
538
611
  { name: rule.name.to_s, kind: :raw }]
539
612
  elsif child[:ordered]
540
- [[rule, attr, :ordered_deferred, nil, delegate_target, type],
613
+ [[rule, attr, :ordered_deferred, nil, delegate_ref, type],
541
614
  { name: rule.name.to_s, kind: :raw }]
542
615
  else
543
616
  row = { name: rule.name.to_s, kind: :nested,
544
617
  plan: child[:tree] }
545
- row[:ns] = child_ns(rule, model_ns) if rule.namespace_set?
546
- [[rule, attr, :nested, nil, delegate_target, type], row]
618
+ if rule.namespace_set?
619
+ row[:ns] = child_ns(rule, model_ns)
620
+ elsif local_form
621
+ row[:ns] = local_form
622
+ end
623
+ [[rule, attr, :nested, nil, delegate_ref, type], row]
624
+ end
625
+ end
626
+
627
+ # The ns form of a scalar element row: the rule's own
628
+ # namespace, else the attribute type's (the interpretive
629
+ # resolve step 4 — nil when it equals the model's own), else
630
+ # the local element's form per the parent mapping's
631
+ # elementFormDefault. A locally declared scalar under an
632
+ # explicitly :unqualified parent in its own namespace is
633
+ # spelled bare (apply_unqualified_form_default); a type with
634
+ # no namespace at all follows the parent's form. Returns the
635
+ # opt-out clause when the type form is not row-expressible.
636
+ def scalar_row_ns(rule, attr, register, model_ns, unqualified_form,
637
+ blank_form)
638
+ return child_ns(rule, model_ns) if rule.namespace_set?
639
+
640
+ type_ns_form = element_type_ns_form(rule, attr, register, model_ns)
641
+ return type_ns_form if type_ns_form
642
+ return unless unqualified_row?(rule)
643
+
644
+ local_element_form(nil, model_ns, rule, unqualified_form,
645
+ blank_form &&
646
+ attr.type_namespace_class(register).nil?)
647
+ end
648
+
649
+ # The ns form of a locally declared element (no rule
650
+ # namespace), per the parent mapping's elementFormDefault: the
651
+ # element's own namespace (a nested model's) is kept unless the
652
+ # explicit :unqualified form spells it bare; without one it
653
+ # follows the parent's form — :unqualified, or :blank (no
654
+ # namespace) when the form is not set.
655
+ def local_element_form(own_ns, model_ns, rule, unqualified_form,
656
+ blank_form)
657
+ return unless unqualified_row?(rule)
658
+
659
+ # The rule's own form: :unqualified always spells bare
660
+ # (determine_element_namespace priority 3), whatever the
661
+ # parent's form declares.
662
+ return :unqualified if rule.form == :unqualified
663
+
664
+ own_uri = own_ns.is_a?(::Hash) ? own_ns[:exact] : nil
665
+ if own_uri
666
+ return :unqualified if unqualified_form &&
667
+ own_uri == model_ns&.[](:exact)
668
+ elsif unqualified_form
669
+ return :unqualified
670
+ elsif blank_form
671
+ return :blank
547
672
  end
673
+ nil
548
674
  end
549
675
 
550
676
  def compilable_mapping?(mapping)
@@ -636,6 +762,13 @@ module Lutaml
636
762
  form
637
763
  end
638
764
 
765
+ # A locally declared element the writer would leave to its
766
+ # parent's form: no rule namespace, and no :qualified form
767
+ # override forcing inheritance.
768
+ def unqualified_row?(rule)
769
+ !rule.namespace_set? && rule.form != :qualified
770
+ end
771
+
639
772
  def content_rows(rows)
640
773
  rows.count { |r| r[:kind] == :content }
641
774
  end
@@ -653,6 +786,18 @@ module Lutaml
653
786
  prefix: ns_class.prefix_default&.to_s }
654
787
  end
655
788
 
789
+ # Whether a row resolves its child class at parse time — the
790
+ # interpretive cast's own inputs: the rule's polymorphic option
791
+ # (a class map or `true`), its legacy polymorphic_map, the
792
+ # attribute's polymorphic class, or a union. Such a row defers
793
+ # its subtree to the interpretive cast.
794
+ def polymorphic_shape?(rule, attr)
795
+ option = rule.polymorphic
796
+ rule.polymorphic_mapping? ||
797
+ (option.is_a?(::Hash) ? !option.empty? : !!option) ||
798
+ attr.polymorphic? || attr.union?
799
+ end
800
+
656
801
  def scalar_type?(attr, register)
657
802
  type = attr.type(register)
658
803
  type.is_a?(Class) && type < ::Lutaml::Model::Type::Value &&