lutaml-model 0.8.202 → 0.8.203

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: 40f311f7fc21d88ef8df8d86e76da0fc15ef1fcbf53c1553d295e9edfcb068a9
4
- data.tar.gz: 853924b3e3c3c486fc36076b4272124eb9571a1d9ecd128ae000e86768c2b9d9
3
+ metadata.gz: d56a455e638084b3c3373c78a2be2d53a759a71601e4b1fc019fa8b289d68087
4
+ data.tar.gz: e6c4b3d25dc38f58a9c8aa4ad2c0c38c2bf0e8fc24da3d149858a43e3cbb1510
5
5
  SHA512:
6
- metadata.gz: 2037c368c6cb876b065c63648cca792c8eeaee64b71fce7345124a5096c9f6996d14f91ddc76c7e4369d7ab68433c53af98bca6c719f3946e5f67e07dd711a53
7
- data.tar.gz: 415d00ae9c95ee6c7a429f40174f1cfb29a84b0878f77a1bd1bfb0a83bdfd6bc61c23713a3294c8dcf4158410e19074953c99955ea1f77943de3a08fe05ae581
6
+ metadata.gz: 1895519a0829dc535771a764109f7ecc3bc58b947a9e0f3bb8b919f1db871ed07027376466f8845d926605941fff42c2663fab8db427b9769d02ad9f2692d25f
7
+ data.tar.gz: a0986405eadf06fa748f91fb53d73a69e3c064988fefe9890dce9d497e75fc529ae42da587cbfabc143dedc12520b58397240127a22c18bb292cd26eacc3dc5b
@@ -47,6 +47,25 @@ jobs:
47
47
  repos-json-file: .github/workflows/dependent-repos.json
48
48
  matrix_ruby: '[{"version":"4.0","rubygems":"latest","bundler":"default","experimental":false}]'
49
49
  matrix_os: ${{ github.event_name == 'pull_request' && '["ubuntu-latest","windows-latest"]' || '' }}
50
+ # metanorma/ci's tool-setup-action retired inkscape and ghostscript
51
+ # (2026-11-12) and ffmpeg (2026-11-26), so those setup-tools names
52
+ # are ignored and ukiryu's specs for them are skipped rather than
53
+ # run. Install them here, the way the retired actions did.
54
+ before-setup-ruby: |
55
+ case "$RUNNER_OS" in
56
+ Linux)
57
+ sudo apt-get update
58
+ sudo apt-get install -y --no-install-recommends inkscape ghostscript ffmpeg
59
+ ;;
60
+ macOS)
61
+ export HOMEBREW_NO_AUTO_UPDATE=1
62
+ HOMEBREW_CASK_OPTS=--no-quarantine brew install --cask inkscape
63
+ brew install ghostscript ffmpeg
64
+ ;;
65
+ Windows)
66
+ choco install --no-progress -y inkscape ghostscript ffmpeg
67
+ ;;
68
+ esac
50
69
  after-setup-ruby: |
51
70
  if [ -f frontend/package.json ]; then cd frontend && npm install && npm run build; fi
52
71
  secrets:
@@ -67,9 +86,28 @@ jobs:
67
86
  # dependent log is attributable at a glance (#1225).
68
87
  command: set +e; echo "spec-files=$(find spec -name '*_spec.rb' 2>/dev/null | wc -l)"; echo "lutaml-model-under-test:"; bundle info lutaml-model 2>/dev/null | sed -n '1,4p'; bundle exec rspec; code=$?; echo "rspec-exit=$code"; exit $code
69
88
  repos-json-file: .github/workflows/dependent-repos-tools.json
70
- setup-tools: 'inkscape,ghostscript,exiftool,ffmpeg,imagemagick,yq'
89
+ setup-tools: 'exiftool,imagemagick,yq'
71
90
  matrix_ruby: '[{"version":"4.0","rubygems":"latest","bundler":"default","experimental":false}]'
72
91
  matrix_os: ${{ github.event_name == 'pull_request' && '["ubuntu-latest","windows-latest"]' || '' }}
92
+ # metanorma/ci's tool-setup-action retired inkscape and ghostscript
93
+ # (2026-11-12) and ffmpeg (2026-11-26), so those setup-tools names
94
+ # are ignored and ukiryu's specs for them are skipped rather than
95
+ # run. Install them here, the way the retired actions did.
96
+ before-setup-ruby: |
97
+ case "$RUNNER_OS" in
98
+ Linux)
99
+ sudo apt-get update
100
+ sudo apt-get install -y --no-install-recommends inkscape ghostscript ffmpeg
101
+ ;;
102
+ macOS)
103
+ export HOMEBREW_NO_AUTO_UPDATE=1
104
+ HOMEBREW_CASK_OPTS=--no-quarantine brew install --cask inkscape
105
+ brew install ghostscript ffmpeg
106
+ ;;
107
+ Windows)
108
+ choco install --no-progress -y inkscape ghostscript ffmpeg
109
+ ;;
110
+ esac
73
111
  after-setup-ruby: |
74
112
  if [ -f frontend/package.json ]; then cd frontend && npm install && npm run build; fi
75
113
  secrets:
@@ -86,9 +124,28 @@ jobs:
86
124
  with:
87
125
  command: set +e; echo "spec-files=$(find spec -name '*_spec.rb' 2>/dev/null | wc -l)"; echo "lutaml-model-under-test:"; bundle info lutaml-model 2>/dev/null | sed -n '1,4p'; bundle exec rspec > rspec-run.log 2>&1; code=$?; echo "informational rspec-exit=$code (non-blocking notice)"; tail -25 rspec-run.log; grep -B2 -A14 "Failure/Error" rspec-run.log | head -160; exit 0
88
126
  repos-json-file: .github/workflows/dependent-repos-tools-informational.json
89
- setup-tools: 'inkscape,ghostscript,exiftool,ffmpeg,imagemagick,yq'
127
+ setup-tools: 'exiftool,imagemagick,yq'
90
128
  matrix_ruby: '[{"version":"4.0","rubygems":"latest","bundler":"default","experimental":false}]'
91
129
  matrix_os: ${{ github.event_name == 'pull_request' && '["ubuntu-latest","windows-latest"]' || '' }}
130
+ # metanorma/ci's tool-setup-action retired inkscape and ghostscript
131
+ # (2026-11-12) and ffmpeg (2026-11-26), so those setup-tools names
132
+ # are ignored and ukiryu's specs for them are skipped rather than
133
+ # run. Install them here, the way the retired actions did.
134
+ before-setup-ruby: |
135
+ case "$RUNNER_OS" in
136
+ Linux)
137
+ sudo apt-get update
138
+ sudo apt-get install -y --no-install-recommends inkscape ghostscript ffmpeg
139
+ ;;
140
+ macOS)
141
+ export HOMEBREW_NO_AUTO_UPDATE=1
142
+ HOMEBREW_CASK_OPTS=--no-quarantine brew install --cask inkscape
143
+ brew install ghostscript ffmpeg
144
+ ;;
145
+ Windows)
146
+ choco install --no-progress -y inkscape ghostscript ffmpeg
147
+ ;;
148
+ esac
92
149
  after-setup-ruby: |
93
150
  if [ -f frontend/package.json ]; then cd frontend && npm install && npm run build; fi
94
151
  secrets:
@@ -146,9 +146,12 @@ Each row is the compiled form of one mapping rule:
146
146
  |===
147
147
 
148
148
  The in-memory precedent for this tuple is the shipped row shape
149
- `[rule, attr, kind, type, child_rows, child_register]`; the wire form
150
- replaces the Ruby object references (rule, attr, type, class) with
151
- portable identifiers.
149
+ `[rule, attr, kind, type, child_rows, child_register, discriminator,
150
+ absent_action]`. The seventh slot is the child's class-level
151
+ discriminator rule (`polymorphic_map:`) for `model` rows, resolved per
152
+ document; the eighth is the compiled absent-key action (<<absent>>).
153
+ The wire form replaces the Ruby object references (rule, attr, type,
154
+ class) with portable identifiers.
152
155
 
153
156
  The plan graph is a graph, not a tree. A recursive model graph — a tree
154
157
  whose nodes reference their own type, directly or through other model
@@ -187,14 +190,32 @@ shapes that are not ops-expressible — are **refusal gates**
187
190
  `type.cast(v)` is equivalent to `cast_value` — this equivalence is
188
191
  what made the plan gates the correct cast-precondition set.
189
192
 
193
+ [[absent]]
190
194
  === Absent keys and defaults
191
195
 
192
196
  Present-key detection uses the absent-vs-nil distinction the compiled
193
197
  walk makes (`fetch` with a sentinel; present-nil values read as nil).
194
- Absent rows carry the rule forward for default application.
195
- Resolution (<<open>>): the absent set returns to Ruby for the
196
- interpretive default path first; engine-side default application
197
- lands as a coverage card carrying defaults in the plan.
198
+ Each row carries a compiled **absent action**, decided at plan compile
199
+ from the attribute definition (#1311):
200
+
201
+ - `set`: assign the attribute's default through its writer — nil when
202
+ it has none — exactly as the walk's absent path does. Scalar rows and
203
+ model rows without a default take this action; the implicit
204
+ empty-collection default (`lazy_collection_default?`) does too,
205
+ because its value is provably the collection itself, so the writer
206
+ receives the same argument either way.
207
+ - `skip`: do nothing. A child-map rule with no default owns this
208
+ action: the walk returns before assigning, leaving the constructor
209
+ seed in place.
210
+ - `walk`: route the row through the interpretive absent path. Explicit
211
+ defaults on model-valued and child-map rules take it, because whether
212
+ a proc default materializes a document the rule must translate is not
213
+ a per-class constant. Runtime value-map overrides (`nil:`, `empty:`,
214
+ `omitted:`) also force it for every absent row of the node.
215
+
216
+ The plan therefore carries the absent outcome for the proven shapes
217
+ natively; the interpretive path remains the correctness reference for
218
+ `walk` rows.
198
219
 
199
220
  === Emission (Ruby → engine)
200
221
 
@@ -226,9 +247,14 @@ construction, so hydration depth is bound by the heap and never by the
226
247
  VM stack (#1297). Children complete before the parent instance is
227
248
  constructed; siblings hydrate in row order and collection items in
228
249
  document order; each child is linked to its parent, and to its root
229
- when it has none, once the parent instance exists. A child shape the
230
- plan does not cover unwinds the whole build to the fallback, which owns
231
- that shape's semantics and errors.
250
+ when it has none, once the parent instance exists. The frame's instance
251
+ is allocated through the compiled walk seed (`init_walk_state`); a
252
+ class with its own `initialize` runs that constructor body against the
253
+ seed with the generic attribute walk stood down (the #1059 contract),
254
+ so constructor-internal state and the generated defaults are identical
255
+ to the fallback's. A child shape the plan does not cover unwinds the
256
+ whole build to the fallback, which owns that shape's semantics and
257
+ errors.
232
258
 
233
259
  Parse errors: the engine raises the strict-parse contract (the
234
260
  `strict:` kwarg lineage — malformed input is a format error, never a
@@ -300,14 +326,15 @@ the gates table as the ledger.
300
326
  [[open]]
301
327
  == Resolutions (decided, with rationale)
302
328
 
303
- 1. *Defaults placement*: Ruby-side absent-row handling first
304
- (defaults apply through the interpretive absent path exactly as
305
- `kv_group_build` does today); engine-side default application
306
- lands as a coverage card carrying defaults in the plan.
307
- Rationale: the fallback is the correctness reference — shipping
308
- hydration before default relocation keeps the engine the strict
309
- subset; the measured win (the 61%) is in present-key casting,
310
- which this sequencing captures in full.
329
+ 1. *Defaults placement*: the absent action ships compiled per row
330
+ (#1311) — `set` rows assign the default through the writer, `skip`
331
+ rows leave the constructor seed, `walk` rows keep the interpretive
332
+ absent path. Rationale: the fallback remains the correctness
333
+ reference for the shapes whose outcome is not a per-class constant,
334
+ while the common shapes no longer pay a full rule-pipeline walk per
335
+ absent key — on the 3MB expressir wire those walks were ~three
336
+ quarters of the lane's samples. Engine-side default application
337
+ consumes the same per-row action rather than re-deriving it.
311
338
  2. *Custom type bindings*: accepted. Custom `Type::Value` subclasses
312
339
  ride the `Materialization` `type_bindings` pattern at plan level;
313
340
  unbound subclasses refuse to the fallback. Rationale: refusing
@@ -42,8 +42,15 @@ module Lutaml
42
42
  # lutaml-model#88: nil unless the mapping partitions a wire key
43
43
  # with when_attribute rules — the common case pays one scan.
44
44
  partition = kv_partition(rules)
45
+ # #1311 lever 3: parent/root are the same for every rule of one
46
+ # node, so one cast-options hash serves the whole rule walk and
47
+ # carries the memoized propagated child options across its
48
+ # child-valued attributes.
49
+ cast_options = { lutaml_parent: instance,
50
+ lutaml_root: instance.lutaml_root || instance }
45
51
  rules.each do |rule|
46
- process_mapping_rule(data, instance, format, rule, options, partition)
52
+ process_mapping_rule(data, instance, format, rule, options,
53
+ partition, cast_options)
47
54
  end
48
55
  collect_unmapped_keys(data, instance, mappings, rules)
49
56
 
@@ -264,10 +271,18 @@ module Lutaml
264
271
  # dispatch and registered type substitutions own their cast
265
272
  # (the per-rule walk threads them through cast options).
266
273
  return false if attr.collection? && attr.collection.is_a?(Range)
267
- return false if attr.polymorphic? || attr.union? ||
268
- attr.custom_collection?
274
+ return false if attr.union? || attr.custom_collection?
269
275
 
270
276
  type = attr.type(register)
277
+ # A polymorphic VALUE attribute (a model-typed attribute whose
278
+ # concrete class the walk resolves) owns its cast on the walk.
279
+ # A polymorphic_class flag on a SCALAR attribute marks the
280
+ # class's discriminator (`_class`): it is a plain scalar here —
281
+ # with or without the map on this rule, exactly as the walk
282
+ # reads it — and the concrete child class comes from each
283
+ # document at build time (kv_group_child_class).
284
+ return false if attr.polymorphic? &&
285
+ !(type.is_a?(Class) && type < Lutaml::Model::Type::Value)
271
286
  return false if Lutaml::Model::GlobalContext.context(register)
272
287
  .substitution_for(type).any?
273
288
 
@@ -284,7 +299,8 @@ module Lutaml
284
299
  rule.hash_mappings)
285
300
  return false unless child_plan
286
301
 
287
- rows << [rule, attr, :child_map, child_plan, nil, nil]
302
+ rows << [rule, attr, :child_map, child_plan, nil, nil, nil,
303
+ kv_absent_action(attr, :child_map)]
288
304
  next
289
305
  end
290
306
 
@@ -296,12 +312,18 @@ module Lutaml
296
312
 
297
313
  # child_register rides the row: the plan was BUILT under it,
298
314
  # and rebuilding the resolution per model row per document
299
- # cost a Concurrent::Map hop each time (TODO.perf 14).
300
- rows << [rule, attr, :model, type, child_rows, child_register]
315
+ # cost a Concurrent::Map hop each time (TODO.perf 14). The
316
+ # seventh slot is the child's class-level discriminator rule
317
+ # (polymorphic_map:), if any: the concrete class comes from
318
+ # each document, so these rows serve the fallthrough case.
319
+ poly = type.mappings_for(format, child_register)&.polymorphic_mapping
320
+ rows << [rule, attr, :model, type, child_rows, child_register, poly,
321
+ kv_absent_action(attr, :model)]
301
322
  elsif type.is_a?(Class) && type < Lutaml::Model::Type::Value &&
302
323
  !attr.value_policy.whole_value?(type) &&
303
324
  !Lutaml::Model::Attribute.custom_from_probe?(type)
304
- rows << [rule, attr, :scalar, nil, nil, nil]
325
+ rows << [rule, attr, :scalar, nil, nil, nil, nil,
326
+ kv_absent_action(attr, :scalar)]
305
327
  else
306
328
  return false
307
329
  end
@@ -312,12 +334,59 @@ module Lutaml
312
334
  Array(rule.name).each { |name| claimed[name.to_s] = true } if rule.name
313
335
  end
314
336
  claimed.freeze
315
- rows << [nil, nil, :unmapped, unmapped_target, claimed, nil]
337
+ rows << [nil, nil, :unmapped, unmapped_target, claimed, nil, nil, nil]
316
338
  end
317
339
  # No eligible rows at all (empty mapping) — nothing to gain.
318
340
  rows.empty? ? nil : rows
319
341
  end
320
342
 
343
+ # The absent-key outcome of a plan row, compiled once (#1311):
344
+ # the walk assigns the attribute's default through its writer —
345
+ # nil when the attribute has none — except for a child-map rule
346
+ # with no default, where it returns before assigning. The implicit
347
+ # empty-collection default is the one proc default whose value is
348
+ # provably the collection itself, so the writer sees the same
349
+ # argument either way. Explicit defaults on model-valued and
350
+ # child-map rules keep the walk: whether a proc default materializes
351
+ # a document the rule must translate is not a per-class constant.
352
+ def self.kv_absent_action(attr, kind)
353
+ case kind
354
+ when :scalar
355
+ :set
356
+ when :model, :child_map
357
+ return :set if attr.default_kind == :none
358
+ return :set if attr.lazy_collection_default?
359
+
360
+ :walk
361
+ else
362
+ :walk
363
+ end
364
+ end
365
+
366
+ # The concrete class behind a :model row whose declared type carries
367
+ # a class-level discriminator (polymorphic_map:), read from one
368
+ # document the way resolve_polymorphic reads it — the discriminator
369
+ # key, the map entry, Object.const_get — with an entry that resolves
370
+ # to the declared type itself falling through to the row's own rows.
371
+ # Returns [class, rows, register]: the concrete class hydrates through
372
+ # its own cached plan; nil when that plan is ineligible, so the node
373
+ # takes the walk (which owns the error for a missing discriminator).
374
+ def self.kv_group_child_class(type, rows, poly, doc, format, register)
375
+ key = doc[poly.name]
376
+ klass_name = key && poly.polymorphic_map[key]
377
+ return nil unless klass_name
378
+
379
+ klass = Object.const_get(klass_name)
380
+ return [type, rows, register] if klass == type
381
+
382
+ klass_register = Lutaml::Model::Register.resolve_for_child(klass,
383
+ register)
384
+ klass_rows = kv_group_plan(klass, format, klass_register)
385
+ return nil unless klass_rows
386
+
387
+ [klass, klass_rows, klass_register]
388
+ end
389
+
321
390
  # Class-level twin of the walk's child-map compile: the registry
322
391
  # lookups (child rules, per-attribute wire names, path casts)
323
392
  # resolve once per plan instead of once per node per child. nil
@@ -497,7 +566,7 @@ child_mappings)
497
566
  # the separate string_or_symbol_key? probe goes away.
498
567
  v = Lutaml::Model::Utils.fetch_str_or_sym(frame.doc, rule.name, MISSING)
499
568
  if v.equal?(MISSING)
500
- (frame.absent ||= []) << rule
569
+ (frame.absent ||= []) << row
501
570
  i += 1
502
571
  next
503
572
  end
@@ -547,11 +616,26 @@ child_mappings)
547
616
  private
548
617
 
549
618
  # :child when the frame's pending row has a document the plan
550
- # covers, :fallback otherwise.
619
+ # covers, :fallback otherwise. A :model row whose declared type
620
+ # carries a class-level discriminator (polymorphic_map:, the seventh
621
+ # slot) resolves the concrete class from EACH document, so the
622
+ # frame's request follows the document, not the row.
551
623
  def request_child(frame)
552
624
  document = frame.next_child_document
553
625
  return :fallback unless document
554
626
 
627
+ row = frame.rows[frame.pending_index]
628
+ if (poly = row[6])
629
+ resolved = self.class.kv_group_child_class(row[3], row[4], poly,
630
+ document, frame.format,
631
+ row[5])
632
+ return :fallback unless resolved
633
+
634
+ frame.request_transform = self.class.cached_transform(resolved[0],
635
+ resolved[2])
636
+ frame.request_rows = resolved[1]
637
+ frame.request_register = resolved[2]
638
+ end
555
639
  frame.request_doc = document
556
640
  :child
557
641
  end
@@ -587,9 +671,7 @@ child_mappings)
587
671
 
588
672
  instance.public_send(row[0].kv_setter, v)
589
673
  end
590
- frame.absent&.each do |rule|
591
- process_mapping_rule(frame.doc, instance, frame.format, rule, frame.options, nil)
592
- end
674
+ kv_apply_absent(frame, instance)
593
675
  frame.children&.each do |child|
594
676
  child.lutaml_parent = instance
595
677
  child.lutaml_root ||= instance.lutaml_root || instance
@@ -611,6 +693,35 @@ child_mappings)
611
693
  instance
612
694
  end
613
695
 
696
+ # Absent keys through the compiled per-row action (#1311) instead
697
+ # of the per-rule walk: on the expressir corpus the absent walks
698
+ # were ~three quarters of the lane's samples. A :set row assigns
699
+ # the attribute's default exactly as the walk would — the
700
+ # DEFAULT_VALUE_MAP sends the omitted sentinel to nil, and the
701
+ # writer casts and marks the value set; :skip leaves the constructor
702
+ # seed (child-map rules with no default). Runtime value-map
703
+ # overrides keep the walk: their outcome is not a per-class
704
+ # constant.
705
+ def kv_apply_absent(frame, instance)
706
+ return unless frame.absent
707
+
708
+ overrides = frame.options[:nil] || frame.options[:empty] ||
709
+ frame.options[:omitted]
710
+ frame.absent.each do |row|
711
+ rule = row[0]
712
+ action = overrides ? :walk : row[7]
713
+ case action
714
+ when :set
715
+ value = row[1].default_value(frame.register, instance)
716
+ value = nil if Lutaml::Model::Utils.uninitialized?(value)
717
+ instance.public_send(rule.kv_setter, value)
718
+ when :walk
719
+ process_mapping_rule(frame.doc, instance, frame.format, rule,
720
+ frame.options, nil)
721
+ end
722
+ end
723
+ end
724
+
614
725
  def process_rule!(instance, rule, hash, format, _mappings, options)
615
726
  return handle_delegate(instance, rule, hash, format) if rule.delegate
616
727
 
@@ -978,7 +1089,7 @@ child_mappings)
978
1089
  end
979
1090
 
980
1091
  def process_mapping_rule(doc, instance, format, rule, options = {},
981
- partition = nil)
1092
+ partition = nil, cast_options = nil)
982
1093
  plans = walk_plans_for(format)
983
1094
  plan = plans[rule] ||= build_walk_plan(format, rule)
984
1095
  kind = plan.kind
@@ -1035,7 +1146,7 @@ partition = nil)
1035
1146
  end
1036
1147
  unless kind == :hash_map
1037
1148
  value = cast_value(value, attr, format, rule,
1038
- instance)
1149
+ instance, cast_options)
1039
1150
  end
1040
1151
 
1041
1152
  # Range errors stay eager here. The over-count error only fires when the
@@ -1272,12 +1383,20 @@ partition = nil)
1272
1383
  end
1273
1384
  end
1274
1385
 
1275
- def cast_value(value, attr, format, rule, instance)
1276
- cast_options = rule.polymorphic ? { polymorphic: rule.polymorphic } : {}
1277
- cast_options[:lutaml_parent] = instance if instance
1278
- if instance
1279
- cast_options[:lutaml_root] =
1280
- (instance.lutaml_root || instance)
1386
+ def cast_value(value, attr, format, rule, instance, cast_options = nil)
1387
+ # #1311 lever 3: the node's shared cast-options hash replaces the
1388
+ # per-rule allocation; the rule's polymorphic list must not leak
1389
+ # into siblings, so it rides a copy.
1390
+ unless cast_options
1391
+ cast_options = {}
1392
+ cast_options[:lutaml_parent] = instance if instance
1393
+ if instance
1394
+ cast_options[:lutaml_root] =
1395
+ (instance.lutaml_root || instance)
1396
+ end
1397
+ end
1398
+ if rule.polymorphic
1399
+ cast_options = cast_options.merge(polymorphic: rule.polymorphic)
1281
1400
  end
1282
1401
  attr.cast(value, format, lutaml_register, cast_options)
1283
1402
  end
@@ -1261,17 +1261,22 @@ instance_object = nil)
1261
1261
  )
1262
1262
  end
1263
1263
 
1264
- propagated = Serialize::DeserializationContext.propagate(options)
1264
+ # #1311 lever 3: the child options are a per-node constant,
1265
+ # memoized on this node's cast-options hash instead of being
1266
+ # rebuilt (slice + merge) per attribute with a child value.
1267
+ child_options = Serialize::DeserializationContext.child_options(
1268
+ options, register
1269
+ )
1265
1270
  return klass.apply_mappings(
1266
- { content_attr => value }, format,
1267
- propagated.merge(register: register)
1271
+ { content_attr => value }, format, child_options
1268
1272
  )
1269
1273
  end
1270
1274
 
1271
1275
  if can_serialize?(klass, value, format, options)
1272
- propagated = Serialize::DeserializationContext.propagate(options)
1273
- klass.apply_mappings(value, format,
1274
- propagated.merge(register: register))
1276
+ child_options = Serialize::DeserializationContext.child_options(
1277
+ options, register
1278
+ )
1279
+ klass.apply_mappings(value, format, child_options)
1275
1280
  elsif needs_conversion?(klass, value)
1276
1281
  klass.public_send(:"from_#{format}", value)
1277
1282
  else
@@ -45,6 +45,27 @@ module Lutaml
45
45
  def self.propagate(options)
46
46
  options.slice(*PROPAGATION_KEYS)
47
47
  end
48
+
49
+ # Memo key for the per-node propagated options (#1311). The
50
+ # propagable keys and the register are identical for every child
51
+ # value of one node, so deriving the child options hash per
52
+ # attribute built and discarded duplicate hashes — `Hash#merge`
53
+ # 7.5% plus `Hash#slice` 3.9% self on the KV walk profile. The
54
+ # key is deliberately not in PROPAGATION_KEYS, so a child never
55
+ # receives the parent's memo as context.
56
+ CHILD_OPTIONS_KEY = :__lutaml_child_options__
57
+
58
+ # The propagated child options for one node's cast-options hash.
59
+ # Reused across that node's attributes; recomputed when the
60
+ # register differs (an options hash can outlive one node).
61
+ def self.child_options(options, register)
62
+ cached = options[CHILD_OPTIONS_KEY]
63
+ return cached if cached && cached[:register] == register
64
+
65
+ propagated = propagate(options).merge(register: register)
66
+ options[CHILD_OPTIONS_KEY] = propagated unless options.frozen?
67
+ propagated
68
+ end
48
69
  end
49
70
  end
50
71
  end
@@ -518,17 +518,27 @@ module Lutaml
518
518
  def allocate_for_walk(register = nil)
519
519
  register_id = extract_register_id(register)
520
520
  ensure_imports!(register_id) if finalized? && deferred_imports_pending?(register_id)
521
- # Classes with their own initialize set up internal state the
522
- # compiled seed cannot know about (SchemaRepository's
523
- # @parsed_schemas/@namespace_registry/@type_index): construct
524
- # through the real constructor for them, exactly as the walk
525
- # did before the compiled seed existed, register included. The
526
- # seed stays the fast path for classes using the standard
527
- # initializer.
528
- return new(lutaml_register: register) if constructs_through_initialize?
529
-
530
521
  instance = allocate
531
522
  instance.init_walk_state(register_id)
523
+ return instance unless constructs_through_initialize?
524
+
525
+ # Classes with their own initialize set up internal state the
526
+ # compiled seed cannot know about (SchemaRepository's
527
+ # @parsed_schemas/@namespace_registry/@type_index): their
528
+ # constructor body still has to run. Run it against the
529
+ # compiled walk seed with the generic attribute walk stood
530
+ # down — the #1059 contract allocate_for_deserialization has
531
+ # used since the deserialization constructor landed: the walk
532
+ # only re-materializes state the seed already wrote, and on
533
+ # expression-model trees (75 classes, every node overriding
534
+ # initialize for parent wiring) that walk was half of the
535
+ # whole hydration cost (#1311). The register rides the call
536
+ # exactly as `new(lutaml_register:)` passed it: a constructor
537
+ # that tells a blank construction from a caller's by the
538
+ # keyword sees the same call either way.
539
+ instance.lutaml_constructor_seeded = true
540
+ instance.send(:initialize, lutaml_register: register_id)
541
+ instance.lutaml_constructor_seeded = nil
532
542
  instance
533
543
  end
534
544
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.202"
5
+ VERSION = "0.8.203"
6
6
  end
7
7
  end
@@ -215,6 +215,10 @@ RSpec.describe AddressPersonSpec do
215
215
  end
216
216
 
217
217
  it "serializes to XML with a collection of persons" do
218
+ # leptris drops xmlns="" on no-namespace children of a
219
+ # default-namespaced parent (leptris/leptris#2294).
220
+ skip "leptris/leptris#2294" if Lutaml::Xml.moxml_engine == :leptris
221
+
218
222
  expected_xml = <<~XML
219
223
  <Address>
220
224
  <Country>USA</Country>
@@ -339,6 +343,10 @@ RSpec.describe AddressPersonSpec do
339
343
  end
340
344
 
341
345
  it "serializes to XML" do
346
+ # leptris drops xmlns="" on no-namespace children of a
347
+ # default-namespaced parent (leptris/leptris#2294).
348
+ skip "leptris/leptris#2294" if Lutaml::Xml.moxml_engine == :leptris
349
+
342
350
  expect(model.to_xml).to be_xml_equivalent_to(xml)
343
351
  end
344
352
 
data/spec/address_spec.rb CHANGED
@@ -104,6 +104,10 @@ RSpec.describe Address do
104
104
  end
105
105
 
106
106
  it "serializes to XML with a collection of persons" do
107
+ # leptris drops xmlns="" on no-namespace children of a
108
+ # default-namespaced parent (leptris/leptris#2294).
109
+ skip "leptris/leptris#2294" if Lutaml::Xml.moxml_engine == :leptris
110
+
107
111
  # W3C minimal-subtree principle: nsp1 namespace is declared at the LCA
108
112
  # (Person element) rather than repeated on each child element.
109
113
  expected_xml = <<~XML
@@ -223,6 +223,128 @@ RSpec.describe "KV group-then-bulk hydration" do
223
223
  expect(ranged.from_hash("tags" => %w[a b]).tags).to eq(%w[a b])
224
224
  end
225
225
 
226
+ it "plans a class-level discriminator and hydrates concrete children" do
227
+ # The `_class` shape: a scalar attribute flagged polymorphic_class
228
+ # whose rule carries the class map. The discriminator is a scalar
229
+ # row; the concrete child class comes from each document.
230
+ shape = Class.new(Lutaml::Model::Serializable) do
231
+ attribute :kind, :string, polymorphic_class: true
232
+ attribute :name, :string
233
+ hsh do
234
+ map "kind", to: :kind, polymorphic_map: {
235
+ "KvGroup::Circle" => "KvGroup::Circle",
236
+ "KvGroup::Square" => "KvGroup::Square",
237
+ "KvGroup::Shape" => "KvGroup::Shape",
238
+ }
239
+ map "name", to: :name
240
+ end
241
+ end
242
+ circle = Class.new(shape) do
243
+ attribute :radius, :integer
244
+ hsh do
245
+ map "kind", to: :kind
246
+ map "name", to: :name
247
+ map "radius", to: :radius
248
+ end
249
+ end
250
+ square = Class.new(shape) do
251
+ attribute :side, :integer
252
+ hsh do
253
+ map "kind", to: :kind
254
+ map "name", to: :name
255
+ map "side", to: :side
256
+ end
257
+ end
258
+ sheet = Class.new(Lutaml::Model::Serializable) do
259
+ attribute :shapes, shape, collection: true
260
+ attribute :first, shape
261
+ hsh do
262
+ map "shapes", to: :shapes
263
+ map "first", to: :first
264
+ end
265
+ end
266
+ stub_const("KvGroup::Shape", shape)
267
+ stub_const("KvGroup::Circle", circle)
268
+ stub_const("KvGroup::Square", square)
269
+ stub_const("KvGroup::Sheet", sheet)
270
+
271
+ expect(shape.attributes[:kind].polymorphic?).to be(true)
272
+ expect(Lutaml::KeyValue::Transform.kv_group_plan(sheet, :hash,
273
+ :default)).to be_truthy
274
+
275
+ doc = {
276
+ "shapes" => [
277
+ { "kind" => "KvGroup::Circle", "name" => "c", "radius" => 2 },
278
+ { "kind" => "KvGroup::Square", "name" => "s", "side" => 3 },
279
+ # The fallthrough: an entry resolving to the declared type
280
+ # itself hydrates through the declared type's own rows.
281
+ { "kind" => "KvGroup::Shape", "name" => "bare" },
282
+ ],
283
+ "first" => { "kind" => "KvGroup::Square", "name" => "f", "side" => 1 },
284
+ }
285
+ sheet_built = sheet.from_hash(doc)
286
+
287
+ expect(sheet_built.shapes.map(&:class)).to eq([circle, square, shape])
288
+ expect(sheet_built.shapes.map(&:to_hash)).to eq(doc["shapes"])
289
+ expect(sheet_built.first).to be_a(square)
290
+ expect(sheet_built.first.side).to eq(1)
291
+ expect(sheet_built.shapes.first.lutaml_parent).to equal(sheet_built)
292
+ expect(sheet_built.to_hash).to eq(doc)
293
+ end
294
+
295
+ it "takes the walk per document when a discriminator names an ineligible class" do
296
+ shape = Class.new(Lutaml::Model::Serializable) do
297
+ attribute :kind, :string, polymorphic_class: true
298
+ hsh do
299
+ map "kind", to: :kind, polymorphic_map: {
300
+ "KvGroup::RangedShape" => "KvGroup::RangedShape",
301
+ "KvGroup::PlainShape" => "KvGroup::PlainShape",
302
+ }
303
+ end
304
+ end
305
+ ranged_shape = Class.new(shape) do
306
+ attribute :tags, :string, collection: 1..2
307
+ hsh do
308
+ map "kind", to: :kind
309
+ map "tags", to: :tags
310
+ end
311
+ end
312
+ plain_shape = Class.new(shape) do
313
+ attribute :label, :string
314
+ hsh do
315
+ map "kind", to: :kind
316
+ map "label", to: :label
317
+ end
318
+ end
319
+ sheet = Class.new(Lutaml::Model::Serializable) do
320
+ attribute :shapes, shape, collection: true
321
+ hsh { map "shapes", to: :shapes }
322
+ end
323
+ stub_const("KvGroup::ShapeBase", shape)
324
+ stub_const("KvGroup::RangedShape", ranged_shape)
325
+ stub_const("KvGroup::PlainShape", plain_shape)
326
+ stub_const("KvGroup::MixedSheet", sheet)
327
+
328
+ # The holder is eligible; the ineligible concrete class is a
329
+ # per-document verdict (its range collection keeps the walk).
330
+ expect(Lutaml::KeyValue::Transform.kv_group_plan(sheet, :hash,
331
+ :default)).to be_truthy
332
+ expect(Lutaml::KeyValue::Transform.kv_group_plan(ranged_shape, :hash,
333
+ :default)).to be(false)
334
+ expect(Lutaml::KeyValue::Transform.kv_group_plan(plain_shape, :hash,
335
+ :default)).to be_truthy
336
+
337
+ doc = { "shapes" => [
338
+ { "kind" => "KvGroup::RangedShape", "tags" => %w[a b] },
339
+ { "kind" => "KvGroup::PlainShape", "label" => "p" },
340
+ ] }
341
+ built = sheet.from_hash(doc)
342
+
343
+ expect(built.shapes.map(&:class)).to eq([ranged_shape, plain_shape])
344
+ expect(built.shapes.map(&:to_hash)).to eq(doc["shapes"])
345
+ expect(built.to_hash).to eq(doc)
346
+ end
347
+
226
348
  it "compiles self-referential models as a plan referencing itself (#1297)" do
227
349
  node = Class.new(Lutaml::Model::Serializable) do
228
350
  attribute :name, :string
@@ -311,5 +433,86 @@ RSpec.describe "KV group-then-bulk hydration" do
311
433
  end
312
434
  end
313
435
  end
314
-
436
+
437
+ # #1311 levers 1 and 2: absent keys take a compiled per-row action
438
+ # instead of the per-rule walk, and custom-initialize classes
439
+ # construct against the compiled walk seed (the #1059 contract)
440
+ # rather than the generic attribute walk.
441
+ describe "absent-key actions and walk-seed construction" do
442
+ let(:model_class) do
443
+ Class.new(Lutaml::Model::Serializable) do
444
+ attribute :plain, :string
445
+ attribute :with_default, :string, default: "d"
446
+ attribute :items, :string, collection: true, initialize_empty: true
447
+ attribute :plain_items, :string, collection: true
448
+
449
+ key_value do
450
+ map "plain", to: :plain
451
+ map "with_default", to: :with_default
452
+ map "items", to: :items
453
+ map "plain_items", to: :plain_items
454
+ end
455
+ end
456
+ end
457
+
458
+ before { stub_const("KvGroup::Absent", model_class) }
459
+
460
+ it "seeds absent defaults through the writer, as the per-rule walk does" do
461
+ model = KvGroup::Absent.from_hash({})
462
+
463
+ expect(model.with_default).to eq("d")
464
+ expect(model.using_default?(:with_default)).to be(false)
465
+ expect(model.using_default?(:plain)).to be(false)
466
+ expect(model.items).to eq([])
467
+ expect(model.plain_items).to be_nil
468
+ expect(model.to_hash).to eq({ "with_default" => "d" })
469
+ end
470
+
471
+ it "keeps the writer outcome for a present-nil key" do
472
+ model = KvGroup::Absent.from_hash({ "plain" => nil })
473
+
474
+ expect(model.plain).to be_nil
475
+ expect(model.using_default?(:plain)).to be(false)
476
+ expect(model.to_hash).to eq({ "with_default" => "d" })
477
+ end
478
+
479
+ it "routes absent keys through the walk when value-map overrides are given" do
480
+ model = KvGroup::Absent.from_hash({}, omitted: :empty)
481
+
482
+ expect(model.plain).to eq("")
483
+ expect(model.using_default?(:plain)).to be(false)
484
+ # An empty string is dropped from the serialized form (render_empty
485
+ # is off by default), exactly as the per-rule walk renders it.
486
+ expect(model.to_hash).to eq({ "with_default" => "d" })
487
+ end
488
+
489
+ it "runs a custom initialize against the compiled walk seed" do
490
+ custom = Class.new(Lutaml::Model::Serializable) do
491
+ attribute :a, :string
492
+ attribute :b, :string, default: "d"
493
+ attribute :items, :string, collection: true, initialize_empty: true
494
+
495
+ key_value do
496
+ map "a", to: :a
497
+ map "b", to: :b
498
+ map "items", to: :items
499
+ end
500
+
501
+ attr_reader :seen
502
+
503
+ def initialize(options = {})
504
+ super
505
+ @seen = [a, b, items]
506
+ end
507
+ end
508
+ stub_const("KvGroup::CustomInit", custom)
509
+
510
+ model = KvGroup::CustomInit.from_hash({ "a" => "x" })
511
+
512
+ expect(model.seen).to eq([nil, "d", []])
513
+ expect(model.a).to eq("x")
514
+ expect(model.b).to eq("d")
515
+ expect(model.to_hash).to eq({ "a" => "x", "b" => "d" })
516
+ end
517
+ end
315
518
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # #1311 lever 3: the propagated child options are a per-node constant.
6
+ # child_options memoizes them on the node's cast-options hash so the
7
+ # slice + merge pair runs once per node instead of once per attribute
8
+ # with a child value.
9
+ RSpec.describe Lutaml::Model::Serialize::DeserializationContext do
10
+ let(:parent_model) { Struct.new(:name).new("parent") }
11
+ let(:options) do
12
+ {
13
+ lutaml_parent: parent_model,
14
+ lutaml_root: parent_model,
15
+ default_namespace: "urn:example",
16
+ namespace_uri: "urn:internal",
17
+ resolved_type: String,
18
+ arbitrary: "ignored",
19
+ }
20
+ end
21
+
22
+ describe ".child_options" do
23
+ it "extracts only the propagable keys" do
24
+ child = described_class.child_options(options, :default)
25
+
26
+ expect(child[:lutaml_parent]).to equal(parent_model)
27
+ expect(child[:lutaml_root]).to equal(parent_model)
28
+ expect(child[:default_namespace]).to eq("urn:example")
29
+ expect(child[:register]).to eq(:default)
30
+ expect(child).not_to have_key(:namespace_uri)
31
+ expect(child).not_to have_key(:resolved_type)
32
+ expect(child).not_to have_key(:arbitrary)
33
+ end
34
+
35
+ it "returns the same hash for repeat calls under one register" do
36
+ first = described_class.child_options(options, :default)
37
+ second = described_class.child_options(options, :default)
38
+
39
+ expect(second).to equal(first)
40
+ end
41
+
42
+ it "recomputes when the register differs" do
43
+ first = described_class.child_options(options, :default)
44
+ other = described_class.child_options(options, :other)
45
+
46
+ expect(other[:register]).to eq(:other)
47
+ expect(other).not_to equal(first)
48
+ expect(described_class.child_options(options, :default)[:register])
49
+ .to eq(:default)
50
+ end
51
+
52
+ it "never propagates its own memo key to children" do
53
+ child = described_class.child_options(options, :default)
54
+
55
+ expect(child).not_to have_key(
56
+ Lutaml::Model::Serialize::DeserializationContext::CHILD_OPTIONS_KEY,
57
+ )
58
+ end
59
+
60
+ it "computes without mutating frozen options" do
61
+ frozen = options.dup.freeze
62
+ child = described_class.child_options(frozen, :default)
63
+
64
+ expect(child[:register]).to eq(:default)
65
+ expect(described_class.child_options(frozen, :default)).not_to equal(child)
66
+ end
67
+ end
68
+ end
data/spec/spec_helper.rb CHANGED
@@ -98,11 +98,10 @@ require_relative "support/test_adapter_config"
98
98
  # require_relative "../lib/lutaml/model/toml_adapter/toml_rb_adapter"
99
99
 
100
100
  Lutaml::Model::Config.configure do |config|
101
- config.xml_adapter_type = if ENV["XML_ADAPTER"]
102
- ENV["XML_ADAPTER"].to_sym
103
- else
104
- :nokogiri
105
- end
101
+ # No XML pin by default: the suite rides the production resolution —
102
+ # the moxml facade with the runtime default engine (leptris preferred,
103
+ # Nokogiri as fallback). XML_ADAPTER pins an engine for a run.
104
+ config.xml_adapter_type = ENV["XML_ADAPTER"].to_sym if ENV["XML_ADAPTER"]
106
105
  config.hash_adapter_type = :standard
107
106
  config.json_adapter_type = :standard
108
107
  config.yaml_adapter_type = :standard
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.202
4
+ version: 0.8.203
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -2153,6 +2153,7 @@ files:
2153
2153
  - spec/lutaml/model/serializable_validation_spec.rb
2154
2154
  - spec/lutaml/model/serialization_profiles_spec.rb
2155
2155
  - spec/lutaml/model/serialize/builder_spec.rb
2156
+ - spec/lutaml/model/serialize/deserialization_context_spec.rb
2156
2157
  - spec/lutaml/model/serialize_perf_guard_spec.rb
2157
2158
  - spec/lutaml/model/services/transformer_spec.rb
2158
2159
  - spec/lutaml/model/simple_model_spec.rb