lutaml-model 0.8.207 → 0.8.208

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: d100be5e75a2c176f69d9367bc66bf67ea65655ca6c181047b9aa97b1b985ac2
4
- data.tar.gz: '09942944fdc1ef7e16a229e56d88e07c4304307748dd6fe0cafc846f688299e2'
3
+ metadata.gz: ad86126c9e0284053e6ff23df2d047c5c658d679e8c9c7712cc722fd7aa7c46b
4
+ data.tar.gz: 7b03f53eaa1ea056110bffe25f9db569397819bb835be2c10964de9359571ce2
5
5
  SHA512:
6
- metadata.gz: 8474edf89b9345787bc78e804d8b1495cdb144a3d0e8c4821a11e1943634dc8b7d2c32ba5dc1d17e25d40290a2ed83691a668f472c34d537195ba9367ddc4b72
7
- data.tar.gz: fbedc7fb3cee383cd5351cbd35f4205af91f052331be46eceba41839bc1dd14451176a73ba30b20c583592f5e4b039fa40f7da802f9856b3d279243a930b99a3
6
+ metadata.gz: dd2ad49d9eb69cb840e7e99c88519665214e015e6fbc299bc1c9644eb52c2ea96b28be3372c3eeacc62c0c29e5dbd50807bb641ef6ec661f3ab0e33fd15e5f91
7
+ data.tar.gz: fdb971b77d5d19c7436491538114e2b5a14a930fe070ae59a25778311e8dd3a007eac2ad4d7f663c365b740963d1ed9f7b1a89f7d8de18e7284b700fff5a43a4
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "complex1-46KB": {
3
- "avg_time": 0.035969980333324734,
4
- "min_time": 0.03559261399999514,
5
- "max_time": 0.036185082999992346,
6
- "allocations": 50994,
7
- "ips": 27.800960432373113
3
+ "avg_time": 0.04722797766667478,
4
+ "min_time": 0.04673070100000132,
5
+ "max_time": 0.04775104800000918,
6
+ "allocations": 48293,
7
+ "ips": 21.17388991452887
8
8
  },
9
9
  "complex4-50KB": {
10
- "avg_time": 0.04991650399999988,
11
- "min_time": 0.04966256299999827,
12
- "max_time": 0.050061231000000816,
13
- "allocations": 71837,
14
- "ips": 20.03345426594784
10
+ "avg_time": 0.06411800366667346,
11
+ "min_time": 0.06375282199996946,
12
+ "max_time": 0.06461941400004889,
13
+ "allocations": 67871,
14
+ "ips": 15.596243532450604
15
15
  },
16
16
  "size1000-245KB": {
17
- "avg_time": 0.28591416999999336,
18
- "min_time": 0.2818747909999786,
19
- "max_time": 0.2920046950000028,
20
- "allocations": 406438,
21
- "ips": 3.4975531293185758
17
+ "avg_time": 0.36015998900001023,
18
+ "min_time": 0.35448734000004833,
19
+ "max_time": 0.36499723800000083,
20
+ "allocations": 382355,
21
+ "ips": 2.776543843130703
22
22
  },
23
23
  "complex3-567KB": {
24
- "avg_time": 0.40071153599999815,
25
- "min_time": 0.3966769870000064,
26
- "max_time": 0.4045028329999809,
27
- "allocations": 584888,
28
- "ips": 2.49556079663253
24
+ "avg_time": 0.5304185873333344,
25
+ "min_time": 0.5280918569999926,
26
+ "max_time": 0.5321308450000402,
27
+ "allocations": 560145,
28
+ "ips": 1.8853034638689303
29
29
  }
30
30
  }
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "metrology-4KB": {
3
- "avg_time": 0.008145603666662282,
4
- "min_time": 0.007795773999987432,
5
- "max_time": 0.008779492999906324,
6
- "allocations": 7544,
7
- "ips": 122.76560963710097
3
+ "avg_time": 0.013593411333332028,
4
+ "min_time": 0.013364400000000387,
5
+ "max_time": 0.013884672999999736,
6
+ "allocations": 6555,
7
+ "ips": 73.56505114708973
8
8
  },
9
9
  "bmj-49KB": {
10
- "avg_time": 0.0723875503333223,
11
- "min_time": 0.07129628399991361,
12
- "max_time": 0.07395147099998667,
13
- "allocations": 72378,
14
- "ips": 13.814530197462258
10
+ "avg_time": 0.11374726233333377,
11
+ "min_time": 0.11275422599999985,
12
+ "max_time": 0.11427680500000292,
13
+ "allocations": 61623,
14
+ "ips": 8.791420377833118
15
15
  },
16
16
  "pnas-152KB": {
17
- "avg_time": 0.34388590966667226,
18
- "min_time": 0.3415666549999514,
19
- "max_time": 0.34598907000008694,
20
- "allocations": 259437,
21
- "ips": 2.9079411859860658
17
+ "avg_time": 0.49736028199999777,
18
+ "min_time": 0.4924356920000008,
19
+ "max_time": 0.5007860399999942,
20
+ "allocations": 232230,
21
+ "ips": 2.0106149127525317
22
22
  }
23
23
  }
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "feature-doc-23KB": {
3
- "avg_time": 0.05333307866665867,
4
- "min_time": 0.05320582799998874,
5
- "max_time": 0.05343820699999924,
6
- "allocations": 33278,
7
- "ips": 18.750089531680324
3
+ "avg_time": 0.08004215133333521,
4
+ "min_time": 0.07967459299999291,
5
+ "max_time": 0.08053014500001154,
6
+ "allocations": 29557,
7
+ "ips": 12.49341732252428
8
8
  },
9
9
  "tbx-nisosts-101KB": {
10
- "avg_time": 0.1555278476666615,
11
- "min_time": 0.15451645799998914,
12
- "max_time": 0.15693985299998303,
13
- "allocations": 134268,
14
- "ips": 6.429716703488832
10
+ "avg_time": 0.24897703766667215,
11
+ "min_time": 0.24684493000000884,
12
+ "max_time": 0.2523852099999999,
13
+ "allocations": 116651,
14
+ "ips": 4.016434645426176
15
15
  },
16
16
  "iso-13849-1MB": {
17
- "avg_time": 5.377177922666665,
18
- "min_time": 5.361418570000012,
19
- "max_time": 5.4017619889999935,
20
- "allocations": 1735227,
21
- "ips": 0.1859711570607798
17
+ "avg_time": 7.87387151599999,
18
+ "min_time": 7.842370211000002,
19
+ "max_time": 7.923985744999982,
20
+ "allocations": 1679721,
21
+ "ips": 0.12700232636104922
22
22
  },
23
23
  "din-iso-1.1MB": {
24
- "avg_time": 0.5874012496666694,
25
- "min_time": 0.5822755989999848,
26
- "max_time": 0.5889298150000002,
27
- "allocations": 877606,
28
- "ips": 1.7024138109468896
24
+ "avg_time": 0.9811890356666595,
25
+ "min_time": 0.9675218900000004,
26
+ "max_time": 0.9853540780000003,
27
+ "allocations": 846635,
28
+ "ips": 1.0191716006289855
29
29
  }
30
30
  }
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "800-expressions": {
3
- "avg_time": 0.544940995666669,
4
- "min_time": 0.5414146770000059,
5
- "max_time": 0.5454735960000079,
6
- "allocations": 138102,
7
- "ips": 1.835061057897877
3
+ "avg_time": 0.1067107063333405,
4
+ "min_time": 0.10537397500002044,
5
+ "max_time": 0.10742932200000155,
6
+ "allocations": 139002,
7
+ "ips": 9.371130923604072
8
8
  }
9
9
  }
@@ -1,30 +1,30 @@
1
1
  {
2
2
  "ea251": {
3
- "avg_time": 0.17969961900000916,
4
- "min_time": 0.17811001200001897,
5
- "max_time": 0.18114700399999606,
6
- "allocations": 182292,
7
- "ips": 5.564842071256417
3
+ "avg_time": 0.18316421099999994,
4
+ "min_time": 0.18038664600001653,
5
+ "max_time": 0.1853450769999938,
6
+ "allocations": 179861,
7
+ "ips": 5.459581839380185
8
8
  },
9
9
  "medium": {
10
- "avg_time": 0.44894484466666046,
11
- "min_time": 0.43719278499997927,
12
- "max_time": 0.45557199699999273,
13
- "allocations": 471852,
14
- "ips": 2.2274451124224304
10
+ "avg_time": 0.4485483150000012,
11
+ "min_time": 0.44155083600000467,
12
+ "max_time": 0.4588752709999824,
13
+ "allocations": 465885,
14
+ "ips": 2.229414238240974
15
15
  },
16
16
  "citygml": {
17
- "avg_time": 0.506995725666665,
18
- "min_time": 0.503759285000001,
19
- "max_time": 0.5088972249999983,
20
- "allocations": 583260,
21
- "ips": 1.972403216388201
17
+ "avg_time": 0.5182148209999866,
18
+ "min_time": 0.5057585739999979,
19
+ "max_time": 0.5237662360000002,
20
+ "allocations": 577293,
21
+ "ips": 1.929701659381961
22
22
  },
23
23
  "large": {
24
- "avg_time": 4.185453844333334,
25
- "min_time": 4.1155831300000045,
26
- "max_time": 4.230669285999994,
27
- "allocations": 4506877,
28
- "ips": 0.23892271595681203
24
+ "avg_time": 4.232644015666655,
25
+ "min_time": 4.1436827659999835,
26
+ "max_time": 4.302634627999993,
27
+ "allocations": 4449467,
28
+ "ips": 0.23625894270782344
29
29
  }
30
30
  }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.207"
5
+ VERSION = "0.8.208"
6
6
  end
7
7
  end
@@ -19,8 +19,40 @@ module Lutaml
19
19
  # child_row_plans).
20
20
  CHILD_ROW_KINDS = %i[nested ordered_deferred].freeze
21
21
  XSI_URI = "http://www.w3.org/2001/XMLSchema-instance"
22
+ # Fiber-local bridge context and its nesting depth (see call).
23
+ BRIDGE_CONTEXT_KEY = :lutaml_plan_hydrator_bridge_context
24
+ BRIDGE_DEPTH_KEY = :lutaml_plan_hydrator_bridge_depth
22
25
 
23
26
  class << self
27
+ # Wrappers for deferred islands bridge leptris nodes into one
28
+ # moxml context per top-level deserialization, never for the
29
+ # process: on Ruby >= 4.0 (and < 3.4) moxml's wrapper registry
30
+ # is a strong Hash (moxml#303), and a context kept any longer
31
+ # pins every bridged document's whole tree for the life of the
32
+ # process — one registered wrapper references its native, the
33
+ # native its document, the document every node
34
+ # (lutaml-model#1335). Wrappers a model keeps stay alive
35
+ # through the model's own references; the registry's go when
36
+ # the deserialization returns.
37
+ def call(model_class, plan, value, **kwargs)
38
+ depth = (Thread.current[BRIDGE_DEPTH_KEY] || 0) + 1
39
+ Thread.current[BRIDGE_DEPTH_KEY] = depth
40
+ begin
41
+ hydrate(model_class, plan, value, **kwargs)
42
+ ensure
43
+ release_bridge_context(depth)
44
+ end
45
+ end
46
+
47
+ def release_bridge_context(depth)
48
+ if depth > 1
49
+ Thread.current[BRIDGE_DEPTH_KEY] = depth - 1
50
+ else
51
+ Thread.current[BRIDGE_DEPTH_KEY] = nil
52
+ Thread.current[BRIDGE_CONTEXT_KEY] = nil
53
+ end
54
+ end
55
+
24
56
  # plan: the compiled plan entry for model_class
25
57
  # value: the walk root PlanValue (element)
26
58
  # node: the parsed source element (leptris); ordered/mixed
@@ -31,8 +63,8 @@ module Lutaml
31
63
  # :lazy is honored as :eager — the lazy variant pins the
32
64
  # raw engine DOM, the retention the eager default exists
33
65
  # to avoid)
34
- def call(model_class, plan, value, parent: nil, node: nil,
35
- register: nil, declaration_plan_mode: :eager, intern: nil)
66
+ def hydrate(model_class, plan, value, parent: nil, node: nil,
67
+ register: nil, declaration_plan_mode: :eager, intern: nil)
36
68
  # The child model's declared lutaml_default_register takes
37
69
  # precedence over the ambient register — the same
38
70
  # resolve_for_child contract the interpretive path applies.
@@ -108,6 +140,8 @@ module Lutaml
108
140
  instance
109
141
  end
110
142
 
143
+ private :hydrate, :release_bridge_context
144
+
111
145
  # leptris#1874: snapshot rows -> the ns_data hash
112
146
  # ({path => {prefix_key => {uri:, prefix:, format:}}}), the
113
147
  # exact shape the recursion builds. Public: it is the
@@ -804,7 +838,7 @@ module Lutaml
804
838
  end
805
839
 
806
840
  def bridge_context
807
- @bridge_context ||= Moxml::Context.new(:leptris)
841
+ Thread.current[BRIDGE_CONTEXT_KEY] ||= Moxml::Context.new(:leptris)
808
842
  end
809
843
 
810
844
  # Rows carry the delegate NAME (the interpretive
@@ -12,6 +12,10 @@ module Lutaml
12
12
  # - Used by model's serialization pipeline via Transform.for(:xml)
13
13
  #
14
14
  class ModelTransform < ::Lutaml::Model::Transform
15
+ # The child-name set of a childless element: names nothing, so
16
+ # every named rule is unmatched there (see apply_xml_mapping).
17
+ EMPTY_CHILD_NAMES = Set.new.freeze
18
+
15
19
  # Per (model class, register): compiled records for the mapping
16
20
  # rules — attribute, derived/valid flags, and #765 group-skip —
17
21
  # keyed on the mapping's finalize version so late changes rebuild.
@@ -330,18 +334,42 @@ module Lutaml
330
334
  child_names_set = nil
331
335
  if doc.is_a?(::Lutaml::Xml::XmlElement) || doc.is_a?(::Lutaml::Xml::Document)
332
336
  ec = doc.element_children
333
- unless ec.empty?
334
- child_names_set = Set.new
335
- ec.each do |child|
336
- child_names_set.add(child.namespaced_name)
337
- child_names_set.add(child.unprefixed_name)
338
- # URI-qualified spelling: prefix-independent exact matching
339
- # against the rule's URI-form namespaced name — derived
340
- # models bind per schema unit while instances spell
341
- # arbitrary prefixes (#1212).
342
- if (c_uri = child.namespace_uri)
343
- child_names_set.add("#{c_uri}:#{child.unprefixed_name}")
344
- end
337
+ # A childless element names no child: its named element rules
338
+ # are all unmatched, and replay them like any other element's
339
+ # (an empty set keeps the fast-skip and replay branches open).
340
+ child_names_set = if ec.empty?
341
+ EMPTY_CHILD_NAMES
342
+ else
343
+ Set.new
344
+ end
345
+ ec.each do |child|
346
+ child_names_set.add(child.namespaced_name)
347
+ child_names_set.add(child.unprefixed_name)
348
+ # URI-qualified spelling: prefix-independent exact matching
349
+ # against the rule's URI-form namespaced name — derived
350
+ # models bind per schema unit while instances spell
351
+ # arbitrary prefixes (#1212).
352
+ if (c_uri = child.namespace_uri)
353
+ child_names_set.add("#{c_uri}:#{child.unprefixed_name}")
354
+ end
355
+ end
356
+ end
357
+
358
+ # An attribute rule can only match an attribute whose local name
359
+ # one of its spellings names: the exact-name, URI-resolved and
360
+ # lenient local-name lookups all key on it. Classes declare
361
+ # hundreds of attributes and instances carry a few (the MathML
362
+ # token classes declare ~280 and set one or two), so the
363
+ # unmatched majority is replayed without its lookup machinery
364
+ # (lutaml-model#1333).
365
+ attribute_local_names = nil
366
+ if doc.is_a?(::Lutaml::Xml::XmlElement) || doc.is_a?(::Lutaml::Xml::Document)
367
+ el = doc.is_a?(::Lutaml::Xml::XmlElement) ? doc : doc.root
368
+ if el.is_a?(::Lutaml::Xml::XmlElement)
369
+ attribute_local_names = []
370
+ el.attributes.each_value do |xml_attr|
371
+ attribute_local_names << xml_attr.name
372
+ xml_attr.local_names.each { |n| attribute_local_names << n }
345
373
  end
346
374
  end
347
375
  end
@@ -375,9 +403,9 @@ module Lutaml
375
403
 
376
404
  # #903/#922: replay-or-drop unmatched rules; never clobber
377
405
  # a mid-parse custom-writer assignment.
378
- next if replay_unmatched_rule(child_names_set, record, rule,
379
- instance, defaults_used,
380
- xml_mapping)
406
+ next if replay_unmatched_rule(child_names_set,
407
+ attribute_local_names, record,
408
+ rule, instance, defaults_used)
381
409
 
382
410
  rule_to = rule.to
383
411
 
@@ -957,9 +985,14 @@ _effective_register)
957
985
  Lutaml::Xml::HtmlEntities.decode(value)
958
986
  end
959
987
 
988
+ # unmatched_kind: :attribute / :element for a replayable rule
989
+ # (nil otherwise); unmatched_name: a replayable rule's own wire
990
+ # name, so the per-element presence test needs no block.
960
991
  Record = Struct.new(:attr, :derived, :group_skip, :valid,
961
992
  :unmatched_mode, :replay_writer,
962
- :unmatched_names, :raw_reader)
993
+ :unmatched_names, :raw_reader,
994
+ :unmatched_local, :unmatched_kind,
995
+ :unmatched_name)
963
996
 
964
997
  def compiled_rule_records(model_class, register, grouped_plain_rules)
965
998
  return {}.compare_by_identity unless model_class.include?(Lutaml::Model::Serialize)
@@ -990,10 +1023,14 @@ _effective_register)
990
1023
  # :write_nil (setter write + flag churn) or :drop (nothing).
991
1024
  # Everything else -- defaults, aliases, custom from:,
992
1025
  # delegates, when_attribute dispatch, transformers, value
993
- # maps beyond those two -- keeps its visit.
1026
+ # maps beyond those two -- keeps its visit. So does every rule
1027
+ # the name test cannot classify: content and raw mappings,
1028
+ # name-less rules, the catch-all.
994
1029
  unmatched_mode = nil
995
1030
  replay_writer = nil
996
- if attr && !attr.derived? &&
1031
+ if attr && !attr.derived? && rule.name &&
1032
+ !rule.content_mapping? && !rule.raw_mapping? &&
1033
+ !mapping.any_element_rule.equal?(rule) &&
997
1034
  !rule.render_default && !rule.multiple_mappings? &&
998
1035
  !rule.has_custom_method_for_deserialization? &&
999
1036
  !rule.delegate &&
@@ -1034,6 +1071,15 @@ _effective_register)
1034
1071
  else
1035
1072
  [rule.name.to_s]
1036
1073
  end
1074
+ # An attribute rule's local name — the part every spelling it
1075
+ # resolves to shares, and the key the element's attribute
1076
+ # names are tested against.
1077
+ unmatched_local = nil
1078
+ if rule.attribute? && !rule.multiple_mappings?
1079
+ rule_name = rule.name.to_s
1080
+ colon = rule_name.rindex(":")
1081
+ unmatched_local = colon ? rule_name[(colon + 1)..] : rule_name
1082
+ end
1037
1083
  if unmatched_mode == :write_nil
1038
1084
  replay_writer =
1039
1085
  rule.class.parsed_assign_writer(model_class, rule.to)
@@ -1057,6 +1103,9 @@ _effective_register)
1057
1103
  rule.to.to_s.match?(/\A[a-zA-Z_][a-zA-Z0-9_]*\z/)
1058
1104
  :"__lutaml_raw_#{rule.to}"
1059
1105
  end,
1106
+ unmatched_local,
1107
+ unmatched_mode && rule.attribute? ? :attribute : nil,
1108
+ rule.name.to_s,
1060
1109
  )
1061
1110
  end
1062
1111
  entry = Struct.new(:version, :records).new(version, records)
@@ -1774,20 +1823,32 @@ effective_register = lutaml_register)
1774
1823
  raw.equal?(::Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION)
1775
1824
  end
1776
1825
 
1777
- def replay_unmatched_rule(child_names_set, record, rule, instance,
1778
- defaults_used, xml_mapping)
1779
- return false unless child_names_set && record
1826
+ def replay_unmatched_rule(child_names_set, attribute_local_names,
1827
+ record, rule, instance, defaults_used)
1828
+ return false unless record
1780
1829
 
1781
1830
  mode = record.unmatched_mode
1782
1831
  return false unless mode
1783
- return false if rule.attribute? || rule.content_mapping? ||
1784
- rule.raw_mapping? || !rule.name ||
1785
- xml_mapping.any_element_rule.equal?(rule)
1786
1832
 
1787
- matched = record.unmatched_names.any? do |n|
1788
- child_names_set.include?(n)
1833
+ # Attribute rules name the element's attributes, element rules
1834
+ # its children; an unmatched rule of either kind has the same
1835
+ # two endings (lutaml-model#1333).
1836
+ if record.unmatched_kind
1837
+ return false unless attribute_local_names
1838
+
1839
+ return false if attribute_local_names.include?(record.unmatched_name) ||
1840
+ attribute_local_names.include?(record.unmatched_local)
1841
+ else
1842
+ return false unless child_names_set
1843
+
1844
+ names = record.unmatched_names
1845
+ matched = if names.size == 1
1846
+ child_names_set.include?(names.first)
1847
+ else
1848
+ names.any? { |n| child_names_set.include?(n) }
1849
+ end
1850
+ return false if matched
1789
1851
  end
1790
- return false if matched
1791
1852
 
1792
1853
  return true if mode == :drop
1793
1854
 
@@ -1813,8 +1874,8 @@ effective_register = lutaml_register)
1813
1874
  return true unless raw_initialized?(record, instance)
1814
1875
 
1815
1876
  defaults_used << rule_to
1877
+ # The parsed writer marks the attribute set itself.
1816
1878
  instance.public_send(record.replay_writer, nil)
1817
- instance.value_set_for(rule_to)
1818
1879
  true
1819
1880
  end
1820
1881
 
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1333: an unmatched rule is replayed instead of visited —
6
+ # for attribute rules too, keyed on the element's attribute names, and
7
+ # for the rules of a childless element. The replay must leave exactly the
8
+ # state the visit did: what the visit would bind, flag, or default. The
9
+ # replay is the standard pipeline's: these examples pin that lane (the
10
+ # suite default is the leptris plan lane, which hydrates these models
11
+ # natively).
12
+ module UnmatchedReplaySpec
13
+ class Wide < Lutaml::Model::Serializable
14
+ attribute :a0, :string, default: "zero"
15
+ attribute :a1, :string
16
+ attribute :a2, :string
17
+ (3...120).each { |i| attribute :"a#{i}", :string }
18
+
19
+ xml do
20
+ element "wide"
21
+ (0...120).each { |i| map_attribute "a#{i}", to: :"a#{i}" }
22
+ end
23
+ end
24
+
25
+ class NilledChild < Lutaml::Model::Serializable
26
+ attribute :name, :string
27
+ attribute :note, :string
28
+
29
+ xml do
30
+ element "nilled"
31
+ map_attribute "name", to: :name
32
+ map_element "note", to: :note, render_nil: true
33
+ end
34
+ end
35
+
36
+ class Omitted < Lutaml::Model::Serializable
37
+ attribute :name, :string
38
+ attribute :note, :string
39
+
40
+ xml do
41
+ element "omitted"
42
+ map_attribute "name", to: :name
43
+ map_attribute "note", to: :note, value_map: { from: { omitted: :omitted } }
44
+ end
45
+ end
46
+
47
+ class Childless < Lutaml::Model::Serializable
48
+ attribute :lead, :string
49
+ attribute :items, :string, collection: true
50
+ attribute :sized, :string, default: "one"
51
+
52
+ xml do
53
+ element "childless"
54
+ map_element "lead", to: :lead
55
+ map_element "item", to: :items
56
+ map_element "sized", to: :sized
57
+ end
58
+ end
59
+ end
60
+
61
+ RSpec.describe "unmatched rules replay" do
62
+ around do |example|
63
+ previous = Lutaml::Model::Config.xml_adapter_type
64
+ Lutaml::Model::Config.xml_adapter_type = :nokogiri
65
+ example.run
66
+ ensure
67
+ if previous
68
+ Lutaml::Model::Config.xml_adapter_type = previous
69
+ else
70
+ Lutaml::Model::Config.unset_xml_adapter_type
71
+ end
72
+ end
73
+
74
+ describe "a wide attribute vocabulary" do
75
+ it "binds the attributes present and leaves the others at their seed" do
76
+ instance = UnmatchedReplaySpec::Wide.from_xml('<wide a1="given" a40="x"/>')
77
+
78
+ expect(instance.a1).to eq("given")
79
+ expect(instance.a40).to eq("x")
80
+ expect(instance.a0).to eq("zero")
81
+ expect(instance.a2).to be_nil
82
+ expect(instance.using_default?(:a2)).to be(true)
83
+ end
84
+
85
+ it "writes nil for an absent render_nil element rule as the visit would" do
86
+ instance = UnmatchedReplaySpec::NilledChild.from_xml('<nilled name="n"/>')
87
+
88
+ expect(instance.note).to be_nil
89
+ expect(instance.to_xml).to include('<note xsi:nil="true"/>')
90
+ end
91
+
92
+ it "writes nothing for an omitted: :omitted attribute the element lacks" do
93
+ instance = UnmatchedReplaySpec::Omitted.from_xml('<omitted name="n"/>')
94
+
95
+ expect(instance.name).to eq("n")
96
+ expect(Lutaml::Model::Utils.uninitialized?(instance.note)).to be(true)
97
+ expect(instance.to_xml).not_to include("note")
98
+ end
99
+
100
+ it "serializes an element that carried two attributes of many" do
101
+ output = UnmatchedReplaySpec::Wide.from_xml('<wide a1="given" a40="x"/>').to_xml
102
+
103
+ expect(output).to be_xml_equivalent_to('<wide a1="given" a40="x"/>')
104
+ end
105
+ end
106
+
107
+ describe "a childless element" do
108
+ it "leaves its element rules at their seed and keeps defaults" do
109
+ instance = UnmatchedReplaySpec::Childless.from_xml("<childless/>")
110
+
111
+ expect(instance.lead).to be_nil
112
+ expect(instance.items).to eq([])
113
+ expect(instance.sized).to eq("one")
114
+ expect(instance.to_xml).to be_xml_equivalent_to("<childless/>")
115
+ end
116
+
117
+ it "binds the children a populated element carries" do
118
+ instance = UnmatchedReplaySpec::Childless.from_xml(
119
+ "<childless><lead>L</lead><item>a</item><item>b</item></childless>",
120
+ )
121
+
122
+ expect(instance.lead).to eq("L")
123
+ expect(instance.items).to eq(%w[a b])
124
+ expect(instance.sized).to eq("one")
125
+ end
126
+ end
127
+ end
@@ -19,6 +19,41 @@ module WriterAssigned
19
19
  end
20
20
  end
21
21
 
22
+ # lutaml-model#1333: an unmatched ATTRIBUTE rule replays like an
23
+ # unmatched element rule — the writer's assignment survives it too,
24
+ # whichever rule the mapping visits first.
25
+ class AttrTextAfter < Lutaml::Model::Serializable
26
+ attribute :code, :string
27
+ attribute :text, :string
28
+
29
+ xml do
30
+ element "probe"
31
+ map_element "code", to: :code
32
+ map_attribute "text", to: :text
33
+ end
34
+
35
+ def code=(val)
36
+ super
37
+ self.text = "derived-#{val}" if val
38
+ end
39
+ end
40
+
41
+ class AttrTextBefore < Lutaml::Model::Serializable
42
+ attribute :code, :string
43
+ attribute :text, :string
44
+
45
+ xml do
46
+ element "probe"
47
+ map_attribute "text", to: :text
48
+ map_element "code", to: :code
49
+ end
50
+
51
+ def code=(val)
52
+ super
53
+ self.text = "derived-#{val}" if val
54
+ end
55
+ end
56
+
22
57
  class Defaulted < Lutaml::Model::Serializable
23
58
  attribute :code, :string
24
59
  attribute :text, :string, default: -> { "" }
@@ -59,4 +94,30 @@ RSpec.describe "attributes assigned inside another attribute's writer" do
59
94
  end
60
95
  end
61
96
  end
97
+
98
+ [WriterAssigned::AttrTextAfter, WriterAssigned::AttrTextBefore].each do |klass|
99
+ describe "with #{klass.name.split('::').last} (text mapped as an attribute)" do
100
+ it "keeps the derived value through the unmatched attribute rule" do
101
+ instance = klass.from_xml("<probe><code>67.060</code></probe>")
102
+
103
+ expect(instance.text).to eq("derived-67.060")
104
+ expect(instance.using_default?(:text)).to be(false)
105
+ end
106
+
107
+ it "binds an absent text attribute to nothing" do
108
+ instance = klass.from_xml("<probe><code>67.060</code></probe>")
109
+
110
+ expect(instance.text).to eq("derived-67.060")
111
+ expect(instance.code).to eq("67.060")
112
+ end
113
+
114
+ it "leaves an absent attribute at its seed when nothing assigns it" do
115
+ instance = klass.from_xml("<probe/>")
116
+
117
+ expect(instance.text).to be_nil
118
+ expect(instance.code).to be_nil
119
+ expect(instance.to_xml).to be_xml_equivalent_to("<probe/>")
120
+ end
121
+ end
122
+ end
62
123
  end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1335: wrappers for deferred islands bridge leptris nodes
6
+ # into a moxml context. On Ruby >= 4.0 (and < 3.4) that context's
7
+ # wrapper registry is a strong Hash, so a context kept for the process
8
+ # pinned every deserialized document's whole tree for the life of the
9
+ # process. The context lives for one top-level deserialization instead.
10
+ module BridgeScopeSpec
11
+ class Inner < Lutaml::Model::Serializable
12
+ attribute :name, :string
13
+
14
+ xml do
15
+ element "inner"
16
+ map_element "name", to: :name
17
+ end
18
+ end
19
+
20
+ class Leaf < Lutaml::Model::Serializable
21
+ # Custom from: methods run on a stand-in receiver; state they keep
22
+ # lives on the hydrated model.
23
+ attr_accessor :seen_element
24
+
25
+ attribute :label, :string
26
+
27
+ xml do
28
+ element "leaf"
29
+ map_element "label", to: :label, with: { from: :label_from, to: :label_to }
30
+ end
31
+
32
+ def label_from(model, element)
33
+ # The island runs inside the deserialization: the model keeps the
34
+ # bridged element and uses it after the parse.
35
+ model.seen_element = element
36
+ model.label = element.text
37
+ end
38
+
39
+ def label_to(model, parent, doc); end
40
+ end
41
+
42
+ class Holder < Lutaml::Model::Serializable
43
+ attribute :inner, Inner
44
+ attribute :leaf, Leaf
45
+
46
+ xml do
47
+ element "holder"
48
+ map_element "inner", to: :inner
49
+ map_element "leaf", to: :leaf
50
+ end
51
+ end
52
+
53
+ # A custom method that deserializes another model mid-island: the
54
+ # outer deserialization's context must outlive the nested one.
55
+ class NestedCaller < Lutaml::Model::Serializable
56
+ attr_accessor :nested, :context_after_nested
57
+
58
+ attribute :inner, Inner
59
+
60
+ xml do
61
+ element "caller"
62
+ map_element "inner", to: :inner, with: { from: :inner_from, to: :inner_to }
63
+ end
64
+
65
+ def inner_from(model, element)
66
+ model.nested = Inner.from_xml("<inner><name>#{element.text}</name></inner>")
67
+ model.context_after_nested =
68
+ Thread.current[Lutaml::Xml::Leptris::PlanHydrator::BRIDGE_CONTEXT_KEY]
69
+ model.inner = model.nested
70
+ end
71
+
72
+ def inner_to(model, parent, doc); end
73
+ end
74
+ end
75
+
76
+ RSpec.describe Lutaml::Xml::Leptris::PlanHydrator do
77
+ around do |example|
78
+ previous = Lutaml::Model::Config.xml_adapter_type
79
+ Lutaml::Model::Config.xml_adapter_type = :leptris
80
+ example.run
81
+ ensure
82
+ if previous
83
+ Lutaml::Model::Config.xml_adapter_type = previous
84
+ else
85
+ Lutaml::Model::Config.unset_xml_adapter_type
86
+ end
87
+ end
88
+
89
+ let(:context_key) { described_class::BRIDGE_CONTEXT_KEY }
90
+
91
+ it "keeps the bridge context live through the deserialization only" do
92
+ before = Thread.current[context_key]
93
+ holder = BridgeScopeSpec::Holder.from_xml(
94
+ "<holder><inner><name>a</name></inner><leaf><label>b</label></leaf></holder>",
95
+ )
96
+
97
+ expect(before).to be_nil
98
+ expect(Thread.current[context_key]).to be_nil
99
+ expect(holder.inner.name).to eq("a")
100
+ expect(holder.leaf.label).to eq("b")
101
+ end
102
+
103
+ it "lets the model keep using a bridged element after the deserialization" do
104
+ holder = BridgeScopeSpec::Holder.from_xml(
105
+ '<holder><leaf><label xml:lang="en">b</label></leaf></holder>',
106
+ )
107
+ GC.start
108
+
109
+ expect(holder.leaf.label).to eq("b")
110
+ expect(holder.leaf.seen_element.text).to eq("b")
111
+ expect(holder.leaf.seen_element.find_attribute_value("xml:lang")).to eq("en")
112
+ end
113
+
114
+ it "releases the context once a nested deserialization inside an island returns" do
115
+ caller = BridgeScopeSpec::NestedCaller.from_xml("<caller><inner>x</inner></caller>")
116
+
117
+ expect(caller.nested.name).to eq("x")
118
+ expect(caller.context_after_nested).not_to be_nil
119
+ expect(Thread.current[context_key]).to be_nil
120
+ end
121
+
122
+ # The context is not memoized across deserializations — the mechanism
123
+ # behind #1335 — and a fresh one is created for the next parse. (That a
124
+ # dropped model's tree is then collected is asserted in the release
125
+ # gates, in an isolated process: a GC outcome inside the shared suite
126
+ # process is order- and platform-dependent.)
127
+ it "gives every top-level deserialization its own bridge context" do
128
+ seen = {}
129
+ probe = Class.new(Lutaml::Model::Serializable) do
130
+ attr_accessor :context
131
+
132
+ attribute :label, :string
133
+ xml do
134
+ element "probe"
135
+ map_element "label", to: :label, with: { from: :label_from, to: :label_to }
136
+ end
137
+ define_method(:label_from) do |model, element|
138
+ model.context = Thread.current[Lutaml::Xml::Leptris::PlanHydrator::BRIDGE_CONTEXT_KEY]
139
+ model.label = element.text
140
+ end
141
+ def label_to(model, parent, doc); end
142
+ end
143
+ probe.from_xml("<probe><label>a</label></probe>").context.then { |c| seen[:first] = c }
144
+ probe.from_xml("<probe><label>b</label></probe>").context.then { |c| seen[:second] = c }
145
+
146
+ expect(seen[:first]).not_to be_nil
147
+ expect(seen[:second]).not_to be_nil
148
+ expect(seen[:second]).not_to equal(seen[:first])
149
+ expect(Thread.current[context_key]).to be_nil
150
+ end
151
+ 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.207
4
+ version: 0.8.208
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -2273,6 +2273,7 @@ files:
2273
2273
  - spec/lutaml/model/union_attribute_spec.rb
2274
2274
  - spec/lutaml/model/union_lenient_model_spec.rb
2275
2275
  - spec/lutaml/model/union_spec.rb
2276
+ - spec/lutaml/model/unmatched_rule_replay_spec.rb
2276
2277
  - spec/lutaml/model/unregistered_context_resolution_spec.rb
2277
2278
  - spec/lutaml/model/utils_spec.rb
2278
2279
  - spec/lutaml/model/validation/concerns/has_issues_spec.rb
@@ -2381,6 +2382,7 @@ files:
2381
2382
  - spec/lutaml/xml/leptris/native_emission_ordered_spec.rb
2382
2383
  - spec/lutaml/xml/leptris/native_emission_plain_model_spec.rb
2383
2384
  - spec/lutaml/xml/leptris/native_emission_render_contract_spec.rb
2385
+ - spec/lutaml/xml/leptris/plan_hydrator_bridge_scope_spec.rb
2384
2386
  - spec/lutaml/xml/leptris/plan_hydrator_ns_snapshot_spec.rb
2385
2387
  - spec/lutaml/xml/leptris_string_encoding_spec.rb
2386
2388
  - spec/lutaml/xml/line_ending_spec.rb