lutaml-model 0.8.207 → 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 +4 -4
- data/bench/baselines/main_mml.json +20 -20
- data/bench/baselines/main_niso.json +15 -15
- data/bench/baselines/main_sts.json +20 -20
- data/bench/baselines/main_unitsml.json +5 -5
- data/bench/baselines/main_xmi.json +20 -20
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/leptris/native_emission.rb +21 -0
- data/lib/lutaml/xml/leptris/plan_hydrator.rb +64 -16
- data/lib/lutaml/xml/leptris/plan_serializer.rb +228 -41
- data/lib/lutaml/xml/model_transform.rb +89 -28
- data/spec/lutaml/model/unmatched_rule_replay_spec.rb +127 -0
- data/spec/lutaml/model/writer_assigned_attribute_spec.rb +61 -0
- data/spec/lutaml/xml/leptris/nested_namespace_declaration_spec.rb +234 -0
- data/spec/lutaml/xml/leptris/plan_hydrator_bridge_scope_spec.rb +151 -0
- metadata +4 -1
|
@@ -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,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
|
|
@@ -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.
|
|
4
|
+
version: 0.8.209
|
|
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,8 @@ 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/nested_namespace_declaration_spec.rb
|
|
2386
|
+
- spec/lutaml/xml/leptris/plan_hydrator_bridge_scope_spec.rb
|
|
2384
2387
|
- spec/lutaml/xml/leptris/plan_hydrator_ns_snapshot_spec.rb
|
|
2385
2388
|
- spec/lutaml/xml/leptris_string_encoding_spec.rb
|
|
2386
2389
|
- spec/lutaml/xml/line_ending_spec.rb
|