lutaml-model 0.8.197 → 0.8.199

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: 266b072f3fabe54b418f8691aba2049c5fcf24ec66072d00d4faef5cff40fca4
4
- data.tar.gz: 1aa8bf7e64783e944f9ee906db6b5a6d9be3454042eb2086bed7a38a8b6803e6
3
+ metadata.gz: 47aaf2a9887e7cda54173eee34113fe951be0a54e62ee9a014b9e1a5f3518922
4
+ data.tar.gz: cd540af8382bcb29af2847fa8fc9bcd3b825bfa437d61d89a1281d8f7b68d1fb
5
5
  SHA512:
6
- metadata.gz: 2a84249d40f2ea758e09a1ad76b4c6af13e986cea14d392622332b2377eb4131dde4271a324930fa04e9504e9a8e89f2feb62c59b8b3360711192c25f7ddfbb3
7
- data.tar.gz: 5ff6600865d03bd6dce93089b043630dd87b63191670b118fdb41dec5c8125035fd3613c6542641504d921cd5d727c8eb2f42bae65f61a7f46593b01e262128c
6
+ metadata.gz: b605d218d698f6696e7643d5b5cb53b6d26fe110611b4940bfd871a2fe64dfc385e2fd77969743779684b13fd8c520c19e32dde52ebf17e62bc9da3aa88b356e
7
+ data.tar.gz: 45b305d0dc57ac40aaa1cc261596519666e2b2e117c40ea10a1fc0a0c6e709bdeb2ea393c264c9d9b61af944ea94d8c269b1093a98121cef5f4af1259a3fbaee
@@ -66,7 +66,6 @@ require "lutaml/lml/symbol_index"
66
66
  require "lutaml/lml/transform"
67
67
  require "lutaml/lml/types"
68
68
  require "lutaml/lml/validator"
69
- require "lutaml/lml/version"
70
69
  require "lutaml/lml/view_resolution"
71
70
  require "lutaml/lml/view_resolver"
72
71
  require "lutaml/lml/yaml_parser"
@@ -1069,12 +1069,59 @@ child_mappings, options)
1069
1069
  parent[rule.serialized_name] = child_value unless child_value.nil?
1070
1070
  end
1071
1071
 
1072
- # Create value for an item (handles nested models and primitives)
1073
- #
1074
- # @param rule [CompiledRule] The rule
1075
- # @param value [Object] The value
1076
- # @param options [Hash] Options
1077
- # @return [Object] The value (could be Hash, primitive, or KeyValueElement)
1072
+ # Whether the value is a listed scalar member of the attribute's
1073
+ # polymorphic list: a Type::Value member admits non-model scalars
1074
+ # (and, with :array, arrays of them); a list of model classes
1075
+ # only admits model instances, so a scalar under it still raises
1076
+ # in the nested-model validation.
1077
+ def polymorphic_scalar_member?(rule, value)
1078
+ list = polymorphic_list_for(rule)
1079
+ return false unless list.is_a?(::Array)
1080
+
1081
+ scalar_member = list.any? do |member|
1082
+ member.is_a?(Class) && member < Lutaml::Model::Type::Value
1083
+ end
1084
+ return false unless scalar_member
1085
+
1086
+ case value
1087
+ when ::Hash, Lutaml::Model::Serialize
1088
+ false
1089
+ when ::Array
1090
+ value.all? do |item|
1091
+ !item.is_a?(::Hash) && !item.is_a?(Lutaml::Model::Serialize)
1092
+ end
1093
+ else
1094
+ true
1095
+ end
1096
+ end
1097
+
1098
+ # The attribute's polymorphic list, read off the declaring
1099
+ # class (the compiled rule carries the type, not the
1100
+ # attribute).
1101
+ def polymorphic_list_for(rule)
1102
+ polymorphic_attribute_for(rule)&.options&.[](:polymorphic)
1103
+ end
1104
+
1105
+ # Whether the attribute's list admits this Array of members on a
1106
+ # singular attribute: each item then takes its own lane — scalars the
1107
+ # scalar lane, model instances the nested one (#1292).
1108
+ def polymorphic_member_array?(rule, value)
1109
+ !union_rule?(rule) && polymorphic_attribute_for(rule)
1110
+ &.polymorphic_member_array?(value)
1111
+ end
1112
+
1113
+ def polymorphic_attribute_for(rule)
1114
+ klass = model_class
1115
+ return unless klass.is_a?(Class) &&
1116
+ klass.include?(Lutaml::Model::Serialize)
1117
+
1118
+ klass.attributes[rule.attribute_name.to_sym]
1119
+ end
1120
+
1121
+ def union_rule?(rule)
1122
+ Lutaml::Model::Type::Union.rule?(rule)
1123
+ end
1124
+
1078
1125
  def coerce_accepted_nested_value(value, declared_class)
1079
1126
  NestedValueCoercion.coerce(value, declared_class)
1080
1127
  end
@@ -1088,9 +1135,22 @@ child_mappings, options)
1088
1135
  build_child_transformation(type_class)
1089
1136
  end
1090
1137
 
1138
+ # Create value for an item (handles nested models and primitives)
1139
+ #
1140
+ # @param rule [CompiledRule] The rule
1141
+ # @param value [Object] The value
1142
+ # @param options [Hash] Options
1143
+ # @return [Object] The value (could be Hash, primitive, or KeyValueElement)
1091
1144
  def create_value_for_item(rule, value, options)
1092
1145
  return nil if value.nil?
1093
1146
 
1147
+ # A singular attribute whose polymorphic list admits an Array of
1148
+ # members serializes item by item: the scalar members ride the
1149
+ # scalar lane below, the model instances the nested one (#1292).
1150
+ if polymorphic_member_array?(rule, value)
1151
+ return value.map { |item| create_value_for_item(rule, item, options) }
1152
+ end
1153
+
1094
1154
  # Union: dispatch on the value's own class (stateless, like polymorphism).
1095
1155
  # A model member serializes via its class; a scalar member emits as-is.
1096
1156
  union = Lutaml::Model::Type::Union.rule?(rule)
@@ -1101,8 +1161,20 @@ child_mappings, options)
1101
1161
  rule.attribute_type < Lutaml::Model::Serialize
1102
1162
  end
1103
1163
 
1164
+ # A polymorphic list admits scalar members (Type::String,
1165
+ # :array, ...) alongside model classes: a value that is a
1166
+ # listed scalar member serializes through the scalar lane
1167
+ # (#1292). The nested-model validation below demands a Serialize
1168
+ # instance for every value, which rejected the scalar members
1169
+ # the declaration explicitly allowed.
1170
+ if is_nested_model && polymorphic_scalar_member?(rule, value)
1171
+ is_nested_model = false
1172
+
1173
+ end
1174
+
1104
1175
  if is_nested_model && !value.is_a?(Lutaml::Model::Serialize)
1105
1176
  # The KV mirror of the XML lane's #971 collapse: a tolerated
1177
+
1106
1178
  # singular attribute holding an over-assigned collection
1107
1179
  # serializes its leading value instead of raising.
1108
1180
  attr = if model_class.respond_to?(:attributes)
@@ -1237,7 +1309,10 @@ child_mappings, options)
1237
1309
 
1238
1310
  # Validate that value is an instance of the expected Serializable type
1239
1311
  # When attribute_type is a Serializable class, value must be an instance of that class
1240
- if plan[:nested]
1312
+ # A listed scalar member of a polymorphic list skips the nested
1313
+ # lane (the dispatch in create_value_for_item and here must agree:
1314
+ # #1292).
1315
+ if plan[:nested] && !polymorphic_scalar_member?(rule, value)
1241
1316
  unless value.is_a?(rule.attribute_type)
1242
1317
  value = coerce_accepted_nested_value(value, rule.attribute_type)
1243
1318
  unless value.is_a?(rule.attribute_type)
data/lib/lutaml/lml.rb CHANGED
@@ -83,7 +83,6 @@ module Lutaml
83
83
  autoload :Diagnostic, "lutaml/lml/diagnostic"
84
84
  autoload :SourcePosition, "lutaml/lml/source_position"
85
85
  autoload :Positioned, "lutaml/lml/positioned"
86
- autoload :VERSION, "lutaml/lml/version"
87
86
 
88
87
  # Type classes (Lutaml::Model::Type::Value subclasses, not models)
89
88
  autoload :LiteralValue, "lutaml/lml/literal_value"
@@ -727,6 +727,46 @@ module Lutaml
727
727
  type == Lutaml::Model::Type::Hash
728
728
  end
729
729
 
730
+ # Whether the attribute's polymorphic list admits this value as a
731
+ # listed scalar member: a Type::Value member is a scalar shape (and
732
+ # admits arrays of scalars; the :array symbol admits arrays too).
733
+ # A list of model classes only does not admit scalars (#1292).
734
+ def polymorphic_scalar_member?(value)
735
+ list = @options[:polymorphic]
736
+ return false unless list.is_a?(::Array)
737
+
738
+ scalar_member = list.any? do |member|
739
+ member.is_a?(Class) && member < Type::Value
740
+ end
741
+ return false unless scalar_member
742
+ return false if value.is_a?(::Hash) || value.is_a?(Serialize)
743
+ return true unless value.is_a?(::Array)
744
+
745
+ list.include?(:array) ||
746
+ value.all? { |item| !item.is_a?(::Hash) && !item.is_a?(Serialize) }
747
+ end
748
+
749
+ # Whether the list admits this Array on a singular attribute: a list
750
+ # with a scalar member admits an Array of members — scalars and model
751
+ # instances alike — each item taking its own lane (#1292).
752
+ def polymorphic_member_array?(value)
753
+ value.is_a?(Array) && value.none?(Hash) &&
754
+ has_polymorphic_list? &&
755
+ options[:polymorphic].any? { |member| member.is_a?(Class) && member < Type::Value }
756
+ end
757
+
758
+ # The listed member type that owns this value: the first Value
759
+ # member whose cast preserves the value's class, falling back to
760
+ # the first Value member. The probe only runs on the rare
761
+ # polymorphic-scalar path.
762
+ def polymorphic_member_type_for(value)
763
+ members = @options[:polymorphic].select do |member|
764
+ member.is_a?(Class) && member < Type::Value
765
+ end
766
+ members.find { |member| member.cast(value).class == value.class } ||
767
+ members.first
768
+ end
769
+
730
770
  # The attribute a plain scalar coerces onto for a Serializable
731
771
  # type (#1228): the whole-value `map to: :content` form (a
732
772
  # content/root mapping whose sole target is a scalar attribute)
@@ -984,7 +1024,7 @@ instance_object = nil)
984
1024
  end
985
1025
  return true unless options[:polymorphic]
986
1026
 
987
- valid_polymorphic_type?(value, resolved_type)
1027
+ valid_polymorphic_type?(value)
988
1028
  end
989
1029
 
990
1030
  def validate_polymorphic!(value, resolved_type)
@@ -1037,7 +1077,8 @@ instance_object = nil)
1037
1077
 
1038
1078
  def valid_collection!(value, caller)
1039
1079
  if collection_instance?(value) && !collection? &&
1040
- !value_policy.whole_value?(unresolved_type)
1080
+ !value_policy.whole_value?(unresolved_type) &&
1081
+ !polymorphic_member_array?(value)
1041
1082
  raise Lutaml::Model::CollectionTrueMissingError.new(name,
1042
1083
  caller)
1043
1084
  end
@@ -1193,6 +1234,14 @@ instance_object = nil)
1193
1234
  "not build one (received #{casted.class})"
1194
1235
  end
1195
1236
 
1237
+ # A polymorphic list with a scalar member (Type::String, ...)
1238
+ # accepts a listed scalar verbatim: the member type owns the
1239
+ # value, so the content-coercion contract below does not apply
1240
+ # (#1292 - the declaration explicitly allows the scalar).
1241
+ if serializable_type?(register) && polymorphic_scalar_member?(value)
1242
+ return Type.lookup(polymorphic_member_type_for(value)).cast(value)
1243
+ end
1244
+
1196
1245
  # The 0.8.x scalar-coercion contract (#1228): a plain string for
1197
1246
  # a mapped-model attribute whose type does not own a scalar
1198
1247
  # .cast coerces onto the type's `content` mapping (title:
@@ -1279,6 +1328,7 @@ instance_object = nil)
1279
1328
 
1280
1329
  def process_options!
1281
1330
  validate_options!(@options)
1331
+ validate_polymorphic_entries!(@options[:polymorphic])
1282
1332
  if union?
1283
1333
  @options[:union_member_types] =
1284
1334
  Lutaml::Model::Type::Union.validate_members!(@options[:union_member_types])
@@ -1649,10 +1699,37 @@ instance_object = nil)
1649
1699
  "Invalid type: #{type}, must be a Symbol, String or a Class"
1650
1700
  end
1651
1701
 
1652
- def valid_polymorphic_type?(value, resolved_type)
1702
+ # Validation agrees with the serializer. A listed scalar member admits
1703
+ # scalars and arrays of them (polymorphic_scalar_member?) and, since
1704
+ # the nested lane takes any model instance under the declared base,
1705
+ # model instances too; a list of model classes only restricts them to
1706
+ # the listed classes. The old predicate compared entries to
1707
+ # value.class (Type::String is never a String) and then demanded the
1708
+ # declared base type, so every admitted value failed here.
1709
+ def valid_polymorphic_type?(value)
1653
1710
  return value.is_a?(type) unless has_polymorphic_list?
1654
1711
 
1655
- options[:polymorphic].include?(value.class) && value.is_a?(resolved_type)
1712
+ return true if polymorphic_scalar_member?(value)
1713
+ return false unless value.is_a?(Serialize)
1714
+
1715
+ list = options[:polymorphic]
1716
+ list.any? { |member| member.is_a?(Class) && member < Type::Value } ||
1717
+ list.any? { |entry| entry.is_a?(Class) && value.is_a?(entry) }
1718
+ end
1719
+
1720
+ # Definition-time contract for the polymorphic list: an entry that can
1721
+ # never match anything — a Symbol other than :array — fails the
1722
+ # declaration instead of surfacing at serialization (#1292).
1723
+ def validate_polymorphic_entries!(entries)
1724
+ return unless entries.is_a?(Array)
1725
+
1726
+ entries.each do |entry|
1727
+ next if entry.is_a?(Class) || entry == :array
1728
+
1729
+ raise ArgumentError,
1730
+ "polymorphic entry #{entry.inspect} is not a type: use a " \
1731
+ "class or :array"
1732
+ end
1656
1733
  end
1657
1734
 
1658
1735
  def has_polymorphic_list?
@@ -206,6 +206,15 @@ module Lutaml
206
206
  # yields each resolved element-order item with its owning schema.
207
207
  def each_schema_item(schemas, &dispatch)
208
208
  schemas.each do |schema|
209
+ # Override-applied documents ride the override collection
210
+ # alongside the Override declarations themselves (the mixed
211
+ # shape the include/import collections already have); only
212
+ # the applied Schema objects carry a walkable element order.
213
+ if schema.respond_to?(:override) && schema.override&.any?
214
+ schema.override.grep(Lutaml::Xml::Schema::Xsd::Schema).each do |applied|
215
+ each_schema_item([applied], &dispatch)
216
+ end
217
+ end
209
218
  if schema.include&.any?
210
219
  each_schema_item(schema.include,
211
220
  &dispatch)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.197"
5
+ VERSION = "0.8.199"
6
6
  end
7
7
  end
@@ -10,6 +10,7 @@ module Lutaml
10
10
  attribute :min_occurs, :string
11
11
  attribute :annotation, :annotation
12
12
  attribute :element, :element, collection: true, initialize_empty: true
13
+ attribute :any, :any, collection: true, initialize_empty: true
13
14
 
14
15
  xml do
15
16
  element "all"
@@ -21,6 +22,7 @@ module Lutaml
21
22
  map_attribute :minOccurs, to: :min_occurs
22
23
  map_element :annotation, to: :annotation
23
24
  map_element :element, to: :element
25
+ map_element :any, to: :any
24
26
  end
25
27
 
26
28
  Lutaml::Xml::Schema::Xsd.register_model(self, :all)
@@ -7,6 +7,7 @@ module Lutaml
7
7
  class Key < Base
8
8
  attribute :id, :string
9
9
  attribute :name, :string
10
+ attribute :ref, :string
10
11
  attribute :selector, :selector
11
12
  attribute :annotation, :annotation
12
13
  attribute :field, :field, collection: true, initialize_empty: true
@@ -19,6 +20,7 @@ module Lutaml
19
20
 
20
21
  map_attribute :id, to: :id
21
22
  map_attribute :name, to: :name
23
+ map_attribute :ref, to: :ref
22
24
  map_element :annotation, to: :annotation
23
25
  map_element :selector, to: :selector
24
26
  map_element :field, to: :field
@@ -7,6 +7,7 @@ module Lutaml
7
7
  class Keyref < Base
8
8
  attribute :id, :string
9
9
  attribute :name, :string
10
+ attribute :ref, :string
10
11
  attribute :refer, :string
11
12
  attribute :selector, :selector
12
13
  attribute :annotation, :annotation
@@ -20,6 +21,7 @@ module Lutaml
20
21
 
21
22
  map_attribute :id, to: :id
22
23
  map_attribute :name, to: :name
24
+ map_attribute :ref, to: :ref
23
25
  map_attribute :refer, to: :refer
24
26
  map_element :field, to: :field
25
27
  map_element :selector, to: :selector
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Xml
5
+ module Schema
6
+ module Xsd
7
+ # xs:override (XSD 1.1 §4.2.4): a schema-wide component
8
+ # replacement mechanism — the referenced document's components
9
+ # with matching names are replaced by the children carried
10
+ # here (#1286).
11
+ class Override < Base
12
+ # The component kinds an override may replace, keyed by the
13
+ # attribute carrying the component name.
14
+ COMPONENT_KINDS = {
15
+ element: :name,
16
+ attribute: :name,
17
+ notation: :name,
18
+ simple_type: :name,
19
+ complex_type: :name,
20
+ group: :name,
21
+ attribute_group: :name,
22
+ }.freeze
23
+
24
+ attribute :id, :string
25
+ attribute :schema_path, :string
26
+ attribute :annotation, :annotation
27
+ attribute :element, :element, collection: true, initialize_empty: true
28
+ attribute :attribute, :attribute, collection: true, initialize_empty: true
29
+ attribute :notation, :notation, collection: true, initialize_empty: true
30
+ attribute :simple_type, :simple_type, collection: true, initialize_empty: true
31
+ attribute :complex_type, :complex_type, collection: true, initialize_empty: true
32
+ attribute :group, :group, collection: true, initialize_empty: true
33
+ attribute :attribute_group, :attribute_group, collection: true,
34
+ initialize_empty: true
35
+
36
+ xml do
37
+ element "override"
38
+ mixed_content
39
+ namespace Lutaml::Xml::Schema::XsdNamespace
40
+
41
+ map_attribute :id, to: :id
42
+ map_attribute :schemaLocation, to: :schema_path
43
+ map_element :annotation, to: :annotation
44
+ map_element :element, to: :element
45
+ map_element :attribute, to: :attribute
46
+ map_element :notation, to: :notation
47
+ map_element :simpleType, to: :simple_type
48
+ map_element :complexType, to: :complex_type
49
+ map_element :group, to: :group
50
+ map_element :attributeGroup, to: :attribute_group
51
+ end
52
+
53
+ # The referenced document, read through the same location
54
+ # machinery includes and imports use (the assembly pass).
55
+ def fetch_schema
56
+ Glob.include_schema(schema_path) if schema_path && Glob.location?
57
+ end
58
+
59
+ # XSD 1.1 §4.2.4: the referenced document's components are
60
+ # replaced by this override's same-named children. Matching is
61
+ # by component kind and name; a child with no counterpart adds
62
+ # to the referenced document (lenient); children without a name
63
+ # (ref-form, anonymous) carry no replacement. Applied on a copy
64
+ # of the referenced schema so the override stays local to the
65
+ # referencing document.
66
+ def apply_to!(target_schema)
67
+ COMPONENT_KINDS.each do |kind, name_attr|
68
+ Array(public_send(kind)).each do |replacement|
69
+ name = replacement.public_send(name_attr)
70
+ next if name.nil?
71
+
72
+ components = target_schema.public_send(kind)
73
+ index = components.index do |component|
74
+ component.public_send(name_attr) == name
75
+ end
76
+ if index
77
+ components[index] = replacement
78
+ else
79
+ components << replacement
80
+ end
81
+ end
82
+ end
83
+ target_schema
84
+ end
85
+
86
+ Lutaml::Xml::Schema::Xsd.register_model(self, :override)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -41,6 +41,14 @@ module Lutaml
41
41
  initialize_empty: true
42
42
  attribute :attribute_group, :attribute_group, collection: true,
43
43
  initialize_empty: true
44
+ attribute :override, :override, collection: true,
45
+ initialize_empty: true
46
+ attribute :unique, :unique, collection: true,
47
+ initialize_empty: true
48
+ attribute :key, :key, collection: true,
49
+ initialize_empty: true
50
+ attribute :keyref, :keyref, collection: true,
51
+ initialize_empty: true
44
52
 
45
53
  xml do
46
54
  element "schema"
@@ -56,6 +64,10 @@ module Lutaml
56
64
  map_element :simpleType, to: :simple_type
57
65
  map_element :complexType, to: :complex_type
58
66
  map_element :attributeGroup, to: :attribute_group
67
+ map_element :override, to: :override
68
+ map_element :unique, to: :unique
69
+ map_element :key, to: :key
70
+ map_element :keyref, to: :keyref
59
71
  map_element :import, to: :import,
60
72
  with: { from: :import_from_schema, to: :import_to_schema }
61
73
  map_element :include, to: :include,
@@ -282,6 +294,33 @@ module Lutaml
282
294
  alias attributes attribute
283
295
  alias groups group
284
296
 
297
+ def apply_overrides!(seen = nil)
298
+ seen ||= {}.compare_by_identity
299
+ return self if seen[self]
300
+
301
+ seen[self] = true
302
+ Array(override).each do |entry|
303
+ next unless entry.is_a?(Override) && entry.schema_path
304
+ next if self.class.in_progress?(entry.schema_path)
305
+
306
+ self.class.add_in_progress(entry.schema_path)
307
+ referenced = insert_in_processed_schemas(entry)
308
+ self.class.remove_in_progress(entry.schema_path)
309
+ next unless referenced
310
+
311
+ applied = Lutaml::Model::Utils.deep_dup(referenced)
312
+ entry.apply_to!(applied)
313
+ override(applied)
314
+ applied.apply_overrides!(seen)
315
+ end
316
+ %i[include import].each do |kind|
317
+ Array(public_send(kind)).each do |nested|
318
+ nested.apply_overrides!(seen) if nested.is_a?(Schema)
319
+ end
320
+ end
321
+ self
322
+ end
323
+
285
324
  private
286
325
 
287
326
  def namespace_prefix_for(element, namespace_uri)
@@ -299,6 +338,15 @@ module Lutaml
299
338
  namespaces.uniq
300
339
  end
301
340
 
341
+ # XSD 1.1 s4.2.4 override application (the assembly pass): each
342
+ # override resolves its referenced document through the same
343
+ # location machinery includes use, replaces the same-named
344
+ # components on a COPY (the processed-schemas memo shares
345
+ # originals across references; an override is local to this
346
+ # reference), and appends the applied document to the override
347
+ # collection so the schema walk reaches it in place of the raw
348
+ # document. Runs after the tree is parsed (the override's
349
+ # children are populated), before root assignment.
302
350
  def setup_import_and_include(klass, model, schema, args = {})
303
351
  instance = init_instance_of(klass, schema.attributes || {}, args)
304
352
  annotation_object(instance, schema)
@@ -7,6 +7,7 @@ module Lutaml
7
7
  class Unique < Base
8
8
  attribute :id, :string
9
9
  attribute :name, :string
10
+ attribute :ref, :string
10
11
  attribute :selector, :selector
11
12
  attribute :annotation, :annotation
12
13
  attribute :field, :field, collection: true, initialize_empty: true
@@ -18,6 +19,7 @@ module Lutaml
18
19
 
19
20
  map_attribute :id, to: :id
20
21
  map_attribute :name, to: :name
22
+ map_attribute :ref, to: :ref
21
23
  map_element :annotation, to: :annotation
22
24
  map_element :selector, to: :selector
23
25
  map_element :field, to: :field
@@ -34,7 +34,7 @@ module Lutaml
34
34
  Element Enumeration ExtensionComplexContent ExtensionSimpleContent
35
35
  Field FractionDigits Glob Group Import Include Key Keyref Length List
36
36
  MaxExclusive MaxInclusive MaxLength MaxScale MinExclusive MinInclusive
37
- MinLength MinScale Notation OpenContent Pattern Redefine
37
+ MinLength MinScale Notation OpenContent Override Pattern Redefine
38
38
  RestrictionComplexContent RestrictionSimpleContent
39
39
  RestrictionSimpleType Schema SchemaPath Selector Sequence SimpleContent
40
40
  SimpleType TotalDigits Union Unique WhiteSpace
@@ -102,6 +102,7 @@ module Lutaml
102
102
  autoload :Notation, "#{__dir__}/xsd/notation"
103
103
  autoload :Pattern, "#{__dir__}/xsd/pattern"
104
104
  autoload :Redefine, "#{__dir__}/xsd/redefine"
105
+ autoload :Override, "#{__dir__}/xsd/override"
105
106
  autoload :RestrictionComplexContent,
106
107
  "#{__dir__}/xsd/restriction_complex_content"
107
108
  autoload :RestrictionSimpleContent,
@@ -164,7 +165,9 @@ module Lutaml
164
165
  Schema.reset_processed_schemas unless nested_schema
165
166
 
166
167
  Glob.with_location(location) do
167
- Schema.from_xml(xsd, register: register).assign_root!
168
+ Schema.from_xml(xsd, register: register)
169
+ .apply_overrides!
170
+ .assign_root!
168
171
  end
169
172
  end
170
173
  end
@@ -208,13 +208,16 @@ RSpec.describe Lutaml::KeyValue::Transformation::ValueSerializer do
208
208
  end
209
209
  end
210
210
 
211
- # Helper method to build a mock rule
211
+ # A real compiled rule: the serializer's plan reads the rule's option
212
+ # hash (value map, when_attribute), which a bare double never carries.
212
213
  def build_rule(attribute_type:, attribute_name: :test,
213
- child_transformation: nil)
214
- double("CompiledRule",
215
- attribute_type: attribute_type,
216
- attribute_name: attribute_name,
217
- child_transformation: child_transformation,
218
- collection?: false)
214
+ child_transformation: nil)
215
+ Lutaml::Model::CompiledRule.new(
216
+ attribute_name: attribute_name,
217
+ serialized_name: attribute_name.to_s,
218
+ attribute_type: attribute_type,
219
+ child_transformation: child_transformation,
220
+ mapping_type: :key_value,
221
+ )
219
222
  end
220
223
  end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1292: a polymorphic attribute with a Serializable base
6
+ # rejected every scalar value at serialization time — the declaration
7
+ # explicitly listed Type::String, yet a String raised
8
+ # IncorrectModelError at to_json/to_yaml. The nested-model validation
9
+ # demanded a Serialize instance for every value and never consulted
10
+ # the polymorphic list; the deserialize side routed the scalar into
11
+ # the #1228 content-coercion contract. A listed scalar member now rides
12
+ # the scalar lane on both sides; model-only lists keep rejecting.
13
+ RSpec.describe "polymorphic scalar members under a Serializable base" do
14
+ let(:text_element_class) do
15
+ Class.new(Lutaml::Model::Serializable) do
16
+ attribute :content, Lutaml::Model::Serializable, default: -> { "" },
17
+ polymorphic: [Lutaml::Model::Type::String, :array]
18
+ end
19
+ end
20
+
21
+ let(:model_member_class) do
22
+ Class.new(Lutaml::Model::Serializable) do
23
+ attribute :x, :string
24
+ end
25
+ end
26
+
27
+ let(:strict_class) do
28
+ member = model_member_class
29
+ Class.new(Lutaml::Model::Serializable) do
30
+ attribute :content, Lutaml::Model::Serializable, polymorphic: [member]
31
+ end
32
+ end
33
+
34
+ before do
35
+ stub_const("PolyTextElement", text_element_class)
36
+ stub_const("PolyModelMember", model_member_class)
37
+ stub_const("PolyStrictHolder", strict_class)
38
+ end
39
+
40
+ it "serializes a String member to json" do
41
+ expect(PolyTextElement.new(content: "plain").to_json).to eq('{"content":"plain"}')
42
+ end
43
+
44
+ it "serializes a String member to yaml" do
45
+ expect(PolyTextElement.new(content: "plain").to_yaml.strip).to eq("---\ncontent: plain")
46
+ end
47
+
48
+ it "serializes an array member" do
49
+ expect(PolyTextElement.new(content: %w[a b]).to_json).to eq('{"content":["a","b"]}')
50
+ end
51
+
52
+ it "deserializes a String member verbatim" do
53
+ expect(PolyTextElement.from_json('{"content":"plain"}').content).to eq("plain")
54
+ end
55
+
56
+ it "deserializes an array member verbatim" do
57
+ expect(PolyTextElement.from_json('{"content":["a","b"]}').content).to eq(%w[a b])
58
+ end
59
+
60
+ it "round-trips a String member" do
61
+ out = PolyTextElement.new(content: "plain").to_json
62
+ expect(PolyTextElement.from_json(out).to_json).to eq(out)
63
+ end
64
+
65
+ it "still serializes model members under the same declaration" do
66
+ nested = PolyTextElement.new(content: "nested")
67
+ expect(PolyTextElement.new(content: nested).to_json).to eq('{"content":{"content":"nested"}}')
68
+ end
69
+
70
+ it "keeps rejecting scalars under a model-only polymorphic list" do
71
+ expect { PolyStrictHolder.new(content: "plain").to_json }
72
+ .to raise_error(Lutaml::Model::IncorrectModelError)
73
+ end
74
+
75
+ it "serializes model members under a model-only list" do
76
+ expect(PolyStrictHolder.new(content: PolyModelMember.new(x: "v")).to_json)
77
+ .to eq('{"content":{"x":"v"}}')
78
+ end
79
+
80
+ # The list is the attribute's contract at every boundary: validation
81
+ # agrees with the serializer, a mixed member array rides item by item,
82
+ # and an entry that can never match fails the declaration.
83
+ describe "the list as the attribute's contract" do
84
+ it "validates the members the serializer admits" do
85
+ expect(PolyTextElement.new(content: "plain").validate).to eq([])
86
+ expect(PolyTextElement.new(content: ["a"]).validate).to eq([])
87
+ expect(PolyTextElement.new(content: ["a", PolyModelMember.new(x: "v")]).validate).to eq([])
88
+ expect(PolyStrictHolder.new(content: PolyModelMember.new(x: "v")).validate).to eq([])
89
+ end
90
+
91
+ it "still reports a scalar under a model-only list" do
92
+ errors = PolyStrictHolder.new(content: "plain").validate
93
+ expect(errors.first).to be_a(Lutaml::Model::PolymorphicError)
94
+ end
95
+
96
+ it "serializes and deserializes a mixed member array item by item" do
97
+ json = PolyTextElement.new(content: ["a", PolyTextElement.new(content: "b")]).to_json
98
+
99
+ expect(JSON.parse(json)).to eq("content" => ["a", { "content" => "b" }])
100
+ back = PolyTextElement.from_json(json).content
101
+ expect(back.first).to eq("a")
102
+ expect(back.last).to be_a(Lutaml::Model::Serializable)
103
+ end
104
+
105
+ it "raises at class definition for an entry that names no type" do
106
+ expect do
107
+ Class.new(Lutaml::Model::Serializable) do
108
+ attribute :content, Lutaml::Model::Serializable, polymorphic: [:frobnicate]
109
+ end
110
+ end.to raise_error(ArgumentError, /polymorphic entry :frobnicate is not a type/)
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1287: xs:all drops xs:any members — XSD 1.1 (§3.8.2)
6
+ # permits wildcards as xs:all members, and the All model mapped
7
+ # element only, so the wildcard member was erased at parse and the
8
+ # content model lost its absorbing slot (the W3C suite zing shape).
9
+ RSpec.describe "xs:all wildcard members" do
10
+ let(:source) do
11
+ <<~XML
12
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
13
+ <xs:complexType name="zing">
14
+ <xs:all>
15
+ <xs:element ref="a" minOccurs="0"/>
16
+ <xs:element name="b" type="xs:string" minOccurs="0"/>
17
+ <xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>
18
+ </xs:all>
19
+ </xs:complexType>
20
+ </xs:schema>
21
+ XML
22
+ end
23
+
24
+ let(:root) { Lutaml::Xml::Schema::Xsd.parse(source, register: :xsd) }
25
+ let(:all_node) { root.complex_type.first.all }
26
+
27
+ it "parses the wildcard member alongside the elements" do
28
+ expect(all_node.element.first.ref).to eq("a")
29
+ expect(all_node.element.last.name).to eq("b")
30
+ expect(all_node.any).not_to be_empty
31
+ end
32
+
33
+ it "keeps the wildcard's occurrences and processContents" do
34
+ any_member = all_node.any.first
35
+ expect(any_member.min_occurs).to eq("0")
36
+ expect(any_member.max_occurs).to eq("unbounded")
37
+ expect(any_member.process_contents).to eq("skip")
38
+ end
39
+
40
+ it "round-trips the wildcard member through to_xml" do
41
+ out = root.to_xml
42
+ expect(out).to include('<xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/>')
43
+ expect(out).to include('<xs:element ref="a" minOccurs="0"/>')
44
+ end
45
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1285: xs:key/xs:unique/xs:keyref drop the `ref` attribute
6
+ # — XSD 1.1 §3.11.2 permits identity constraints by reference (a
7
+ # top-level constraint definition reached via ref), and the reference
8
+ # was erased at parse, so the referring element carried no evaluable
9
+ # constraint (the W3C suite Id/id040 + id043 shape).
10
+ RSpec.describe "identity constraints by reference" do
11
+ let(:source) do
12
+ <<~XML
13
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
14
+ xmlns:s="http://www.example.com/schema">
15
+ <xs:element name="appx" type="s:chap">
16
+ <xs:unique ref="s:unique-clause"/>
17
+ <xs:key ref="s:key-clause"/>
18
+ <xs:keyref name="kr" refer="s:key-clause">
19
+ <xs:selector xpath="s:section"/>
20
+ <xs:field xpath="@nr"/>
21
+ </xs:keyref>
22
+ </xs:element>
23
+ <xs:unique name="unique-clause">
24
+ <xs:selector xpath="s:section"/>
25
+ <xs:field xpath="@nr"/>
26
+ </xs:unique>
27
+ <xs:key name="key-clause">
28
+ <xs:selector xpath="s:section"/>
29
+ <xs:field xpath="@nr"/>
30
+ </xs:key>
31
+ </xs:schema>
32
+ XML
33
+ end
34
+
35
+ let(:root) { Lutaml::Xml::Schema::Xsd.parse(source, register: :xsd) }
36
+ let(:appx) { root.element.first }
37
+
38
+ it "parses the ref on a referenced xs:unique" do
39
+ unique = appx.unique.first
40
+ expect(unique.ref).to eq("s:unique-clause")
41
+ end
42
+
43
+ it "parses the ref on a referenced xs:key" do
44
+ key = appx.key.first
45
+ expect(key.ref).to eq("s:key-clause")
46
+ end
47
+
48
+ it "round-trips the ref spelling through to_xml" do
49
+ out = root.to_xml
50
+ expect(out).to include('<xs:unique ref="s:unique-clause"/>')
51
+ expect(out).to include('<xs:key ref="s:key-clause"/>')
52
+ end
53
+
54
+ it "keeps the named top-level constraint definitions intact" do
55
+ out = root.to_xml
56
+ expect(out).to include('<xs:unique name="unique-clause">')
57
+ expect(out).to include('<xs:key name="key-clause">')
58
+ expect(out).to include('<xs:keyref name="kr" refer="s:key-clause">')
59
+ end
60
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "tmpdir"
5
+ require "fileutils"
6
+
7
+ # lutaml-model#1286 part 2: override APPLICATION in the schema assembly.
8
+ # The Override model (the parse substrate) is pinned in xsd_override_spec;
9
+ # these cells pin the assembly pass — the referenced document's
10
+ # same-named components are replaced by the override's children
11
+ # (XSD 1.1 §4.2.4), on a copy so the override stays local to the
12
+ # referencing document, and the compiler's schema walk reaches the
13
+ # applied document in place of the raw one.
14
+ RSpec.describe "xs:override application in the assembly" do
15
+ let(:dir) { Dir.mktmpdir }
16
+
17
+ let(:base_schema) do
18
+ <<~XSD
19
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
20
+ <xs:element name="para" type="xs:string"/>
21
+ <xs:element name="other" type="xs:string"/>
22
+ </xs:schema>
23
+ XSD
24
+ end
25
+
26
+ let(:main_schema) do
27
+ <<~XSD
28
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
29
+ <xs:override schemaLocation="base.xsd">
30
+ <xs:element name="para" type="xs:dateTime"/>
31
+ </xs:override>
32
+ </xs:schema>
33
+ XSD
34
+ end
35
+
36
+ before do
37
+ File.write(File.join(dir, "base.xsd"), base_schema)
38
+ end
39
+
40
+ after do
41
+ FileUtils.rm_rf(dir)
42
+ end
43
+
44
+ def assembled
45
+ Lutaml::Xml::Schema::Xsd.parse(main_schema, location: dir, register: :xsd)
46
+ end
47
+
48
+ it "resolves the referenced document and applies the replacement" do
49
+ applied = assembled.override.grep(Lutaml::Xml::Schema::Xsd::Schema).first
50
+ expect(applied).not_to be_nil
51
+
52
+ para = applied.element.find { |e| e.name == "para" }
53
+ expect(para.type).to eq("xs:dateTime")
54
+ end
55
+
56
+ it "keeps the non-overridden components of the referenced document" do
57
+ applied = assembled.override.grep(Lutaml::Xml::Schema::Xsd::Schema).first
58
+ expect(applied.element.map(&:name)).to include("other")
59
+ end
60
+
61
+ it "applies on a copy, leaving the raw referenced document intact" do
62
+ root = assembled
63
+ applied = root.override.grep(Lutaml::Xml::Schema::Xsd::Schema).first
64
+ raw = begin
65
+ Lutaml::Xml::Schema::Xsd::Schema.schema_processed(
66
+ File.join(dir, "base.xsd"),
67
+ )
68
+ rescue StandardError
69
+ nil
70
+ end
71
+ expect(applied.element.find { |e| e.name == "para" }.type).to eq("xs:dateTime")
72
+ expect(raw).to be_nil.or(satisfy { |s| s.element.find { |e| e.name == "para" }&.type == "xs:string" })
73
+ end
74
+
75
+ it "generates models from the overridden components end to end" do
76
+ out_dir = Dir.mktmpdir
77
+ begin
78
+ Lutaml::Model::Schema::XmlCompiler.to_models(
79
+ main_schema,
80
+ location: dir,
81
+ output_dir: out_dir,
82
+ create_files: true,
83
+ module_namespace: "OverrideAssemblySpec",
84
+ )
85
+ require File.join(out_dir, "overrideassemblyspec_registry.rb")
86
+ expect(defined?(OverrideAssemblySpec)).to eq("constant")
87
+ registry = File.read(File.join(out_dir, "overrideassemblyspec_registry.rb"))
88
+ expect(registry).to include("OverrideAssemblySpec")
89
+ ensure
90
+ FileUtils.rm_rf(out_dir)
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1286: xs:override (XSD 1.1 §4.2.4) — the schema-wide
6
+ # component replacement mechanism — was absent from the Schema model,
7
+ # so the roundtrip erased both the schemaLocation target and the
8
+ # replacing components (the W3C suite saxonData/Override shape).
9
+ RSpec.describe "xs:override" do
10
+ let(:source) do
11
+ <<~XML
12
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
13
+ <xs:override schemaLocation="over002a.xsd">
14
+ <xs:element name="para" type="xs:dateTime"/>
15
+ </xs:override>
16
+ <xs:element name="kept" type="xs:string"/>
17
+ </xs:schema>
18
+ XML
19
+ end
20
+
21
+ let(:root) { Lutaml::Xml::Schema::Xsd.parse(source, register: :xsd) }
22
+
23
+ it "parses the override with its schemaLocation target" do
24
+ expect(root.override).not_to be_empty
25
+ expect(root.override.first.schema_path).to eq("over002a.xsd")
26
+ end
27
+
28
+ it "carries the replacing components" do
29
+ override = root.override.first
30
+ expect(override.element.size).to eq(1)
31
+ expect(override.element.first.name).to eq("para")
32
+ expect(override.element.first.type).to eq("xs:dateTime")
33
+ end
34
+
35
+ it "keeps the non-overridden siblings" do
36
+ expect(root.element.first.name).to eq("kept")
37
+ end
38
+
39
+ it "round-trips the whole override through to_xml" do
40
+ out = root.to_xml
41
+ expect(out).to include('<xs:override schemaLocation="over002a.xsd">')
42
+ expect(out).to include('<xs:element name="para" type="xs:dateTime"/>')
43
+ expect(out).to include('<xs:element name="kept" type="xs:string"/>')
44
+ end
45
+
46
+ it "carries multiple replacing component kinds" do
47
+ multi = <<~XML
48
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
49
+ <xs:override schemaLocation="o.xsd">
50
+ <xs:complexType name="t1"/>
51
+ <xs:attribute name="a1" type="xs:string"/>
52
+ <xs:group ref="g1"/>
53
+ </xs:override>
54
+ </xs:schema>
55
+ XML
56
+ root_multi = Lutaml::Xml::Schema::Xsd.parse(multi, register: :xsd)
57
+ override = root_multi.override.first
58
+ expect(override.complex_type.first.name).to eq("t1")
59
+ expect(override.attribute.first.name).to eq("a1")
60
+ expect(override.group.first.ref).to eq("g1")
61
+ end
62
+ 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.197
4
+ version: 0.8.199
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-12-01 00:00:00.000000000 Z
11
+ date: 2026-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -1034,6 +1034,7 @@ files:
1034
1034
  - lib/lutaml/xml/schema/xsd/namespace_uri_remapping.rb
1035
1035
  - lib/lutaml/xml/schema/xsd/notation.rb
1036
1036
  - lib/lutaml/xml/schema/xsd/open_content.rb
1037
+ - lib/lutaml/xml/schema/xsd/override.rb
1037
1038
  - lib/lutaml/xml/schema/xsd/pattern.rb
1038
1039
  - lib/lutaml/xml/schema/xsd/redefine.rb
1039
1040
  - lib/lutaml/xml/schema/xsd/restriction_complex_content.rb
@@ -2143,6 +2144,7 @@ files:
2143
2144
  - spec/lutaml/model/parse_dispatch_hoist_spec.rb
2144
2145
  - spec/lutaml/model/parsed_model_mutation_spec.rb
2145
2146
  - spec/lutaml/model/phantom_value_spec.rb
2147
+ - spec/lutaml/model/polymorphic_scalar_member_spec.rb
2146
2148
  - spec/lutaml/model/polymorphic_spec.rb
2147
2149
  - spec/lutaml/model/processing_instruction_spec.rb
2148
2150
  - spec/lutaml/model/raw_adapter_require_spec.rb
@@ -2423,6 +2425,10 @@ files:
2423
2425
  - spec/lutaml/xml/schema/xsd/vc_versioning_spec.rb
2424
2426
  - spec/lutaml/xml/schema/xsd/xsd_11_constructs_spec.rb
2425
2427
  - spec/lutaml/xml/schema/xsd/xsd_spec.rb
2428
+ - spec/lutaml/xml/schema/xsd_all_any_spec.rb
2429
+ - spec/lutaml/xml/schema/xsd_identity_ref_spec.rb
2430
+ - spec/lutaml/xml/schema/xsd_override_assembly_spec.rb
2431
+ - spec/lutaml/xml/schema/xsd_override_spec.rb
2426
2432
  - spec/lutaml/xml/schema/xsd_scale_vc_spec.rb
2427
2433
  - spec/lutaml/xml/schema_primer_spec.rb
2428
2434
  - spec/lutaml/xml/serializable_namespace_spec.rb