lutaml-model 0.8.199 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47aaf2a9887e7cda54173eee34113fe951be0a54e62ee9a014b9e1a5f3518922
4
- data.tar.gz: cd540af8382bcb29af2847fa8fc9bcd3b825bfa437d61d89a1281d8f7b68d1fb
3
+ metadata.gz: 9a7958849b1b037485644838e6cd7b3264546a8f25fdfd8d29d694595fb2c4be
4
+ data.tar.gz: 16e38f14e5d3c06b55073d626dcf79e8fb93f33c1775bf2ef2288d62d614c617
5
5
  SHA512:
6
- metadata.gz: b605d218d698f6696e7643d5b5cb53b6d26fe110611b4940bfd871a2fe64dfc385e2fd77969743779684b13fd8c520c19e32dde52ebf17e62bc9da3aa88b356e
7
- data.tar.gz: 45b305d0dc57ac40aaa1cc261596519666e2b2e117c40ea10a1fc0a0c6e709bdeb2ea393c264c9d9b61af944ea94d8c269b1093a98121cef5f4af1259a3fbaee
6
+ metadata.gz: 290f49e6e4d3801291845ffd8ef58822363f70fdc8a8ee17013757a5a5afc3b10ff73187bf75a8e11a20271750534f3e6c0b888569dc1f81f71db12e36d906b9
7
+ data.tar.gz: 2df50e5220736a2d7468cd5ae6713e819cce37bfcfaacf7c7248901db6f31023d01a92015f7dd1bbb02db1876f66b9def21ea2309de15414f777d714227aeb15
@@ -150,6 +150,16 @@ The in-memory precedent for this tuple is the shipped row shape
150
150
  replaces the Ruby object references (rule, attr, type, class) with
151
151
  portable identifiers.
152
152
 
153
+ The plan graph is a graph, not a tree. A recursive model graph — a tree
154
+ whose nodes reference their own type, directly or through other model
155
+ types — compiles as a self-referential plan: the `model` row's nested
156
+ plan reference may be the enclosing plan. The wire form therefore
157
+ identifies each plan and references plans by identifier; it never
158
+ expands a nested plan inline, which would be unbounded for a tree shape
159
+ (#1297). A plan compile session commits its plans only when the
160
+ outermost build succeeds, so a plan that references rows an incomplete
161
+ outer build never finished is never published.
162
+
153
163
  === Row kinds (XML lane; extracted from the shipped kinds)
154
164
 
155
165
  The XML lane's compiled kinds are already enumerated in code
@@ -210,6 +220,16 @@ unmapped merges exactly as `kv_group_build` does today. The
210
220
  Ruby-visible object graph, validation surface, and error classes are
211
221
  byte-for-byte the fallback's.
212
222
 
223
+ Executor contract (the reference the crate's executor mirrors): the
224
+ plan runs on an explicit frame stack, one frame per model under
225
+ construction, so hydration depth is bound by the heap and never by the
226
+ VM stack (#1297). Children complete before the parent instance is
227
+ constructed; siblings hydrate in row order and collection items in
228
+ 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.
232
+
213
233
  Parse errors: the engine raises the strict-parse contract (the
214
234
  `strict:` kwarg lineage — malformed input is a format error, never a
215
235
  best-effort tree), mapped to `Lutaml::Model::InvalidFormatError` at
@@ -152,10 +152,10 @@ module Lutaml
152
152
  # ---- TODO.max-perf/37: group-then-instantiate fast path ----
153
153
 
154
154
  # [model class, format, register] -> rows or false (ineligible).
155
- # Rows: [[rule, attr, kind, child_rows]] with kind :scalar or
156
- # :model; child_rows is the child model's own row set. Cycle-safe:
157
- # an in-progress model resolves false, so self-referential models
158
- # take the interpretive walk.
155
+ # Rows: [[rule, attr, kind, child_rows, child_register]] with kind
156
+ # :scalar, :model, :child_map or :unmapped; child_rows is the child
157
+ # model's own row set. A recursive model graph compiles as a
158
+ # self-referential plan: child_rows may be the plan itself.
159
159
  # Concurrent::Map under threaded MRI, plain Hash under Opal
160
160
  # (Concurrent is unavailable there) — the RULE_RECORDS pattern.
161
161
  # Writes are idempotent (the same deterministic plan is computed),
@@ -193,24 +193,43 @@ module Lutaml
193
193
  plan = KV_GROUP_PLANS[key]
194
194
  return plan unless plan.nil?
195
195
 
196
- # Cycle detection rides a THREAD-LOCAL recursion stack: a shared
197
- # in-progress set races (a concurrent same-key build would cache
198
- # false permanently), and the stack is per-build by definition.
199
- # The false at the cycle point is NOT cached — the outermost
200
- # build completes and caches the model's real verdict.
201
- stack = (Thread.current[:kv_group_plan_stack] ||= [])
202
- return false if stack.include?(key)
203
-
204
- stack.push(key)
196
+ # A recursive model graph (a tree: the model references itself,
197
+ # directly or through other models) compiles as a
198
+ # self-referential plan: the build in progress publishes its row
199
+ # list to the builds it spawns, so the cycle point references
200
+ # the same rows instead of refusing the model (#1297). Both the
201
+ # publication and the session's results are thread-local: a
202
+ # concurrent build never sees them, and a session commits only
203
+ # when its outermost build succeeds — an inner plan referencing
204
+ # rows a failed outer build never finished must not survive.
205
+ building = (Thread.current[:kv_group_plan_building] ||= {})
206
+ return building[key] if building.key?(key)
207
+
208
+ rows = []
209
+ building[key] = rows
205
210
  begin
206
- KV_GROUP_PLANS[key] = build_kv_group_plan(model_class, format,
207
- register)
211
+ built = build_kv_group_plan(model_class, format, register, rows)
212
+ (Thread.current[:kv_group_plan_session] ||= []) << [key, built]
208
213
  ensure
209
- stack.pop
214
+ building.delete(key)
215
+ if building.empty?
216
+ session = Thread.current[:kv_group_plan_session]
217
+ Thread.current[:kv_group_plan_session] = nil
218
+ # Only the outermost verdict decides: an inner plan that
219
+ # completed references the outer rows, so a failed outer
220
+ # build (false, an exception) commits nothing at all. A
221
+ # concurrent build that committed first keeps its plan.
222
+ if built
223
+ session&.each do |built_key, result|
224
+ KV_GROUP_PLANS[built_key] = result unless KV_GROUP_PLANS.key?(built_key)
225
+ end
226
+ end
227
+ end
210
228
  end
229
+ built
211
230
  end
212
231
 
213
- def self.build_kv_group_plan(model_class, format, register)
232
+ def self.build_kv_group_plan(model_class, format, register, rows = [])
214
233
  return false unless model_class.is_a?(Class) &&
215
234
  model_class.include?(Lutaml::Model::Serialize)
216
235
 
@@ -229,8 +248,6 @@ module Lutaml
229
248
  "map_unmapped target :#{unmapped_target} is not a " \
230
249
  "declared attribute of #{model_class}"
231
250
  end
232
- rows = nil
233
- claimed = nil
234
251
  mapping.mappings(register).each do |rule|
235
252
  eligible = !rule.name.nil? && !rule.multiple_mappings? &&
236
253
  rule.delegate.nil? &&
@@ -274,7 +291,6 @@ module Lutaml
274
291
  rule.hash_mappings)
275
292
  return false unless child_plan
276
293
 
277
- rows ||= []
278
294
  rows << [rule, attr, :child_map, child_plan, nil, nil]
279
295
  next
280
296
  end
@@ -285,7 +301,6 @@ module Lutaml
285
301
  child_rows = kv_group_plan(type, format, child_register)
286
302
  return false unless child_rows
287
303
 
288
- rows ||= []
289
304
  # child_register rides the row: the plan was BUILT under it,
290
305
  # and rebuilding the resolution per model row per document
291
306
  # cost a Concurrent::Map hop each time (TODO.perf 14).
@@ -293,7 +308,6 @@ module Lutaml
293
308
  elsif type.is_a?(Class) && type < Lutaml::Model::Type::Value &&
294
309
  !attr.value_policy.whole_value?(type) &&
295
310
  !Lutaml::Model::Attribute.custom_from_probe?(type)
296
- rows ||= []
297
311
  rows << [rule, attr, :scalar, nil, nil, nil]
298
312
  else
299
313
  return false
@@ -305,11 +319,10 @@ module Lutaml
305
319
  Array(rule.name).each { |name| claimed[name.to_s] = true } if rule.name
306
320
  end
307
321
  claimed.freeze
308
- (rows ||= []) << [nil, nil, :unmapped, unmapped_target, claimed,
309
- nil]
322
+ rows << [nil, nil, :unmapped, unmapped_target, claimed, nil]
310
323
  end
311
324
  # No eligible rows at all (empty mapping) — nothing to gain.
312
- rows
325
+ rows.empty? ? nil : rows
313
326
  end
314
327
 
315
328
  # Class-level twin of the walk's child-map compile: the registry
@@ -338,30 +351,150 @@ child_mappings)
338
351
  nil
339
352
  end
340
353
 
354
+ # One model under construction by the explicit-stack group build:
355
+ # its row scan, the positional values collected so far and the
356
+ # child frame it needs next. One frame per nesting level, reused
357
+ # across siblings, so hydration depth is bound by the heap rather
358
+ # than the VM stack (#1297).
359
+ GroupBuildFrame = ::Struct.new(
360
+ :transform, :rows, :doc, :format, :register, :options,
361
+ :slots, :children, :absent, :unmapped, :index,
362
+ :pending_index, :pending_items, :pending_cursor, :pending_built,
363
+ :request_transform, :request_rows, :request_register, :request_doc,
364
+ :instance
365
+ ) do
366
+ def start(transform, rows, doc, format, register, options)
367
+ self.transform = transform
368
+ self.rows = rows
369
+ self.doc = doc
370
+ self.format = format
371
+ self.register = register
372
+ self.options = options
373
+ # Positional values, not per-row [name, value] pairs: rows are
374
+ # a fixed per-class list, so the writer to apply lives at the
375
+ # row's index and the pair arrays were the lane's biggest
376
+ # allocation (~6 arrays per node on hydrate-heavy corpora).
377
+ self.slots = Array.new(rows.size, MISSING)
378
+ # Lazy: the scalar hot path never pushes a child; the shared
379
+ # wiring pass tolerates nil.
380
+ self.children = nil
381
+ # Lazy: the present-key hot path never pushes, and this runs
382
+ # once per hydrated model instance.
383
+ self.absent = nil
384
+ self.unmapped = nil
385
+ self.index = 0
386
+ clear_pending
387
+ self.instance = nil
388
+ self
389
+ end
390
+
391
+ # A :model row hydrates through child frames, one per document:
392
+ # singular rows have a single pending item.
393
+ def begin_pending(row_index, items, transform, rows, register)
394
+ self.pending_index = row_index
395
+ self.pending_items = items
396
+ self.pending_cursor = 0
397
+ self.pending_built = []
398
+ self.request_transform = transform
399
+ self.request_rows = rows
400
+ self.request_register = register
401
+ end
402
+
403
+ def pending?
404
+ !pending_items.nil?
405
+ end
406
+
407
+ def pending_remaining?
408
+ pending_cursor < pending_items.size
409
+ end
410
+
411
+ # The next child document, or nil for a shape the plan does not
412
+ # cover (a non-Hash item where a model was expected).
413
+ def next_child_document
414
+ item = pending_items[pending_cursor]
415
+ item.is_a?(::Hash) ? item : nil
416
+ end
417
+
418
+ # A finished child joins the row being hydrated; the row's value
419
+ # slot is written once its last child is in.
420
+ def deliver(child)
421
+ pending_built << child
422
+ (self.children ||= []) << child
423
+ self.pending_cursor += 1
424
+ complete_pending! unless pending_remaining?
425
+ end
426
+
427
+ def complete_pending!
428
+ collection = rows[pending_index][1].collection?
429
+ slots[pending_index] = collection ? pending_built : pending_built.first
430
+ clear_pending
431
+ end
432
+
433
+ def clear_pending
434
+ self.pending_index = nil
435
+ self.pending_items = nil
436
+ self.pending_cursor = 0
437
+ self.pending_built = nil
438
+ end
439
+ end
440
+
341
441
  # Build values bottom-up, then ONE instance per model: present
342
442
  # keys through the casting setters, ABSENT rules through the real
343
443
  # per-rule walk (their extractor/defaults/sentinel semantics are
344
444
  # the walk's own — reproducing them here would fork them). The
345
445
  # per-rule walk is eliminated exactly for the present-key hot
346
- # path. Returns nil (caller falls back) on any shape the plan
347
- # does not cover, e.g. a non-Hash item where a model was expected.
446
+ # path. Children hydrate through frames on an explicit stack, not
447
+ # recursion: depth is not bound by the VM stack (#1297). Returns
448
+ # nil (caller falls back) on any shape the plan does not cover,
449
+ # e.g. a non-Hash item where a model was expected.
348
450
  def kv_group_build(rows, doc, format, register, options)
349
- # Positional values, not per-row [name, value] pairs: rows are a
350
- # fixed per-class list, so the writer to apply lives at the row's
351
- # index and the pair arrays were the lane's biggest allocation
352
- # (~6 arrays per node on hydrate-heavy corpora).
353
- values = Array.new(rows.size, MISSING)
354
- # Lazy: the scalar hot path never pushes a child; the shared
355
- # wiring pass tolerates nil.
356
- children = nil
357
- # Lazy: the present-key hot path never pushes, and this runs
358
- # once per hydrated model instance.
359
- absent = nil
360
- unmapped = nil
361
- i = 0
362
- rows.each do |rule, attr, kind, type, child_rows, child_register|
451
+ frames = []
452
+ frame = GroupBuildFrame.new.start(self, rows, doc, format, register,
453
+ options)
454
+ frames << frame
455
+ loop do
456
+ # Each frame is stepped by its own transform, so the walk's
457
+ # private per-rule machinery stays private.
458
+ case frame.transform.kv_frame_step(frame)
459
+ when :child
460
+ # The request rides the frame being stepped; read it before
461
+ # the new frame takes the loop variable.
462
+ child = frames[frames.size] ||= GroupBuildFrame.new
463
+ frame = child.start(frame.request_transform, frame.request_rows,
464
+ frame.request_doc, format,
465
+ frame.request_register, options)
466
+ when :fallback
467
+ return nil
468
+ else
469
+ instance = frame.instance
470
+ frames.pop
471
+ return instance if frames.empty?
472
+
473
+ frame = frames.last
474
+ frame.deliver(instance)
475
+ end
476
+ end
477
+ end
478
+
479
+ # Advances a frame one step: the next child frame it needs
480
+ # (:child), the finished instance (:done), or a shape the plan does
481
+ # not cover (:fallback).
482
+ def kv_frame_step(frame)
483
+ if frame.pending?
484
+ if frame.pending_remaining?
485
+ return request_child(frame)
486
+ end
487
+
488
+ frame.complete_pending!
489
+ end
490
+ rows = frame.rows
491
+ i = frame.index
492
+ while i < rows.size
493
+ row = rows[i]
494
+ rule = row[0]
495
+ kind = row[2]
363
496
  if kind == :unmapped
364
- unmapped = [type, child_rows]
497
+ frame.unmapped = [row[3], row[4]]
365
498
  i += 1
366
499
  next
367
500
  end
@@ -369,9 +502,9 @@ child_mappings)
369
502
  # One hash pass per row: fetch with a sentinel distinguishes
370
503
  # absent keys from present-nil values (both read as nil), so
371
504
  # the separate string_or_symbol_key? probe goes away.
372
- v = Lutaml::Model::Utils.fetch_str_or_sym(doc, rule.name, MISSING)
505
+ v = Lutaml::Model::Utils.fetch_str_or_sym(frame.doc, rule.name, MISSING)
373
506
  if v.equal?(MISSING)
374
- (absent ||= []) << rule
507
+ (frame.absent ||= []) << rule
375
508
  i += 1
376
509
  next
377
510
  end
@@ -383,89 +516,95 @@ child_mappings)
383
516
  # parent/root wiring rides the shared children pass. A
384
517
  # non-Hash value falls back to the walk, which owns that
385
518
  # error.
386
- return nil unless v.is_a?(::Hash)
387
-
388
- built = []
389
- keys_only = type.keys_only && v.values.all?(::Hash)
390
- v.each do |child_key, child_value|
391
- child_hash = {}
392
- type.child_mappings.each_key do |attr_name|
393
- child_hash[type.wire_names[attr_name]] =
394
- extract_compiled_child_value(type.paths[attr_name],
395
- child_key, child_value)
396
- end
397
- child_hash.update(child_value) if keys_only
398
- built << self.class.data_to_model(type.attr_type,
399
- child_hash, format, { mappings: type.child_rules })
400
- end
401
- values[i] = built
402
- (children ||= []).concat(built)
519
+ return :fallback unless v.is_a?(::Hash)
520
+
521
+ frame.slots[i] = kv_build_child_map(row[3], v, frame.format)
522
+ (frame.children ||= []).concat(frame.slots[i])
403
523
  i += 1
404
524
  next
405
525
  end
406
526
 
407
527
  if kind == :scalar
408
- values[i] = v
528
+ frame.slots[i] = v
409
529
  i += 1
410
530
  next
411
531
  end
412
532
 
413
- if attr.collection?
414
- # A present-but-nil collection reaches the per-rule walk,
415
- # which owns the sentinel interplay for that edge
416
- # (render_nil :as_empty semantics).
417
- return nil if v.nil?
418
-
419
- items = v.is_a?(::Array) ? v : [v]
420
- built = []
421
- items.each do |item|
422
- return nil unless item.is_a?(::Hash)
533
+ # A present-but-nil collection reaches the per-rule walk,
534
+ # which owns the sentinel interplay for that edge
535
+ # (render_nil :as_empty semantics).
536
+ return :fallback if row[1].collection? && v.nil?
537
+
538
+ items = if row[1].collection?
539
+ v.is_a?(::Array) ? v : [v]
540
+ else
541
+ [v]
542
+ end
543
+ frame.index = i + 1
544
+ frame.begin_pending(i, items,
545
+ self.class.cached_transform(row[3], row[5]),
546
+ row[4], row[5])
547
+ return request_child(frame)
548
+ end
549
+ frame.index = i
550
+ frame.instance = kv_finalize_frame(frame)
551
+ :done
552
+ end
423
553
 
424
- child = self.class.kv_group_instance(type, child_rows, item,
425
- format, child_register,
426
- options)
427
- return nil if child.nil?
554
+ private
428
555
 
429
- built << child
430
- end
431
- values[i] = built
432
- (children ||= []).concat(built)
433
- else
434
- return nil unless v.is_a?(::Hash)
556
+ # :child when the frame's pending row has a document the plan
557
+ # covers, :fallback otherwise.
558
+ def request_child(frame)
559
+ document = frame.next_child_document
560
+ return :fallback unless document
435
561
 
436
- child = self.class.kv_group_instance(type, child_rows, v,
437
- format, child_register,
438
- options)
439
- return nil if child.nil?
562
+ frame.request_doc = document
563
+ :child
564
+ end
440
565
 
441
- values[i] = child
442
- (children ||= []) << child
566
+ def kv_build_child_map(plan, value, format)
567
+ built = []
568
+ keys_only = plan.keys_only && value.values.all?(::Hash)
569
+ value.each do |child_key, child_value|
570
+ child_hash = {}
571
+ plan.child_mappings.each_key do |attr_name|
572
+ child_hash[plan.wire_names[attr_name]] =
573
+ extract_compiled_child_value(plan.paths[attr_name],
574
+ child_key, child_value)
443
575
  end
444
- i += 1
576
+ child_hash.update(child_value) if keys_only
577
+ built << self.class.data_to_model(plan.attr_type, child_hash,
578
+ format, { mappings: plan.child_rules })
445
579
  end
446
- instance = model_class.allocate_for_walk(register)
580
+ built
581
+ end
582
+
583
+ def kv_finalize_frame(frame)
584
+ instance = model_class.allocate_for_walk(frame.register)
585
+ values = frame.slots
447
586
  i = 0
448
587
  # Index access, not block-parameter splat: `*_rest` re-sliced a
449
588
  # fresh Array per row per instance (the walk's largest our-side
450
589
  # allocation line on the synthetic KV corpus).
451
- rows.each do |row|
590
+ frame.rows.each do |row|
452
591
  v = values[i]
453
592
  i += 1
454
593
  next if v.equal?(MISSING) || row[2] == :unmapped
455
594
 
456
595
  instance.public_send(row[0].kv_setter, v)
457
596
  end
458
- absent&.each do |rule|
459
- process_mapping_rule(doc, instance, format, rule, options, nil)
597
+ frame.absent&.each do |rule|
598
+ process_mapping_rule(frame.doc, instance, frame.format, rule, frame.options, nil)
460
599
  end
461
- children&.each do |child|
600
+ frame.children&.each do |child|
462
601
  child.lutaml_parent = instance
463
602
  child.lutaml_root ||= instance.lutaml_root || instance
464
603
  end
465
- if unmapped
466
- target, claimed = unmapped
604
+ if frame.unmapped
605
+ target, claimed = frame.unmapped
467
606
  extras = nil
468
- doc.each do |key, value|
607
+ frame.doc.each do |key, value|
469
608
  next if claimed.key?(key.to_s)
470
609
 
471
610
  (extras ||= {})[key] = value
@@ -479,16 +618,6 @@ child_hash, format, { mappings: type.child_rules })
479
618
  instance
480
619
  end
481
620
 
482
- def self.kv_group_instance(model_class, rows, doc, format, register,
483
- options)
484
- # cached_transform is per (class, register); format rides the
485
- # call, not the cache.
486
- cached_transform(model_class, register)
487
- .kv_group_build(rows, doc, format, register, options)
488
- end
489
-
490
- private
491
-
492
621
  def process_rule!(instance, rule, hash, format, _mappings, options)
493
622
  return handle_delegate(instance, rule, hash, format) if rule.delegate
494
623
 
@@ -206,14 +206,13 @@ module Lutaml
206
206
  # yields each resolved element-order item with its owning schema.
207
207
  def each_schema_item(schemas, &dispatch)
208
208
  schemas.each do |schema|
209
- # Override-applied documents ride the override collection
210
- # alongside the Override declarations themselves (the mixed
211
- # shape the include/import collections already have); only
212
- # the applied Schema objects carry a walkable element order.
213
- if schema.respond_to?(:override) && schema.override&.any?
214
- schema.override.grep(Lutaml::Xml::Schema::Xsd::Schema).each do |applied|
215
- each_schema_item([applied], &dispatch)
216
- end
209
+ # An override's composed view is reachable from its entry
210
+ # (the override collection holds the declarations only);
211
+ # the view carries the referenced document's walkable
212
+ # element order with the replacements substituted.
213
+ Array(schema.override).each do |entry|
214
+ view = entry.composed
215
+ each_schema_item([view], &dispatch) if view
217
216
  end
218
217
  if schema.include&.any?
219
218
  each_schema_item(schema.include,
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.199"
5
+ VERSION = "0.8.200"
6
6
  end
7
7
  end
@@ -56,31 +56,72 @@ module Lutaml
56
56
  Glob.include_schema(schema_path) if schema_path && Glob.location?
57
57
  end
58
58
 
59
- # XSD 1.1 §4.2.4: the referenced document's components are
60
- # replaced by this override's same-named children. Matching is
61
- # by component kind and name; a child with no counterpart adds
62
- # to the referenced document (lenient); children without a name
63
- # (ref-form, anonymous) carry no replacement. Applied on a copy
64
- # of the referenced schema so the override stays local to the
65
- # referencing document.
66
- def apply_to!(target_schema)
67
- COMPONENT_KINDS.each do |kind, name_attr|
68
- Array(public_send(kind)).each do |replacement|
69
- name = replacement.public_send(name_attr)
70
- next if name.nil?
59
+ # The composed view of the referenced document (XSD 1.1 §4.2.4):
60
+ # a new schema whose component collections carry each same-named
61
+ # component replaced by this override's child and the rest of
62
+ # the children appended. Matching is by component kind and
63
+ # name; a child with no counterpart adds to the view (lenient);
64
+ # children without a name (ref-form, anonymous) carry no
65
+ # replacement. The referenced document and its components are
66
+ # shared by reference and never mutated, so every document
67
+ # overriding the same target composes against the same
68
+ # originals.
69
+ def composed
70
+ @composed
71
+ end
72
+
73
+ # Composes (once) against the referenced document; the entry is
74
+ # shared by every viewer, so a chase coming back to it finds the
75
+ # view already composed (#1299).
76
+ def compose(referenced)
77
+ return @composed if @composed
78
+
79
+ view = Schema.new(**view_attributes(referenced))
80
+ view.element_order = referenced.element_order
81
+ @composed = view
82
+ end
83
+
84
+ # Root the composed view as its own schema root, as the parsed
85
+ # document it views is.
86
+ def assign_root!(root = self, seen = nil)
87
+ seen ||= {}.compare_by_identity
88
+ super
89
+ composed&.assign_root!(composed, seen)
90
+ self
91
+ end
71
92
 
72
- components = target_schema.public_send(kind)
73
- index = components.index do |component|
74
- component.public_send(name_attr) == name
75
- end
76
- if index
77
- components[index] = replacement
78
- else
79
- components << replacement
80
- end
93
+ private
94
+
95
+ def view_attributes(referenced)
96
+ Schema.attributes.each_with_object({}) do |(name, _attribute), values|
97
+ value = referenced.public_send(name)
98
+ values[name] = if COMPONENT_KINDS.key?(name)
99
+ substitute_components(name, value)
100
+ elsif value.is_a?(Array)
101
+ value.dup
102
+ else
103
+ value
104
+ end
105
+ end
106
+ end
107
+
108
+ def substitute_components(kind, components)
109
+ name_attr = COMPONENT_KINDS[kind]
110
+ substituted = Array(components).dup
111
+ Array(public_send(kind)).each do |replacement|
112
+ name = replacement.public_send(name_attr)
113
+ next if name.nil?
114
+
115
+ index = substituted.index do |component|
116
+ component.public_send(name_attr) == name
117
+ end
118
+ if index
119
+ substituted[index] = replacement
120
+ else
121
+ substituted << replacement
81
122
  end
82
123
  end
83
- target_schema
124
+ substituted
84
125
  end
85
126
 
86
127
  Lutaml::Xml::Schema::Xsd.register_model(self, :override)
@@ -294,24 +294,31 @@ module Lutaml
294
294
  alias attributes attribute
295
295
  alias groups group
296
296
 
297
+ # XSD 1.1 §4.2.4 assembly: every override entry composes the
298
+ # view of its referenced document once. The entry is shared by
299
+ # reference with every viewer, so the view is too, and a chase
300
+ # that comes back to a location on its own chain finds the
301
+ # entry still uncomposed and leaves it so (as for a cyclic
302
+ # include) instead of chasing its own assembly forever
303
+ # (#1299). Runs after the tree is parsed (the override's
304
+ # children are populated), before root assignment.
297
305
  def apply_overrides!(seen = nil)
298
306
  seen ||= {}.compare_by_identity
299
307
  return self if seen[self]
300
308
 
301
309
  seen[self] = true
302
310
  Array(override).each do |entry|
303
- next unless entry.is_a?(Override) && entry.schema_path
304
- next if self.class.in_progress?(entry.schema_path)
311
+ next unless entry.is_a?(Override)
305
312
 
306
- self.class.add_in_progress(entry.schema_path)
307
- referenced = insert_in_processed_schemas(entry)
308
- self.class.remove_in_progress(entry.schema_path)
309
- next unless referenced
313
+ path = entry.schema_path
314
+ next unless path
315
+ next if entry.composed || self.class.in_progress?(path)
310
316
 
311
- applied = Lutaml::Model::Utils.deep_dup(referenced)
312
- entry.apply_to!(applied)
313
- override(applied)
314
- applied.apply_overrides!(seen)
317
+ self.class.add_in_progress(path)
318
+ referenced = insert_in_processed_schemas(entry)
319
+ entry.compose(referenced) if referenced
320
+ ensure
321
+ self.class.remove_in_progress(path)
315
322
  end
316
323
  %i[include import].each do |kind|
317
324
  Array(public_send(kind)).each do |nested|
@@ -338,15 +345,10 @@ module Lutaml
338
345
  namespaces.uniq
339
346
  end
340
347
 
341
- # XSD 1.1 s4.2.4 override application (the assembly pass): each
342
- # override resolves its referenced document through the same
343
- # location machinery includes use, replaces the same-named
344
- # components on a COPY (the processed-schemas memo shares
345
- # originals across references; an override is local to this
346
- # reference), and appends the applied document to the override
347
- # collection so the schema walk reaches it in place of the raw
348
- # document. Runs after the tree is parsed (the override's
349
- # children are populated), before root assignment.
348
+ # An xs:include / xs:import declaration resolves its document
349
+ # through the location machinery; a location already on the
350
+ # chain is not chased again (cyclic references compose as
351
+ # multiple inclusions of the same document).
350
352
  def setup_import_and_include(klass, model, schema, args = {})
351
353
  instance = init_instance_of(klass, schema.attributes || {}, args)
352
354
  annotation_object(instance, schema)
@@ -424,6 +426,7 @@ module Lutaml
424
426
  class << self
425
427
  def reset_processed_schemas
426
428
  @processed_schemas = {}
429
+ @in_progress = []
427
430
  end
428
431
 
429
432
  def processed_schemas
@@ -7,15 +7,18 @@ require "lutaml/lml"
7
7
 
8
8
  RSpec.configure do |config|
9
9
  config.define_derived_metadata(file_path: %r{elk_layout_spec}) do |metadata|
10
-
11
- metadata[:skip] = "elkrb gem not available" unless (require "elkrb"
12
- true)
13
- rescue StandardError
14
- false
15
-
10
+ metadata[:skip] = "elkrb gem not available" unless elkrb_available?
16
11
  end
17
12
  end
18
13
 
14
+ # elkrb is an optional layout engine; its absence skips the layout specs.
15
+ def elkrb_available?
16
+ require "elkrb"
17
+ true
18
+ rescue LoadError
19
+ false
20
+ end
21
+
19
22
  def fixtures_path(path)
20
23
  File.join(File.expand_path("fixtures", __dir__), path)
21
24
  end
@@ -223,20 +223,22 @@ 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 "rejects self-referential models (cycle)" do
226
+ it "compiles self-referential models as a plan referencing itself (#1297)" do
227
227
  node = Class.new(Lutaml::Model::Serializable) do
228
228
  attribute :name, :string
229
229
  json { map "name", to: :name }
230
230
  end
231
231
  node.attribute :child, node
232
- node.json do
232
+ node.json do
233
233
  map "name", to: :name
234
234
  map "child", to: :child
235
235
  end
236
236
  stub_const("KvGroup::Node", node)
237
237
 
238
- expect(Lutaml::KeyValue::Transform.kv_group_plan(node, :json,
239
- :default)).to be(false)
238
+ plan = Lutaml::KeyValue::Transform.kv_group_plan(node, :json, :default)
239
+ expect(plan).to be_an(Array)
240
+ child_row = plan.find { |row| row[0].to == :child }
241
+ expect(child_row[4]).to equal(plan)
240
242
  parsed = node.from_hash("name" => "n", "child" => { "name" => "c" })
241
243
  expect(parsed.child.name).to eq("c")
242
244
  end
@@ -284,7 +286,7 @@ RSpec.describe "KV group-then-bulk hydration" do
284
286
  expect(verdicts.uniq.length).to eq(1)
285
287
  end
286
288
 
287
- it "keeps the cycle verdict per-build across threads" do
289
+ it "builds self-referential plans per thread, each referencing itself" do
288
290
  node = Class.new(Lutaml::Model::Serializable) do
289
291
  attribute :name, :string
290
292
  json { map "name", to: :name }
@@ -303,7 +305,10 @@ RSpec.describe "KV group-then-bulk hydration" do
303
305
  end
304
306
  end.map(&:value)
305
307
 
306
- expect(verdicts).to all(be(false))
308
+ expect(verdicts).to all(be_an(Array))
309
+ verdicts.each do |plan|
310
+ expect(plan.find { |row| row[0].to == :child }[4]).to equal(plan)
311
+ end
307
312
  end
308
313
  end
309
314
 
@@ -0,0 +1,163 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # lutaml-model#1297: a recursive model graph (a tree) compiles as a
6
+ # self-referential plan instead of being refused to the interpretive
7
+ # walk, and the compiled executor hydrates on an explicit frame stack —
8
+ # hydration depth is bound by the heap, not the VM stack.
9
+ class RecursivePlanNode < Lutaml::Model::Serializable
10
+ attribute :name, :string
11
+ attribute :child, RecursivePlanNode
12
+ attribute :children, RecursivePlanNode, collection: true,
13
+ initialize_empty: true
14
+
15
+ key_value do
16
+ map "name", to: :name
17
+ map "child", to: :child
18
+ map "children", to: :children
19
+ end
20
+ end
21
+
22
+ class RecursivePlanTwig < Lutaml::Model::Serializable
23
+ attribute :name, :string
24
+ attribute :branch, "RecursivePlanBranch"
25
+
26
+ key_value do
27
+ map "name", to: :name
28
+ map "branch", to: :branch
29
+ end
30
+ end
31
+
32
+ class RecursivePlanBranch < Lutaml::Model::Serializable
33
+ attribute :name, :string
34
+ attribute :twigs, RecursivePlanTwig, collection: true,
35
+ initialize_empty: true
36
+
37
+ key_value do
38
+ map "name", to: :name
39
+ map "twigs", to: :twigs
40
+ end
41
+ end
42
+
43
+ # The outer's second row is ineligible (a custom deserializer), so the
44
+ # outer build fails after the inner completed against its rows.
45
+ class RecursivePlanInner < Lutaml::Model::Serializable
46
+ attribute :outer, "RecursivePlanOuter"
47
+
48
+ key_value { map "outer", to: :outer }
49
+ end
50
+
51
+ class RecursivePlanOuter < Lutaml::Model::Serializable
52
+ attribute :inner, RecursivePlanInner
53
+ attribute :note, :string
54
+
55
+ key_value do
56
+ map "inner", to: :inner
57
+ map "note", to: :note, with: { from: :note_from }
58
+ end
59
+
60
+ def self.note_from(_model, value)
61
+ value.to_s
62
+ end
63
+ end
64
+
65
+ RSpec.describe "recursive model graphs on the compiled KV plan" do
66
+ def plan_for(klass)
67
+ Lutaml::KeyValue::Transform.kv_group_plan(klass, :hash, :default)
68
+ end
69
+
70
+ def row_for(plan, attribute)
71
+ plan.find { |row| row[0].to == attribute }
72
+ end
73
+
74
+ def chain(depth, key = "child")
75
+ h = { "name" => "leaf" }
76
+ (depth - 1).times do
77
+ h = { "name" => "n", key => (key == "child" ? h : [h]) }
78
+ end
79
+ h
80
+ end
81
+
82
+ def thicket(depth)
83
+ h = { "name" => "leaf" }
84
+ (depth - 1).times do
85
+ h = { "name" => "b", "twigs" => [{ "name" => "t", "branch" => h }] }
86
+ end
87
+ h
88
+ end
89
+
90
+ it "compiles a self-referencing model as a plan that references itself" do
91
+ plan = plan_for(RecursivePlanNode)
92
+ expect(plan).to be_an(Array)
93
+ expect(row_for(plan, :child)[4]).to equal(plan)
94
+ expect(row_for(plan, :children)[4]).to equal(plan)
95
+ end
96
+
97
+ it "compiles a mutually recursive pair into plans that reference each other" do
98
+ branch = plan_for(RecursivePlanBranch)
99
+ twig = plan_for(RecursivePlanTwig)
100
+ expect(branch).to be_an(Array)
101
+ expect(twig).to be_an(Array)
102
+ expect(row_for(branch, :twigs)[4]).to equal(twig)
103
+ expect(row_for(twig, :branch)[4]).to equal(branch)
104
+ end
105
+
106
+ it "caches the recursive plans once the outermost build succeeds" do
107
+ first = plan_for(RecursivePlanNode)
108
+ expect(plan_for(RecursivePlanNode)).to equal(first)
109
+ twig = plan_for(RecursivePlanTwig)
110
+ expect(row_for(plan_for(RecursivePlanBranch), :twigs)[4]).to equal(twig)
111
+ end
112
+
113
+ it "commits nothing when the outer build fails after an inner completed" do
114
+ expect(plan_for(RecursivePlanOuter)).to be(false)
115
+ # a cached inner plan would reference the failed outer's partial rows
116
+ expect(plan_for(RecursivePlanInner)).to be(false)
117
+ end
118
+
119
+ it "hydrates a self-referencing tree past the VM stack depth" do
120
+ node = RecursivePlanNode.from_hash(chain(10_000))
121
+ depth = 0
122
+ while node.child
123
+ depth += 1
124
+ node = node.child
125
+ end
126
+ # 10,000 levels, one child hop each below the root
127
+ expect(depth).to eq(9_999)
128
+ expect(node.name).to eq("leaf")
129
+ end
130
+
131
+ it "hydrates a mutually recursive graph past the VM stack depth" do
132
+ branch = RecursivePlanBranch.from_hash(thicket(5000))
133
+ depth = 0
134
+ while (twig = branch.twigs.first)&.branch
135
+ depth += 1
136
+ branch = twig.branch
137
+ end
138
+ expect(depth).to eq(4999)
139
+ end
140
+
141
+ it "keeps collection order and the parent wiring through the frame stack" do
142
+ doc = {
143
+ "name" => "root",
144
+ "children" => [
145
+ { "name" => "a", "children" => [{ "name" => "aa" }] },
146
+ { "name" => "b" },
147
+ ],
148
+ }
149
+ node = RecursivePlanNode.from_hash(doc)
150
+ expect(node.children.map(&:name)).to eq(%w[a b])
151
+ expect(node.children.first.children.first.name).to eq("aa")
152
+ expect(node.children.first.lutaml_parent).to equal(node)
153
+ expect(node.children.first.children.first.lutaml_parent)
154
+ .to equal(node.children.first)
155
+ expect(node.to_hash).to eq(doc)
156
+ end
157
+
158
+ it "falls back to the walk for a non-Hash item where a model was expected" do
159
+ doc = { "name" => "root", "child" => { "name" => "c" } }
160
+ expect(RecursivePlanNode.from_hash(doc).child.name).to eq("c")
161
+ expect(RecursivePlanNode.from_hash({ "name" => "root" }).child).to be_nil
162
+ end
163
+ 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,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.199
4
+ version: 0.8.200
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -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
@@ -2428,6 +2429,7 @@ files:
2428
2429
  - spec/lutaml/xml/schema/xsd_all_any_spec.rb
2429
2430
  - spec/lutaml/xml/schema/xsd_identity_ref_spec.rb
2430
2431
  - spec/lutaml/xml/schema/xsd_override_assembly_spec.rb
2432
+ - spec/lutaml/xml/schema/xsd_override_cycle_spec.rb
2431
2433
  - spec/lutaml/xml/schema/xsd_override_spec.rb
2432
2434
  - spec/lutaml/xml/schema/xsd_scale_vc_spec.rb
2433
2435
  - spec/lutaml/xml/schema_primer_spec.rb