lutaml-model 0.8.206 → 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.
@@ -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.
@@ -146,8 +150,15 @@ module Lutaml
146
150
  end
147
151
  end
148
152
  root_and_parent_assignment(instance, options)
149
- apply_xml_mapping(ParseSession.new(data, instance, options,
150
- child_register))
153
+ parsed = apply_xml_mapping(ParseSession.new(data, instance, options,
154
+ child_register))
155
+ # A parsed instance is pristine on every lane: the parse's own
156
+ # writer calls are not a mutation of the parsed state (the plan
157
+ # lanes' hydrator resets the same flag after construction).
158
+ if instance_is_serialize
159
+ instance.clear_mutated_after_parse!
160
+ end
161
+ parsed
151
162
  end
152
163
 
153
164
  # Build a DeclarationPlan from the parsed element tree's namespace declarations.
@@ -323,18 +334,42 @@ module Lutaml
323
334
  child_names_set = nil
324
335
  if doc.is_a?(::Lutaml::Xml::XmlElement) || doc.is_a?(::Lutaml::Xml::Document)
325
336
  ec = doc.element_children
326
- unless ec.empty?
327
- child_names_set = Set.new
328
- ec.each do |child|
329
- child_names_set.add(child.namespaced_name)
330
- child_names_set.add(child.unprefixed_name)
331
- # URI-qualified spelling: prefix-independent exact matching
332
- # against the rule's URI-form namespaced name — derived
333
- # models bind per schema unit while instances spell
334
- # arbitrary prefixes (#1212).
335
- if (c_uri = child.namespace_uri)
336
- child_names_set.add("#{c_uri}:#{child.unprefixed_name}")
337
- 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 }
338
373
  end
339
374
  end
340
375
  end
@@ -368,9 +403,9 @@ module Lutaml
368
403
 
369
404
  # #903/#922: replay-or-drop unmatched rules; never clobber
370
405
  # a mid-parse custom-writer assignment.
371
- next if replay_unmatched_rule(child_names_set, record, rule,
372
- instance, defaults_used,
373
- xml_mapping)
406
+ next if replay_unmatched_rule(child_names_set,
407
+ attribute_local_names, record,
408
+ rule, instance, defaults_used)
374
409
 
375
410
  rule_to = rule.to
376
411
 
@@ -950,9 +985,14 @@ _effective_register)
950
985
  Lutaml::Xml::HtmlEntities.decode(value)
951
986
  end
952
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.
953
991
  Record = Struct.new(:attr, :derived, :group_skip, :valid,
954
992
  :unmatched_mode, :replay_writer,
955
- :unmatched_names, :raw_reader)
993
+ :unmatched_names, :raw_reader,
994
+ :unmatched_local, :unmatched_kind,
995
+ :unmatched_name)
956
996
 
957
997
  def compiled_rule_records(model_class, register, grouped_plain_rules)
958
998
  return {}.compare_by_identity unless model_class.include?(Lutaml::Model::Serialize)
@@ -983,10 +1023,14 @@ _effective_register)
983
1023
  # :write_nil (setter write + flag churn) or :drop (nothing).
984
1024
  # Everything else -- defaults, aliases, custom from:,
985
1025
  # delegates, when_attribute dispatch, transformers, value
986
- # 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.
987
1029
  unmatched_mode = nil
988
1030
  replay_writer = nil
989
- 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) &&
990
1034
  !rule.render_default && !rule.multiple_mappings? &&
991
1035
  !rule.has_custom_method_for_deserialization? &&
992
1036
  !rule.delegate &&
@@ -1027,6 +1071,15 @@ _effective_register)
1027
1071
  else
1028
1072
  [rule.name.to_s]
1029
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
1030
1083
  if unmatched_mode == :write_nil
1031
1084
  replay_writer =
1032
1085
  rule.class.parsed_assign_writer(model_class, rule.to)
@@ -1050,6 +1103,9 @@ _effective_register)
1050
1103
  rule.to.to_s.match?(/\A[a-zA-Z_][a-zA-Z0-9_]*\z/)
1051
1104
  :"__lutaml_raw_#{rule.to}"
1052
1105
  end,
1106
+ unmatched_local,
1107
+ unmatched_mode && rule.attribute? ? :attribute : nil,
1108
+ rule.name.to_s,
1053
1109
  )
1054
1110
  end
1055
1111
  entry = Struct.new(:version, :records).new(version, records)
@@ -1767,20 +1823,32 @@ effective_register = lutaml_register)
1767
1823
  raw.equal?(::Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION)
1768
1824
  end
1769
1825
 
1770
- def replay_unmatched_rule(child_names_set, record, rule, instance,
1771
- defaults_used, xml_mapping)
1772
- 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
1773
1829
 
1774
1830
  mode = record.unmatched_mode
1775
1831
  return false unless mode
1776
- return false if rule.attribute? || rule.content_mapping? ||
1777
- rule.raw_mapping? || !rule.name ||
1778
- xml_mapping.any_element_rule.equal?(rule)
1779
1832
 
1780
- matched = record.unmatched_names.any? do |n|
1781
- 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
1782
1851
  end
1783
- return false if matched
1784
1852
 
1785
1853
  return true if mode == :drop
1786
1854
 
@@ -1806,8 +1874,8 @@ effective_register = lutaml_register)
1806
1874
  return true unless raw_initialized?(record, instance)
1807
1875
 
1808
1876
  defaults_used << rule_to
1877
+ # The parsed writer marks the attribute set itself.
1809
1878
  instance.public_send(record.replay_writer, nil)
1810
- instance.value_set_for(rule_to)
1811
1879
  true
1812
1880
  end
1813
1881
 
data/lutaml-model.gemspec CHANGED
@@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
35
35
  spec.add_dependency "bigdecimal"
36
36
  spec.add_dependency "canon"
37
37
  spec.add_dependency "concurrent-ruby"
38
- spec.add_dependency "liquid", ">= 4.0", "< 6.0"
38
+ spec.add_dependency "liquid", "~> 5.14"
39
39
  # 0.5.84 always passes XML_PARSE_HUGE to libxml2: very large documents
40
40
  # parse completely instead of being silently truncated at the 10 MB
41
41
  # input-buffer cap (lutaml-model#871).
@@ -1 +1 @@
1
- {"capabilities":["json","xml"],"corpora":[{"generation":"core-v2","sha256":"sha256:fd1de9747a78a71d737a4076ebfb0cf4a561d96dba9156a43afb5a337ebad0d2"}],"dependencies":[{"name":"lutaml-model","revision":"8a8ef040963b7965388b01b58af33af2b0995f91","sha256":"sha256:14c85f76a3cf2ebb18aa3c97186840897bf2147c886818a5c73670d3a6db170a"}],"files":[{"path":"index.json","sha256":"sha256:fd1de9747a78a71d737a4076ebfb0cf4a561d96dba9156a43afb5a337ebad0d2"},{"path":"schema.json","sha256":"sha256:ce5cc1c61e4a97bc71b2683b71e56c5488dbd3f41fdfa2802c01c4c2cd34fa1e"},{"path":"schema.yaml","sha256":"sha256:5c6f2cf9ddb907bd2aaff813711a6ed0d35e0ff1563d5b4f893301f803dac8e0"}],"generator_version":"ruby-core-oracle-v2","lml_checksum":"sha256:a7fac433ca7be94a6a66f6b8d0dab8134747860cf8076dffc4aaa92a24d1cb34","manifest":1,"schema_version":2,"target":"ruby"}
1
+ {"capabilities":["json","xml"],"corpora":[{"generation":"core-v2","sha256":"sha256:fd1de9747a78a71d737a4076ebfb0cf4a561d96dba9156a43afb5a337ebad0d2"}],"dependencies":[{"name":"lutaml-model","revision":"8a8ef040963b7965388b01b58af33af2b0995f91","sha256":"sha256:480e5a78e34f290b28420d56b1f4aad0162a16c87067c21597e99e9b1dbca39e"}],"files":[{"path":"index.json","sha256":"sha256:fd1de9747a78a71d737a4076ebfb0cf4a561d96dba9156a43afb5a337ebad0d2"},{"path":"schema.json","sha256":"sha256:ce5cc1c61e4a97bc71b2683b71e56c5488dbd3f41fdfa2802c01c4c2cd34fa1e"},{"path":"schema.yaml","sha256":"sha256:5c6f2cf9ddb907bd2aaff813711a6ed0d35e0ff1563d5b4f893301f803dac8e0"}],"generator_version":"ruby-core-oracle-v2","lml_checksum":"sha256:a7fac433ca7be94a6a66f6b8d0dab8134747860cf8076dffc4aaa92a24d1cb34","manifest":1,"schema_version":2,"target":"ruby"}
@@ -1 +1 @@
1
- {"capabilities":["json","xml"],"corpora":[{"generation":"preservation-v1","sha256":"sha256:76d942557692ee653136f10fcf98e5b3beef43d9ed9a9082eb6d17002b64ddd9"}],"dependencies":[{"name":"lutaml-model","revision":"8a8ef040963b7965388b01b58af33af2b0995f91","sha256":"sha256:14c85f76a3cf2ebb18aa3c97186840897bf2147c886818a5c73670d3a6db170a"}],"files":[{"path":"index.json","sha256":"sha256:76d942557692ee653136f10fcf98e5b3beef43d9ed9a9082eb6d17002b64ddd9"},{"path":"schema.json","sha256":"sha256:66b86b75d36568c1c353a2d5cf32d0e6562e21e7dfea0004a757d0f774761264"},{"path":"schema.yaml","sha256":"sha256:cf40d46e901e4a8e7bb4b7f7ad4365ffec687bef81832663dc136626b3963c3d"}],"generator_version":"ruby-preservation-oracle-v1","lml_checksum":"sha256:81eaac6230a81d4285fb4ea743f202234ef3f3c48482ca60ce9e9b642a5622e5","manifest":1,"schema_version":2,"target":"ruby"}
1
+ {"capabilities":["json","xml"],"corpora":[{"generation":"preservation-v1","sha256":"sha256:76d942557692ee653136f10fcf98e5b3beef43d9ed9a9082eb6d17002b64ddd9"}],"dependencies":[{"name":"lutaml-model","revision":"8a8ef040963b7965388b01b58af33af2b0995f91","sha256":"sha256:480e5a78e34f290b28420d56b1f4aad0162a16c87067c21597e99e9b1dbca39e"}],"files":[{"path":"index.json","sha256":"sha256:76d942557692ee653136f10fcf98e5b3beef43d9ed9a9082eb6d17002b64ddd9"},{"path":"schema.json","sha256":"sha256:66b86b75d36568c1c353a2d5cf32d0e6562e21e7dfea0004a757d0f774761264"},{"path":"schema.yaml","sha256":"sha256:cf40d46e901e4a8e7bb4b7f7ad4365ffec687bef81832663dc136626b3963c3d"}],"generator_version":"ruby-preservation-oracle-v1","lml_checksum":"sha256:81eaac6230a81d4285fb4ea743f202234ef3f3c48482ca60ce9e9b642a5622e5","manifest":1,"schema_version":2,"target":"ruby"}
@@ -334,24 +334,14 @@ RSpec.describe Lutaml::Model::Cli do
334
334
 
335
335
  after { FileUtils.rm_rf(temp_dir) }
336
336
 
337
- it "handles malformed XML according to the engine's parse contract" do
337
+ it "reports malformed XML instead of comparing a recovered tree" do
338
338
  cli.options = options
339
339
 
340
- # The leptris plan lane parses strictly (malformed XML raises on
341
- # every lane, 17a4658d): the CLI reports the parse failure. A
342
- # recovering engine (Nokogiri) hands it a best-effort tree and
343
- # the comparison runs on that.
344
- if Lutaml::Xml.moxml_engine == :leptris
345
- expect do
346
- cli.compare(malformed_xml, files[:target_xml_path])
347
- end.to raise_error(StandardError, /Error parsing file .*malformed\.xml/)
348
- else
349
- expect do
350
- cli.compare(malformed_xml, files[:target_xml_path])
351
- end.to output(
352
- a_string_including("Differences between", "Similarity score:"),
353
- ).to_stdout
354
- end
340
+ # The comparison parses strictly: a malformed document is an
341
+ # error on every engine, never a best-effort tree to diff.
342
+ expect do
343
+ cli.compare(malformed_xml, files[:target_xml_path])
344
+ end.to raise_error(StandardError, /Error parsing file .*malformed\.xml/)
355
345
  end
356
346
 
357
347
  it "raises error when parsing malformed YAML" do
@@ -84,6 +84,10 @@ RSpec.describe "LML review round-4 probe fixes (encoding, float enums, bundle in
84
84
  end
85
85
 
86
86
  describe "Bundle construction" do
87
+ # The sets are large enough that a build takes tens of milliseconds: at
88
+ # 1000/4000 models the builds took 5-25 ms, and a shared runner could
89
+ # report a 14x ratio for the same linear code (lutaml-model#1328). The
90
+ # best of three drops the stragglers.
87
91
  it "scales linearly with the number of models" do
88
92
  build = lambda do |count|
89
93
  models = (0...count).to_h { |i| ["R5M#{i}@default", minimal_model("R5M#{i}")] }
@@ -91,9 +95,9 @@ RSpec.describe "LML review round-4 probe fixes (encoding, float enums, bundle in
91
95
  lml::Bundle.new(roots: [], models: models)
92
96
  Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
93
97
  end
94
- build.call(500)
95
- small = [build.call(1000), build.call(1000)].min
96
- large = [build.call(4000), build.call(4000)].min
98
+ build.call(2_000)
99
+ small = Array.new(3) { build.call(10_000) }.min
100
+ large = Array.new(3) { build.call(40_000) }.min
97
101
 
98
102
  expect(large / small).to be < 12
99
103
  end
@@ -132,8 +132,9 @@ RSpec.describe RenderNil do
132
132
  end
133
133
 
134
134
  it "serializes to XML with render_nil option" do
135
+ # xsi:nil spells the xsi prefix: the root declares it.
135
136
  expected_xml = <<~XML
136
- <render_nil>
137
+ <render_nil xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
137
138
  <name xsi:nil="true"/>
138
139
  <glaze xsi:nil="true"/>
139
140
  <render_nil_nested/>
@@ -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
@@ -185,8 +185,9 @@ RSpec.describe "ValueMap" do
185
185
  end
186
186
 
187
187
  let(:expected_xml) do
188
+ # xsi:nil spells the xsi prefix: the root declares it.
188
189
  <<~XML.strip
189
- <WithValueMaps>
190
+ <WithValueMaps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
190
191
  <omittedAsNil xsi:nil="true"/>
191
192
  <omittedAsEmpty/>
192
193
  <nilAsNil xsi:nil="true"/>
@@ -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