lutaml-model 0.8.205 → 0.8.207

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.
@@ -373,6 +373,220 @@ module ProductionLaneParitySpec
373
373
  map_element "type", to: :type
374
374
  end
375
375
  end
376
+
377
+ class RawCapture < Lutaml::Model::Serializable
378
+ attribute :svg_data, :string
379
+
380
+ xml do
381
+ element "container"
382
+ map_element "svg", to: :svg_data, raw: :element
383
+ end
384
+ end
385
+
386
+ class MixedRawCollection < Lutaml::Model::Serializable
387
+ attribute :name, :string
388
+ attribute :fragments, :string, collection: true
389
+
390
+ xml do
391
+ element "container"
392
+ mixed_content
393
+ map_element "name", to: :name
394
+ map_element "fragment", to: :fragments, raw: :element
395
+ end
396
+ end
397
+
398
+ class InnerContent < Lutaml::Model::Serializable
399
+ attribute :street, :string
400
+
401
+ xml do
402
+ element "address"
403
+ map_element "street", to: :street, raw: :content
404
+ end
405
+ end
406
+
407
+ class NestedTitle < Lutaml::Model::Serializable
408
+ attribute :content, :string
409
+
410
+ xml do
411
+ element "artifact"
412
+ map_element "content", to: :content
413
+ end
414
+ end
415
+
416
+ class NestedTitles < Lutaml::Model::Collection
417
+ instances :items, NestedTitle
418
+
419
+ xml do
420
+ element "title-group"
421
+ map_element "artifact", to: :items
422
+ end
423
+ end
424
+
425
+ class NestedBibitem < Lutaml::Model::Serializable
426
+ attribute :titles, NestedTitles
427
+
428
+ xml do
429
+ element "bibitem"
430
+ map_element "titles", to: :titles
431
+ end
432
+ end
433
+
434
+ class Sequenced < Lutaml::Model::Serializable
435
+ attribute :title, :string
436
+ attribute :note, :string
437
+ attribute :size, :string
438
+
439
+ xml do
440
+ element "sequenced"
441
+ map_element :title, to: :title
442
+ sequence do
443
+ map_element :note, to: :note
444
+ map_element :size, to: :size
445
+ end
446
+ end
447
+ end
448
+
449
+ class SequencedHolder < Lutaml::Model::Serializable
450
+ attribute :items, Sequenced, collection: true
451
+
452
+ xml do
453
+ element "holder"
454
+ map_element "sequenced", to: :items
455
+ end
456
+ end
457
+
458
+ class CustomSingular < Lutaml::Model::Serializable
459
+ attribute :label, :string
460
+
461
+ xml do
462
+ element "custom"
463
+ map_element "label", to: :label,
464
+ with: { from: :label_from, to: :label_to }
465
+ end
466
+
467
+ def self.received
468
+ @received ||= []
469
+ end
470
+
471
+ def label_from(model, node)
472
+ self.class.received << node
473
+ texts = Array(node).map(&:text)
474
+ model.label = texts.size > 1 ? texts : texts.first
475
+ end
476
+
477
+ def label_to(model, parent, doc); end
478
+ end
479
+
480
+ class SingularContent < Lutaml::Model::Serializable
481
+ attribute :bold, :string, collection: true
482
+ attribute :content, :string
483
+
484
+ xml do
485
+ element "r"
486
+ map_element "bold", to: :bold
487
+ map_content to: :content
488
+ end
489
+ end
490
+
491
+ class UnmappedMixedText < Lutaml::Model::Serializable
492
+ attribute :singular, :string
493
+ attribute :items, :string, collection: true
494
+
495
+ xml do
496
+ element "container"
497
+ mixed_content
498
+ map_element "singular", to: :singular
499
+ map_element "items", to: :items
500
+ end
501
+ end
502
+
503
+ # A namespaced model with a raw capture: the parsed root's extra
504
+ # declarations must survive the round trip (the interpretive writer's
505
+ # stored-plan behavior).
506
+ RawDocNs = Class.new(Lutaml::Xml::W3c::XmlNamespace) do
507
+ uri "http://example.com/production-lane/raw-doc"
508
+ prefix_default "d"
509
+ end
510
+
511
+ class RawDoc < Lutaml::Model::Serializable
512
+ attribute :title, :string
513
+ attribute :svg_data, :string
514
+
515
+ xml do
516
+ element "doc"
517
+ namespace RawDocNs
518
+ map_element "title", to: :title
519
+ map_element "svg", to: :svg_data, raw: :element
520
+ end
521
+ end
522
+
523
+ DoubledNs = Class.new(Lutaml::Xml::W3c::XmlNamespace) do
524
+ uri "http://example.com/production-lane/doubled"
525
+ prefix_default "ex"
526
+ end
527
+
528
+ class Doubled < Lutaml::Model::Serializable
529
+ attribute :title, :string
530
+ attribute :code, :string
531
+
532
+ xml do
533
+ element "doubled"
534
+ namespace DoubledNs
535
+ map_element "title", to: :title
536
+ map_attribute "code", to: :code
537
+ end
538
+ end
539
+
540
+ class NilMarked < Lutaml::Model::Serializable
541
+ attribute :name, :string
542
+ attribute :note, :string
543
+
544
+ xml do
545
+ element "nil-marked"
546
+ map_element "name", to: :name
547
+ map_element "note", to: :note, render_nil: true
548
+ end
549
+ end
550
+
551
+ class StrictParsed < Lutaml::Model::Serializable
552
+ attribute :name, :string
553
+
554
+ xml do
555
+ element "strict"
556
+ map_content to: :name
557
+ end
558
+ end
559
+
560
+ class UnmappedChildContent < Lutaml::Model::Serializable
561
+ attribute :content, :string
562
+
563
+ xml do
564
+ element "unmapped-content"
565
+ map_content to: :content
566
+ end
567
+ end
568
+
569
+ class UnmappedHolder < Lutaml::Model::Serializable
570
+ attribute :inner, UnmappedChildContent
571
+
572
+ xml do
573
+ element "holder"
574
+ map_element "unmapped-content", to: :inner
575
+ end
576
+ end
577
+
578
+ # An unordered mapping whose instances may carry a tracked
579
+ # element_order (the order-tracker pattern, lutaml-model#977).
580
+ class Tracked < Lutaml::Model::Serializable
581
+ attribute :singular, :string
582
+ attribute :items, :string, collection: true
583
+
584
+ xml do
585
+ element "container"
586
+ map_element "singular", to: :singular
587
+ map_element "items", to: :items
588
+ end
589
+ end
376
590
  end
377
591
 
378
592
  RSpec.describe "production-lane parity" do
@@ -601,6 +815,208 @@ RSpec.describe "production-lane parity" do
601
815
  .to raise_error(Lutaml::Model::TypeOnlyMappingError)
602
816
  end
603
817
 
818
+ describe "raw captures" do
819
+ it "captures an explicitly prefixed element by local name" do
820
+ doc = ProductionLaneParitySpec::RawCapture.from_xml(
821
+ '<container xmlns:s="http://www.w3.org/2000/svg"><s:svg><s:rect/></s:svg></container>',
822
+ )
823
+ expect(doc.svg_data).to include("<s:svg", "<s:rect")
824
+ end
825
+
826
+ it "injects one captured fragment per order entry of a mixed collection" do
827
+ xml = <<~XML
828
+ <container>
829
+ before
830
+ <name>multi</name>
831
+ <fragment id="1">first</fragment>
832
+ <fragment id="2"><nested>second</nested></fragment>
833
+ after
834
+ </container>
835
+ XML
836
+ doc = ProductionLaneParitySpec::MixedRawCollection.from_xml(xml)
837
+ output = doc.to_xml
838
+
839
+ expect(output).not_to include("<fragment><fragment")
840
+ expect(output).to include('id="1"', "first", 'id="2"', "<nested>second</nested>")
841
+ expect(ProductionLaneParitySpec::MixedRawCollection.from_xml(output).fragments.length)
842
+ .to eq(2)
843
+ end
844
+
845
+ it "captures the inner XML for raw: :content" do
846
+ doc = ProductionLaneParitySpec::InnerContent.from_xml(
847
+ "<address><street><b>123</b> Main St</street></address>",
848
+ )
849
+ expect(doc.street).to include("<b>123</b>", "Main St")
850
+ expect(doc.street).not_to include("<street>")
851
+ expect(ProductionLaneParitySpec::InnerContent.from_xml(doc.to_xml).street)
852
+ .to include("<b>123</b>")
853
+ end
854
+ end
855
+
856
+ describe "sequence validation and element order" do
857
+ it "flags an out-of-order member on the parsed root" do
858
+ doc = ProductionLaneParitySpec::Sequenced.from_xml(
859
+ "<sequenced><size>S</size><note>N</note><title>T</title></sequenced>",
860
+ )
861
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::IncorrectSequenceError))
862
+
863
+ ordered = ProductionLaneParitySpec::Sequenced.from_xml(
864
+ "<sequenced><title>T</title><note>N</note><size>S</size></sequenced>",
865
+ )
866
+ expect(ordered.validate).to be_empty
867
+ end
868
+
869
+ it "flags an out-of-order member on a nested member" do
870
+ doc = ProductionLaneParitySpec::SequencedHolder.from_xml(
871
+ "<holder><sequenced><title>T</title><size>S</size><note>N</note></sequenced></holder>",
872
+ )
873
+ expect(doc.items.first.validate)
874
+ .to include(an_instance_of(Lutaml::Model::IncorrectSequenceError))
875
+ end
876
+
877
+ it "serializes a parsed sequence model in mapping order" do
878
+ doc = ProductionLaneParitySpec::Sequenced.from_xml(
879
+ "<sequenced><note>N</note><size>S</size><title>T</title></sequenced>",
880
+ )
881
+ expect(doc.to_xml).to be_xml_equivalent_to(
882
+ "<sequenced><title>T</title><note>N</note><size>S</size></sequenced>",
883
+ )
884
+ end
885
+
886
+ it "serializes a tracked element_order on an unordered plan through the ordered walk" do
887
+ model = ProductionLaneParitySpec::Tracked.new(items: %w[a b], singular: "s")
888
+ model.element_order = [
889
+ Lutaml::Xml::Element.new("Element", "items", node_type: :element),
890
+ Lutaml::Xml::Element.new("Element", "singular", node_type: :element),
891
+ Lutaml::Xml::Element.new("Element", "items", node_type: :element),
892
+ ]
893
+ xml = model.to_xml
894
+ expect(xml.index("<items>a</items>")).to be < xml.index("<singular>s</singular>")
895
+ expect(xml.index("<singular>s</singular>")).to be < xml.index("<items>b</items>")
896
+ end
897
+ end
898
+
899
+ it "hands a singular attribute's over-count to the custom method as a whole array" do
900
+ klass = ProductionLaneParitySpec::CustomSingular
901
+ klass.received.clear
902
+ doc = klass.from_xml("<custom><label>a</label><label>b</label></custom>")
903
+
904
+ expect(klass.received.size).to eq(1)
905
+ expect(klass.received.first).to be_a(Array)
906
+ expect(doc.label).to eq(%w[a b])
907
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::CollectionTrueMissingError))
908
+ end
909
+
910
+ describe "singular content" do
911
+ it "binds the text runs when the element has element children, and flags the over-count" do
912
+ source = "<r>one <bold>b</bold> two <bold>c</bold> three</r>"
913
+ doc = ProductionLaneParitySpec::SingularContent.from_xml(source)
914
+ expect(doc.content).to eq(["one ", " two ", " three"])
915
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::CollectionTrueMissingError))
916
+ expect(doc.to_xml).to include("one <bold>b</bold> two <bold>c</bold> three")
917
+
918
+ plain = ProductionLaneParitySpec::SingularContent.from_xml("<r>plain</r>")
919
+ expect(plain.content).to eq("plain")
920
+ end
921
+ end
922
+
923
+ it "round-trips mixed content whose text is unmapped" do
924
+ source = "<container>before <items>a</items> mid <singular>x</singular> end</container>"
925
+ parsed = ProductionLaneParitySpec::UnmappedMixedText.from_xml(source)
926
+
927
+ expect(parsed.to_xml).to include("before <items>a</items> mid <singular>x</singular> end")
928
+ end
929
+
930
+ it "unwraps a collection's own root under the mapped element" do
931
+ xml = <<~XML
932
+ <bibitem>
933
+ <titles>
934
+ <title-group>
935
+ <artifact><content>Title One</content></artifact>
936
+ <artifact><content>Title Two</content></artifact>
937
+ </title-group>
938
+ </titles>
939
+ </bibitem>
940
+ XML
941
+ doc = ProductionLaneParitySpec::NestedBibitem.from_xml(xml)
942
+
943
+ expect(doc.titles).to be_a(ProductionLaneParitySpec::NestedTitles)
944
+ expect(doc.titles.items.map(&:content)).to eq(["Title One", "Title Two"])
945
+ expect(doc.to_xml).to be_xml_equivalent_to(xml)
946
+ end
947
+
948
+ describe "source declarations" do
949
+ it "keeps the prefix a captured raw fragment spells" do
950
+ source = '<d:doc xmlns:d="http://example.com/production-lane/raw-doc" ' \
951
+ 'xmlns:s="http://www.w3.org/2000/svg">' \
952
+ "<d:title>T</d:title><s:svg><s:rect/></s:svg></d:doc>"
953
+ output = ProductionLaneParitySpec::RawDoc.from_xml(source).to_xml
954
+
955
+ expect(output).to include('xmlns:d="http://example.com/production-lane/raw-doc"',
956
+ 'xmlns:s="http://www.w3.org/2000/svg"', "<s:svg>")
957
+ end
958
+
959
+ it "keeps an extra vocabulary the parsed root declared" do
960
+ source = '<ex:doubled xmlns:ex="http://example.com/production-lane/doubled" ' \
961
+ 'xmlns:x="http://example.com/production-lane/extra" ex:code="c">' \
962
+ "<ex:title>T</ex:title></ex:doubled>"
963
+ output = ProductionLaneParitySpec::Doubled.from_xml(source).to_xml
964
+
965
+ expect(output).to include('xmlns:ex="http://example.com/production-lane/doubled"',
966
+ 'xmlns:x="http://example.com/production-lane/extra"')
967
+ end
968
+
969
+ it "keeps a second prefix a default-namespace root bound its namespace to" do
970
+ source = '<doubled xmlns="http://example.com/production-lane/doubled" ' \
971
+ 'xmlns:b="http://example.com/production-lane/doubled" code="c">' \
972
+ "<title>T</title></doubled>"
973
+ output = ProductionLaneParitySpec::Doubled.from_xml(source).to_xml
974
+
975
+ expect(output).to include('xmlns="http://example.com/production-lane/doubled"',
976
+ 'xmlns:b="http://example.com/production-lane/doubled"')
977
+ end
978
+ end
979
+
980
+ describe "parse and render contracts" do
981
+ it "declares the xsi prefix when a nil-marked element is rendered" do
982
+ output = ProductionLaneParitySpec::NilMarked.new(name: "a", note: nil).to_xml
983
+
984
+ expect(output).to include(
985
+ 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"', 'xsi:nil="true"'
986
+ )
987
+ expect { Lutaml::Xml.adapter.parse(output) }.not_to raise_error
988
+ end
989
+
990
+ it "raises on malformed input under strict: true" do
991
+ expect do
992
+ ProductionLaneParitySpec::StrictParsed.from_xml(
993
+ "<?xml version='1.0'?><strict>>malformed<<xml>", strict: true
994
+ )
995
+ end.to raise_error(Lutaml::Model::InvalidFormatError)
996
+ end
997
+
998
+ it "leaves a parsed instance pristine" do
999
+ parsed = ProductionLaneParitySpec::NilMarked.from_xml(
1000
+ "<nil-marked><name>a</name></nil-marked>",
1001
+ )
1002
+ expect(parsed.mutated_after_parse?).to be(false)
1003
+
1004
+ parsed.name = "b"
1005
+ expect(parsed.mutated_after_parse?).to be(true)
1006
+ end
1007
+
1008
+ it "binds the runs of a singular content element split by an unmapped child" do
1009
+ at_root = ProductionLaneParitySpec::UnmappedChildContent.from_xml(
1010
+ "<unmapped-content>a<unknown/></unmapped-content>",
1011
+ )
1012
+ nested = ProductionLaneParitySpec::UnmappedHolder.from_xml(
1013
+ "<holder><unmapped-content>one<unknown/>two</unmapped-content></holder>",
1014
+ )
1015
+ expect(at_root.content).to eq(["a"])
1016
+ expect(nested.inner.content).to eq(["one", "two"])
1017
+ end
1018
+ end
1019
+
604
1020
  describe "conversion caching" do
605
1021
  let(:store) { Lutaml::Store::BasicStore.new(adapter: { type: :memory }) }
606
1022
 
data/spec/spec_helper.rb CHANGED
@@ -39,12 +39,29 @@ RSpec.configure do |config|
39
39
  # machinery follows the CURRENT config by design (#1184), which
40
40
  # turned that leak into order-dependent schema-suite failures —
41
41
  # the restore pins every example back to the spec_helper default.
42
+ #
43
+ # "Previous" has three shapes and each needs its own restore: a type
44
+ # name, a pre-resolved adapter class (the resolver records it under
45
+ # its internal fixed-adapter marker, which the type setter refuses),
46
+ # or nothing — the production lane, which a leaked pin must be
47
+ # UNSET from rather than left in place. Skipping the nil case let the
48
+ # first spec that pinned :nokogiri hand every later spec that engine:
49
+ # the suite then passed on output the production leptris lane never
50
+ # produced (cdata rules serialized as escaped text).
42
51
  config.around do |example|
43
52
  previous = Lutaml::Model::Config.xml_adapter_type
53
+ fixed = previous == Lutaml::Model::FIXED_ADAPTER_TYPE
54
+ previous_adapter = Lutaml::Model::Config.xml_adapter if fixed
44
55
  begin
45
56
  example.run
46
57
  ensure
47
- Lutaml::Model::Config.xml_adapter_type = previous if previous
58
+ if fixed
59
+ Lutaml::Model::Config.xml_adapter = previous_adapter
60
+ elsif previous
61
+ Lutaml::Model::Config.xml_adapter_type = previous
62
+ else
63
+ Lutaml::Model::Config.unset_xml_adapter_type
64
+ end
48
65
  end
49
66
  end
50
67
 
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.205
4
+ version: 0.8.207
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-12-05 00:00:00.000000000 Z
11
+ date: 2026-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64
@@ -70,22 +70,16 @@ dependencies:
70
70
  name: liquid
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '4.0'
76
- - - "<"
73
+ - - "~>"
77
74
  - !ruby/object:Gem::Version
78
- version: '6.0'
75
+ version: '5.14'
79
76
  type: :runtime
80
77
  prerelease: false
81
78
  version_requirements: !ruby/object:Gem::Requirement
82
79
  requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- version: '4.0'
86
- - - "<"
80
+ - - "~>"
87
81
  - !ruby/object:Gem::Version
88
- version: '6.0'
82
+ version: '5.14'
89
83
  - !ruby/object:Gem::Dependency
90
84
  name: moxml
91
85
  requirement: !ruby/object:Gem::Requirement