lutaml-model 0.8.195 → 0.8.196

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: 3ad5a94aa9e61c22558746e7a29d19c9dc4ae4670359ea46c3a58e8cee1513dc
4
- data.tar.gz: bb5db6b8392998ea97777d865e1e4b3a034db6faa2f7e62362e75f3d2994cedf
3
+ metadata.gz: 85e0b7e03682f50ff60810fd5635731bdb25ecfaf8459d4aacefe83868add8e8
4
+ data.tar.gz: 618235ad9e8b5fc2ae70e6c3056d04558927de3a86aaa8bb4b129e19e02bc018
5
5
  SHA512:
6
- metadata.gz: 5cb95be5f9492d733a6b5d40d1c6c7d38e9ad1e3f418d88916a8816f20d93597214caa5cf2ac017f3e47cbab9d6fd65bb486cf109bbdd782802555fc2fc5f7a2
7
- data.tar.gz: 7414355c20764590c18b6e8c0289bc70d5d2e6144b6b6b3b9fb1f5dde405704123bd7247315a656f926c66e92ba45e814b606fb3b521f44ac6c092f43d9d6d7c
6
+ metadata.gz: fa2a5aac349920a13a01ec25575d98af17cda245c2a9c8ad95c30b4ca6dd6dc5ee19b62c4f6514ec2182a9f6d4c1c4ff97b6bd3f2978b0d5a87277cc7be91811
7
+ data.tar.gz: b172fe00497c499c054b2705a3624b9d2f7d43997e1ee29ff8a87f50615366073ad9878ab289f02eef03bf71cea3bb8850fe11a3938874f1a6609ddeac0e048a
@@ -413,6 +413,7 @@ require "lutaml/model/serialize/enum_handling"
413
413
  require "lutaml/model/serialize/format_conversion"
414
414
  require "lutaml/model/serialize/initialization"
415
415
  require "lutaml/model/serialize/model_import"
416
+ require "lutaml/model/serialize/tracking_default_hash"
416
417
  require "lutaml/model/serialize/transformation_builder"
417
418
  require "lutaml/model/serialize/value_mapping"
418
419
  require "lutaml/model/services/base"
@@ -123,6 +123,7 @@ module Lutaml
123
123
  # @return [Object] the passed value
124
124
  def record_mutation(attribute_name, value = nil)
125
125
  @__mutated_after_parse__ = true
126
+ propagate_default_mutation!
126
127
  return value unless @__order_tracking__
127
128
 
128
129
  track_order(attribute_name, value, nil)
@@ -144,6 +145,7 @@ module Lutaml
144
145
  # @return [Object] the passed value
145
146
  def record_mutation_collection(attribute_name, value)
146
147
  @__mutated_after_parse__ = true
148
+ propagate_default_mutation!
147
149
  return value unless @__order_tracking__
148
150
  return value if value.nil? || Lutaml::Model::Utils.uninitialized?(value)
149
151
 
@@ -667,10 +667,29 @@ module Lutaml
667
667
  value = if Lutaml::Model::Utils.uninitialized?(default)
668
668
  nil
669
669
  else
670
- attr.cast_value(default, register_id)
670
+ casted = attr.cast_value(default, register_id)
671
+ case casted
672
+ when Serialize::TrackingDefaultHash
673
+ casted
674
+ when ::Hash
675
+ Serialize::TrackingDefaultHash.for(
676
+ casted, instance, name
677
+ )
678
+ else
679
+ casted
680
+ end
671
681
  end
672
682
  instance.public_send(writers[name], value)
673
683
  instance.using_default_for(name)
684
+ # A mutable default handed to the consumer is mutated
685
+ # through its own object (#1272) - the owner's mark never
686
+ # sees it. Register the wiring so the mutation entry
687
+ # point (record_mutation) can clear the owner's mark;
688
+ # Hash defaults carry the tracking wrapper above.
689
+ if value.is_a?(Serialize)
690
+ value.__lutaml_default_owner__ = instance
691
+ value.__lutaml_default_attribute_name__ = name
692
+ end
674
693
  end
675
694
  end
676
695
  reserved = (attrs.key?(:lutaml_register) ? 1 : 0) +
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Model
5
+ module Serialize
6
+ # A Hash that surfaces its own in-place mutation to the model
7
+ # whose constructor default it seeds (#1272).
8
+ #
9
+ # A constructor-defaulted Hash attribute is handed out by the
10
+ # reader and mutated through plain Hash faces (`h[k] = v`) -
11
+ # events the owning model can never see, so the attribute stays
12
+ # using-default and is silently dropped at serialization. The
13
+ # constructor seeds this subclass instead; every mutating face
14
+ # clears the owner's mark (the seed itself runs with the owner
15
+ # unwired, so seeding does not flip the mark).
16
+ #
17
+ # merge!/update bypass the overridden []= (C-level Hash loop),
18
+ # so they are overridden explicitly, as is replace.
19
+ class TrackingDefaultHash < ::Hash
20
+ attr_accessor :__lutaml_default_owner__,
21
+ :__lutaml_default_attribute_name__
22
+
23
+ def self.for(seed, owner, attribute_name)
24
+ hash = new
25
+ hash.replace(seed)
26
+ hash.__lutaml_default_owner__ = owner
27
+ hash.__lutaml_default_attribute_name__ = attribute_name
28
+ hash
29
+ end
30
+
31
+ def []=(key, value)
32
+ super
33
+ __lutaml_default_owner__&.value_set_for(
34
+ __lutaml_default_attribute_name__,
35
+ )
36
+ __lutaml_default_owner__&.propagate_default_mutation!
37
+ value
38
+ end
39
+
40
+ alias store []=
41
+
42
+ def merge!(other)
43
+ super
44
+ __lutaml_default_owner__&.value_set_for(
45
+ __lutaml_default_attribute_name__,
46
+ )
47
+ __lutaml_default_owner__&.propagate_default_mutation!
48
+ self
49
+ end
50
+
51
+ alias update merge!
52
+
53
+ def replace(other)
54
+ super
55
+ __lutaml_default_owner__&.value_set_for(
56
+ __lutaml_default_attribute_name__,
57
+ )
58
+ __lutaml_default_owner__&.propagate_default_mutation!
59
+ self
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -16,6 +16,7 @@ module Lutaml
16
16
  autoload :TransformationBuilder,
17
17
  "#{__dir__}/serialize/transformation_builder"
18
18
  autoload :Builder, "#{__dir__}/serialize/builder"
19
+ autoload :TrackingDefaultHash, "#{__dir__}/serialize/tracking_default_hash"
19
20
 
20
21
  include ComparableModel
21
22
  include Validation
@@ -258,6 +259,35 @@ module Lutaml
258
259
  @using_default[attribute_name] = true
259
260
  end
260
261
 
262
+ # The constructor-defaulted mutable-object backlink (#1272).
263
+ #
264
+ # When `.new` seeds an attribute with a MUTABLE default (a nested
265
+ # Serialize model or a TrackingDefaultHash), in-place mutation
266
+ # through the reader-returned object fires on THAT object -
267
+ # never on the owner - so the owner's using_default? mark
268
+ # survives and the entire attribute is silently dropped at
269
+ # serialization. The seed stamps the backlink on the default
270
+ # object itself (nil on every untouched instance); the single
271
+ # mutation entry point (record_mutation) walks it up the chain.
272
+ attr_accessor :__lutaml_default_owner__,
273
+ :__lutaml_default_attribute_name__
274
+
275
+ # Walk the backlink chain clear of using-default marks: each hop
276
+ # clears its owner's mark for the attribute the object was
277
+ # seeded under. A seen set guards cycles (default lambdas can
278
+ # reference models that carry the same default).
279
+ def propagate_default_mutation!
280
+ seen = nil
281
+ current = self
282
+ while (owner = current.__lutaml_default_owner__)
283
+ owner.value_set_for(current.__lutaml_default_attribute_name__)
284
+ (seen ||= []) << current
285
+ current = owner
286
+ break if seen.include?(current)
287
+ end
288
+ nil
289
+ end
290
+
261
291
  def value_set_for(attribute_name)
262
292
  # Only allocate hash when transitioning from "all defaults" (nil)
263
293
  # Hash.new(true) ensures unset keys still return true
@@ -428,11 +458,36 @@ module Lutaml
428
458
  value = self.class.apply_value_map(value, vmap, attr)
429
459
  # Performance: Only call ensure_utf8 for string values
430
460
  value = self.class.ensure_utf8(value) if transcode_to_utf8?(value, attr)
461
+ if default
462
+ # A constructor-defaulted mutable object is mutated through
463
+ # its own object (#1272) - the owner's mark never sees it.
464
+ # Hash defaults are seeded as a tracking wrapper whose
465
+ # mutating faces clear the owner's mark; nested Serialize
466
+ # models register at the seed below (their own writers are
467
+ # the mutation entry point). The wrapper is re-pointed after
468
+ # the writer: the compiled writer's cast_value duplicates
469
+ # Hash subclasses back into plain Hashes.
470
+ wrapped = case value
471
+ when Serialize::TrackingDefaultHash then value
472
+ when ::Hash
473
+ Serialize::TrackingDefaultHash.for(value, self, name)
474
+ end
475
+ if wrapped && !wrapped.equal?(value)
476
+ public_send(self.class.instantiate_writers(lutaml_register)[name], value)
477
+ using_default_for(name) if default
478
+ instance_variable_set(:"@#{name}", wrapped)
479
+ return value
480
+ end
481
+ end
431
482
  # Interpolating the writer symbol allocated a String per
432
483
  # attribute per instance; the per-register writer table is
433
484
  # interned once per class.
434
485
  public_send(self.class.instantiate_writers(lutaml_register)[name], value)
435
486
  using_default_for(name) if default
487
+ if default && value.is_a?(Serialize)
488
+ value.__lutaml_default_owner__ = self
489
+ value.__lutaml_default_attribute_name__ = name
490
+ end
436
491
  end
437
492
 
438
493
  # A binary String from a non-text type holds bytes, not text:
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.195"
5
+ VERSION = "0.8.196"
6
6
  end
7
7
  end
@@ -88,7 +88,11 @@ module Lutaml
88
88
  end
89
89
 
90
90
  def to_xml(_builder = nil)
91
- @moxml_node.to_xml(declaration: false, expand_empty: false)
91
+ # Verbatim raw capture (#223): the markup must come back byte-for-byte,
92
+ # so serialization opts out of the context's default indent — a
93
+ # formatted emission would compound onto the source's own
94
+ # whitespace-only text nodes and corrupt the captured subtree.
95
+ @moxml_node.to_xml(declaration: false, expand_empty: false, indent: 0)
92
96
  end
93
97
 
94
98
  def build_xml(builder = nil)
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require_relative "../../../lib/lutaml/model"
5
+
6
+ module MutableDefaultSpec
7
+ class Definition < Lutaml::Model::Serializable
8
+ attribute :terms, :string, collection: true
9
+
10
+ yaml do
11
+ map "terms", to: :terms
12
+ end
13
+ end
14
+
15
+ class Concept < Lutaml::Model::Serializable
16
+ attribute :id, :string
17
+ attribute :data, Definition, default: -> { Definition.new }
18
+ attribute :meta, :hash, default: -> { {} }
19
+
20
+ yaml do
21
+ map "id", to: :id
22
+ map "data", to: :data
23
+ map "meta", to: :meta
24
+ end
25
+ end
26
+
27
+ class LocalizedConcept < Lutaml::Model::Serializable
28
+ attribute :id, :string
29
+ attribute :data, Definition, default: -> { Definition.new }
30
+
31
+ yaml do
32
+ map "id", to: :id
33
+ map "data", to: :data
34
+ end
35
+
36
+ # The delegating-setter shape from the issue: a custom writer that
37
+ # writes through the default nested object.
38
+ def language_code=(value)
39
+ data.terms = [value]
40
+ end
41
+
42
+ def language_code
43
+ data.terms&.first
44
+ end
45
+ end
46
+ end
47
+
48
+ RSpec.describe "mutable constructor defaults" do
49
+ describe "nested model default mutated in place" do
50
+ it "serializes the mutation (issue #1272)" do
51
+ concept = MutableDefaultSpec::Concept.new
52
+ concept.data.terms = ["thermal-neutron"]
53
+
54
+ yaml = concept.to_yaml
55
+
56
+ expect(yaml).to include("thermal-neutron")
57
+ end
58
+
59
+ it "still omits an untouched default" do
60
+ concept = MutableDefaultSpec::Concept.new
61
+
62
+ expect(concept.to_yaml).not_to include("data")
63
+ end
64
+
65
+ it "reads the mutation back through the nested reader" do
66
+ concept = MutableDefaultSpec::Concept.new
67
+ concept.data.terms = ["thermal-neutron"]
68
+
69
+ round_tripped = MutableDefaultSpec::Concept.from_yaml(concept.to_yaml)
70
+
71
+ expect(round_tripped.data.terms).to eq(["thermal-neutron"])
72
+ end
73
+ end
74
+
75
+ describe "delegating writer through the default" do
76
+ it "serializes the delegated mutation" do
77
+ concept = MutableDefaultSpec::LocalizedConcept.new
78
+ concept.language_code = "eng"
79
+
80
+ yaml = concept.to_yaml
81
+
82
+ expect(yaml).to include("eng")
83
+ end
84
+ end
85
+
86
+ describe "hash default mutated in place" do
87
+ it "serializes the mutation" do
88
+ concept = MutableDefaultSpec::Concept.new
89
+ concept.meta[:register] = "main"
90
+
91
+ yaml = concept.to_yaml
92
+
93
+ expect(yaml).to include("register")
94
+ end
95
+ end
96
+
97
+ describe "parsed instances keep the absent-omission semantics (#922)" do
98
+ it "keeps a read-but-unmutated absent attribute omitted" do
99
+ concept = MutableDefaultSpec::Concept.from_yaml("id: c1\n")
100
+
101
+ _ = concept.data # the read materializes the default object
102
+ _ = concept.meta
103
+
104
+ yaml = concept.to_yaml
105
+
106
+ expect(yaml).not_to include("data")
107
+ expect(yaml).not_to include("meta")
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # Raw markup capture through a custom `with:` handler (the lane isodoc's
6
+ # BibdataConfig#bibdata_from_xml rides) must return the source subtree
7
+ # byte-for-byte. moxml's document serializer pretty-prints when the
8
+ # context's default indent applies, which compounds onto the source's own
9
+ # whitespace-only text nodes ("<v>\n <x/>" round-tripping as
10
+ # "<v>\n <x/>"); AdapterElement#to_xml opts out of formatting so the
11
+ # verbatim contract holds.
12
+ RSpec.describe "AdapterElement#to_xml verbatim raw markup" do
13
+ let(:handler_class) do
14
+ Class.new(Lutaml::Model::Serializable) do
15
+ attribute :frag, :string
16
+
17
+ xml do
18
+ element "d"
19
+ map_element "frag", to: :frag,
20
+ with: {
21
+ from: :frag_from_xml, to: :frag_to_xml
22
+ }
23
+ end
24
+
25
+ def frag_from_xml(model, node)
26
+ model.frag = node.to_xml
27
+ end
28
+
29
+ def frag_to_xml(_model, _parent, _doc); end
30
+ end
31
+ end
32
+
33
+ it "returns the subtree byte-for-byte when the source is single-spaced" do
34
+ source = "<d><frag>\n <x a=\"1\">inner</x>\n</frag></d>"
35
+ expect(handler_class.from_xml(source).frag)
36
+ .to eq("<frag>\n <x a=\"1\">inner</x>\n</frag>")
37
+ end
38
+
39
+ it "does not compound onto pre-existing indentation on nested sources" do
40
+ source = "<d><frag><v>\n <x>inner</x>\n</v></frag></d>"
41
+ expect(handler_class.from_xml(source).frag)
42
+ .to eq("<frag><v>\n <x>inner</x>\n</v></frag>")
43
+ end
44
+ end
@@ -64,9 +64,9 @@ RSpec.describe "nested model namespace declarations on the plan serialization pa
64
64
 
65
65
  let(:xml) do
66
66
  '<root xmlns="http://example.com/outer">' \
67
- '<symbol>mm</symbol>' \
67
+ "<symbol>mm</symbol>" \
68
68
  '<symbol><math xmlns="http://example.com/inner">' \
69
- '<mi>mm</mi></math></symbol>' \
69
+ "<mi>mm</mi></math></symbol>" \
70
70
  "</root>"
71
71
  end
72
72
 
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.195
4
+ version: 0.8.196
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-30 00:00:00.000000000 Z
11
+ date: 2026-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -760,6 +760,7 @@ files:
760
760
  - lib/lutaml/model/serialize/format_conversion.rb
761
761
  - lib/lutaml/model/serialize/initialization.rb
762
762
  - lib/lutaml/model/serialize/model_import.rb
763
+ - lib/lutaml/model/serialize/tracking_default_hash.rb
763
764
  - lib/lutaml/model/serialize/transformation_builder.rb
764
765
  - lib/lutaml/model/serialize/value_mapping.rb
765
766
  - lib/lutaml/model/services.rb
@@ -2124,6 +2125,7 @@ files:
2124
2125
  - spec/lutaml/model/model_import_reentry_spec.rb
2125
2126
  - spec/lutaml/model/model_transformer_spec.rb
2126
2127
  - spec/lutaml/model/multiple_mapping_spec.rb
2128
+ - spec/lutaml/model/mutable_default_spec.rb
2127
2129
  - spec/lutaml/model/namespace_autoload_spec.rb
2128
2130
  - spec/lutaml/model/namespace_versioning_spec.rb
2129
2131
  - spec/lutaml/model/one_entry_cache_spec.rb
@@ -2278,6 +2280,7 @@ files:
2278
2280
  - spec/lutaml/xml/adapter/ox_adapter_spec.rb
2279
2281
  - spec/lutaml/xml/adapter/rexml_adapter_spec.rb
2280
2282
  - spec/lutaml/xml/adapter/xml_namespace_spec.rb
2283
+ - spec/lutaml/xml/adapter_element_verbatim_spec.rb
2281
2284
  - spec/lutaml/xml/adapter_loader_spec.rb
2282
2285
  - spec/lutaml/xml/adapter_metadata_default_spec.rb
2283
2286
  - spec/lutaml/xml/adapter_moxml_engine_spec.rb