lutaml-model 0.8.208 → 0.8.209

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: ad86126c9e0284053e6ff23df2d047c5c658d679e8c9c7712cc722fd7aa7c46b
4
- data.tar.gz: 7b03f53eaa1ea056110bffe25f9db569397819bb835be2c10964de9359571ce2
3
+ metadata.gz: c396586c2729a6d9858da3c633b98c574358efdb100964340d531f20545a618b
4
+ data.tar.gz: 8833a24a372af34a4cdcaf82ff7921ada9fe9053140609e1a89d6180de2b6c6d
5
5
  SHA512:
6
- metadata.gz: dd2ad49d9eb69cb840e7e99c88519665214e015e6fbc299bc1c9644eb52c2ea96b28be3372c3eeacc62c0c29e5dbd50807bb641ef6ec661f3ab0e33fd15e5f91
7
- data.tar.gz: fdb971b77d5d19c7436491538114e2b5a14a930fe070ae59a25778311e8dd3a007eac2ad4d7f663c365b740963d1ed9f7b1a89f7d8de18e7284b700fff5a43a4
6
+ metadata.gz: b5748f05813b63d2b93c7957a07aba9ff3faa5bcd770c0609aaded9e38ecbc6eb5e246eaaf188654cb75679e9dae582fce48580947f91204558a34ffa22c6bf3
7
+ data.tar.gz: bcd7c3a604633dc61fdfbe8007c39dc09545d3f27bbf83b7a26218576279bf69af5582f3e76a2022413510a7fbc219bc25134525f353785eb7ec052b7626803c
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "complex1-46KB": {
3
- "avg_time": 0.04722797766667478,
4
- "min_time": 0.04673070100000132,
5
- "max_time": 0.04775104800000918,
6
- "allocations": 48293,
7
- "ips": 21.17388991452887
3
+ "avg_time": 0.0364748943333287,
4
+ "min_time": 0.035120398999993085,
5
+ "max_time": 0.037068958999995516,
6
+ "allocations": 48203,
7
+ "ips": 27.41611780589194
8
8
  },
9
9
  "complex4-50KB": {
10
- "avg_time": 0.06411800366667346,
11
- "min_time": 0.06375282199996946,
12
- "max_time": 0.06461941400004889,
13
- "allocations": 67871,
14
- "ips": 15.596243532450604
10
+ "avg_time": 0.05115535466666188,
11
+ "min_time": 0.04903352099999836,
12
+ "max_time": 0.05247602199999335,
13
+ "allocations": 67781,
14
+ "ips": 19.548295706601824
15
15
  },
16
16
  "size1000-245KB": {
17
- "avg_time": 0.36015998900001023,
18
- "min_time": 0.35448734000004833,
19
- "max_time": 0.36499723800000083,
20
- "allocations": 382355,
21
- "ips": 2.776543843130703
17
+ "avg_time": 0.2900418076666635,
18
+ "min_time": 0.2876202389999918,
19
+ "max_time": 0.29324903599999175,
20
+ "allocations": 382265,
21
+ "ips": 3.447778815215738
22
22
  },
23
23
  "complex3-567KB": {
24
- "avg_time": 0.5304185873333344,
25
- "min_time": 0.5280918569999926,
26
- "max_time": 0.5321308450000402,
27
- "allocations": 560145,
28
- "ips": 1.8853034638689303
24
+ "avg_time": 0.42179371233333995,
25
+ "min_time": 0.4200932080000115,
26
+ "max_time": 0.42285213400001,
27
+ "allocations": 560055,
28
+ "ips": 2.370827185801453
29
29
  }
30
30
  }
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "metrology-4KB": {
3
- "avg_time": 0.013593411333332028,
4
- "min_time": 0.013364400000000387,
5
- "max_time": 0.013884672999999736,
6
- "allocations": 6555,
7
- "ips": 73.56505114708973
3
+ "avg_time": 0.013732422666663524,
4
+ "min_time": 0.013354440000000523,
5
+ "max_time": 0.01399934799999869,
6
+ "allocations": 6644,
7
+ "ips": 72.8203627483426
8
8
  },
9
9
  "bmj-49KB": {
10
- "avg_time": 0.11374726233333377,
11
- "min_time": 0.11275422599999985,
12
- "max_time": 0.11427680500000292,
13
- "allocations": 61623,
14
- "ips": 8.791420377833118
10
+ "avg_time": 0.11612695866666438,
11
+ "min_time": 0.11488018500000408,
12
+ "max_time": 0.11780903400000398,
13
+ "allocations": 61486,
14
+ "ips": 8.611264873218985
15
15
  },
16
16
  "pnas-152KB": {
17
- "avg_time": 0.49736028199999777,
18
- "min_time": 0.4924356920000008,
19
- "max_time": 0.5007860399999942,
20
- "allocations": 232230,
21
- "ips": 2.0106149127525317
17
+ "avg_time": 0.4207894683333298,
18
+ "min_time": 0.4172780809999779,
19
+ "max_time": 0.4247577579999984,
20
+ "allocations": 233115,
21
+ "ips": 2.3764853335346467
22
22
  }
23
23
  }
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "feature-doc-23KB": {
3
- "avg_time": 0.08004215133333521,
4
- "min_time": 0.07967459299999291,
5
- "max_time": 0.08053014500001154,
6
- "allocations": 29557,
7
- "ips": 12.49341732252428
3
+ "avg_time": 0.04404333933335162,
4
+ "min_time": 0.04362466399999221,
5
+ "max_time": 0.0444422120000354,
6
+ "allocations": 29387,
7
+ "ips": 22.70490873617193
8
8
  },
9
9
  "tbx-nisosts-101KB": {
10
- "avg_time": 0.24897703766667215,
11
- "min_time": 0.24684493000000884,
12
- "max_time": 0.2523852099999999,
13
- "allocations": 116651,
14
- "ips": 4.016434645426176
10
+ "avg_time": 0.15528667266666693,
11
+ "min_time": 0.15354131700001972,
12
+ "max_time": 0.15623446500001137,
13
+ "allocations": 114125,
14
+ "ips": 6.43970266622021
15
15
  },
16
16
  "iso-13849-1MB": {
17
- "avg_time": 7.87387151599999,
18
- "min_time": 7.842370211000002,
19
- "max_time": 7.923985744999982,
20
- "allocations": 1679721,
21
- "ips": 0.12700232636104922
17
+ "avg_time": 2.9371749226666943,
18
+ "min_time": 2.9121822650000126,
19
+ "max_time": 2.9545246739999698,
20
+ "allocations": 1419414,
21
+ "ips": 0.34046320914795525
22
22
  },
23
23
  "din-iso-1.1MB": {
24
- "avg_time": 0.9811890356666595,
25
- "min_time": 0.9675218900000004,
26
- "max_time": 0.9853540780000003,
27
- "allocations": 846635,
28
- "ips": 1.0191716006289855
24
+ "avg_time": 0.6223951049999906,
25
+ "min_time": 0.6178283360000023,
26
+ "max_time": 0.6269072419999588,
27
+ "allocations": 846514,
28
+ "ips": 1.606696440840445
29
29
  }
30
30
  }
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "800-expressions": {
3
- "avg_time": 0.1067107063333405,
4
- "min_time": 0.10537397500002044,
5
- "max_time": 0.10742932200000155,
3
+ "avg_time": 0.13142044666666663,
4
+ "min_time": 0.12954049200000384,
5
+ "max_time": 0.13376315700000418,
6
6
  "allocations": 139002,
7
- "ips": 9.371130923604072
7
+ "ips": 7.60916604199641
8
8
  }
9
9
  }
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "ea251": {
3
- "avg_time": 0.18316421099999994,
4
- "min_time": 0.18038664600001653,
5
- "max_time": 0.1853450769999938,
6
- "allocations": 179861,
7
- "ips": 5.459581839380185
3
+ "avg_time": 0.15194719033331694,
4
+ "min_time": 0.15037986999999475,
5
+ "max_time": 0.15446484799997506,
6
+ "allocations": 175969,
7
+ "ips": 6.581233899793496
8
8
  },
9
9
  "medium": {
10
- "avg_time": 0.4485483150000012,
11
- "min_time": 0.44155083600000467,
12
- "max_time": 0.4588752709999824,
13
- "allocations": 465885,
14
- "ips": 2.229414238240974
10
+ "avg_time": 0.397459121333308,
11
+ "min_time": 0.39441433599995435,
12
+ "max_time": 0.401802325999995,
13
+ "allocations": 460164,
14
+ "ips": 2.515982012553696
15
15
  },
16
16
  "citygml": {
17
- "avg_time": 0.5182148209999866,
18
- "min_time": 0.5057585739999979,
19
- "max_time": 0.5237662360000002,
20
- "allocations": 577293,
21
- "ips": 1.929701659381961
17
+ "avg_time": 0.4615553376666715,
18
+ "min_time": 0.45299937200002205,
19
+ "max_time": 0.4655662110000094,
20
+ "allocations": 571585,
21
+ "ips": 2.1665874455170644
22
22
  },
23
23
  "large": {
24
- "avg_time": 4.232644015666655,
25
- "min_time": 4.1436827659999835,
26
- "max_time": 4.302634627999993,
27
- "allocations": 4449467,
28
- "ips": 0.23625894270782344
24
+ "avg_time": 3.8421088003333352,
25
+ "min_time": 3.769278003000011,
26
+ "max_time": 3.8997934000000214,
27
+ "allocations": 4399032,
28
+ "ips": 0.26027373298570866
29
29
  }
30
30
  }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.208"
5
+ VERSION = "0.8.209"
6
6
  end
7
7
  end
@@ -350,6 +350,7 @@ module Lutaml
350
350
  # refuse here: the doc takes the DOM lane.
351
351
  child_plan = PlanCompiler.compile(item.class, register)
352
352
  return false unless child_plan
353
+ return false if blank_opt_out_unspellable?(child_plan, item)
353
354
  return false unless subtree_gate_ok?(child_plan, item,
354
355
  register)
355
356
  end
@@ -379,6 +380,26 @@ module Lutaml
379
380
  !PlanCompiler.compile(child_type, register).nil?
380
381
  end
381
382
 
383
+ # A nested island in default-namespace form (an EXACT tree ns
384
+ # without a prefix) over an item that recorded no prefix carries
385
+ # :blank rows — its local elements, in no namespace — that must
386
+ # opt out with xmlns="" (the island's own declaration would
387
+ # put them in its namespace). The ops spelling has no opt-out:
388
+ # ns_emittable? refuses the shape on the root, the DOM lane
389
+ # mints it for the island (a MathML island under a
390
+ # namespace-less root, #1342's family).
391
+ def blank_opt_out_unspellable?(child_plan, item)
392
+ return false if item.is_a?(::Lutaml::Model::Serialize) &&
393
+ prefixed_source?(item)
394
+
395
+ ns = child_plan.dig(:tree, :ns)
396
+ return false unless ns.is_a?(::Hash) && ns[:prefix].to_s.empty?
397
+
398
+ (child_plan.dig(:tree, :children) || []).any? do |r|
399
+ r[:ns] == :blank
400
+ end
401
+ end
402
+
382
403
  # Namespace-emittability for ORDERED trees: content and
383
404
  # wildcard rows carry no spelling of their own (content
384
405
  # emits text; wildcards bail in subtree_gate_ok?), so the
@@ -63,8 +63,25 @@ module Lutaml
63
63
  # :lazy is honored as :eager — the lazy variant pins the
64
64
  # raw engine DOM, the retention the eager default exists
65
65
  # to avoid)
66
+ # parent_prefix: the enclosing instance's recorded source prefix
67
+ # (nil for the root) — a nested instance records its own only
68
+ # under a parent that recorded none, the interpretive reader's
69
+ # rule
66
70
  def hydrate(model_class, plan, value, parent: nil, node: nil,
67
- register: nil, declaration_plan_mode: :eager, intern: nil)
71
+ register: nil, declaration_plan_mode: :eager, intern: nil,
72
+ parent_prefix: nil)
73
+ # Prefix fidelity (the interpretive transform records it from
74
+ # the source element): prefixed sources keep their prefixes
75
+ # through the serialize lane's format preservation — on the
76
+ # root, and on a nested instance whose parent recorded none
77
+ # (the interpretive reader's rule; a nested island spelled
78
+ # `<p:leaf>` under a default-namespace root re-emits with its
79
+ # prefix, #1342).
80
+ source_prefix = if node.respond_to?(:prefix) then node.prefix end
81
+ own_prefix = if parent_prefix.to_s.empty? && source_prefix &&
82
+ !source_prefix.empty?
83
+ source_prefix
84
+ end
68
85
  # The child model's declared lutaml_default_register takes
69
86
  # precedence over the ambient register — the same
70
87
  # resolve_for_child contract the interpretive path applies.
@@ -87,7 +104,7 @@ module Lutaml
87
104
  child_kwargs, children, delegates, run_children,
88
105
  element_children =
89
106
  children_kwargs(model_class, plan, value, buckets, register,
90
- intern, node: node)
107
+ intern, node: node, parent_prefix: own_prefix)
91
108
  # A collection absent from the document keeps the
92
109
  # deserialization seed's lazy sentinel and its default mark
93
110
  # — the interpretive parse's state. The reader materializes
@@ -100,14 +117,7 @@ module Lutaml
100
117
  instance = model_class.instantiate(attr_kwargs, register)
101
118
  instance.lutaml_parent = parent if parent
102
119
  instance.lutaml_root ||= parent&.lutaml_root || parent
103
- # Root prefix fidelity (the interpretive transform records
104
- # it from the source element): prefixed sources keep their
105
- # prefixes through the serialize lane's format
106
- # preservation.
107
- if parent.nil? && node.respond_to?(:prefix) && node.prefix &&
108
- !node.prefix.empty?
109
- instance.xml_namespace_prefix = node.prefix
110
- end
120
+ instance.xml_namespace_prefix = own_prefix if own_prefix
111
121
  # #909: order summaries are per-instance pinned records —
112
122
  # read-only consumers skip them via the config. Ordered and
113
123
  # sequence models record them: the ordered walk replays
@@ -439,7 +449,8 @@ module Lutaml
439
449
  # parent/root links once the parent exists; delegate values
440
450
  # wait for the instance (their target object must exist).
441
451
  def children_kwargs(_model_class, plan, value, buckets = nil,
442
- register = nil, intern = {}, node: nil)
452
+ register = nil, intern = {}, node: nil,
453
+ parent_prefix: nil)
443
454
  register ||= Lutaml::Model::Config.default_register
444
455
  grouped, tagged, run_children = group_children(value,
445
456
  plan[:row_tags])
@@ -540,7 +551,8 @@ module Lutaml
540
551
  items = (selected || []).map do |n|
541
552
  call(child_type, child_plan,
542
553
  child_plan[:descriptor].walk(n), node: n,
543
- register: child_register, intern: intern)
554
+ register: child_register, intern: intern,
555
+ parent_prefix: parent_prefix)
544
556
  end
545
557
  unless items.empty?
546
558
  children.concat(items)
@@ -610,7 +622,8 @@ module Lutaml
610
622
  items = selected.map do |n|
611
623
  call(child_type, child_plan,
612
624
  child_plan[:descriptor].walk(n), node: n,
613
- register: child_register, intern: intern)
625
+ register: child_register, intern: intern,
626
+ parent_prefix: parent_prefix)
614
627
  end
615
628
  else
616
629
  values = grouped.fetch(name, [])
@@ -625,7 +638,8 @@ module Lutaml
625
638
  while pos < count
626
639
  items[pos] = call(child_type, child_plan, values[pos],
627
640
  node: cursor && cursor[pos],
628
- register: child_register, intern: intern)
641
+ register: child_register, intern: intern,
642
+ parent_prefix: parent_prefix)
629
643
  pos += 1
630
644
  end
631
645
  end
@@ -45,14 +45,24 @@ module Lutaml
45
45
  # child out again inside an already opted-out subtree. What the
46
46
  # opt-out does change is scope: a child spelled in
47
47
  # default-namespace form inside it re-declares its namespace.
48
+ #
49
+ # `default` and `prefixes` are the bindings in scope for the
50
+ # element's own children (the root's declarations, then whatever
51
+ # a nested element declared on itself): a nested element in a
52
+ # namespace nothing in scope binds declares it where it appears
53
+ # (the interpretive writer's placement for a child whose
54
+ # namespace differs from its parent's), never on the root — only
55
+ # one default namespace can be declared per element (W3C).
48
56
  class BlankScope
49
- attr_reader :root_default, :blank, :pretty
57
+ attr_reader :root_default, :blank, :pretty, :default, :prefixes
50
58
  attr_accessor :done
51
59
 
52
- def initialize(root_default, blank, pretty)
60
+ def initialize(root_default, blank, pretty, default: nil, prefixes: {})
53
61
  @root_default = root_default
54
62
  @blank = blank
55
63
  @pretty = pretty
64
+ @default = default
65
+ @prefixes = prefixes
56
66
  @done = false
57
67
  end
58
68
  end
@@ -125,12 +135,10 @@ module Lutaml
125
135
  end
126
136
  built = if ordered_walk?(plan, instance)
127
137
  build_ordered(root, instance, plan, doc, register, ns_ctx,
128
- scope: BlankScope.new(ns_ctx.default_namespace,
129
- false, pretty))
138
+ scope: ns_ctx.root_scope(pretty))
130
139
  else
131
140
  build(root, instance, plan, doc, register, ns_ctx,
132
- scope: BlankScope.new(ns_ctx.default_namespace,
133
- false, pretty))
141
+ scope: ns_ctx.root_scope(pretty))
134
142
  end
135
143
  return nil unless built
136
144
 
@@ -156,6 +164,7 @@ module Lutaml
156
164
  attr_reader :unspellable
157
165
 
158
166
  def initialize(instance, plan, register)
167
+ @register = register
159
168
  @model_prefix = nil
160
169
  @forms = {}
161
170
  @hoisted = {}
@@ -191,13 +200,31 @@ module Lutaml
191
200
  @hoisted[uri] = prefix
192
201
  @forms[uri] = { prefix: prefix, uri: uri }
193
202
  end
203
+ # The type-analysis plan keys its hoisted set by PREFIX, and
204
+ # its "remaining namespaces" pass hoists a nested model's
205
+ # prefix-less namespace in default form — displacing the
206
+ # root's own default declaration on the one nil key (the
207
+ # MathML island under a default-namespace UnitsML root,
208
+ # #1342). Only one default namespace can be declared per
209
+ # element (W3C): the root keeps its own, and the nested
210
+ # element declares its namespace where it appears
211
+ # (qualify!). The displaced form stays spellable; it is no
212
+ # longer a root declaration. The root's own entry is
213
+ # restored in the form it was displaced from — default.
214
+ model_uri = plan[:tree][:ns] && plan[:tree][:ns][:exact]
215
+ @hoisted.delete_if do |uri, prefix|
216
+ prefix.nil? && uri != model_uri
217
+ end
218
+ if model_uri && !@hoisted.key?(model_uri)
219
+ @hoisted[model_uri] = nil
220
+ @forms[model_uri] = { prefix: nil, uri: model_uri }
221
+ end
194
222
  # The writer's own root decision (its rule engine's
195
223
  # DefaultPreferenceRule over the built tree): an explicitly
196
224
  # :unqualified elementFormDefault never selects the prefix
197
225
  # format by itself — only prefixed attribute needs do (W3C).
198
226
  # The planner's type-analysis path prefers the prefix format
199
227
  # here so children can omit xmlns; the writer does not.
200
- model_uri = plan[:tree][:ns] && plan[:tree][:ns][:exact]
201
228
  ns_class = mapping.namespace_class
202
229
  if ns_class&.element_form_default_set? &&
203
230
  ns_class.element_form_default == :unqualified &&
@@ -257,14 +284,16 @@ module Lutaml
257
284
  # :qualified) take the model prefix instead of falling
258
285
  # into no namespace.
259
286
  ns_class = mapping.namespace_class
260
- if instance.element_order.nil? &&
261
- !instance.import_declaration_plan.is_a?(
262
- Lutaml::Xml::DeclarationPlan,
263
- ) && ns_class &&
287
+ constructed = instance.element_order.nil? &&
288
+ !instance.import_declaration_plan.is_a?(
289
+ Lutaml::Xml::DeclarationPlan,
290
+ )
291
+ if constructed && ns_class &&
264
292
  ns_class.attribute_form_default == :qualified &&
265
293
  !ns_class.prefix_default.to_s.empty?
266
294
  @model_prefix = ns_class.prefix_default.to_s
267
295
  end
296
+ @parsed = !constructed
268
297
  @default_namespace = @hoisted.any? { |_uri, prefix| prefix.nil? }
269
298
  end
270
299
 
@@ -290,6 +319,14 @@ module Lutaml
290
319
  # out with xmlns="".
291
320
  attr_reader :default_namespace
292
321
 
322
+ # The scope the root's children are built under: the root's
323
+ # own declarations are the bindings in scope.
324
+ def root_scope(pretty)
325
+ BlankScope.new(default_namespace, false, pretty,
326
+ default: default_namespace_uri,
327
+ prefixes: hoisted_prefixes)
328
+ end
329
+
293
330
  def collect(tree)
294
331
  check(tree[:ns])
295
332
  (tree[:attributes] || []).each { |a| check(a[:ns]) }
@@ -314,9 +351,81 @@ module Lutaml
314
351
  @hoisted[uri]
315
352
  end
316
353
 
354
+ # The prefix an exact form spells with: the one the root
355
+ # declares the URI under, else the form's own (a nested
356
+ # element's declared prefix, declared on that element).
357
+ def spelled_prefix(form)
358
+ return prefix_for(form) unless form.is_a?(::Hash)
359
+
360
+ uri = form[:exact]
361
+ prefix = if form[:nested]
362
+ # A nested element's decided form: its recorded
363
+ # prefix outranks a root declaration of the same
364
+ # URI (the doubly-defined `<a:leaf>` under
365
+ # `xmlns=A xmlns:a=A`).
366
+ form[:prefix]
367
+ elsif @hoisted.key?(uri)
368
+ @hoisted[uri]
369
+ else
370
+ form[:prefix]
371
+ end
372
+ prefix.to_s.empty? ? nil : prefix
373
+ end
374
+
375
+ # The form a nested element spells through: its plan's tree
376
+ # form carrying the prefix the nested model's own root
377
+ # decision gives its namespace — the instance's recorded
378
+ # prefix (format preservation), else the planner's decision
379
+ # for the nested class, which is the interpretive writer's
380
+ # form for the nested element (a prefix-default island built
381
+ # in memory spells `<leaf xmlns=P>`, not `<p:leaf>`). A URI
382
+ # the root declares spells with the root's form.
383
+ #
384
+ # Returns nil — unspellable, the document takes the
385
+ # interpretive lane — for a PARSED document whose nested
386
+ # class declares a prefix the decision does not use: its
387
+ # source may have spelled the island with that prefix
388
+ # (`<p:leaf xmlns:p=P>`), and the walk capture carries no
389
+ # element prefix to tell; the interpretive writer reads the
390
+ # prefix off the instance and preserves it.
391
+ def nested_form(item, child_plan)
392
+ form = child_plan[:tree][:ns]
393
+ return form unless form.is_a?(::Hash)
394
+ return form if @hoisted.key?(form[:exact])
395
+
396
+ recorded = if item.is_a?(::Lutaml::Model::Serialize)
397
+ item.xml_namespace_prefix
398
+ end
399
+ prefix = if recorded && !recorded.empty?
400
+ recorded
401
+ else
402
+ (@nested_prefixes ||= {}.compare_by_identity)[child_plan] ||=
403
+ root_prefix_for(item.class, child_plan)
404
+ end
405
+ return form if prefix.to_s == form[:prefix].to_s
406
+ return nil if @parsed && !form[:prefix].to_s.empty?
407
+
408
+ { exact: form[:exact], prefix: prefix, nested: true }
409
+ end
410
+
411
+ # The planner's hoisted prefix for a class's own namespace
412
+ # when the class is a document root (nil for the
413
+ # default-namespace form) — its type-analysis decision.
414
+ def root_prefix_for(klass, child_plan)
415
+ mapping = child_plan[:mapping]
416
+ uri = child_plan[:tree][:ns][:exact]
417
+ needs = Lutaml::Xml::NamespaceCollector.new(@register).collect(
418
+ nil, mapping, mapper_class: klass
419
+ )
420
+ declared = Lutaml::Xml::DeclarationPlanner.new(@register).plan(
421
+ klass, mapping, needs, options: {}
422
+ )
423
+ declared.root_node&.hoisted_declarations&.key(uri)
424
+ end
425
+
317
426
  # The wire spelling for a local name under this form: the
318
- # planner's hoisted prefix for the URI, or unprefixed when
319
- # hoisted as the default namespace.
427
+ # declared prefix for the URI, or unprefixed when the URI is
428
+ # in default-namespace form.
320
429
  def spell(local, ns_form = nil)
321
430
  if ns_form.nil?
322
431
  # No row ns: the element belongs to the model's own
@@ -326,19 +435,19 @@ module Lutaml
326
435
  return @model_prefix ? "#{@model_prefix}:#{local}" : local
327
436
  end
328
437
 
329
- uri = if ns_form.is_a?(Hash)
330
- ns_form[:exact]
331
- else
332
- @forms.key?(ns_form) ? @forms[ns_form][:uri] : nil
333
- end
334
- prefix = uri ? @hoisted[uri] : nil
438
+ prefix = if ns_form.is_a?(Hash)
439
+ spelled_prefix(ns_form)
440
+ else
441
+ uri = @forms.key?(ns_form) ? @forms[ns_form][:uri] : nil
442
+ uri ? @hoisted[uri] : nil
443
+ end
335
444
  prefix ? "#{prefix}:#{local}" : local
336
445
  end
337
446
 
338
447
  def form_for(ns_form)
339
448
  return nil if ns_form.nil? || ns_form == :none
340
449
 
341
- ns_form.is_a?(Hash) ? { prefix: @hoisted[ns_form[:exact]], uri: ns_form[:exact] } : @forms[ns_form]
450
+ ns_form.is_a?(Hash) ? { prefix: spelled_prefix(ns_form), uri: ns_form[:exact] } : @forms[ns_form]
342
451
  end
343
452
 
344
453
  # Declaration order matches the interpretive writer: the
@@ -363,6 +472,26 @@ module Lutaml
363
472
  root.add_namespace_definition(prefix, uri)
364
473
  end
365
474
  end
475
+
476
+ private
477
+
478
+ # The URI the root declares as the default namespace, if any.
479
+ def default_namespace_uri
480
+ entry = @hoisted.find { |_uri, prefix| prefix.nil? }
481
+ entry&.first
482
+ end
483
+
484
+ # The prefix bindings the root declares.
485
+ def hoisted_prefixes
486
+ prefixes = {}
487
+ @hoisted.each do |uri, prefix|
488
+ prefixes[prefix] = uri unless prefix.nil?
489
+ end
490
+ @extra_declarations.each do |prefix, uri|
491
+ prefixes[prefix] = uri unless prefix.nil?
492
+ end
493
+ prefixes
494
+ end
366
495
  end
367
496
 
368
497
  # Whether the compiled plan is serialize-shaped. Ordered/mixed
@@ -449,7 +578,12 @@ module Lutaml
449
578
  child_plan = nested_plans[[item.class, name]] ||
450
579
  (nested_plans[[item.class, name]] = PlanCompiler.compile(item.class, reg))
451
580
  row_ns = nested_child_ns(child_plan, ns_index[name],
452
- inherited_ns)
581
+ inherited_ns, plan[:tree][:ns],
582
+ rule)
583
+ row_ns = ns_ctx.nested_form(item, child_plan) if ns_ctx &&
584
+ row_ns.equal?(child_plan[:tree][:ns])
585
+ return nil unless row_ns
586
+
453
587
  form = spelled_form(row_ns, inherited_ns, plan)
454
588
  # The ROW's wire name wins over the child plan's
455
589
  # root — distinct rows (sz, szCs) share one class.
@@ -710,13 +844,21 @@ module Lutaml
710
844
 
711
845
  child_plan = nested_plans_for(plan)[[item.class, object.name]] ||
712
846
  (nested_plans_for(plan)[[item.class, object.name]] = PlanCompiler.compile(item.class, reg))
713
- child_ns = nested_child_ns(child_plan, row_ns, inherited_ns)
847
+ child_ns = nested_child_ns(child_plan, row_ns, inherited_ns,
848
+ plan[:tree][:ns], row_rule)
849
+ own_ns = child_ns.equal?(child_plan[:tree][:ns])
850
+ child_ns = ns_ctx.nested_form(item, child_plan) if ns_ctx && own_ns
851
+ return nil unless child_ns
852
+
714
853
  child = element.create_child(
715
854
  spell(ns_ctx, object.name, child_ns),
716
855
  )
856
+ # A child model's own namespace (reached over the row's
857
+ # bare match form) is not the row's blank spelling.
717
858
  child_scope = subtree_scope(
718
859
  scope,
719
- qualify!(child, child_ns, blank, scope, ns_ctx),
860
+ qualify!(child, child_ns, own_ns ? false : blank,
861
+ scope, ns_ctx),
720
862
  )
721
863
  if ordered_walk?(child_plan, item)
722
864
  return nil unless build_ordered(child, item, child_plan,
@@ -949,12 +1091,18 @@ module Lutaml
949
1091
  nested_plans_for(plan)[[item.class, name]] ||
950
1092
  (nested_plans_for(plan)[[item.class, name]] =
951
1093
  PlanCompiler.compile(item.class, reg))
952
- child_ns = nested_child_ns(child_plan, ns_form, inherited_ns)
1094
+ child_ns = nested_child_ns(child_plan, ns_form, inherited_ns,
1095
+ plan[:tree][:ns], row_rule)
1096
+ own_ns = child_ns.equal?(child_plan[:tree][:ns])
1097
+ child_ns = ns_ctx.nested_form(item, child_plan) if ns_ctx && own_ns
1098
+ return false unless child_ns
1099
+
953
1100
  child = element.create_child(
954
1101
  spell(ns_ctx, name, child_ns),
955
1102
  )
956
1103
  child_scope = subtree_scope(
957
- scope, qualify!(child, child_ns, blank, scope, ns_ctx)
1104
+ scope, qualify!(child, child_ns, own_ns ? false : blank,
1105
+ scope, ns_ctx)
958
1106
  )
959
1107
  if ordered_walk?(child_plan, item)
960
1108
  return nil unless build_ordered(child, item, child_plan,
@@ -1042,11 +1190,25 @@ module Lutaml
1042
1190
  # re-routes to the same attribute on reparse.
1043
1191
  # A nested child's namespace form. A row spelled :unqualified wins
1044
1192
  # over the child plan's own namespace: under a parent whose
1045
- # elementFormDefault is not :qualified, locally declared elements
1046
- # stay bare even when the child model declares the namespace
1047
- # (the interpretive writer's determine_element_namespace scope).
1048
- def nested_child_ns(child_plan, row_ns, inherited_ns)
1049
- return row_ns if %i[unqualified blank].include?(row_ns)
1193
+ # elementFormDefault is not :qualified, locally declared
1194
+ # elements stay bare even when the child model declares the
1195
+ # namespace (the interpretive writer's determine_element_namespace
1196
+ # scope). Except under a NAMESPACE-LESS parent: there the compiler
1197
+ # binds the row :unqualified as the walk's match form only (bare,
1198
+ # or an inherited default xmlns — leptris#1560), and a child model
1199
+ # that declares a namespace still spells and declares it (the
1200
+ # interpretive priority-1 rule: the type's namespace wins). An
1201
+ # explicit `form :unqualified` rule stays bare.
1202
+ def nested_child_ns(child_plan, row_ns, inherited_ns,
1203
+ parent_ns = nil, rule = nil)
1204
+ if %i[unqualified blank].include?(row_ns)
1205
+ if row_ns == :unqualified && parent_ns.nil? &&
1206
+ child_plan[:tree][:ns] && !(rule && rule.form == :unqualified)
1207
+ return child_plan[:tree][:ns]
1208
+ end
1209
+
1210
+ return row_ns
1211
+ end
1050
1212
 
1051
1213
  child_plan[:tree][:ns] || row_ns || inherited_ns
1052
1214
  end
@@ -1085,28 +1247,53 @@ module Lutaml
1085
1247
  # Bare, or spelled through a default-namespace form: the
1086
1248
  # child opts out, on the first such child of each parent
1087
1249
  # under pretty output.
1088
- default_spelled = !form.is_a?(::Hash) || (ns_ctx &&
1089
- ns_ctx.prefix_for(form[:exact]).nil?)
1250
+ default_spelled = !form.is_a?(::Hash) ||
1251
+ (ns_ctx && ns_ctx.spelled_prefix(form).nil?)
1090
1252
  if default_spelled && scope.root_default &&
1091
1253
  !(scope.pretty && scope.done)
1092
1254
  child.set_attribute("xmlns", "")
1093
1255
  scope.done = true
1094
- BlankScope.new(scope.root_default, true, scope.pretty)
1256
+ BlankScope.new(scope.root_default, true, scope.pretty,
1257
+ prefixes: scope.prefixes)
1095
1258
  end
1096
- elsif scope.blank && form.is_a?(::Hash) && ns_ctx &&
1097
- ns_ctx.prefix_for(form[:exact]).nil?
1098
- # A default-namespace child inside an opted-out subtree
1099
- # re-declares its namespace here: the root's declaration
1100
- # no longer reaches it.
1101
- child.set_attribute("xmlns", form[:exact].to_s)
1102
- BlankScope.new(scope.root_default, false, scope.pretty)
1259
+ elsif form.is_a?(::Hash) && ns_ctx
1260
+ declare_on_self(child, form, scope, ns_ctx)
1261
+ end
1262
+ end
1263
+
1264
+ # A child spelled in a namespace nothing in scope binds declares
1265
+ # it on itself (the interpretive writer's placement for a child
1266
+ # whose namespace differs from its parent's — the MathML island
1267
+ # under a default-namespace UnitsML root, #1342): the default
1268
+ # form when the URI is not the default in scope, the prefix form
1269
+ # when the prefix is not bound to it. The scope the child's own
1270
+ # children are built under, nil when nothing changed.
1271
+ def declare_on_self(child, form, scope, ns_ctx)
1272
+ uri = form[:exact].to_s
1273
+ return if uri.empty?
1274
+
1275
+ prefix = ns_ctx.spelled_prefix(form)
1276
+ if prefix.nil?
1277
+ return if scope.default == uri
1278
+
1279
+ child.set_attribute("xmlns", uri)
1280
+ BlankScope.new(scope.root_default || uri, false, scope.pretty,
1281
+ default: uri, prefixes: scope.prefixes)
1282
+ else
1283
+ return if scope.prefixes[prefix] == uri
1284
+
1285
+ child.set_attribute("xmlns:#{prefix}", uri)
1286
+ BlankScope.new(scope.root_default, false, scope.pretty,
1287
+ default: scope.default,
1288
+ prefixes: scope.prefixes.merge(prefix => uri))
1103
1289
  end
1104
1290
  end
1105
1291
 
1106
1292
  # The scope a nested child's subtree is built under.
1107
1293
  def subtree_scope(scope, changed)
1108
1294
  changed || BlankScope.new(scope.root_default, scope.blank,
1109
- scope.pretty)
1295
+ scope.pretty, default: scope.default,
1296
+ prefixes: scope.prefixes)
1110
1297
  end
1111
1298
 
1112
1299
  def add_leaf(element, name, value, doc, attrs: nil, cdata: false,
@@ -0,0 +1,234 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1342: a nested element in a namespace of its own declares
6
+ # that namespace where it appears. The plan serializer's type-analysis
7
+ # plan keys its hoisted set by prefix, so a nested model's prefix-less
8
+ # namespace displaced the root's own default declaration and both were
9
+ # re-emitted on the root (`<UnitsML xmlns="…MathML" xmlns="…unitsml">`,
10
+ # a duplicate attribute the strict parsers reject). The examples pin the
11
+ # leptris lane and assert the interpretive writer's output: one default
12
+ # namespace per element, the island's declaration on the island, and the
13
+ # opt-out for a local element in no namespace.
14
+ module NestedNamespaceDeclarationSpec
15
+ UNITS_URI = "https://example.org/unitsml"
16
+ MATH_URI = "http://www.w3.org/1998/Math/MathML"
17
+ PREFIXED_URI = "https://example.org/prefixed"
18
+
19
+ class UnitsNs < Lutaml::Xml::Namespace
20
+ uri UNITS_URI
21
+ end
22
+
23
+ class MathNs < Lutaml::Xml::Namespace
24
+ uri MATH_URI
25
+ end
26
+
27
+ class PrefixedNs < Lutaml::Xml::Namespace
28
+ uri PREFIXED_URI
29
+ prefix_default "p"
30
+ end
31
+
32
+ # The MathML island: a default-namespace model whose local element is
33
+ # in no namespace.
34
+ class Island < Lutaml::Model::Serializable
35
+ attribute :v, :string
36
+
37
+ xml do
38
+ namespace MathNs
39
+ element "leaf"
40
+ map_element "v", to: :v
41
+ end
42
+ end
43
+
44
+ # A nested model whose namespace declares a prefix it does not qualify
45
+ # its attributes with.
46
+ class PrefixedIsland < Lutaml::Model::Serializable
47
+ attribute :v, :string
48
+
49
+ xml do
50
+ namespace PrefixedNs
51
+ element "leaf"
52
+ map_element "v", to: :v
53
+ end
54
+ end
55
+
56
+ class RootDefault < Lutaml::Model::Serializable
57
+ attribute :name, :string
58
+ attribute :leaf, Island
59
+
60
+ xml do
61
+ namespace UnitsNs
62
+ element "root"
63
+ map_attribute "name", to: :name
64
+ map_element "leaf", to: :leaf
65
+ end
66
+ end
67
+
68
+ class RootNone < Lutaml::Model::Serializable
69
+ attribute :leaf, Island
70
+
71
+ xml do
72
+ element "root"
73
+ map_element "leaf", to: :leaf
74
+ end
75
+ end
76
+
77
+ class RootPrefixed < Lutaml::Model::Serializable
78
+ attribute :leaf, Island
79
+
80
+ xml do
81
+ namespace PrefixedNs
82
+ element "root"
83
+ map_element "leaf", to: :leaf
84
+ end
85
+ end
86
+
87
+ class RootDefaultPrefixedIsland < Lutaml::Model::Serializable
88
+ attribute :leaf, PrefixedIsland
89
+
90
+ xml do
91
+ namespace UnitsNs
92
+ element "root"
93
+ map_element "leaf", to: :leaf
94
+ end
95
+ end
96
+
97
+ # The report's shape: a mixed-content UnitsML element carrying a MathML
98
+ # symbol.
99
+ class UnitSymbol < Lutaml::Model::Serializable
100
+ attribute :type, :string
101
+ attribute :text, :string, collection: true
102
+ attribute :leaf, Island, collection: true
103
+
104
+ xml do
105
+ namespace UnitsNs
106
+ element "UnitSymbol"
107
+ mixed_content
108
+ map_attribute "type", to: :type
109
+ map_content to: :text
110
+ map_element "leaf", to: :leaf
111
+ end
112
+ end
113
+
114
+ class UnitsRoot < Lutaml::Model::Serializable
115
+ attribute :symbols, UnitSymbol, collection: true
116
+
117
+ xml do
118
+ namespace UnitsNs
119
+ element "UnitsML"
120
+ map_element "UnitSymbol", to: :symbols
121
+ end
122
+ end
123
+ end
124
+
125
+ RSpec.describe "nested namespace declarations on the plan lane" do
126
+ around do |example|
127
+ previous = Lutaml::Model::Config.xml_adapter_type
128
+ fixed = previous == Lutaml::Model::FIXED_ADAPTER_TYPE
129
+ previous_adapter = Lutaml::Model::Config.xml_adapter if fixed
130
+ Lutaml::Model::Config.xml_adapter_type = :leptris
131
+ example.run
132
+ ensure
133
+ if fixed
134
+ Lutaml::Model::Config.xml_adapter = previous_adapter
135
+ elsif previous
136
+ Lutaml::Model::Config.xml_adapter_type = previous
137
+ else
138
+ Lutaml::Model::Config.unset_xml_adapter_type
139
+ end
140
+ end
141
+
142
+ def squashed(xml)
143
+ xml.gsub(/\s+/, " ").strip
144
+ end
145
+
146
+ let(:units_xml) do
147
+ '<UnitsML xmlns="https://example.org/unitsml"><UnitSymbol type="MathMl">' \
148
+ '<leaf xmlns="http://www.w3.org/1998/Math/MathML"><v>x</v></leaf>' \
149
+ "</UnitSymbol></UnitsML>"
150
+ end
151
+
152
+ it "declares a MathML island on the island, under a default-namespace mixed-content parent" do
153
+ xml = NestedNamespaceDeclarationSpec::UnitsRoot.from_xml(units_xml).to_xml
154
+
155
+ expect(squashed(xml)).to eq(
156
+ '<UnitsML xmlns="https://example.org/unitsml"> <UnitSymbol type="MathMl"> ' \
157
+ '<leaf xmlns="http://www.w3.org/1998/Math/MathML"> <v xmlns="">x</v> </leaf> ' \
158
+ "</UnitSymbol> </UnitsML>",
159
+ )
160
+ expect(xml.scan(%r{ xmlns="https?://}).length).to eq(2)
161
+ expect(xml).not_to match(%r{<UnitSymbol[^>]*xmlns=})
162
+ expect { NestedNamespaceDeclarationSpec::UnitsRoot.from_xml(xml) }.not_to raise_error
163
+ end
164
+
165
+ it "matches the interpretive writer for the report's shape" do
166
+ leptris = NestedNamespaceDeclarationSpec::UnitsRoot.from_xml(units_xml).to_xml
167
+ interpretive = Lutaml::Model::Config.with_adapter(xml: :nokogiri) do
168
+ NestedNamespaceDeclarationSpec::UnitsRoot.from_xml(units_xml).to_xml
169
+ end
170
+
171
+ expect(squashed(leptris)).to eq(squashed(interpretive))
172
+ end
173
+
174
+ it "declares the island's namespace on the island under a default-namespace root" do
175
+ source = '<root xmlns="https://example.org/unitsml" name="n">' \
176
+ '<leaf xmlns="http://www.w3.org/1998/Math/MathML"><v>x</v></leaf></root>'
177
+ xml = NestedNamespaceDeclarationSpec::RootDefault.from_xml(source).to_xml
178
+
179
+ expect(squashed(xml)).to eq(
180
+ '<root xmlns="https://example.org/unitsml" name="n"> ' \
181
+ '<leaf xmlns="http://www.w3.org/1998/Math/MathML"> <v xmlns="">x</v> </leaf> </root>',
182
+ )
183
+ end
184
+
185
+ it "declares the island's namespace and opts its local element out under a namespace-less root" do
186
+ source = "<root><leaf xmlns=\"http://www.w3.org/1998/Math/MathML\"><v>x</v></leaf></root>"
187
+ xml = NestedNamespaceDeclarationSpec::RootNone.from_xml(source).to_xml
188
+
189
+ expect(squashed(xml)).to eq(
190
+ "<root> <leaf xmlns=\"http://www.w3.org/1998/Math/MathML\"> <v xmlns=\"\">x</v> </leaf> </root>",
191
+ )
192
+ end
193
+
194
+ it "keeps a prefix-parsed root's prefix and declares the island on the island" do
195
+ source = '<p:root xmlns:p="https://example.org/prefixed">' \
196
+ '<leaf xmlns="http://www.w3.org/1998/Math/MathML"><v>x</v></leaf></p:root>'
197
+ xml = NestedNamespaceDeclarationSpec::RootPrefixed.from_xml(source).to_xml
198
+
199
+ expect(squashed(xml)).to eq(
200
+ '<p:root xmlns:p="https://example.org/prefixed"> ' \
201
+ '<leaf xmlns="http://www.w3.org/1998/Math/MathML"> <v xmlns="">x</v> </leaf> </p:root>',
202
+ )
203
+ end
204
+
205
+ it "spells a constructed nested model the way the interpretive writer does" do
206
+ model = NestedNamespaceDeclarationSpec::RootDefaultPrefixedIsland.new(
207
+ leaf: NestedNamespaceDeclarationSpec::PrefixedIsland.new(v: "x"),
208
+ )
209
+ xml = model.to_xml
210
+
211
+ expect(squashed(xml)).to eq(
212
+ '<root xmlns="https://example.org/unitsml"> <leaf xmlns="https://example.org/prefixed"> ' \
213
+ '<v xmlns="">x</v> </leaf> </root>',
214
+ )
215
+ end
216
+
217
+ # The plan lane's walk capture carries no element prefix, so a nested
218
+ # island parsed with its declared prefix re-emits in the planner's
219
+ # default form here (the interpretive writer preserves the prefix). The
220
+ # output stays well-formed and namespace-correct either way.
221
+ it "never declares two default namespaces on one element for a prefix-parsed nested model" do
222
+ source = '<root xmlns="https://example.org/unitsml">' \
223
+ '<p:leaf xmlns:p="https://example.org/prefixed"><p:v>x</p:v></p:leaf></root>'
224
+ xml = NestedNamespaceDeclarationSpec::RootDefaultPrefixedIsland.from_xml(source).to_xml
225
+
226
+ xml.scan(/<[^>]+>/).each do |tag|
227
+ expect(tag.scan(' xmlns="').length).to be <= 1
228
+ end
229
+ expect(xml).to include('xmlns="https://example.org/unitsml"',
230
+ 'xmlns="https://example.org/prefixed"')
231
+ reparsed = NestedNamespaceDeclarationSpec::RootDefaultPrefixedIsland.from_xml(xml)
232
+ expect(reparsed.leaf.v).to eq("x")
233
+ end
234
+ end
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.208
4
+ version: 0.8.209
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -2382,6 +2382,7 @@ files:
2382
2382
  - spec/lutaml/xml/leptris/native_emission_ordered_spec.rb
2383
2383
  - spec/lutaml/xml/leptris/native_emission_plain_model_spec.rb
2384
2384
  - spec/lutaml/xml/leptris/native_emission_render_contract_spec.rb
2385
+ - spec/lutaml/xml/leptris/nested_namespace_declaration_spec.rb
2385
2386
  - spec/lutaml/xml/leptris/plan_hydrator_bridge_scope_spec.rb
2386
2387
  - spec/lutaml/xml/leptris/plan_hydrator_ns_snapshot_spec.rb
2387
2388
  - spec/lutaml/xml/leptris_string_encoding_spec.rb