lutaml-model 0.8.158 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74c783e3897939ae61d1cbd03782641f0ed8facb94ed8098588a7491504db44c
4
- data.tar.gz: c30df2f8628c15e232559c658ca820e1d4597b976a87d98936703c64bf99d5c9
3
+ metadata.gz: b57a42ee46afb01c4b14ed7b3a9f7563d8f7a88ff567ca912cd31304865f3b53
4
+ data.tar.gz: 5d2e30a7d52e49f45bec3b8cdd302c181ab9c284e8046bed5b32a00f3bc4098c
5
5
  SHA512:
6
- metadata.gz: 2c48df71e7b19c791954371707b6ff671f2fd234f8de0be7ef4c5dbce1838b7ab349b669124793c15037f6bdd788a9260924e195cfc1d34ca779ecc9f07471ab
7
- data.tar.gz: f4069257888fb2b6c9fa5e9bd8913fd7bb2b0a369c5d65336f154902e96b5d5e3295c182adef140e78a0cb3ee008d8a8dceaefa0563fee71ee16a70bf3309c1d
6
+ metadata.gz: 3a6d56103c63a1bf5552f25a7766e3b791ce14b3e83cf755c4e2f346bd70c84cf163d396a198423b4957594684fc0de2112a8b9c603d409dd77ca643fee18560
7
+ data.tar.gz: 6da4328f548094e614753bd012e75261b770929aa7115ad4bf307ec5170f9de03ef6b943806882a53d4801cfc33b8e3bd046779d0a6e2a07cc719f1728a97615
@@ -114,6 +114,7 @@ model_class: nil)
114
114
  # @param options [Hash] Serialization options
115
115
  # @return [Hash, nil] The serialized hash or nil if empty
116
116
  def serialize_nested_model(value, rule, options = {})
117
+ value = coerce_nested_for_rule(value, rule)
117
118
  validate_nested_model_type!(value, rule)
118
119
 
119
120
  # Determine the actual type for polymorphism support
@@ -152,6 +153,7 @@ model_class: nil)
152
153
  # For Serializable types, use to_#{format} method
153
154
  if rule.attribute_type.is_a?(Class) &&
154
155
  rule.attribute_type < Lutaml::Model::Serialize
156
+ value = coerce_nested_for_rule(value, rule)
155
157
  validate_serializable_type!(value, rule)
156
158
  return value.public_send(:"to_#{format}")
157
159
  end
@@ -245,6 +247,25 @@ model_class: nil)
245
247
 
246
248
  private
247
249
 
250
+ # Coerce a value the model accepted into the rule's declared
251
+ # nested class (#1089) before the validators run. Registered
252
+ # type substitutions pass through untouched — the validators
253
+ # accept them as-is.
254
+ #
255
+ # @param value [Object] The stored attribute value
256
+ # @param rule [CompiledRule] The compiled rule
257
+ # @return [Object] The coerced value, or the value unchanged
258
+ def coerce_nested_for_rule(value, rule)
259
+ expected = rule.attribute_type.model || rule.attribute_type
260
+ return value if value.is_a?(expected)
261
+
262
+ context = Lutaml::Model::GlobalContext.context(register_id)
263
+ return value if context.substitution_for(rule.attribute_type)
264
+ .any? { |s| s.to_type == value.class }
265
+
266
+ NestedValueCoercion.coerce(value, expected)
267
+ end
268
+
248
269
  # Validate that a nested model value matches the expected type.
249
270
  #
250
271
  # @param value [Object] The value to validate
@@ -15,6 +15,44 @@ module Lutaml
15
15
  #
16
16
  # This is a critical step toward symmetric OOP architecture
17
17
  # across all serialization formats.
18
+ # Coerces values the model layer accepted into a rule's declared
19
+ # nested type (lutaml-model#1089): constructors and writers accept
20
+ # raw hashes, sibling Serializable instances, and content scalars,
21
+ # but the KeyValue serializer demanded exact declared-class
22
+ # instances — accept-then-explode mid-render. Hashes construct
23
+ # directly; Serializable siblings round-trip through the framework
24
+ # hash; scalars wrap onto the declared type's content attribute
25
+ # when its XML mapping declares one (`map_content to: :x` — the
26
+ # same semantics the XML lane applies to text). Genuinely
27
+ # un-coercible values pass through unchanged so the caller's
28
+ # validation raises with its own message.
29
+ module NestedValueCoercion
30
+ @content_attributes = {}.compare_by_identity
31
+
32
+ # The attribute a declared type's XML mapping designates as its
33
+ # textual content. Class-level memo: the answer is a static
34
+ # per-type mapping fact. Keyed by type only; register-specific
35
+ # content mappings are not a supported combination.
36
+ def self.content_attribute_for(type)
37
+ @content_attributes[type] ||= begin
38
+ mapping = type.mappings_for(:xml)
39
+ mapping&.content_mapping&.to
40
+ end
41
+ end
42
+
43
+ def self.coerce(value, declared_class)
44
+ if value.is_a?(::Hash)
45
+ declared_class.new(value)
46
+ elsif value.is_a?(Lutaml::Model::Serialize)
47
+ declared_class.new(value.to_hash)
48
+ elsif (content_key = content_attribute_for(declared_class))
49
+ declared_class.new(content_key => value)
50
+ else
51
+ value
52
+ end
53
+ end
54
+ end
55
+
18
56
  class Transformation < Lutaml::Model::Transformation
19
57
  # Builtin Value types whose cast of an already-native value is the
20
58
  # identity — used by the serialize_value fast path.
@@ -1037,6 +1075,10 @@ child_mappings, options)
1037
1075
  # @param value [Object] The value
1038
1076
  # @param options [Hash] Options
1039
1077
  # @return [Object] The value (could be Hash, primitive, or KeyValueElement)
1078
+ def coerce_accepted_nested_value(value, declared_class)
1079
+ NestedValueCoercion.coerce(value, declared_class)
1080
+ end
1081
+
1040
1082
  # Child transformations memoized on this transformation instance:
1041
1083
  # the factory binds this instance's mapping/format/register, so
1042
1084
  # the memo is register-safe, and building one per serialized node
@@ -1083,15 +1125,21 @@ child_mappings, options)
1083
1125
  # Mapper class: value should be an instance of the mapped model
1084
1126
  # or a valid substituted type
1085
1127
  unless value.is_a?(rule.attribute_type.model) || uses_type_substitution
1086
- msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a '#{rule.attribute_type.model}'"
1087
- raise Lutaml::Model::IncorrectModelError, msg
1128
+ value = coerce_accepted_nested_value(value, rule.attribute_type.model)
1129
+ unless value.is_a?(rule.attribute_type.model)
1130
+ msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a '#{rule.attribute_type.model}'"
1131
+ raise Lutaml::Model::IncorrectModelError, msg
1132
+ end
1088
1133
  end
1089
1134
  else
1090
1135
  # Regular Serializable class: value should be a Serialize instance
1091
1136
  # or a valid substituted type
1092
1137
  unless value.is_a?(Lutaml::Model::Serialize) || uses_type_substitution
1093
- msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a kind of 'Lutaml::Model::Serialize'"
1094
- raise Lutaml::Model::IncorrectModelError, msg
1138
+ value = coerce_accepted_nested_value(value, rule.attribute_type)
1139
+ unless value.is_a?(Lutaml::Model::Serialize)
1140
+ msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a kind of 'Lutaml::Model::Serialize'"
1141
+ raise Lutaml::Model::IncorrectModelError, msg
1142
+ end
1095
1143
  end
1096
1144
  end
1097
1145
 
@@ -1191,8 +1239,11 @@ child_mappings, options)
1191
1239
  # When attribute_type is a Serializable class, value must be an instance of that class
1192
1240
  if plan[:nested]
1193
1241
  unless value.is_a?(rule.attribute_type)
1194
- msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a '#{rule.attribute_type}'"
1195
- raise Lutaml::Model::IncorrectModelError, msg
1242
+ value = coerce_accepted_nested_value(value, rule.attribute_type)
1243
+ unless value.is_a?(rule.attribute_type)
1244
+ msg = "attribute '#{rule.attribute_name}' value is a '#{value.class}' but should be a '#{rule.attribute_type}'"
1245
+ raise Lutaml::Model::IncorrectModelError, msg
1246
+ end
1196
1247
  end
1197
1248
 
1198
1249
  # Value is a valid Serialize instance - serialize it using its own to_#{format} method
@@ -478,9 +478,17 @@ module Lutaml
478
478
 
479
479
  def initialize(items = EMPTY_ITEMS,
480
480
  lutaml_register: Lutaml::Model::Config.default_register)
481
+ # Seed constructor-equivalent state in one compiled pass
482
+ # (#1059's walk-seed) and stand the generic walk down via the
483
+ # seed flag: 92% of a materialization corpus's constructor
484
+ # invocations are Collection wrappers whose generic super()
485
+ # walk only materialized defaults that the items mapping below
486
+ # overwrites again.
487
+ self.lutaml_constructor_seeded = true
488
+ init_walk_state(lutaml_register)
481
489
  super()
490
+ self.lutaml_constructor_seeded = nil
482
491
 
483
- @lutaml_register = lutaml_register
484
492
  items = [items].compact unless items.is_a?(Array)
485
493
 
486
494
  type = Lutaml::Model::GlobalContext.resolve_type(
@@ -57,21 +57,17 @@ module Lutaml
57
57
  # so thread it through when one is supplied. Plain Array collections and
58
58
  # register-less callers keep the prior construction path untouched.
59
59
  #
60
- # @param args [Array] Values to include in the collection
60
+ # The former *args splat + flatten ceremony allocated a splat Array
61
+ # plus a keyword-options hash per call (15k+ per corpus parse);
62
+ # every call site passes at most one already-flat Array, and
63
+ # Attribute#assert_flat_collection_items! guards nesting loudly.
64
+ #
65
+ # @param items [Array, nil] Values to include in the collection
61
66
  # @param register [Symbol, nil] Register for custom-collection type resolution
62
67
  # @return [Object] A new collection instance
63
- def build_collection(*args, register: nil)
64
- # args.flatten allocated a fresh Array even for the dominant
65
- # calls: no args, or one already-flat Array from cast_items
66
- # (71,949 flatten allocations per ISO-13849 parse).
67
- items = if args.empty?
68
- []
69
- elsif args.length == 1 && (first = args[0]).is_a?(::Array) &&
70
- first.none?(::Array)
71
- first
72
- else
73
- args.flatten
74
- end
68
+ def build_collection(items = nil, register: nil)
69
+ items = [] if items.nil?
70
+ items = [items] unless items.is_a?(::Array)
75
71
  unless register && initialize_accepts_register?
76
72
  return collection_class.new(items)
77
73
  end
@@ -134,6 +134,10 @@ module Lutaml
134
134
  # @param collection [Boolean] Whether the enum is a collection
135
135
  def add_enum_setter_if_not_defined(klass, enum_name, _values,
136
136
  collection)
137
+ # Static per-definition symbols close over instead of being
138
+ # minted per setter call (11k+ per corpus parse).
139
+ ivar = :"@#{enum_name}"
140
+ reader = enum_name.to_sym
137
141
  Utils.add_method_if_not_defined(klass, "#{enum_name}=") do |value|
138
142
  value = [] if value.nil?
139
143
  value = [value] if !value.is_a?(Array)
@@ -141,16 +145,16 @@ module Lutaml
141
145
  value_set_for(enum_name)
142
146
 
143
147
  if collection
144
- curr_value = public_send(:"#{enum_name}")
148
+ curr_value = public_send(reader)
145
149
 
146
150
  # Build a new Array rather than appending into the stored one.
147
151
  # The reader hands back the real Array now, so `curr_value` may
148
152
  # be this model's own collection — or, when the model defines
149
153
  # its own reader, something shared between instances or not an
150
154
  # Array at all. Duplicates are the reader's job either way.
151
- instance_variable_set(:"@#{enum_name}", curr_value + value)
155
+ instance_variable_set(ivar, curr_value + value)
152
156
  else
153
- instance_variable_set(:"@#{enum_name}", value)
157
+ instance_variable_set(ivar, value)
154
158
  end
155
159
  end
156
160
  end
@@ -111,6 +111,12 @@ module Lutaml
111
111
  # Opal strings are JS strings with no per-string encoding;
112
112
  # String#encode there doesn't accept MRI's keyword options.
113
113
  return value if Lutaml::Model.opal?
114
+ # The encode-with-options forces a fresh copy per value even
115
+ # when the string is already valid UTF-8 — the overwhelming
116
+ # case on parsed documents. Scan instead: no allocation on
117
+ # the happy path; encode still runs for genuinely invalid
118
+ # input.
119
+ return value if value.encoding == Encoding::UTF_8 && value.valid_encoding?
114
120
 
115
121
  value.encode("UTF-8", invalid: :replace, undef: :replace,
116
122
  replace: "")
@@ -271,20 +271,25 @@ module Lutaml
271
271
  end
272
272
 
273
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}"
274
277
  add_method_if_not_defined(klass, attribute) do
275
- instance_variable_get(:"@__#{attribute}")
278
+ instance_variable_get(ivar)
276
279
  end
277
280
  end
278
281
 
279
282
  def add_boolean_getter_if_not_defined(klass, attribute)
283
+ ivar = :"@__#{attribute}"
280
284
  add_method_if_not_defined(klass, "#{attribute}?") do
281
- !!instance_variable_get(:"@__#{attribute}")
285
+ !!instance_variable_get(ivar)
282
286
  end
283
287
  end
284
288
 
285
289
  def add_setter_if_not_defined(klass, attribute)
290
+ ivar = :"@__#{attribute}"
286
291
  add_method_if_not_defined(klass, "#{attribute}=") do |value|
287
- instance_variable_set(:"@__#{attribute}", value)
292
+ instance_variable_set(ivar, value)
288
293
  end
289
294
  end
290
295
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.158"
5
+ VERSION = "0.8.159"
6
6
  end
7
7
  end
@@ -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 ? "#{ns_prefix}:#{attr.name}" : attr.name
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 = @prefix_resolver.resolve_with_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
- decision.prefix
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
- raise ::Lutaml::Xml::Schema::Xsd::Error,
168
- "#{message}: #{path} " \
169
- "(original location: #{original_location})"
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,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 "raises error with helpful message for absolute path mapping" do
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 raise_error(
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 error message" do
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 raise_error(
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 "raises error with helpful message" do
309
+ it "warns and returns nil" do
312
310
  expect do
313
- described_class.include_schema("nonexistent_schema.xsd")
314
- end.to raise_error(
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 error message" do
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 raise_error(
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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.158
4
+ version: 0.8.159
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-02 00:00:00.000000000 Z
11
+ date: 2026-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -1801,6 +1801,7 @@ files:
1801
1801
  - spec/lutaml/model/json_state_nesting_spec.rb
1802
1802
  - spec/lutaml/model/jsonl/standard_adapter_spec.rb
1803
1803
  - spec/lutaml/model/jsonl_spec.rb
1804
+ - spec/lutaml/model/key_value_accepted_values_spec.rb
1804
1805
  - spec/lutaml/model/key_value_data_model/key_value_element_spec.rb
1805
1806
  - spec/lutaml/model/key_value_mapping_spec.rb
1806
1807
  - spec/lutaml/model/late_attribute_declaration_spec.rb
@@ -2084,6 +2085,7 @@ files:
2084
2085
  - spec/lutaml/xml/schema/xsd/schema_mapping_spec.rb
2085
2086
  - spec/lutaml/xml/schema/xsd/schema_validator_spec.rb
2086
2087
  - spec/lutaml/xml/schema/xsd/spec_helper.rb
2088
+ - spec/lutaml/xml/schema/xsd/unresolvable_schema_location_spec.rb
2087
2089
  - spec/lutaml/xml/schema/xsd/validation_error_spec.rb
2088
2090
  - spec/lutaml/xml/schema/xsd/validation_result_spec.rb
2089
2091
  - spec/lutaml/xml/schema/xsd/xsd_spec.rb