lutaml-model 0.8.204 → 0.8.205
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/docs/_guides/native-engines.adoc +10 -0
- data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
- data/lib/lutaml/model/serialize/initialization.rb +2 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/leptris/native_emission.rb +81 -3
- data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
- data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
- data/lib/lutaml/xml/leptris/plan_serializer.rb +526 -100
- data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
- data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
- data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
- data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
- data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
- metadata +3 -2
|
@@ -20,6 +20,7 @@ module Lutaml
|
|
|
20
20
|
document = ::Leptris::XML.parse(xml)
|
|
21
21
|
root = document.root
|
|
22
22
|
return nil unless root && root.name == plan[:tree][:name]
|
|
23
|
+
return nil if PlanHydrator.xsi_nil_below_root?(root)
|
|
23
24
|
|
|
24
25
|
PlanHydrator.call(model_class, plan,
|
|
25
26
|
plan[:descriptor].walk(root),
|
|
@@ -30,7 +30,11 @@ module Lutaml
|
|
|
30
30
|
yield(@builder) if block
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
# Generate the schema XML string
|
|
33
|
+
# Generate the schema XML string in the Nokogiri wrapper's byte
|
|
34
|
+
# shape: leptris indents only when indent_text rides along with
|
|
35
|
+
# indent, and the moxml backends expand empty elements (<x></x>)
|
|
36
|
+
# unless told otherwise. Generated schemas are artifacts consumers
|
|
37
|
+
# diff and commit, so the spelling must not follow the engine.
|
|
34
38
|
# @param options [Hash] formatting options
|
|
35
39
|
# @option options [Boolean] :pretty Pretty print with indentation
|
|
36
40
|
# @option options [Integer] :indent Number of spaces (default: 2)
|
|
@@ -39,9 +43,9 @@ module Lutaml
|
|
|
39
43
|
doc = @builder.document
|
|
40
44
|
if options[:pretty]
|
|
41
45
|
indent = options[:indent] || 2
|
|
42
|
-
doc.to_xml(indent: indent)
|
|
46
|
+
doc.to_xml(indent: indent, indent_text: " ", expand_empty: false)
|
|
43
47
|
else
|
|
44
|
-
doc.to_xml
|
|
48
|
+
doc.to_xml(expand_empty: false)
|
|
45
49
|
end
|
|
46
50
|
end
|
|
47
51
|
end
|
|
@@ -173,6 +173,10 @@ module Lutaml
|
|
|
173
173
|
collection_element_count(value, rule)
|
|
174
174
|
end
|
|
175
175
|
|
|
176
|
+
# Public: the plan lanes' coverage pre-check measures a parsed
|
|
177
|
+
# order against the same count before deciding to reconcile.
|
|
178
|
+
public :expected_element_count
|
|
179
|
+
|
|
176
180
|
def collection_element_count(value, rule)
|
|
177
181
|
# Mirror the applier's own dispatch. A value it will not iterate
|
|
178
182
|
# — nil, or a String — never reaches process_collection_item; it
|
|
@@ -188,13 +188,12 @@ model_class, register_id)
|
|
|
188
188
|
end
|
|
189
189
|
end
|
|
190
190
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
#
|
|
194
|
-
#
|
|
195
|
-
#
|
|
196
|
-
#
|
|
197
|
-
# emitted, and must be skipped rather than crash serialization.
|
|
191
|
+
# Whether an element_order entry is something the ordered path
|
|
192
|
+
# can interpret. element_order is a plain public accessor, so
|
|
193
|
+
# arrays holding foreign objects (Integers, strings, ...) exist
|
|
194
|
+
# in the wild. Anything without #type cannot be matched to a
|
|
195
|
+
# rule or emitted, and must be skipped rather than crash
|
|
196
|
+
# serialization. Shared with the plan lanes' order walks.
|
|
198
197
|
#
|
|
199
198
|
# @param object [Object] An entry from element_order
|
|
200
199
|
# @return [Boolean] true when the entry is a usable order entry
|
|
@@ -202,6 +201,8 @@ model_class, register_id)
|
|
|
202
201
|
object.respond_to?(:type)
|
|
203
202
|
end
|
|
204
203
|
|
|
204
|
+
private
|
|
205
|
+
|
|
205
206
|
# Match by name AND namespace when object has a namespace URI.
|
|
206
207
|
# Falls back to name-only match for backward compatibility.
|
|
207
208
|
# Also checks uri_aliases for namespace alias support (e.g., ReqIF versions).
|
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "lutaml/store"
|
|
5
|
+
|
|
6
|
+
# Production-lane parity for shapes the XML plan lanes got wrong while a
|
|
7
|
+
# leaked adapter pin kept most of the suite off leptris: every example
|
|
8
|
+
# pins the leptris lane itself and asserts the interpretive writer's
|
|
9
|
+
# output, so the coverage does not depend on suite-wide lane integrity.
|
|
10
|
+
module ProductionLaneParitySpec
|
|
11
|
+
class TextElement < Lutaml::Model::Serializable
|
|
12
|
+
attribute :sup, :string
|
|
13
|
+
attribute :sub, :string
|
|
14
|
+
attribute :text, :string, collection: true
|
|
15
|
+
|
|
16
|
+
xml do
|
|
17
|
+
element "text-element"
|
|
18
|
+
|
|
19
|
+
map_content to: :text
|
|
20
|
+
|
|
21
|
+
map_element "sup", to: :sup
|
|
22
|
+
map_element "sub", to: :sub
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class DocidModel
|
|
27
|
+
attr_accessor :id, :prefix
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class Docid < Lutaml::Model::Serializable
|
|
31
|
+
model DocidModel
|
|
32
|
+
attribute :id, TextElement
|
|
33
|
+
|
|
34
|
+
xml do
|
|
35
|
+
element "docid"
|
|
36
|
+
mixed_content
|
|
37
|
+
|
|
38
|
+
map_content to: :text, delegate: :id
|
|
39
|
+
map_element :sub, to: :sub, delegate: :id
|
|
40
|
+
map_element :sup, to: :sup, delegate: :id
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# elementFormDefault not set: local elements are in no namespace.
|
|
45
|
+
UnsetFormNs = Class.new(Lutaml::Xml::W3c::XmlNamespace) do
|
|
46
|
+
uri "http://example.com/production-lane/unset"
|
|
47
|
+
prefix_default "ex"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class UnsetForm < Lutaml::Model::Serializable
|
|
51
|
+
attribute :a, :string
|
|
52
|
+
attribute :b, :string
|
|
53
|
+
|
|
54
|
+
xml do
|
|
55
|
+
element "unset"
|
|
56
|
+
namespace UnsetFormNs
|
|
57
|
+
map_element "a", to: :a
|
|
58
|
+
map_element "b", to: :b
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# elementFormDefault explicitly :unqualified: the root keeps the
|
|
63
|
+
# default-namespace format and locals spell bare.
|
|
64
|
+
UnqualifiedFormNs = Class.new(Lutaml::Xml::W3c::XmlNamespace) do
|
|
65
|
+
uri "http://example.com/production-lane/unqualified"
|
|
66
|
+
prefix_default "ex"
|
|
67
|
+
element_form_default :unqualified
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
class UnqualifiedForm < Lutaml::Model::Serializable
|
|
71
|
+
attribute :a, :string
|
|
72
|
+
|
|
73
|
+
xml do
|
|
74
|
+
element "unqualified"
|
|
75
|
+
namespace UnqualifiedFormNs
|
|
76
|
+
map_element "a", to: :a
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
QualifiedNs = Class.new(Lutaml::Xml::W3c::XmlNamespace) do
|
|
81
|
+
uri "http://example.com/production-lane/qualified"
|
|
82
|
+
prefix_default "ex"
|
|
83
|
+
element_form_default :qualified
|
|
84
|
+
attribute_form_default :qualified
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
class FormOverride < Lutaml::Model::Serializable
|
|
88
|
+
attribute :content, :string
|
|
89
|
+
|
|
90
|
+
xml do
|
|
91
|
+
element "override"
|
|
92
|
+
namespace QualifiedNs
|
|
93
|
+
map_element "content", to: :content, form: :unqualified
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
class BlankRule < Lutaml::Model::Serializable
|
|
98
|
+
attribute :note, :string
|
|
99
|
+
|
|
100
|
+
xml do
|
|
101
|
+
element "blank-rule"
|
|
102
|
+
namespace UnsetFormNs
|
|
103
|
+
map_element "note", to: :note, namespace: nil
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
class Declared < Lutaml::Model::Serializable
|
|
108
|
+
attribute :name, :string
|
|
109
|
+
|
|
110
|
+
xml do
|
|
111
|
+
element "declared"
|
|
112
|
+
map_element "name", to: :name
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
class Actual < Lutaml::Model::Serializable
|
|
117
|
+
attribute :different_name, :string
|
|
118
|
+
|
|
119
|
+
xml do
|
|
120
|
+
element "actual"
|
|
121
|
+
map_element "different-name", to: :different_name
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
class Holder < Lutaml::Model::Serializable
|
|
126
|
+
attribute :child, Declared
|
|
127
|
+
|
|
128
|
+
xml do
|
|
129
|
+
element "holder"
|
|
130
|
+
map_element "child", to: :child
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
class Vase < Lutaml::Model::Serializable
|
|
135
|
+
attribute :material, :string
|
|
136
|
+
attribute :height, :integer
|
|
137
|
+
|
|
138
|
+
xml do
|
|
139
|
+
element "Vase"
|
|
140
|
+
map_element "material", to: :material
|
|
141
|
+
map_element "height", to: :height
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
class Cached < Lutaml::Model::Serializable
|
|
146
|
+
attribute :name, :string
|
|
147
|
+
|
|
148
|
+
xml do
|
|
149
|
+
element "cached"
|
|
150
|
+
map_element "name", to: :name
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
cache_conversions
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
class ExplicitNil < Lutaml::Model::Serializable
|
|
157
|
+
attribute :items, :string, collection: true
|
|
158
|
+
|
|
159
|
+
xml do
|
|
160
|
+
element "explicit-nil"
|
|
161
|
+
map_element "items", to: :items, render_nil: :as_nil
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
class ExplicitBlank < Lutaml::Model::Serializable
|
|
166
|
+
attribute :items, :string, collection: true
|
|
167
|
+
|
|
168
|
+
xml do
|
|
169
|
+
element "explicit-blank"
|
|
170
|
+
map_element "items", to: :items, render_empty: :as_blank
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
class NilScalar < Lutaml::Model::Serializable
|
|
175
|
+
attribute :name, :string, default: "Unnamed"
|
|
176
|
+
|
|
177
|
+
xml do
|
|
178
|
+
element "nil-scalar"
|
|
179
|
+
map_element "name", to: :name, render_nil: true
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
class Pushed < Lutaml::Model::Serializable
|
|
184
|
+
attribute :lead, :string
|
|
185
|
+
attribute :items, :string, collection: true, default: -> { [] }
|
|
186
|
+
|
|
187
|
+
xml do
|
|
188
|
+
element "pushed"
|
|
189
|
+
ordered
|
|
190
|
+
map_element "lead", to: :lead
|
|
191
|
+
map_element "item", to: :items
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
class Nilled < Lutaml::Model::Serializable
|
|
196
|
+
attribute :lead, :string
|
|
197
|
+
attribute :items, :string, collection: true
|
|
198
|
+
|
|
199
|
+
xml do
|
|
200
|
+
element "nilled"
|
|
201
|
+
ordered
|
|
202
|
+
map_element "lead", to: :lead
|
|
203
|
+
map_element "item", to: :items, render_nil: :as_nil
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
class OrderedBlank < Lutaml::Model::Serializable
|
|
208
|
+
attribute :authority, :string
|
|
209
|
+
attribute :topic, :string, collection: true
|
|
210
|
+
|
|
211
|
+
xml do
|
|
212
|
+
element "subject"
|
|
213
|
+
ordered
|
|
214
|
+
map_attribute "authority", to: :authority
|
|
215
|
+
map_element "topic", to: :topic, render_empty: :as_nil
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
class ReservedPrefix < Lutaml::Model::Serializable
|
|
220
|
+
attribute :lang, :string
|
|
221
|
+
|
|
222
|
+
xml do
|
|
223
|
+
element "reserved"
|
|
224
|
+
map_attribute "xml:lang", to: :lang
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
class ForeignOrder < Lutaml::Model::Serializable
|
|
229
|
+
attribute :lead, :string
|
|
230
|
+
|
|
231
|
+
xml do
|
|
232
|
+
element "foreign"
|
|
233
|
+
ordered
|
|
234
|
+
map_element "lead", to: :lead
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
class Hashed < Lutaml::Model::Serializable
|
|
239
|
+
attribute :h, :hash
|
|
240
|
+
|
|
241
|
+
xml do
|
|
242
|
+
element "hashed"
|
|
243
|
+
map_element "h", to: :h
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
class OverCount < Lutaml::Model::Serializable
|
|
248
|
+
attribute :name, :string
|
|
249
|
+
|
|
250
|
+
xml do
|
|
251
|
+
element "over-count"
|
|
252
|
+
map_element "name", to: :name
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
class HookedType < Lutaml::Model::Type::Value
|
|
257
|
+
def self.from_xml(_value)
|
|
258
|
+
"from_xml_hooked"
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def to_xml
|
|
262
|
+
"to_xml_hooked"
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
class Hooked < Lutaml::Model::Serializable
|
|
267
|
+
attribute :value, HookedType
|
|
268
|
+
attribute :attr, HookedType
|
|
269
|
+
|
|
270
|
+
xml do
|
|
271
|
+
element "hooked"
|
|
272
|
+
map_element "value", to: :value
|
|
273
|
+
map_attribute "attr", to: :attr
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
class RefBase < Lutaml::Model::Serializable
|
|
278
|
+
attribute :name, :string
|
|
279
|
+
|
|
280
|
+
xml do
|
|
281
|
+
map_element "name", to: :name
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
class DocRef < RefBase
|
|
286
|
+
attribute :_class, :string
|
|
287
|
+
attribute :document_id, :string
|
|
288
|
+
|
|
289
|
+
xml do
|
|
290
|
+
map_element "document_id", to: :document_id
|
|
291
|
+
map_attribute "_class", to: :_class
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
class RefSet < Lutaml::Model::Serializable
|
|
296
|
+
attribute :references, RefBase, collection: true, polymorphic: [DocRef]
|
|
297
|
+
|
|
298
|
+
xml do
|
|
299
|
+
element "refset"
|
|
300
|
+
map_element "references", to: :references, polymorphic: {
|
|
301
|
+
attribute: "_class",
|
|
302
|
+
class_map: { "document-ref" => "ProductionLaneParitySpec::DocRef" },
|
|
303
|
+
}
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
class Animal < Lutaml::Model::Serializable
|
|
308
|
+
attribute :type, :string
|
|
309
|
+
attribute :name, :string
|
|
310
|
+
|
|
311
|
+
xml do
|
|
312
|
+
element "animal"
|
|
313
|
+
map_attribute "type", to: :type
|
|
314
|
+
map_element "name", to: :name
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
class Dog < Animal
|
|
319
|
+
attribute :breed, :string
|
|
320
|
+
|
|
321
|
+
xml do
|
|
322
|
+
map_element "breed", to: :breed
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
class Zoo < Lutaml::Model::Collection
|
|
327
|
+
instances :animals, Animal, polymorphic: true
|
|
328
|
+
|
|
329
|
+
xml do
|
|
330
|
+
element "zoo"
|
|
331
|
+
map_element "animal", to: :animals, polymorphic: {
|
|
332
|
+
attribute: "type",
|
|
333
|
+
class_map: { "dog" => "ProductionLaneParitySpec::Dog" },
|
|
334
|
+
}
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
class Omitted < Lutaml::Model::Serializable
|
|
339
|
+
attribute :as_nil, :string
|
|
340
|
+
attribute :as_empty, :string
|
|
341
|
+
|
|
342
|
+
xml do
|
|
343
|
+
element "omitted"
|
|
344
|
+
map_element "as_nil", to: :as_nil, value_map: { to: { omitted: :nil } }
|
|
345
|
+
map_element "as_empty", to: :as_empty,
|
|
346
|
+
value_map: { from: { omitted: :empty } }
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
class Imported < Lutaml::Model::Serializable
|
|
351
|
+
attribute :shout, :string
|
|
352
|
+
|
|
353
|
+
xml do
|
|
354
|
+
element "imported"
|
|
355
|
+
map_element "shout", to: :shout,
|
|
356
|
+
transform: { import: ->(v) { v.to_s.upcase } }
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
class LateBound < Lutaml::Model::Serializable
|
|
361
|
+
attribute :name, :string
|
|
362
|
+
|
|
363
|
+
xml do
|
|
364
|
+
element "late"
|
|
365
|
+
map_element "name", to: :name
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
class TypeOnly < Lutaml::Model::Serializable
|
|
370
|
+
attribute :type, :string
|
|
371
|
+
|
|
372
|
+
xml do
|
|
373
|
+
map_element "type", to: :type
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
RSpec.describe "production-lane parity" do
|
|
379
|
+
around do |example|
|
|
380
|
+
previous = Lutaml::Model::Config.xml_adapter_type
|
|
381
|
+
fixed = previous == Lutaml::Model::FIXED_ADAPTER_TYPE
|
|
382
|
+
previous_adapter = Lutaml::Model::Config.xml_adapter if fixed
|
|
383
|
+
Lutaml::Model::Config.xml_adapter_type = :leptris
|
|
384
|
+
example.run
|
|
385
|
+
ensure
|
|
386
|
+
if fixed
|
|
387
|
+
Lutaml::Model::Config.xml_adapter = previous_adapter
|
|
388
|
+
elsif previous
|
|
389
|
+
Lutaml::Model::Config.xml_adapter_type = previous
|
|
390
|
+
else
|
|
391
|
+
Lutaml::Model::Config.unset_xml_adapter_type
|
|
392
|
+
end
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
it "round-trips rules delegated to a mapped child" do
|
|
396
|
+
xml = "<docid>Str<sub>2</sub>text<sup>1</sup>123</docid>"
|
|
397
|
+
docid = ProductionLaneParitySpec::Docid.from_xml(xml)
|
|
398
|
+
|
|
399
|
+
expect(ProductionLaneParitySpec::Docid.to_xml(docid)).to be_xml_equivalent_to(xml)
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
describe "elementFormDefault" do
|
|
403
|
+
it "opts form-not-set locals out of the default namespace" do
|
|
404
|
+
model = ProductionLaneParitySpec::UnsetForm.new(a: "1", b: "2")
|
|
405
|
+
|
|
406
|
+
expect(model.to_xml).to include('<unset xmlns="http://example.com/production-lane/unset">')
|
|
407
|
+
expect(model.to_xml).to include('<a xmlns="">1</a>', '<b xmlns="">2</b>')
|
|
408
|
+
expect(model.to_xml(pretty: true)).to include('<a xmlns="">1</a>', "<b>2</b>")
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
it "keeps the default-namespace root and bare locals under an explicit :unqualified form" do
|
|
412
|
+
model = ProductionLaneParitySpec::UnqualifiedForm.new(a: "1")
|
|
413
|
+
xml = model.to_xml
|
|
414
|
+
|
|
415
|
+
expect(xml).to include('<unqualified xmlns="http://example.com/production-lane/unqualified">')
|
|
416
|
+
expect(xml).to include("<a>1</a>")
|
|
417
|
+
expect(xml).not_to include("xmlns=\"\"", "<ex:a")
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
it "spells a form: :unqualified rule bare under a qualified namespace" do
|
|
421
|
+
model = ProductionLaneParitySpec::FormOverride.new(content: "x")
|
|
422
|
+
|
|
423
|
+
expect(model.to_xml).to include("<content>x</content>")
|
|
424
|
+
expect(model.to_xml).not_to include("<ex:content")
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
it "opts a namespace: nil rule out of the default namespace" do
|
|
428
|
+
model = ProductionLaneParitySpec::BlankRule.new(note: "n")
|
|
429
|
+
|
|
430
|
+
expect(model.to_xml).to include('<note xmlns="">n</note>')
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
it "serializes a nested value of another class through its own mapping" do
|
|
435
|
+
model = ProductionLaneParitySpec::Holder.new
|
|
436
|
+
model.child = ProductionLaneParitySpec::Actual.new(different_name: "x")
|
|
437
|
+
|
|
438
|
+
expect(model.to_xml).to include("<different-name>x</different-name>")
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
it "never declares the reserved xml prefix" do
|
|
442
|
+
model = ProductionLaneParitySpec::ReservedPrefix.new(lang: "en")
|
|
443
|
+
|
|
444
|
+
expect(model.to_xml).to include('xml:lang="en"')
|
|
445
|
+
expect(model.to_xml).not_to include("xmlns:xml=")
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
it "generates schemas in the Nokogiri wrapper's byte shape" do
|
|
449
|
+
nokogiri = Lutaml::Xml::Schema::XsdSchema.generate(ProductionLaneParitySpec::Vase,
|
|
450
|
+
pretty: true, adapter: :nokogiri)
|
|
451
|
+
leptris = Lutaml::Xml::Schema::XsdSchema.generate(ProductionLaneParitySpec::Vase,
|
|
452
|
+
pretty: true, adapter: :leptris)
|
|
453
|
+
|
|
454
|
+
expect(leptris).to eq(nokogiri)
|
|
455
|
+
end
|
|
456
|
+
|
|
457
|
+
describe "render policies" do
|
|
458
|
+
it "renders an explicit nil element" do
|
|
459
|
+
model = ProductionLaneParitySpec::ExplicitNil.new(items: nil)
|
|
460
|
+
|
|
461
|
+
expect(model.to_xml).to include('<items xsi:nil="true"/>')
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
it "renders a blank element for an empty collection" do
|
|
465
|
+
model = ProductionLaneParitySpec::ExplicitBlank.new(items: [])
|
|
466
|
+
|
|
467
|
+
expect(model.to_xml).to include("<items/>")
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
it "binds an xsi:nil element as an empty collection" do
|
|
471
|
+
parsed = ProductionLaneParitySpec::ExplicitNil
|
|
472
|
+
.from_xml('<explicit-nil><items xsi:nil="true"/></explicit-nil>')
|
|
473
|
+
|
|
474
|
+
expect(parsed.items).to eq([])
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
it "binds an xsi:nil element as a nil scalar" do
|
|
478
|
+
parsed = ProductionLaneParitySpec::NilScalar
|
|
479
|
+
.from_xml('<nil-scalar><name xsi:nil="true"/></nil-scalar>')
|
|
480
|
+
|
|
481
|
+
expect(parsed.name).to be_nil
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
it "renders the nil element of a parsed ordered model" do
|
|
485
|
+
xml = '<subject authority="lcsh"><topic xsi:nil="true"/></subject>'
|
|
486
|
+
parsed = ProductionLaneParitySpec::OrderedBlank.from_xml(xml)
|
|
487
|
+
|
|
488
|
+
expect(parsed.to_xml).to include('<topic xsi:nil="true"/>')
|
|
489
|
+
end
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
describe "parsed-model mutation" do
|
|
493
|
+
it "emits an item pushed onto a default collection after parse" do
|
|
494
|
+
model = ProductionLaneParitySpec::Pushed.from_xml("<pushed><lead>L</lead></pushed>")
|
|
495
|
+
model.items << "y"
|
|
496
|
+
|
|
497
|
+
expect(model.to_xml).to include("<item>y</item>")
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
it "renders a collection set to nil after parse when the element was present" do
|
|
501
|
+
model = ProductionLaneParitySpec::Nilled.from_xml("<nilled><lead>L</lead><item>x</item></nilled>")
|
|
502
|
+
model.items = nil
|
|
503
|
+
|
|
504
|
+
expect(model.to_xml).to include('xsi:nil="true"')
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
it "emits nothing when the element was absent" do
|
|
508
|
+
model = ProductionLaneParitySpec::Nilled.from_xml("<nilled><lead>L</lead></nilled>")
|
|
509
|
+
model.items = nil
|
|
510
|
+
|
|
511
|
+
expect(model.to_xml).not_to include("<item")
|
|
512
|
+
end
|
|
513
|
+
|
|
514
|
+
it "skips foreign entries in element_order" do
|
|
515
|
+
model = ProductionLaneParitySpec::ForeignOrder.from_xml("<foreign><lead>L</lead></foreign>")
|
|
516
|
+
model.element_order = model.element_order.dup << 42
|
|
517
|
+
|
|
518
|
+
expect { model.to_xml }.not_to raise_error
|
|
519
|
+
expect(model.to_xml).to include("<lead>L</lead>")
|
|
520
|
+
end
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
it "expands a Hash-typed element into child elements and reads it back" do
|
|
524
|
+
xml = "<hashed><h><key>value</key></h></hashed>"
|
|
525
|
+
expect(ProductionLaneParitySpec::Hashed.new(h: { key: "value" }).to_xml)
|
|
526
|
+
.to be_xml_equivalent_to(xml)
|
|
527
|
+
expect(ProductionLaneParitySpec::Hashed.from_xml(xml).h).to eq("key" => "value")
|
|
528
|
+
end
|
|
529
|
+
|
|
530
|
+
it "keeps an over-count for a singular attribute so validate reports it" do
|
|
531
|
+
obj = ProductionLaneParitySpec::OverCount.from_xml(
|
|
532
|
+
"<over-count><name>A</name><name>B</name></over-count>",
|
|
533
|
+
)
|
|
534
|
+
|
|
535
|
+
expect(obj.name).to eq(%w[A B])
|
|
536
|
+
expect(obj.validate).to include(an_instance_of(Lutaml::Model::CollectionTrueMissingError))
|
|
537
|
+
end
|
|
538
|
+
|
|
539
|
+
it "spells a Value type's own XML hooks" do
|
|
540
|
+
model = ProductionLaneParitySpec::Hooked.new(value: "x", attr: "y")
|
|
541
|
+
|
|
542
|
+
expect(model.to_xml).to include("<value>to_xml_hooked</value>", 'attr="to_xml_hooked"')
|
|
543
|
+
parsed = ProductionLaneParitySpec::Hooked.from_xml("<hooked attr=\"v\"><value>v</value></hooked>")
|
|
544
|
+
expect(parsed.value).to eq("from_xml_hooked")
|
|
545
|
+
expect(parsed.attr).to eq("from_xml_hooked")
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
it "resolves a polymorphic class map on deferred rows" do
|
|
549
|
+
xml = '<refset><references _class="document-ref"><name>n</name>' \
|
|
550
|
+
"<document_id>d</document_id></references></refset>"
|
|
551
|
+
parsed = ProductionLaneParitySpec::RefSet.from_xml(xml)
|
|
552
|
+
|
|
553
|
+
expect(parsed.references.first).to be_a(ProductionLaneParitySpec::DocRef)
|
|
554
|
+
expect(ProductionLaneParitySpec::RefSet.to_xml(parsed)).to be_xml_equivalent_to(xml)
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
it "resolves a polymorphic Collection's instances by their class map" do
|
|
558
|
+
zoo = ProductionLaneParitySpec::Zoo.from_xml(
|
|
559
|
+
'<zoo><animal type="dog"><name>Fido</name><breed>Lab</breed></animal></zoo>',
|
|
560
|
+
)
|
|
561
|
+
|
|
562
|
+
expect(zoo.animals.first).to be_a(ProductionLaneParitySpec::Dog)
|
|
563
|
+
expect(zoo.animals.first.breed).to eq("Lab")
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
it "renders an uninitialized value under its value_map and binds an omitted one" do
|
|
567
|
+
# An uninitialized value is decided by its value_map's :omitted key:
|
|
568
|
+
# :nil renders the xsi:nil element.
|
|
569
|
+
model = ProductionLaneParitySpec::Omitted.new({}, { omitted: :omitted })
|
|
570
|
+
|
|
571
|
+
expect(model.to_xml).to include('as_nil xsi:nil="true"')
|
|
572
|
+
expect(ProductionLaneParitySpec::Omitted.from_xml("<omitted/>").as_empty).to eq("")
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
it "applies a hash/proc import transform" do
|
|
576
|
+
parsed = ProductionLaneParitySpec::Imported.from_xml("<imported><shout>soft</shout></imported>")
|
|
577
|
+
|
|
578
|
+
expect(parsed.shout).to eq("SOFT")
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
it "follows an attribute and mapping added after the first parse" do
|
|
582
|
+
klass = ProductionLaneParitySpec::LateBound
|
|
583
|
+
klass.from_xml("<late><name>initial</name></late>")
|
|
584
|
+
klass.class_eval do
|
|
585
|
+
attribute :extra, :string
|
|
586
|
+
|
|
587
|
+
xml do
|
|
588
|
+
map_element "extra", to: :extra
|
|
589
|
+
end
|
|
590
|
+
end
|
|
591
|
+
parsed = klass.from_xml("<late><name>hello</name><extra>world</extra></late>")
|
|
592
|
+
|
|
593
|
+
expect(parsed.name).to eq("hello")
|
|
594
|
+
expect(parsed.extra).to eq("world")
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
it "leaves the encoding option and a type-only model's error to the standard pipeline" do
|
|
598
|
+
expect { ProductionLaneParitySpec::ForeignOrder.new(lead: "L").to_xml(encoding: "ABC") }
|
|
599
|
+
.to raise_error(StandardError, /ABC/)
|
|
600
|
+
expect { ProductionLaneParitySpec::TypeOnly.from_xml("<type>Data</type>") }
|
|
601
|
+
.to raise_error(Lutaml::Model::TypeOnlyMappingError)
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
describe "conversion caching" do
|
|
605
|
+
let(:store) { Lutaml::Store::BasicStore.new(adapter: { type: :memory }) }
|
|
606
|
+
|
|
607
|
+
around do |example|
|
|
608
|
+
previous = Lutaml::Model::Config.conversion_cache
|
|
609
|
+
Lutaml::Model::Config.conversion_cache = store
|
|
610
|
+
example.run
|
|
611
|
+
ensure
|
|
612
|
+
Lutaml::Model::Config.conversion_cache = previous
|
|
613
|
+
end
|
|
614
|
+
|
|
615
|
+
it "serves repeated XML serialization from the store" do
|
|
616
|
+
instance = ProductionLaneParitySpec::Cached.new(name: "a")
|
|
617
|
+
instance.to_xml
|
|
618
|
+
expect(store.size).to eq(1)
|
|
619
|
+
store.each_key { |key| store.set(key, "<sentinel/>") }
|
|
620
|
+
|
|
621
|
+
expect(instance.to_xml).to eq("<sentinel/>")
|
|
622
|
+
end
|
|
623
|
+
|
|
624
|
+
it "serves repeated XML parsing from the store" do
|
|
625
|
+
xml = "<cached><name>a</name></cached>"
|
|
626
|
+
first = ProductionLaneParitySpec::Cached.from_xml(xml)
|
|
627
|
+
expect(store.size).to eq(1)
|
|
628
|
+
|
|
629
|
+
expect(ProductionLaneParitySpec::Cached.from_xml(xml)).to equal(first)
|
|
630
|
+
end
|
|
631
|
+
end
|
|
632
|
+
end
|