lutaml-model 0.8.198 → 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: 52519a6eccfb4c87dee327752e2d4ff471c602a3965e89b4de1c7402d2230928
4
- data.tar.gz: f40139e2b94c4694af956b6785ad01ec59cccbe3024ca1e2e658a0cba6d8f48e
3
+ metadata.gz: 47aaf2a9887e7cda54173eee34113fe951be0a54e62ee9a014b9e1a5f3518922
4
+ data.tar.gz: cd540af8382bcb29af2847fa8fc9bcd3b825bfa437d61d89a1281d8f7b68d1fb
5
5
  SHA512:
6
- metadata.gz: 69512c548fcdcac33af59da6256bdca71460da66d3c575083d272a438eddf618389c78053f60913b0f914f74448bf18f9fdf5f550651928ba05f4eeb5932848b
7
- data.tar.gz: 89c2e28d7c550e15880cd626d5b15e597de8beaee3d804043a1b0ac1955444458a12ee0e9327f9374b18ea3d2981ec078c44d8c75d401195d3d252e3a7bcdd85
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?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.198"
5
+ VERSION = "0.8.199"
6
6
  end
7
7
  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
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.198
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-02 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
@@ -2144,6 +2144,7 @@ files:
2144
2144
  - spec/lutaml/model/parse_dispatch_hoist_spec.rb
2145
2145
  - spec/lutaml/model/parsed_model_mutation_spec.rb
2146
2146
  - spec/lutaml/model/phantom_value_spec.rb
2147
+ - spec/lutaml/model/polymorphic_scalar_member_spec.rb
2147
2148
  - spec/lutaml/model/polymorphic_spec.rb
2148
2149
  - spec/lutaml/model/processing_instruction_spec.rb
2149
2150
  - spec/lutaml/model/raw_adapter_require_spec.rb