lutaml-model 0.8.192 → 0.8.194

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.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.192"
5
+ VERSION = "0.8.194"
6
6
  end
7
7
  end
@@ -40,10 +40,11 @@ module Lutaml
40
40
  }
41
41
  )
42
42
 
43
- # Date — ISO8601 with optional timezone
43
+ # Date — ISO8601 with optional timezone; delegates to the
44
+ # resolved type class (subclass serialize overrides fire)
44
45
  v.register_format_type_serializer(
45
46
  :toml, Lutaml::Model::Type::Date,
46
- to: ->(inst) { Lutaml::Model::Type::Date.serialize(inst.value) }
47
+ to: ->(inst) { inst.class.serialize(inst.value) }
47
48
  )
48
49
 
49
50
  # TimeWithoutDate — HH:MM:SS.mmm with milliseconds
@@ -62,22 +62,24 @@ module Lutaml
62
62
  }
63
63
  )
64
64
 
65
- # Date — ISO8601 with Z for UTC
65
+ # Date — ISO8601 with Z for UTC. Delegates to the resolved
66
+ # type class so a subclass's own self.serialize override
67
+ # reaches to_xml (issue #1263).
66
68
  v.register_format_type_serializer(
67
69
  :xml, Lutaml::Model::Type::Date,
68
70
  to: lambda { |inst|
69
71
  return nil unless inst.value
70
72
 
71
- result = Lutaml::Model::Type::Date.serialize(inst.value)
73
+ result = inst.class.serialize(inst.value)
72
74
  result = result.sub(/\+00:00$/, "Z") if result.include?("+00:00")
73
75
  result
74
76
  }
75
77
  )
76
78
 
77
- # TimeWithoutDate — delegates to self.class.serialize
79
+ # TimeWithoutDate — delegates to the resolved type class
78
80
  v.register_format_type_serializer(
79
81
  :xml, Lutaml::Model::Type::TimeWithoutDate,
80
- to: ->(inst) { Lutaml::Model::Type::TimeWithoutDate.serialize(inst.value) }
82
+ to: ->(inst) { inst.class.serialize(inst.value) }
81
83
  )
82
84
 
83
85
  # Symbol — :symbol: format
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "tmpdir"
5
+ require "securerandom"
6
+
7
+ # When a Serializable declares an attribute with a string type that points
8
+ # into a namespace using autoload + lazy per-file registration, resolving
9
+ # just the leaf constant via const_get leaves sibling types unregistered.
10
+ # Deserialization of the root then fails because child element types are
11
+ # missing from the registry.
12
+ #
13
+ # The fix in `Lutaml::Model::Attribute.cast_from_string!` walks each parent
14
+ # namespace segment of the string type and force-resolves its constants so
15
+ # autoloads fire and per-file registration side-effects complete.
16
+
17
+ RSpec.describe "string-type resolution with namespace autoload" do
18
+ # Build a throwaway namespace with autoloads pointing at files that
19
+ # self-register on load. This mirrors the pattern used by gems like
20
+ # `mml` (where each `lib/mml/v3/<tag>.rb` ends with
21
+ # `Configuration.register_model(...)`).
22
+ def build_namespace(name)
23
+ Object.const_set(name, Module.new)
24
+
25
+ ns = Object.const_get(name)
26
+ ns.const_set(:REGISTRY, {})
27
+ ns.const_set(:LOADED_FILES, [])
28
+ ns.singleton_class.define_method(:loaded_files) { ns::LOADED_FILES }
29
+ ns.singleton_class.define_method(:registry) { ns::REGISTRY }
30
+
31
+ tmpdir = Dir.mktmpdir("lutaml-autoload-#{name.downcase}")
32
+
33
+ root_file = File.join(tmpdir, "root.rb")
34
+ sibling_file = File.join(tmpdir, "sibling.rb")
35
+
36
+ File.write(root_file, <<~RUBY)
37
+ #{name}::LOADED_FILES << "root"
38
+ #{name}::REGISTRY[:root] = Class.new
39
+ #{name}.const_set(:Root, #{name}::REGISTRY[:root])
40
+ RUBY
41
+
42
+ File.write(sibling_file, <<~RUBY)
43
+ #{name}::LOADED_FILES << "sibling"
44
+ #{name}::REGISTRY[:sibling] = Class.new
45
+ #{name}.const_set(:Sibling, #{name}::REGISTRY[:sibling])
46
+ RUBY
47
+
48
+ ns.autoload(:Root, root_file)
49
+ ns.autoload(:Sibling, sibling_file)
50
+ ns
51
+ end
52
+
53
+ # Each spec builds a uniquely-named namespace (via SecureRandom), so
54
+ # cross-test cleanup is unnecessary. Constants leak for the duration of
55
+ # the process but never collide.
56
+
57
+ let(:namespace_name) { :"LutamlAutoloadTest#{SecureRandom.hex(4)}" }
58
+ let(:namespace_module) { Object.const_get(namespace_name) }
59
+
60
+ it "eager-resolves sibling autoloads when a namespaced string type is cast" do
61
+ build_namespace(namespace_name)
62
+ expect(namespace_module.loaded_files).to be_empty
63
+
64
+ Lutaml::Model::Attribute.cast_from_string!("#{namespace_module}::Root")
65
+
66
+ expect(namespace_module.loaded_files).to include("root", "sibling")
67
+ expect(namespace_module.registry.key?(:root)).to be(true)
68
+ expect(namespace_module.registry.key?(:sibling)).to be(true)
69
+ end
70
+
71
+ it "is idempotent across multiple cast_from_string! calls within the same namespace" do
72
+ build_namespace(namespace_name)
73
+
74
+ Lutaml::Model::Attribute.cast_from_string!("#{namespace_module}::Root")
75
+ first_count = namespace_module.loaded_files.length
76
+
77
+ Lutaml::Model::Attribute.cast_from_string!("#{namespace_module}::Sibling")
78
+ expect(namespace_module.loaded_files.length).to eq(first_count)
79
+ end
80
+
81
+ it "raises ArgumentError for unknown types" do
82
+ expect do
83
+ Lutaml::Model::Attribute.cast_from_string!("LutamlAutoloadDoesNotExist::X")
84
+ end.to raise_error(ArgumentError, /Unknown Lutaml::Model::Type/)
85
+ end
86
+ end
@@ -25,7 +25,10 @@ RSpec.describe Lutaml::Model::Type::Date do
25
25
  context "with DateTime object" do
26
26
  let(:value) { DateTime.new(2024, 1, 1, 12, 0, 0) }
27
27
 
28
- it { is_expected.to eq(Date.new(2024, 1, 1)) }
28
+ it "keeps the DateTime verbatim (precision preserved)" do
29
+ expect(cast).to eq(value)
30
+ expect(cast.hour).to eq(12)
31
+ end
29
32
  end
30
33
 
31
34
  context "with Time object" do
@@ -58,9 +61,10 @@ RSpec.describe Lutaml::Model::Type::Date do
58
61
  end
59
62
 
60
63
  it "parses RFC 3339" do
61
- expect(described_class.cast("2024-01-01T12:00:00Z")).to eq(Date.new(
62
- 2024, 1, 1
63
- ))
64
+ result = described_class.cast("2024-01-01T12:00:00Z")
65
+ expect(result).to be_a(DateTime)
66
+ expect(result.hour).to eq(12)
67
+ expect(result.offset).to eq(Rational(0))
64
68
  end
65
69
 
66
70
  it "parses common formats" do
@@ -155,7 +159,7 @@ RSpec.describe Lutaml::Model::Type::Date do
155
159
  let(:value) { DateTime.new(2019, 9, 28, 0, 0, 0, Rational(0)) }
156
160
 
157
161
  it "serializes with +00:00 offset" do
158
- expect(serialize).to eq("2019-09-28+00:00")
162
+ expect(serialize).to eq("2019-09-28T00:00:00+00:00")
159
163
  end
160
164
  end
161
165
 
@@ -163,7 +167,7 @@ RSpec.describe Lutaml::Model::Type::Date do
163
167
  let(:value) { DateTime.new(2019, 12, 2, 0, 0, 0, Rational(8, 24)) }
164
168
 
165
169
  it "serializes with offset" do
166
- expect(serialize).to eq("2019-12-02+08:00")
170
+ expect(serialize).to eq("2019-12-02T00:00:00+08:00")
167
171
  end
168
172
  end
169
173
 
@@ -171,7 +175,23 @@ RSpec.describe Lutaml::Model::Type::Date do
171
175
  let(:value) { DateTime.new(2019, 12, 2, 0, 0, 0, Rational(-5, 24)) }
172
176
 
173
177
  it "serializes with offset" do
174
- expect(serialize).to eq("2019-12-02-05:00")
178
+ expect(serialize).to eq("2019-12-02T00:00:00-05:00")
179
+ end
180
+ end
181
+
182
+ context "with time-of-day (issue #1256)" do
183
+ let(:value) { DateTime.parse("2018-04-15T10:30:00Z") }
184
+
185
+ it "serializes as ISO 8601 dateTime, keeping the time" do
186
+ expect(serialize).to eq("2018-04-15T10:30:00+00:00")
187
+ end
188
+ end
189
+
190
+ context "with fractional seconds" do
191
+ let(:value) { DateTime.parse("2018-04-15T10:30:00.123Z") }
192
+
193
+ it "keeps fractional precision" do
194
+ expect(serialize).to eq("2018-04-15T10:30:00.123+00:00")
175
195
  end
176
196
  end
177
197
 
@@ -191,7 +211,7 @@ RSpec.describe Lutaml::Model::Type::Date do
191
211
  let(:value) { DateTime.new(2019, 9, 28, 0, 0, 0, Rational(0)) }
192
212
 
193
213
  it "uses Z notation for UTC" do
194
- expect(xml_value).to eq("2019-09-28Z")
214
+ expect(xml_value).to eq("2019-09-28T00:00:00Z")
195
215
  end
196
216
  end
197
217
 
@@ -199,7 +219,7 @@ RSpec.describe Lutaml::Model::Type::Date do
199
219
  let(:value) { DateTime.new(2019, 12, 2, 0, 0, 0, Rational(8, 24)) }
200
220
 
201
221
  it "uses offset notation" do
202
- expect(xml_value).to eq("2019-12-02+08:00")
222
+ expect(xml_value).to eq("2019-12-02T00:00:00+08:00")
203
223
  end
204
224
  end
205
225
 
@@ -207,7 +227,15 @@ RSpec.describe Lutaml::Model::Type::Date do
207
227
  let(:value) { DateTime.new(2019, 12, 2, 0, 0, 0, Rational(-5, 24)) }
208
228
 
209
229
  it "uses offset notation" do
210
- expect(xml_value).to eq("2019-12-02-05:00")
230
+ expect(xml_value).to eq("2019-12-02T00:00:00-05:00")
231
+ end
232
+ end
233
+
234
+ context "with time-of-day (issue #1256)" do
235
+ let(:value) { DateTime.parse("2010-10-10T12:21:00Z") }
236
+
237
+ it "round-trips the full instant" do
238
+ expect(xml_value).to eq("2010-10-10T12:21:00Z")
211
239
  end
212
240
  end
213
241
 
@@ -227,7 +255,7 @@ RSpec.describe Lutaml::Model::Type::Date do
227
255
  let(:value) { DateTime.new(2019, 9, 28, 0, 0, 0, Rational(0)) }
228
256
 
229
257
  it "uses +00:00 notation for UTC" do
230
- expect(json_value).to eq("2019-09-28+00:00")
258
+ expect(json_value).to eq("2019-09-28T00:00:00+00:00")
231
259
  end
232
260
  end
233
261
 
@@ -235,7 +263,7 @@ RSpec.describe Lutaml::Model::Type::Date do
235
263
  let(:value) { DateTime.new(2019, 12, 2, 0, 0, 0, Rational(8, 24)) }
236
264
 
237
265
  it "uses offset notation" do
238
- expect(json_value).to eq("2019-12-02+08:00")
266
+ expect(json_value).to eq("2019-12-02T00:00:00+08:00")
239
267
  end
240
268
  end
241
269
 
@@ -0,0 +1,86 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "Type subclass serialize override" do
4
+ # The documented override pattern: subclass a builtin type and
5
+ # redefine self.serialize. The format-serializer table must resolve
6
+ # to the SUBCLASS's own serialize, not the base class's registered
7
+ # static (issue #1263).
8
+ let(:plain_date) do
9
+ Class.new(Lutaml::Model::Type::Date) do
10
+ def self.cast(value, *)
11
+ value.is_a?(String) ? DateTime.parse(value) : value
12
+ end
13
+
14
+ def self.serialize(value, *)
15
+ return value.strftime("%FT%TZ") if value.is_a?(DateTime)
16
+
17
+ value.iso8601
18
+ end
19
+ end
20
+ end
21
+
22
+ let(:model_class) do
23
+ type = plain_date
24
+ Class.new(Lutaml::Model::Serializable) do
25
+ attribute :begins, type
26
+
27
+ xml do
28
+ root "validity"
29
+ map_element "begins", to: :begins
30
+ end
31
+
32
+ json do
33
+ map "begins", to: :begins
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "xml serialization" do
39
+ it "uses the subclass's serialize override" do
40
+ instance = model_class.new(begins: "2010-10-10T12:21:00Z")
41
+
42
+ expect(instance.to_xml).to eq(<<~XML.strip)
43
+ <validity>
44
+ <begins>2010-10-10T12:21:00Z</begins>
45
+ </validity>
46
+ XML
47
+ end
48
+
49
+ it "does not fall back to the base Date truncation" do
50
+ instance = model_class.new(begins: "2010-10-10T12:21:00Z")
51
+
52
+ expect(instance.to_xml).not_to include("2010-10-10Z")
53
+ end
54
+ end
55
+
56
+ describe "json serialization" do
57
+ it "uses the subclass's serialize override" do
58
+ instance = model_class.new(begins: "2010-10-10T12:21:00Z")
59
+
60
+ expect(instance.to_json).to include('"begins":"2010-10-10T12:21:00Z"')
61
+ end
62
+ end
63
+
64
+ describe "base-class behavior is unchanged" do
65
+ let(:base_model_class) do
66
+ Class.new(Lutaml::Model::Serializable) do
67
+ attribute :begins, Lutaml::Model::Type::Date
68
+
69
+ xml do
70
+ root "validity"
71
+ map_element "begins", to: :begins
72
+ end
73
+ end
74
+ end
75
+
76
+ it "serializes a plain Date as xs:date" do
77
+ instance = base_model_class.new(begins: "2010-10-10")
78
+
79
+ expect(instance.to_xml).to eq(<<~XML.strip)
80
+ <validity>
81
+ <begins>2010-10-10</begins>
82
+ </validity>
83
+ XML
84
+ end
85
+ end
86
+ 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.192
4
+ version: 0.8.194
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-28 00:00:00.000000000 Z
11
+ date: 2026-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -246,6 +246,7 @@ files:
246
246
  - docs/_guides/turtle-serialization.adoc
247
247
  - docs/_guides/type-registers-contexts.adoc
248
248
  - docs/_guides/value-transformations.adoc
249
+ - docs/_guides/xml-guide-0.8.0.adoc
249
250
  - docs/_guides/xml-mapping.adoc
250
251
  - docs/_guides/xml-mappings-guide.adoc
251
252
  - docs/_guides/xml-namespace-declarations.adoc
@@ -322,9 +323,16 @@ files:
322
323
  - docs/conversion_caching.adoc
323
324
  - docs/index.adoc
324
325
  - docs/key_value_data_model.adoc
326
+ - docs/missing_xml_features.adoc
325
327
  - docs/model_transforms.adoc
326
328
  - docs/namespace-management.adoc
327
329
  - docs/xml-schema-qualification.md
330
+ - docs/xml_1.0.adoc
331
+ - docs/xml_base.adoc
332
+ - docs/xml_data_types.adoc
333
+ - docs/xml_namespace.adoc
334
+ - docs/xml_schema.adoc
335
+ - docs/xml_xlink.adoc
328
336
  - docs/yamls_sequence.adoc
329
337
  - exe/lutaml-lml-lsp
330
338
  - exe/lutaml-model
@@ -2116,6 +2124,7 @@ files:
2116
2124
  - spec/lutaml/model/model_import_reentry_spec.rb
2117
2125
  - spec/lutaml/model/model_transformer_spec.rb
2118
2126
  - spec/lutaml/model/multiple_mapping_spec.rb
2127
+ - spec/lutaml/model/namespace_autoload_spec.rb
2119
2128
  - spec/lutaml/model/namespace_versioning_spec.rb
2120
2129
  - spec/lutaml/model/one_entry_cache_spec.rb
2121
2130
  - spec/lutaml/model/opal_smoke_spec.rb
@@ -2185,6 +2194,7 @@ files:
2185
2194
  - spec/lutaml/model/type/reference_integration_spec.rb
2186
2195
  - spec/lutaml/model/type/reference_spec.rb
2187
2196
  - spec/lutaml/model/type/string_spec.rb
2197
+ - spec/lutaml/model/type/subclass_serialize_override_spec.rb
2188
2198
  - spec/lutaml/model/type/symbol_spec.rb
2189
2199
  - spec/lutaml/model/type/time_spec.rb
2190
2200
  - spec/lutaml/model/type/time_without_date_spec.rb