lutaml-model 0.8.157 → 0.8.159
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/.github/workflows/opal.yml +5 -0
- data/docs/_guides/opal.adoc +11 -0
- data/docs/_pages/serialization_adapters.adoc +18 -2
- data/lib/lutaml/key_value/transformation/value_serializer.rb +21 -0
- data/lib/lutaml/key_value/transformation.rb +57 -6
- data/lib/lutaml/model/adapter_resolver.rb +29 -0
- data/lib/lutaml/model/cached_type_resolver/concurrent_map_cache.rb +3 -1
- data/lib/lutaml/model/cached_type_resolver/mutex_hash_cache.rb +3 -1
- data/lib/lutaml/model/cached_type_resolver.rb +13 -22
- data/lib/lutaml/model/collection.rb +63 -6
- data/lib/lutaml/model/collection_handler.rb +9 -13
- data/lib/lutaml/model/mapping/mapping_rule.rb +28 -4
- data/lib/lutaml/model/serialize/builder.rb +1 -1
- data/lib/lutaml/model/serialize/enum_handling.rb +7 -3
- data/lib/lutaml/model/serialize/format_conversion.rb +26 -1
- data/lib/lutaml/model/serialize/value_mapping.rb +6 -0
- data/lib/lutaml/model/serialize.rb +6 -1
- data/lib/lutaml/model/services/rule_value_extractor.rb +39 -35
- data/lib/lutaml/model/utils.rb +16 -4
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/adapter/oga_adapter.rb +4 -0
- data/lib/lutaml/xml/adapter/rexml_adapter.rb +4 -0
- data/lib/lutaml/xml/adapter_element.rb +16 -21
- data/lib/lutaml/xml/declaration_planner.rb +63 -3
- data/lib/lutaml/xml/schema/xsd/schema_path.rb +8 -3
- data/spec/lutaml/model/collection_scalar_maps_spec.rb +58 -0
- data/spec/lutaml/model/key_value_accepted_values_spec.rb +99 -0
- data/spec/lutaml/xml/schema/xsd/glob_spec.rb +14 -18
- data/spec/lutaml/xml/schema/xsd/unresolvable_schema_location_spec.rb +58 -0
- metadata +5 -2
|
@@ -43,6 +43,11 @@ module Lutaml
|
|
|
43
43
|
# getters default to it instead of a `*args` splat.
|
|
44
44
|
NO_ARG = Object.new.freeze
|
|
45
45
|
|
|
46
|
+
# Shared frozen empty hash for constructor default arguments. The init
|
|
47
|
+
# walk only reads attrs/options, so one frozen instance replaces the
|
|
48
|
+
# per-instantiation `{}` defaults (68k+ allocations per corpus parse).
|
|
49
|
+
EMPTY_HASH = {}.freeze
|
|
50
|
+
|
|
46
51
|
INTERNAL_ATTRIBUTES = %i[@using_default @lutaml_register @lutaml_parent @lutaml_root
|
|
47
52
|
@register_records].freeze
|
|
48
53
|
|
|
@@ -162,7 +167,7 @@ module Lutaml
|
|
|
162
167
|
# re-materialize defaults that the post-run reset discards.
|
|
163
168
|
attr_accessor :lutaml_constructor_seeded
|
|
164
169
|
|
|
165
|
-
def initialize(attrs =
|
|
170
|
+
def initialize(attrs = EMPTY_HASH, options = EMPTY_HASH)
|
|
166
171
|
if lutaml_constructor_seeded
|
|
167
172
|
# The deserialization constructor seeded constructor-equivalent
|
|
168
173
|
# compiled state (defaults resolved, non-defaulted nil) and
|
|
@@ -13,39 +13,28 @@ module Lutaml
|
|
|
13
13
|
Concurrent::Map.new
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
# The extractor is pure stateless logic over its arguments; the
|
|
17
|
+
# per-call `new(*args).call` instance (Services::Base's default)
|
|
18
|
+
# allocated an object plus seven ivar writes per rule per element
|
|
19
|
+
# (~24k objects per 60 glossarist documents). The class-level call
|
|
20
|
+
# runs the same walk without the throwaway instance. The instance
|
|
21
|
+
# API is kept for compatibility.
|
|
22
|
+
def self.call(rule, doc, format, attr, register, _options,
|
|
17
23
|
instance_object)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
@rule = rule
|
|
21
|
-
@doc = doc
|
|
22
|
-
@format = format
|
|
23
|
-
@attr = attr
|
|
24
|
-
@register = register
|
|
25
|
-
@options = options
|
|
26
|
-
@instance_object = instance_object
|
|
27
|
-
end
|
|
24
|
+
rule_names = RULE_NAMES_CACHE[rule] ||=
|
|
25
|
+
rule.multiple_mappings? ? rule.name : [rule.name]
|
|
28
26
|
|
|
29
|
-
def call
|
|
30
27
|
rule_names.each do |rule_name|
|
|
31
|
-
value = rule_value_for(
|
|
28
|
+
value = rule_value_for(rule, doc, format, attr, register,
|
|
29
|
+
instance_object, rule_name)
|
|
32
30
|
return value if Utils.initialized?(value)
|
|
33
31
|
end
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
private
|
|
39
|
-
|
|
40
|
-
attr_reader :rule, :doc, :format, :attr, :register, :options,
|
|
41
|
-
:instance_object
|
|
42
|
-
|
|
43
|
-
def rule_names
|
|
44
|
-
RULE_NAMES_CACHE[rule] ||=
|
|
45
|
-
rule.multiple_mappings? ? rule.name : [rule.name]
|
|
33
|
+
Lutaml::Model::UninitializedClass.instance
|
|
46
34
|
end
|
|
47
35
|
|
|
48
|
-
def rule_value_for(
|
|
36
|
+
def self.rule_value_for(rule, doc, format, attr, register,
|
|
37
|
+
instance_object, name)
|
|
49
38
|
# When name is nil but document is a hash-like object with a single key matching
|
|
50
39
|
# the attribute name, extract that value. This handles the case where
|
|
51
40
|
# map to: :content is used and the document is {"content": "value"}
|
|
@@ -58,31 +47,46 @@ instance_object)
|
|
|
58
47
|
end
|
|
59
48
|
end
|
|
60
49
|
|
|
61
|
-
return doc if
|
|
50
|
+
return doc if name.nil? || rule.root_mapping?
|
|
62
51
|
return convert_to_format(doc, format) if rule.raw_mapping?
|
|
63
|
-
return
|
|
52
|
+
return Utils.fetch_str_or_sym(doc, name) if Utils.string_or_symbol_key?(doc, name)
|
|
64
53
|
|
|
65
54
|
if attr
|
|
66
55
|
resolved = attr.default_value(register, instance_object)
|
|
67
56
|
return resolved unless Utils.uninitialized?(resolved)
|
|
68
57
|
end
|
|
69
58
|
|
|
70
|
-
|
|
59
|
+
Lutaml::Model::UninitializedClass.instance
|
|
71
60
|
end
|
|
72
61
|
|
|
73
|
-
def
|
|
74
|
-
|
|
62
|
+
def self.convert_to_format(doc, format)
|
|
63
|
+
adapter = Lutaml::Model::Config.adapter_for(format)
|
|
64
|
+
adapter.new(doc).public_send(:"to_#{format}")
|
|
75
65
|
end
|
|
76
66
|
|
|
77
|
-
def
|
|
78
|
-
|
|
67
|
+
def initialize(rule, doc, format, attr, register, options,
|
|
68
|
+
instance_object)
|
|
69
|
+
super()
|
|
70
|
+
|
|
71
|
+
@rule = rule
|
|
72
|
+
@doc = doc
|
|
73
|
+
@format = format
|
|
74
|
+
@attr = attr
|
|
75
|
+
@register = register
|
|
76
|
+
@options = options
|
|
77
|
+
@instance_object = instance_object
|
|
79
78
|
end
|
|
80
79
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
attr_reader :rule, :doc, :format, :attr, :register, :options,
|
|
81
|
+
:instance_object
|
|
82
|
+
|
|
83
|
+
def call
|
|
84
|
+
self.class.call(rule, doc, format, attr, register, options,
|
|
85
|
+
instance_object)
|
|
84
86
|
end
|
|
85
87
|
|
|
88
|
+
private
|
|
89
|
+
|
|
86
90
|
def uninitialized_value
|
|
87
91
|
Lutaml::Model::UninitializedClass.instance
|
|
88
92
|
end
|
data/lib/lutaml/model/utils.rb
CHANGED
|
@@ -172,7 +172,14 @@ module Lutaml
|
|
|
172
172
|
# string_or_symbol_key?(hash, :key) # => true
|
|
173
173
|
# string_or_symbol_key?(hash, "invalid_key") # => false
|
|
174
174
|
def string_or_symbol_key?(hash, key)
|
|
175
|
-
|
|
175
|
+
if key.is_a?(String)
|
|
176
|
+
hash.key?(key) || hash.key?(key.to_sym)
|
|
177
|
+
elsif key.is_a?(Symbol)
|
|
178
|
+
# Same frozen-string probe as fetch_str_or_sym's Symbol branch.
|
|
179
|
+
hash.key?(key) || hash.key?(key.name)
|
|
180
|
+
else
|
|
181
|
+
hash.key?(key.to_s) || hash.key?(key.to_sym)
|
|
182
|
+
end
|
|
176
183
|
end
|
|
177
184
|
|
|
178
185
|
# Fetch the value from the hash using the key in string or symbol format
|
|
@@ -264,20 +271,25 @@ module Lutaml
|
|
|
264
271
|
end
|
|
265
272
|
|
|
266
273
|
def add_getter_if_not_defined(klass, attribute)
|
|
274
|
+
# The ivar symbol closes over once per definition instead of
|
|
275
|
+
# being minted per call (14k+ per corpus parse on the setter).
|
|
276
|
+
ivar = :"@__#{attribute}"
|
|
267
277
|
add_method_if_not_defined(klass, attribute) do
|
|
268
|
-
instance_variable_get(
|
|
278
|
+
instance_variable_get(ivar)
|
|
269
279
|
end
|
|
270
280
|
end
|
|
271
281
|
|
|
272
282
|
def add_boolean_getter_if_not_defined(klass, attribute)
|
|
283
|
+
ivar = :"@__#{attribute}"
|
|
273
284
|
add_method_if_not_defined(klass, "#{attribute}?") do
|
|
274
|
-
!!instance_variable_get(
|
|
285
|
+
!!instance_variable_get(ivar)
|
|
275
286
|
end
|
|
276
287
|
end
|
|
277
288
|
|
|
278
289
|
def add_setter_if_not_defined(klass, attribute)
|
|
290
|
+
ivar = :"@__#{attribute}"
|
|
279
291
|
add_method_if_not_defined(klass, "#{attribute}=") do |value|
|
|
280
|
-
instance_variable_set(
|
|
292
|
+
instance_variable_set(ivar, value)
|
|
281
293
|
end
|
|
282
294
|
end
|
|
283
295
|
|
data/lib/lutaml/model/version.rb
CHANGED
|
@@ -6,6 +6,10 @@ require "moxml/adapter/oga"
|
|
|
6
6
|
module Lutaml
|
|
7
7
|
module Xml
|
|
8
8
|
module Adapter
|
|
9
|
+
# @deprecated the Oga engine is on the deprecation path — moxml's
|
|
10
|
+
# default engine (Nokogiri, or the leptris native engine when
|
|
11
|
+
# present) is the supported XML lane. Selection emits a
|
|
12
|
+
# deprecation warning; removal lands in a future release.
|
|
9
13
|
class OgaAdapter < BaseAdapter
|
|
10
14
|
MOXML_ADAPTER = Moxml::Adapter::Oga
|
|
11
15
|
BUILDER_CLASS = Builder::Oga
|
|
@@ -7,6 +7,10 @@ require "moxml/adapter/rexml"
|
|
|
7
7
|
module Lutaml
|
|
8
8
|
module Xml
|
|
9
9
|
module Adapter
|
|
10
|
+
# @deprecated the REXML engine is on the deprecation path — moxml's
|
|
11
|
+
# default engine (Nokogiri, or the leptris native engine when
|
|
12
|
+
# present) is the supported XML lane. Selection emits a
|
|
13
|
+
# deprecation warning; removal lands in a future release.
|
|
10
14
|
class RexmlAdapter < BaseAdapter
|
|
11
15
|
MOXML_ADAPTER = Moxml::Adapter::Rexml
|
|
12
16
|
BUILDER_CLASS = Builder::Rexml
|
|
@@ -133,26 +133,6 @@ module Lutaml
|
|
|
133
133
|
raise NotImplementedError, "#{self.class} must implement #adapter_class"
|
|
134
134
|
end
|
|
135
135
|
|
|
136
|
-
def node_attributes(node)
|
|
137
|
-
return {} unless node.is_a?(Moxml::Element)
|
|
138
|
-
|
|
139
|
-
node.attributes.each_with_object({}) do |attr, hash|
|
|
140
|
-
next if attr_is_namespace?(attr)
|
|
141
|
-
|
|
142
|
-
ns_prefix = attr.namespace&.prefix
|
|
143
|
-
ns_prefix = nil if ns_prefix && ns_prefix.empty?
|
|
144
|
-
|
|
145
|
-
attr_name = ns_prefix ? "#{ns_prefix}:#{attr.name}" : attr.name
|
|
146
|
-
|
|
147
|
-
hash[attr_name] = XmlAttribute.new(
|
|
148
|
-
attr_name,
|
|
149
|
-
attribute_value_for_build(attr),
|
|
150
|
-
namespace: ns_prefix ? attr.namespace&.uri : nil,
|
|
151
|
-
namespace_prefix: ns_prefix,
|
|
152
|
-
)
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
136
|
# Shared frozen empties: the parse path hands these straight into
|
|
157
137
|
# XmlElement (which never mutates the containers it is given), so
|
|
158
138
|
# attribute-less elements — the majority in real documents — build
|
|
@@ -161,6 +141,21 @@ module Lutaml
|
|
|
161
141
|
NO_CHILDREN = [].freeze # rubocop:todo Lint/UselessConstantScoping
|
|
162
142
|
EMPTY_ATTRIBUTES = [{}, nil].freeze # rubocop:todo Lint/UselessConstantScoping
|
|
163
143
|
|
|
144
|
+
# Interned "prefix:name" attribute spellings. Attribute names
|
|
145
|
+
# repeat across every same-shaped node in a document (hundreds of
|
|
146
|
+
# distinct spellings against millions of nodes), so one cached
|
|
147
|
+
# composite string replaces one interpolated string per attribute
|
|
148
|
+
# per node. Guarded: parses may run on parallel threads; the
|
|
149
|
+
# cache is bounded by the distinct spellings.
|
|
150
|
+
ATTR_NAME_LOCK = Mutex.new # rubocop:todo Lint/UselessConstantScoping
|
|
151
|
+
ATTR_NAME_INTERN = {} # rubocop:todo Style/MutableConstant, Lint/UselessConstantScoping
|
|
152
|
+
|
|
153
|
+
def prefixed_attr_name(ns_prefix, name)
|
|
154
|
+
ATTR_NAME_LOCK.synchronize do
|
|
155
|
+
(ATTR_NAME_INTERN[ns_prefix] ||= {})[name] ||= "#{ns_prefix}:#{name}"
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
164
159
|
def node_attributes_with_order(node)
|
|
165
160
|
return EMPTY_ATTRIBUTES unless node.is_a?(Moxml::Element)
|
|
166
161
|
|
|
@@ -174,7 +169,7 @@ module Lutaml
|
|
|
174
169
|
ns_prefix = attr.namespace&.prefix
|
|
175
170
|
ns_prefix = nil if ns_prefix && ns_prefix.empty?
|
|
176
171
|
|
|
177
|
-
attr_name = ns_prefix ?
|
|
172
|
+
attr_name = ns_prefix ? prefixed_attr_name(ns_prefix, attr.name) : attr.name
|
|
178
173
|
order << attr_name
|
|
179
174
|
|
|
180
175
|
h[attr_name] = XmlAttribute.new(
|
|
@@ -1078,7 +1078,27 @@ mapper_class: nil)
|
|
|
1078
1078
|
|
|
1079
1079
|
# Use the OOP decision resolver
|
|
1080
1080
|
@prefix_resolver ||= Decisions::ElementPrefixResolver.new
|
|
1081
|
-
decision
|
|
1081
|
+
# The decision is a pure function of the walk-invariant facts:
|
|
1082
|
+
# static per-model objects (mapping, needs, namespace class),
|
|
1083
|
+
# the element's own spelling, the parent-chain inputs, and the
|
|
1084
|
+
# two projections of the mutating parent_hoisted set that the
|
|
1085
|
+
# rules can see for this element's namespace. Same-shaped
|
|
1086
|
+
# elements repeat by the thousands in real documents, so the
|
|
1087
|
+
# memoized verdict replaces the 12-rule engine execution per
|
|
1088
|
+
# element (lutaml-model#1064). Per-parse memo: the planner is
|
|
1089
|
+
# built once per serialize run, so entries die with the parse.
|
|
1090
|
+
memo_key = [
|
|
1091
|
+
mapping, needs,
|
|
1092
|
+
xml_element.namespace_class, xml_element.name,
|
|
1093
|
+
input_prefix_of(xml_element), element_used_prefix,
|
|
1094
|
+
is_root, parent_format, parent_namespace_class,
|
|
1095
|
+
parent_namespace_prefix,
|
|
1096
|
+
hoisted_facts_for(xml_element, parent_hoisted),
|
|
1097
|
+
options[:use_prefix],
|
|
1098
|
+
options[:input_formats].object_id,
|
|
1099
|
+
options[:stored_xml_declaration_plan].object_id
|
|
1100
|
+
]
|
|
1101
|
+
(@element_prefix_memo ||= {})[memo_key] ||= @prefix_resolver.resolve_with_decision(
|
|
1082
1102
|
xml_element, mapping, needs, options,
|
|
1083
1103
|
is_root: is_root,
|
|
1084
1104
|
parent_format: parent_format,
|
|
@@ -1086,9 +1106,49 @@ mapper_class: nil)
|
|
|
1086
1106
|
parent_namespace_prefix: parent_namespace_prefix,
|
|
1087
1107
|
parent_hoisted: parent_hoisted,
|
|
1088
1108
|
element_used_prefix: element_used_prefix
|
|
1089
|
-
)
|
|
1109
|
+
).prefix
|
|
1110
|
+
end
|
|
1111
|
+
|
|
1112
|
+
# The element's own input prefix, the same lookup
|
|
1113
|
+
# DecisionContext#element_namespace_prefix performs.
|
|
1114
|
+
def input_prefix_of(xml_element)
|
|
1115
|
+
return nil unless xml_element
|
|
1116
|
+
|
|
1117
|
+
if xml_element.is_a?(Lutaml::Xml::XmlElement)
|
|
1118
|
+
xml_element.namespace_prefix
|
|
1119
|
+
else
|
|
1120
|
+
prefix = xml_element.xml_namespace_prefix
|
|
1121
|
+
return prefix if prefix && !prefix.empty?
|
|
1090
1122
|
|
|
1091
|
-
|
|
1123
|
+
original = xml_element.original_xml_element
|
|
1124
|
+
original.is_a?(Lutaml::Xml::XmlElement) ? original.namespace_prefix : nil
|
|
1125
|
+
end
|
|
1126
|
+
end
|
|
1127
|
+
|
|
1128
|
+
# The projections of the walk-mutating parent_hoisted set that the
|
|
1129
|
+
# decision rules read for this element's namespace: the prefix
|
|
1130
|
+
# (if any) this namespace is hoisted under, and whether the
|
|
1131
|
+
# parent's default namespace matches this element's namespace.
|
|
1132
|
+
def hoisted_facts_for(xml_element, parent_hoisted)
|
|
1133
|
+
ns_class = xml_element&.namespace_class
|
|
1134
|
+
uri = ns_class&.uri
|
|
1135
|
+
return [nil, false] unless uri
|
|
1136
|
+
|
|
1137
|
+
all_uris = if ns_class.is_a?(Class) && ns_class < Lutaml::Xml::Namespace
|
|
1138
|
+
ns_class.all_uris
|
|
1139
|
+
else
|
|
1140
|
+
[uri]
|
|
1141
|
+
end
|
|
1142
|
+
|
|
1143
|
+
hoisted_prefix = nil
|
|
1144
|
+
parent_hoisted.each do |prefix, hoisted_uri|
|
|
1145
|
+
if all_uris.include?(hoisted_uri)
|
|
1146
|
+
hoisted_prefix = prefix
|
|
1147
|
+
break
|
|
1148
|
+
end
|
|
1149
|
+
end
|
|
1150
|
+
default_matches = parent_hoisted.key?(nil) && all_uris.include?(parent_hoisted[nil])
|
|
1151
|
+
[hoisted_prefix, default_matches]
|
|
1092
1152
|
end
|
|
1093
1153
|
|
|
1094
1154
|
# Build qualified element name with prefix
|
|
@@ -163,10 +163,15 @@ module Lutaml
|
|
|
163
163
|
|
|
164
164
|
def read_absolute_path(path, original_location, mapped: false)
|
|
165
165
|
unless File.exist?(path)
|
|
166
|
+
# schemaLocation is a hint, not a mandate (XSD 1.0 §4.2.3):
|
|
167
|
+
# an unresolvable include/import/redefine warns and the
|
|
168
|
+
# assembly continues without the referenced components —
|
|
169
|
+
# the W3C suite's expected reading (lutaml-model#1087).
|
|
166
170
|
message = mapped ? "Mapped schema file not found" : "Schema file not found"
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
171
|
+
warn "#{message}: #{path} (original location: #{original_location}); " \
|
|
172
|
+
"treating the schemaLocation hint as unresolvable and assembling " \
|
|
173
|
+
"without the referenced components"
|
|
174
|
+
return nil
|
|
170
175
|
end
|
|
171
176
|
|
|
172
177
|
File.read(path)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# lutaml-model#1086: on a Collection subclass, scalar `map` declarations
|
|
6
|
+
# in key_value were silently ignored alongside `instances` — the scalar
|
|
7
|
+
# attributes neither serialized nor deserialized (data loss, no error).
|
|
8
|
+
# The document is hash-shaped (instance key plus scalar keys) when
|
|
9
|
+
# scalar mappings are declared; collections without scalar mappings keep
|
|
10
|
+
# the legacy bare items array.
|
|
11
|
+
module CollectionScalarMapsSpec
|
|
12
|
+
class Entry < Lutaml::Model::Serializable
|
|
13
|
+
attribute :name, :string
|
|
14
|
+
key_value { map "name", to: :name }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class StampToy < Lutaml::Model::Collection
|
|
18
|
+
instances :entries, Entry
|
|
19
|
+
attribute :stamp, :integer, default: -> { 0 }
|
|
20
|
+
key_value do
|
|
21
|
+
map "entries", to: :entries
|
|
22
|
+
map "stamp", to: :stamp
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
RSpec.describe "Collection with scalar key_value mappings (#1086)" do
|
|
28
|
+
let(:toy) do
|
|
29
|
+
collection = CollectionScalarMapsSpec::StampToy.new
|
|
30
|
+
collection.entries = [CollectionScalarMapsSpec::Entry.new(name: "x")]
|
|
31
|
+
collection.stamp = 42
|
|
32
|
+
collection
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "serializes the scalars alongside the instances" do
|
|
36
|
+
expect(toy.to_yaml).to include("stamp: 42")
|
|
37
|
+
expect(toy.to_yaml).to include("entries:")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "deserializes the scalars from the hash-shaped document" do
|
|
41
|
+
parsed = CollectionScalarMapsSpec::StampToy.from_yaml(toy.to_yaml)
|
|
42
|
+
expect(parsed.stamp).to eq(42)
|
|
43
|
+
expect(parsed.entries.size).to eq(1)
|
|
44
|
+
expect(parsed.entries.first.name).to eq("x")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "keeps the legacy bare-array document parseable" do
|
|
48
|
+
parsed = CollectionScalarMapsSpec::StampToy.from_yaml("---\n- name: y\n")
|
|
49
|
+
expect(parsed).to be_a(CollectionScalarMapsSpec::StampToy)
|
|
50
|
+
expect(parsed.entries.size).to eq(1)
|
|
51
|
+
expect(parsed.entries.first.name).to eq("y")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "round-trips through the hash shape" do
|
|
55
|
+
parsed = CollectionScalarMapsSpec::StampToy.from_yaml(toy.to_yaml)
|
|
56
|
+
expect(parsed.to_yaml).to eq(toy.to_yaml)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# lutaml-model#1089: the KeyValue serializer raised IncorrectModelError
|
|
6
|
+
# on values the model layer had accepted — raw scalars and hashes
|
|
7
|
+
# stored through the cast chain's pass-through, and Serializable
|
|
8
|
+
# siblings accepted at construction via already-serialized pass-through.
|
|
9
|
+
# The serializer now coerces accepted values to the declared nested
|
|
10
|
+
# type instead of exploding mid-render.
|
|
11
|
+
module KvAcceptedValuesSpec
|
|
12
|
+
class Content < Lutaml::Model::Serializable
|
|
13
|
+
attribute :content, :string
|
|
14
|
+
|
|
15
|
+
xml do
|
|
16
|
+
map_content to: :content
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
key_value do
|
|
20
|
+
map "content", to: :content
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class TypedContent < Content
|
|
25
|
+
attribute :type, :string
|
|
26
|
+
|
|
27
|
+
xml do
|
|
28
|
+
map_attribute "type", to: :type
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
key_value do
|
|
32
|
+
map "type", to: :type
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class Thing < Lutaml::Model::Serializable
|
|
37
|
+
attribute :label, Content
|
|
38
|
+
attribute :names, TypedContent, collection: true
|
|
39
|
+
|
|
40
|
+
key_value do
|
|
41
|
+
map "label", to: :label
|
|
42
|
+
map "names", to: :names
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
RSpec.describe "KeyValue serialization of accepted nested values (#1089)" do
|
|
48
|
+
subject(:thing) { KvAcceptedValuesSpec::Thing.new(label: value, names: names) }
|
|
49
|
+
|
|
50
|
+
context "with a raw scalar the constructor stored" do
|
|
51
|
+
let(:value) { "JTA" }
|
|
52
|
+
let(:names) { [] }
|
|
53
|
+
|
|
54
|
+
it "wraps onto the declared type's content attribute" do
|
|
55
|
+
expect(thing.to_yaml).to include("content: JTA")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "deserializes back to the declared type" do
|
|
59
|
+
parsed = KvAcceptedValuesSpec::Thing.from_yaml(thing.to_yaml)
|
|
60
|
+
expect(parsed.label).to be_a(KvAcceptedValuesSpec::Content)
|
|
61
|
+
expect(parsed.label.content).to eq("JTA")
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
context "with a raw hash the constructor stored" do
|
|
66
|
+
let(:value) { { "content" => "Hash form" } }
|
|
67
|
+
let(:names) { [] }
|
|
68
|
+
|
|
69
|
+
it "constructs the declared type from the hash" do
|
|
70
|
+
expect(thing.to_yaml).to include("content: Hash form")
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
context "with a Serializable sibling of the declared type" do
|
|
75
|
+
let(:value) { KvAcceptedValuesSpec::Content.new(content: "X") }
|
|
76
|
+
let(:names) { [KvAcceptedValuesSpec::Content.new(content: "Sibling")] }
|
|
77
|
+
|
|
78
|
+
it "round-trips through the framework hash into the declared type" do
|
|
79
|
+
expect(thing.to_yaml).to include("content: X")
|
|
80
|
+
expect(thing.to_yaml).to include("content: Sibling")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "deserializes as the declared type" do
|
|
84
|
+
parsed = KvAcceptedValuesSpec::Thing.from_yaml(thing.to_yaml)
|
|
85
|
+
expect(parsed.names.first).to be_a(KvAcceptedValuesSpec::TypedContent)
|
|
86
|
+
expect(parsed.names.first.content).to eq("Sibling")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
context "with an already-cast value" do
|
|
91
|
+
let(:value) { KvAcceptedValuesSpec::Content.new(content: "fine") }
|
|
92
|
+
let(:names) { [KvAcceptedValuesSpec::TypedContent.new(content: "t")] }
|
|
93
|
+
|
|
94
|
+
it "serializes unchanged" do
|
|
95
|
+
expect(thing.to_yaml).to include("content: fine")
|
|
96
|
+
expect(thing.to_yaml).to include("content: t")
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -278,21 +278,20 @@ RSpec.describe Lutaml::Xml::Schema::Xsd::Glob do
|
|
|
278
278
|
end
|
|
279
279
|
|
|
280
280
|
context "when mapped file does not exist" do
|
|
281
|
-
it "
|
|
281
|
+
it "warns and returns nil for absolute path mapping" do
|
|
282
282
|
nonexistent_path = "/nonexistent/path/to/schema.xsd"
|
|
283
283
|
described_class.schema_mappings = [
|
|
284
284
|
{ from: "http://example.com/schema.xsd", to: nonexistent_path },
|
|
285
285
|
]
|
|
286
286
|
|
|
287
287
|
expect do
|
|
288
|
-
described_class.include_schema("http://example.com/schema.xsd")
|
|
289
|
-
end.to
|
|
290
|
-
Lutaml::Xml::Schema::Xsd::Error,
|
|
288
|
+
expect(described_class.include_schema("http://example.com/schema.xsd")).to be_nil
|
|
289
|
+
end.to output(
|
|
291
290
|
/Mapped schema file not found: #{Regexp.escape(nonexistent_path)}/,
|
|
292
|
-
)
|
|
291
|
+
).to_stderr
|
|
293
292
|
end
|
|
294
293
|
|
|
295
|
-
it "includes original location in
|
|
294
|
+
it "includes original location in the warning" do
|
|
296
295
|
nonexistent_path = "/nonexistent/path/to/schema.xsd"
|
|
297
296
|
described_class.schema_mappings = [
|
|
298
297
|
{ from: "http://example.com/schema.xsd", to: nonexistent_path },
|
|
@@ -300,30 +299,27 @@ RSpec.describe Lutaml::Xml::Schema::Xsd::Glob do
|
|
|
300
299
|
|
|
301
300
|
expect do
|
|
302
301
|
described_class.include_schema("http://example.com/schema.xsd")
|
|
303
|
-
end.to
|
|
304
|
-
Lutaml::Xml::Schema::Xsd::Error,
|
|
302
|
+
end.to output(
|
|
305
303
|
%r{original location: http://example\.com/schema\.xsd},
|
|
306
|
-
)
|
|
304
|
+
).to_stderr
|
|
307
305
|
end
|
|
308
306
|
end
|
|
309
307
|
|
|
310
308
|
context "when schema file does not exist" do
|
|
311
|
-
it "
|
|
309
|
+
it "warns and returns nil" do
|
|
312
310
|
expect do
|
|
313
|
-
described_class.include_schema("nonexistent_schema.xsd")
|
|
314
|
-
end.to
|
|
315
|
-
Lutaml::Xml::Schema::Xsd::Error,
|
|
311
|
+
expect(described_class.include_schema("nonexistent_schema.xsd")).to be_nil
|
|
312
|
+
end.to output(
|
|
316
313
|
/Schema file not found:.*nonexistent_schema\.xsd/,
|
|
317
|
-
)
|
|
314
|
+
).to_stderr
|
|
318
315
|
end
|
|
319
316
|
|
|
320
|
-
it "includes original location in
|
|
317
|
+
it "includes original location in the warning" do
|
|
321
318
|
expect do
|
|
322
319
|
described_class.include_schema("../../missing/schema.xsd")
|
|
323
|
-
end.to
|
|
324
|
-
Lutaml::Xml::Schema::Xsd::Error,
|
|
320
|
+
end.to output(
|
|
325
321
|
%r{original location:.*missing/schema\.xsd},
|
|
326
|
-
)
|
|
322
|
+
).to_stderr
|
|
327
323
|
end
|
|
328
324
|
end
|
|
329
325
|
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../../spec_helper"
|
|
4
|
+
require "tmpdir"
|
|
5
|
+
require "lutaml/xml/schema/xsd"
|
|
6
|
+
|
|
7
|
+
# lutaml-model#1087: schemaLocation on xs:include / xs:import /
|
|
8
|
+
# xs:redefine is a hint (XSD 1.0 §4.2.3). An unresolvable hint warns
|
|
9
|
+
# and the assembly continues without the referenced components — the
|
|
10
|
+
# W3C suite's expected reading (msData anyURI_a001 et al.) — instead
|
|
11
|
+
# of raising Schema file not found.
|
|
12
|
+
RSpec.describe "Xsd unresolvable schemaLocation hints" do
|
|
13
|
+
let(:xsd) do
|
|
14
|
+
<<~XSD
|
|
15
|
+
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
|
16
|
+
<xsd:include schemaLocation="0"/>
|
|
17
|
+
<xsd:import schemaLocation="123" namespace="2"/>
|
|
18
|
+
<xsd:redefine schemaLocation="123"/>
|
|
19
|
+
</xsd:schema>
|
|
20
|
+
XSD
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "warns for each unresolvable hint and still parses" do
|
|
24
|
+
doc = nil
|
|
25
|
+
Dir.mktmpdir do |dir|
|
|
26
|
+
expect do
|
|
27
|
+
doc = Lutaml::Xml::Schema::Xsd.parse(xsd, location: dir)
|
|
28
|
+
end.to output(/Schema file not found/).to_stderr
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
expect(doc.includes.first.schema_path).to eq("0")
|
|
32
|
+
expect(doc.imports.first.schema_path).to eq("123")
|
|
33
|
+
expect(doc.redefine.first.schema_path).to eq("123")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "warns for the unresolvable include" do
|
|
37
|
+
expect do
|
|
38
|
+
parse_with_warnings
|
|
39
|
+
end.to output(%r{original location: 0\)}).to_stderr
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "warns for the unresolvable import" do
|
|
43
|
+
# xs:redefine's schemaLocation resolves through the composition
|
|
44
|
+
# route (schema_path_without_base), not at parse time — the
|
|
45
|
+
# parse-time fetches are the include and the import.
|
|
46
|
+
expect do
|
|
47
|
+
parse_with_warnings
|
|
48
|
+
end.to output(%r{original location: 123\)}).to_stderr
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def parse_with_warnings
|
|
54
|
+
Dir.mktmpdir do |dir|
|
|
55
|
+
Lutaml::Xml::Schema::Xsd.parse(xsd, location: dir)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|