lutaml-model 0.8.198 → 0.8.200

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.
@@ -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
@@ -4,14 +4,14 @@ require "spec_helper"
4
4
  require "tmpdir"
5
5
  require "fileutils"
6
6
 
7
- # lutaml-model#1286 part 2: override APPLICATION in the schema assembly.
7
+ # lutaml-model#1286 part 2: override COMPOSITION in the schema assembly.
8
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
9
+ # these cells pin the composition — the referenced document's same-named
10
+ # components are replaced by the override's children (XSD 1.1 §4.2.4) in
11
+ # a view the entry owns, composed against the referenced document by
12
+ # reference so it stays untouched, and the compiler's schema walk reaches
13
+ # the view in place of the raw document.
14
+ RSpec.describe "xs:override composition in the assembly" do
15
15
  let(:dir) { Dir.mktmpdir }
16
16
 
17
17
  let(:base_schema) do
@@ -45,49 +45,126 @@ RSpec.describe "xs:override application in the assembly" do
45
45
  Lutaml::Xml::Schema::Xsd.parse(main_schema, location: dir, register: :xsd)
46
46
  end
47
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
48
+ def view_of(schema)
49
+ schema.override.first.composed
50
+ end
51
51
 
52
- para = applied.element.find { |e| e.name == "para" }
52
+ it "composes the referenced document with the replacement" do
53
+ para = view_of(assembled).element.find { |e| e.name == "para" }
53
54
  expect(para.type).to eq("xs:dateTime")
54
55
  end
55
56
 
56
57
  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")
58
+ expect(view_of(assembled).element.map(&:name)).to include("other")
59
+ end
60
+
61
+ it "leaves the referenced document untouched" do
62
+ assembled
63
+ raw = Lutaml::Xml::Schema::Xsd::Schema.processed_schemas["base.xsd"]
64
+ expect(raw.element.find { |e| e.name == "para" }.type).to eq("xs:string")
65
+ expect(view_of(assembled).element.find { |e| e.name == "para" }.type)
66
+ .to eq("xs:dateTime")
59
67
  end
60
68
 
61
- it "applies on a copy, leaving the raw referenced document intact" do
69
+ it "composes a view once per entry, shared by every reference to it" do
62
70
  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
71
+ entry = root.override.first
72
+ expect(view_of(root)).to equal(entry.composed)
73
+ expect(root.override.size).to eq(1)
74
+ end
75
+
76
+ it "yields the replaced component to the compiler's document-order walk" do
77
+ view = view_of(assembled)
78
+ walked = view.resolved_element_order.grep(Lutaml::Xml::Schema::Xsd::Element)
79
+ expect(walked.map { |e| [e.name, e.type] })
80
+ .to eq([["para", "xs:dateTime"], ["other", "xs:string"]])
81
+ end
82
+
83
+ context "with an included document overriding the same target" do
84
+ let(:main_schema) do
85
+ <<~XSD
86
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
87
+ <xs:include schemaLocation="sibling.xsd"/>
88
+ <xs:override schemaLocation="base.xsd">
89
+ <xs:element name="para" type="xs:dateTime"/>
90
+ </xs:override>
91
+ </xs:schema>
92
+ XSD
93
+ end
94
+
95
+ let(:sibling_schema) do
96
+ <<~XSD
97
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
98
+ <xs:override schemaLocation="base.xsd">
99
+ <xs:element name="other" type="xs:integer"/>
100
+ </xs:override>
101
+ </xs:schema>
102
+ XSD
103
+ end
104
+
105
+ before do
106
+ File.write(File.join(dir, "sibling.xsd"), sibling_schema)
107
+ end
108
+
109
+ it "composes each against the same originals within one parse" do
110
+ root = assembled
111
+ sibling = root.include.grep(Lutaml::Xml::Schema::Xsd::Schema).first
112
+
113
+ expect(view_of(root).element.map { |e| [e.name, e.type] })
114
+ .to eq([["para", "xs:dateTime"], ["other", "xs:string"]])
115
+ expect(view_of(sibling).element.map { |e| [e.name, e.type] })
116
+ .to eq([["para", "xs:string"], ["other", "xs:integer"]])
70
117
  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
118
  end
74
119
 
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)
120
+ context "with a replaced complex type" do
121
+ let(:base_schema) do
122
+ <<~XSD
123
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
124
+ <xs:complexType name="paraType">
125
+ <xs:sequence>
126
+ <xs:element name="original" type="xs:string"/>
127
+ </xs:sequence>
128
+ </xs:complexType>
129
+ </xs:schema>
130
+ XSD
131
+ end
132
+
133
+ let(:main_schema) do
134
+ <<~XSD
135
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
136
+ <xs:override schemaLocation="base.xsd">
137
+ <xs:complexType name="paraType">
138
+ <xs:sequence>
139
+ <xs:element name="replacement" type="xs:date"/>
140
+ </xs:sequence>
141
+ </xs:complexType>
142
+ </xs:override>
143
+ </xs:schema>
144
+ XSD
145
+ end
146
+
147
+ it "generates models from the replaced components end to end" do
148
+ out_dir = Dir.mktmpdir
149
+ begin
150
+ Lutaml::Model::Schema::XmlCompiler.to_models(
151
+ main_schema,
152
+ location: dir,
153
+ output_dir: out_dir,
154
+ create_files: true,
155
+ module_namespace: "OverrideAssemblySpec",
156
+ )
157
+ expect(File.read(File.join(out_dir, "overrideassemblyspec_registry.rb")))
158
+ .to include("OverrideAssemblySpec")
159
+ # base.xsd is reachable only through the composed view: the
160
+ # replacement member reaching the sources and the original not is
161
+ # the walk running over the view.
162
+ generated = Dir.glob(File.join(out_dir, "**", "*.rb")).map { |p| File.read(p) }.join
163
+ expect(generated).to include("replacement")
164
+ expect(generated).not_to include("original")
165
+ ensure
166
+ FileUtils.rm_rf(out_dir)
167
+ end
91
168
  end
92
169
  end
93
170
  end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "tmpdir"
5
+ require "fileutils"
6
+
7
+ # lutaml-model#1299: a chase that revisits a location already on the
8
+ # current chain stops. A composed view belongs to its override entry and
9
+ # is shared by every reference to that entry, so a document that
10
+ # overrides back finds the entry either already composed or still on the
11
+ # chain, and never chases its own assembly. XSD 1.1 s4.2.4 allows a
12
+ # document in its own override's target set, composing like a cyclic
13
+ # include.
14
+ RSpec.describe "xs:override chase cycle guard" do
15
+ let(:dir) { Dir.mktmpdir }
16
+
17
+ # W3C saxonData/Override/over023: each document overrides the other;
18
+ # the main document replaces the peer's doc element, the peer's
19
+ # override back carries no children.
20
+ let(:main_schema) do
21
+ <<~XSD
22
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
23
+ <xs:override schemaLocation="over023a.xsd">
24
+ <xs:element name="doc" type="xs:date"/>
25
+ </xs:override>
26
+ </xs:schema>
27
+ XSD
28
+ end
29
+
30
+ let(:peer_schema) do
31
+ <<~XSD
32
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
33
+ <xs:override schemaLocation="over023.xsd"/>
34
+ <xs:element name="doc">
35
+ <xs:complexType>
36
+ <xs:sequence>
37
+ <xs:element name="para" maxOccurs="unbounded"/>
38
+ </xs:sequence>
39
+ </xs:complexType>
40
+ </xs:element>
41
+ </xs:schema>
42
+ XSD
43
+ end
44
+
45
+ before do
46
+ File.write(File.join(dir, "over023.xsd"), main_schema)
47
+ File.write(File.join(dir, "over023a.xsd"), peer_schema)
48
+ end
49
+
50
+ after do
51
+ FileUtils.rm_rf(dir)
52
+ end
53
+
54
+ def assembled
55
+ Lutaml::Xml::Schema::Xsd.parse(main_schema, location: dir, register: :xsd)
56
+ end
57
+
58
+ def view_of(schema)
59
+ schema.override.first.composed
60
+ end
61
+
62
+ it "terminates on a mutual override pair" do
63
+ expect { assembled }.not_to raise_error
64
+ end
65
+
66
+ it "applies each document against the other's originals" do
67
+ view = view_of(assembled)
68
+ doc = view.element.find { |e| e.name == "doc" }
69
+ expect(doc.type).to eq("xs:date")
70
+ # the peer's original keeps its own doc element
71
+ peer = Lutaml::Xml::Schema::Xsd::Schema.processed_schemas["over023a.xsd"]
72
+ expect(peer.element.find { |e| e.name == "doc" }.type).to be_nil
73
+ end
74
+
75
+ it "ends the chain at the revisit instead of composing it again" do
76
+ view = view_of(assembled)
77
+ back = view_of(view)
78
+ expect(back.element).to be_empty
79
+ # the main document's entry, reached through the peer's view, was on
80
+ # the chain and stays uncomposed
81
+ expect(view.override.first.composed).to equal(back)
82
+ expect(back.override.first.composed).to be_nil
83
+ end
84
+
85
+ it "leaves no location on the chain once the parse completes" do
86
+ assembled
87
+ expect(Lutaml::Xml::Schema::Xsd::Schema.in_progress).to be_empty
88
+ end
89
+
90
+ context "with an include riding the cycle" do
91
+ let(:main_schema) do
92
+ <<~XSD
93
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
94
+ <xs:override schemaLocation="base.xsd">
95
+ <xs:element name="para" type="xs:dateTime"/>
96
+ </xs:override>
97
+ </xs:schema>
98
+ XSD
99
+ end
100
+
101
+ let(:peer_schema) do
102
+ <<~XSD
103
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
104
+ <xs:include schemaLocation="peer.xsd"/>
105
+ <xs:element name="para" type="xs:string"/>
106
+ </xs:schema>
107
+ XSD
108
+ end
109
+
110
+ let(:peer_include) do
111
+ <<~XSD
112
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
113
+ <xs:override schemaLocation="base.xsd">
114
+ <xs:element name="other" type="xs:integer"/>
115
+ </xs:override>
116
+ </xs:schema>
117
+ XSD
118
+ end
119
+
120
+ before do
121
+ File.write(File.join(dir, "base.xsd"), peer_schema)
122
+ File.write(File.join(dir, "peer.xsd"), peer_include)
123
+ end
124
+
125
+ it "terminates when the included document overrides its includer" do
126
+ expect { assembled }.not_to raise_error
127
+ expect(view_of(assembled).element.find { |e| e.name == "para" }.type)
128
+ .to eq("xs:dateTime")
129
+ end
130
+ end
131
+
132
+ context "with a document overriding itself" do
133
+ let(:main_schema) do
134
+ <<~XSD
135
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
136
+ <xs:override schemaLocation="over023.xsd">
137
+ <xs:element name="doc" type="xs:date"/>
138
+ </xs:override>
139
+ <xs:element name="doc" type="xs:string"/>
140
+ </xs:schema>
141
+ XSD
142
+ end
143
+
144
+ it "terminates and applies the replacement" do
145
+ expect { assembled }.not_to raise_error
146
+ expect(view_of(assembled).element.find { |e| e.name == "doc" }.type)
147
+ .to eq("xs:date")
148
+ end
149
+ end
150
+ 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.200
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
@@ -2026,6 +2026,7 @@ files:
2026
2026
  - spec/lutaml/key_value/adapter/toml/teptris_adapter_spec.rb
2027
2027
  - spec/lutaml/key_value/child_map_plan_spec.rb
2028
2028
  - spec/lutaml/key_value/group_instantiate_spec.rb
2029
+ - spec/lutaml/key_value/recursive_plan_spec.rb
2029
2030
  - spec/lutaml/key_value/subclass_remap_spec.rb
2030
2031
  - spec/lutaml/key_value/transformation/collection_serializer_spec.rb
2031
2032
  - spec/lutaml/key_value/transformation/rule_compiler_spec.rb
@@ -2144,6 +2145,7 @@ files:
2144
2145
  - spec/lutaml/model/parse_dispatch_hoist_spec.rb
2145
2146
  - spec/lutaml/model/parsed_model_mutation_spec.rb
2146
2147
  - spec/lutaml/model/phantom_value_spec.rb
2148
+ - spec/lutaml/model/polymorphic_scalar_member_spec.rb
2147
2149
  - spec/lutaml/model/polymorphic_spec.rb
2148
2150
  - spec/lutaml/model/processing_instruction_spec.rb
2149
2151
  - spec/lutaml/model/raw_adapter_require_spec.rb
@@ -2427,6 +2429,7 @@ files:
2427
2429
  - spec/lutaml/xml/schema/xsd_all_any_spec.rb
2428
2430
  - spec/lutaml/xml/schema/xsd_identity_ref_spec.rb
2429
2431
  - spec/lutaml/xml/schema/xsd_override_assembly_spec.rb
2432
+ - spec/lutaml/xml/schema/xsd_override_cycle_spec.rb
2430
2433
  - spec/lutaml/xml/schema/xsd_override_spec.rb
2431
2434
  - spec/lutaml/xml/schema/xsd_scale_vc_spec.rb
2432
2435
  - spec/lutaml/xml/schema_primer_spec.rb