lutaml-model 0.8.202 → 0.8.204

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.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +5 -0
  3. data/.github/workflows/dependent-tests.yml +59 -2
  4. data/.github/workflows/lml-conformance.yml +71 -0
  5. data/.gitignore +7 -0
  6. data/README.adoc +1 -0
  7. data/docs/_guides/lml-cross-language.adoc +128 -0
  8. data/docs/_references/lml-conformance.adoc +78 -0
  9. data/docs/_references/lml-contract.adoc +290 -0
  10. data/docs/_references/plan-schema-spec.adoc +45 -18
  11. data/lib/lutaml/key_value/transform.rb +140 -21
  12. data/lib/lutaml/model/attribute.rb +11 -6
  13. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  14. data/lib/lutaml/model/lml/bundle.rb +171 -0
  15. data/lib/lutaml/model/lml/document.rb +46 -10
  16. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  17. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  18. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  19. data/lib/lutaml/model/lml/exporter.rb +223 -87
  20. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  21. data/lib/lutaml/model/lml/importer.rb +180 -0
  22. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  23. data/lib/lutaml/model/lml/materialization.rb +257 -0
  24. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  25. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  26. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  27. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  28. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  29. data/lib/lutaml/model/lml.rb +61 -0
  30. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  31. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  32. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  33. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  34. data/lib/lutaml/model/sequence.rb +7 -0
  35. data/lib/lutaml/model/serialize/deserialization_context.rb +21 -0
  36. data/lib/lutaml/model/serialize/initialization.rb +19 -9
  37. data/lib/lutaml/model/version.rb +1 -1
  38. data/lib/lutaml/xml/leptris/native_emission.rb +4 -0
  39. data/lib/lutaml/xml/leptris/plan_serializer.rb +14 -9
  40. data/schema/lml/document.schema.json +179 -0
  41. data/scripts/lml/export_choices.rb +57 -0
  42. data/scripts/lml/export_conformance.rb +23 -0
  43. data/scripts/lml/export_mml.rb +253 -0
  44. data/scripts/lml/export_preservation.rb +421 -0
  45. data/scripts/lml/verify_artifacts.rb +31 -0
  46. data/spec/address_person_spec.rb +8 -0
  47. data/spec/address_spec.rb +4 -0
  48. data/spec/fixtures/lml_artifacts/model.json +1 -0
  49. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  50. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  51. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  52. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  53. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  54. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  55. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  56. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  57. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  58. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  59. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  60. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  61. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  62. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  63. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  64. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  65. data/spec/fixtures/lml_contract/bundle.json +81 -0
  66. data/spec/fixtures/lml_contract/manifest.json +30 -0
  67. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  68. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  69. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  70. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  71. data/spec/fixtures/lml_mml/inventory.json +1 -0
  72. data/spec/fixtures/lml_mml/manifest.json +1 -0
  73. data/spec/fixtures/lml_mml/oracle.json +1 -0
  74. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  75. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  76. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  77. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  78. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  79. data/spec/lutaml/key_value/group_instantiate_spec.rb +204 -1
  80. data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
  81. data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
  82. data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
  83. data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
  84. data/spec/lutaml/model/lml_document_spec.rb +279 -0
  85. data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
  86. data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
  87. data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
  88. data/spec/lutaml/model/lml_importer_spec.rb +290 -0
  89. data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
  90. data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
  91. data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
  92. data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
  93. data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
  94. data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
  95. data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
  96. data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
  97. data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
  98. data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
  99. data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
  100. data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
  101. data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
  102. data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
  103. data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
  104. data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
  105. data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
  106. data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
  107. data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
  108. data/spec/lutaml/model/sequence_spec.rb +48 -0
  109. data/spec/lutaml/model/serialize/deserialization_context_spec.rb +68 -0
  110. data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
  111. data/spec/spec_helper.rb +4 -5
  112. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  113. data/spec/support/lml_conformance.rb +292 -0
  114. data/spec/support/lml_corpus.rb +75 -0
  115. data/spec/support/lml_export_fixtures.rb +190 -0
  116. data/spec/support/lml_graph_fixtures.rb +108 -0
  117. data/spec/support/lml_import_fixtures.rb +52 -0
  118. data/spec/support/lml_mapping_fixtures.rb +170 -0
  119. data/spec/support/lml_materialization_fixtures.rb +53 -0
  120. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  121. data/spec/support/lml_order_fixtures.rb +151 -0
  122. metadata +93 -2
  123. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model/lml"
4
+
5
+ module LmlValueDeclarationFixtures
6
+ class Code < Lutaml::Model::Type::String
7
+ VALUES = %w[a b].freeze
8
+
9
+ def self.cast(value, options = {})
10
+ return if value.nil?
11
+
12
+ super(value, options.dup.tap { |copy| copy[:values] ||= VALUES })
13
+ end
14
+ end
15
+
16
+ class QualifiedNamespace < Lutaml::Xml::Namespace
17
+ uri "urn:declared-values"
18
+ prefix_default "d"
19
+ attribute_form_default :qualified
20
+ end
21
+
22
+ class QualifiedModel < Lutaml::Model::Serializable
23
+ attribute :code, Code
24
+ xml do
25
+ root "qualified"
26
+ namespace QualifiedNamespace
27
+ map_attribute "code", to: :code
28
+ end
29
+ end
30
+
31
+ class Model < Lutaml::Model::Serializable
32
+ choice(min: 1, max: 1) do
33
+ attribute :code, Code, default: "a"
34
+ attribute :other, :string
35
+ end
36
+ json do
37
+ map "code", to: :code
38
+ map "other", to: :other
39
+ end
40
+ xml do
41
+ root "declared"
42
+ map_element "code", to: :code
43
+ map_element "other", to: :other
44
+ end
45
+ end
46
+ end
47
+
48
+ RSpec.describe Lutaml::Model::Lml::ValueDeclarations do
49
+ let(:source) { LmlValueDeclarationFixtures::Model }
50
+ let(:scalar) { LmlValueDeclarationFixtures::Code }
51
+ let(:definition) { { "base" => "string", "facets" => { "enumeration" => %w[a b] } } }
52
+ let(:registry) { described_class.new({ scalar => definition }) }
53
+ let(:lml) { Lutaml::Model::Lml }
54
+
55
+ it "keeps arbitrary named casters refused without explicit declarations" do
56
+ expect { lml.export_bundle(source) }.to raise_error(lml::RejectedError) { |error| expect(error.reason).to eq(:unsupported_type) }
57
+ end
58
+
59
+ it "exports named identity, declared facets, defaults and choices without flattening" do
60
+ tree = lml.export_bundle(source, value_declarations: registry).to_h
61
+ id = "#{scalar.name}@default"
62
+ expect(tree.fetch("value_types")).to eq(id => definition)
63
+ model = tree.fetch("models").values.first
64
+ expect(model.fetch("attributes").first.fetch("type")).to eq("kind" => "value", "id" => id)
65
+ expect(model.fetch("attributes").first.fetch("default")).to eq("present" => true, "value" => "a")
66
+ expect(model.fetch("choices").fetch("validation")).to eq("explicit")
67
+ expect(lml::Importer.bundle(tree).to_h).to eq(tree)
68
+ end
69
+
70
+ it "requires native bindings and preserves their source reader and validation behavior" do
71
+ bundle = lml.export_bundle(source, value_declarations: registry)
72
+ expect { lml.import_bundle(bundle) }.to raise_error(lml::RejectedError) { |error| expect(error.reason).to eq(:missing_type_binding) }
73
+ imported = lml.import_bundle(bundle, type_bindings: { "#{scalar.name}@default" => scalar }).fetch("#{source.name}@default")
74
+ [{ code: "a" }, { code: "b" }, { code: "a", other: "x" }].each do |input|
75
+ original = source.new(input)
76
+ actual = imported.new(input)
77
+ expect(actual.to_json).to eq(original.to_json)
78
+ expect(actual.to_xml).to eq(original.to_xml)
79
+ expect(actual.validate.map(&:class)).to eq(original.validate.map(&:class))
80
+ end
81
+ expect { imported.from_json('{"code":"unknown"}') }.to raise_error(Lutaml::Model::Type::InvalidValueError)
82
+ end
83
+
84
+ it "preserves qualified attribute namespaces while importing declared scalar models" do
85
+ original_class = LmlValueDeclarationFixtures::QualifiedModel
86
+ bundle = lml.export_bundle(original_class, value_declarations: registry)
87
+ rule = bundle.models.values.first.fetch("mappings").fetch("xml").fetch("rules").first
88
+ expect(rule.fetch("namespace")).to eq("mode" => "uri", "uri" => "urn:declared-values", "prefix" => "d")
89
+ imported = lml.import_bundle(bundle, type_bindings: { "#{scalar.name}@default" => scalar }).fetch("#{original_class.name}@default")
90
+ original = original_class.new(code: "a").to_xml(prefix: true)
91
+ expect(imported.from_xml(original).to_xml(prefix: true)).to eq(original)
92
+ end
93
+
94
+ it "treats facet payloads as data rather than model references" do
95
+ definition.fetch("facets")["opaque_payload"] = { "kind" => "model", "id" => "Missing@default" }
96
+ tree = lml.export_bundle(source, value_declarations: registry).to_h
97
+ expect(tree.fetch("models").keys).to eq(["#{source.name}@default"])
98
+ expect(tree.fetch("value_types").values.first.fetch("facets").fetch("opaque_payload")).to eq("kind" => "model", "id" => "Missing@default")
99
+ end
100
+
101
+ it "uses exact source class identity rather than names or inherited behavior" do
102
+ expect(registry.fetch(scalar.name)).to be_nil
103
+ subclass = Class.new(scalar)
104
+ stub_const("LmlUnregisteredCode", subclass)
105
+ expect(registry.fetch(subclass)).to be_nil
106
+ expect { described_class.new({ scalar.name => definition }) }.to raise_error(lml::RejectedError)
107
+ end
108
+
109
+ it "freezes a private data copy without freezing the caller's declarations" do
110
+ item = described_class.new({ scalar => definition })
111
+ definition.fetch("facets").fetch("enumeration") << "c"
112
+ expect(item.fetch(scalar).fetch("facets").fetch("enumeration")).to eq(%w[a b])
113
+ expect(item.fetch(scalar)).to be_frozen
114
+ expect(item.to_h.fetch("version")).to eq(1)
115
+ end
116
+
117
+ it "refuses unsupported registry versions and executable metadata" do
118
+ expect { described_class.new({ scalar => definition }, version: 2) }.to raise_error(lml::RejectedError) { |error| expect(error.reason).to eq(:unsupported_version) }
119
+ invoked = false
120
+ definition["facets"]["callback"] = -> { invoked = true }
121
+ expect { described_class.new({ scalar => definition }) }.to raise_error(lml::RejectedError) { |error| expect(error.reason).to eq(:invalid_document) }
122
+ expect(invoked).to be(false)
123
+ end
124
+
125
+ it "refuses unvalidated raw registries at the public exporter boundary" do
126
+ expect { lml.export_bundle(source, value_declarations: { scalar => definition }) }.to raise_error(ArgumentError, /ValueDeclarations/)
127
+ end
128
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "lutaml/model/schema"
5
+
6
+ RSpec.describe Lutaml::Model::Schema::ClassLoader do
7
+ let(:definition) do
8
+ Lutaml::Model::Schema::Definitions::Model.new(
9
+ class_name: "Entry",
10
+ xml_root: Lutaml::Model::Schema::Definitions::XmlRoot.new(
11
+ kind: :element, name: "entry",
12
+ ),
13
+ )
14
+ end
15
+
16
+ let(:entry) do
17
+ Lutaml::Model::Schema::CompiledOutput::Entry.new(
18
+ "Entry", definition, :model
19
+ )
20
+ end
21
+
22
+ it "loads generated classes from a nested namespace directory" do
23
+ stub_const("ClassLoaderRegression", Module.new)
24
+
25
+ output = Lutaml::Model::Schema::CompiledOutput.new(
26
+ entries: [entry],
27
+ module_namespace: "ClassLoaderRegression::Nested",
28
+ )
29
+
30
+ expect { described_class.load(output) }.not_to raise_error
31
+
32
+ expect(ClassLoaderRegression::Nested::Entry.ancestors)
33
+ .to include(Lutaml::Model::Serialize::ClassMethods)
34
+ end
35
+
36
+ it "keeps single-level namespace autoloads working" do
37
+ registry = Lutaml::Model::Schema::RegistryGenerator.generate(
38
+ [entry], module_namespace: "SingleLevel"
39
+ )
40
+
41
+ expect(registry).to include('File.join(__dir__, "singlelevel", "entry")')
42
+ end
43
+
44
+ it "keeps flat class output free of a registry namespace" do
45
+ registry = Lutaml::Model::Schema::RegistryGenerator.generate(
46
+ [entry], module_namespace: nil
47
+ )
48
+
49
+ expect(registry).to be_nil
50
+ end
51
+ end
@@ -52,6 +52,26 @@ RSpec.describe "runtime XSD class loading" do
52
52
  end
53
53
  end
54
54
 
55
+ # `use="required"` on an xs:attribute compiles to a required model
56
+ # attribute, so an instance the schema forbids fails validation.
57
+ it "carries use=\"required\" through to the generated attribute" do
58
+ Dir.mktmpdir do |dir|
59
+ Lutaml::Model::Schema.from_xml(
60
+ xsd, output_dir: File.join(dir, "required_attribute_models"),
61
+ load_classes: true
62
+ )
63
+
64
+ expect(RequiredAttributeModels::EntryType.attributes[:id].options[:required])
65
+ .to be true
66
+
67
+ missing = RequiredAttributeModels::EntryType.from_xml(
68
+ "<entry><name>Alpha</name><year>2024</year></entry>",
69
+ )
70
+ expect { missing.validate! }
71
+ .to raise_error(Lutaml::Model::ValidationError, /id/)
72
+ end
73
+ end
74
+
55
75
  # Anonymous (inline) complexTypes get CT_-prefixed internal names;
56
76
  # the generated classes must still (a) map skippable XSD built-ins
57
77
  # like xs:int to Lutaml primitives and (b) serialize the owning
@@ -396,6 +396,54 @@ RSpec.describe "Sequence" do
396
396
  end
397
397
  let(:klass) { SequenceSpec::CeramicRestricted }
398
398
 
399
+ # An optional scalar member (XSD minOccurs="0", maxOccurs="1") may be
400
+ # absent: the sequence validator skips it and continues with the next
401
+ # member instead of reporting the element that follows as out of order.
402
+ context "with an optional scalar member between required ones" do
403
+ let(:model) do
404
+ Class.new(Lutaml::Model::Serializable) do
405
+ attribute :title, :string, required: true
406
+ attribute :note, :string
407
+ attribute :meta, :string
408
+ attribute :items, :string, collection: 0..Float::INFINITY
409
+
410
+ xml do
411
+ element "Doc"
412
+ sequence do
413
+ map_element :title, to: :title
414
+ map_element :note, to: :note
415
+ map_element :meta, to: :meta
416
+ map_element :items, to: :items
417
+ end
418
+ end
419
+ end
420
+ end
421
+
422
+ it "accepts an instance that omits the optional member" do
423
+ instance = model.from_xml("<Doc><title>t</title><meta>m</meta></Doc>")
424
+ expect { instance.validate! }.not_to raise_error
425
+ end
426
+
427
+ it "accepts an instance that omits the optional member before a collection" do
428
+ instance = model.from_xml(
429
+ "<Doc><title>t</title><items>a</items><items>b</items></Doc>",
430
+ )
431
+ expect { instance.validate! }.not_to raise_error
432
+ end
433
+
434
+ it "still rejects a member that is out of order" do
435
+ instance = model.from_xml("<Doc><meta>m</meta><title>t</title></Doc>")
436
+ expect { instance.validate! }
437
+ .to raise_error(Lutaml::Model::ValidationError, /title/)
438
+ end
439
+
440
+ it "still rejects a missing required member" do
441
+ instance = model.from_xml("<Doc><note>n</note></Doc>")
442
+ expect { instance.validate! }
443
+ .to raise_error(Lutaml::Model::ValidationError, /title/)
444
+ end
445
+ end
446
+
399
447
  it "does not raise for correct order" do
400
448
  expect do
401
449
  sequence.validate_content!(
@@ -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
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ # cdata: true must serialize as a CDATA section, not escaped text, on
6
+ # every engine lane. The equivalence-based cdata specs compare XML
7
+ # semantically (CDATA and escaped text are equivalent under that
8
+ # comparison), which is how the leptris plan lanes came to emit plain
9
+ # text nodes for cdata rules.
10
+ module CdataEmissionSpec
11
+ class Content < Lutaml::Model::Serializable
12
+ attribute :id, :string
13
+ attribute :label, :string
14
+
15
+ xml do
16
+ element "item"
17
+ map_attribute "id", to: :id
18
+ map_content to: :label, cdata: true
19
+ end
20
+ end
21
+
22
+ class Element < Lutaml::Model::Serializable
23
+ attribute :label, :string
24
+
25
+ xml do
26
+ element "labeled"
27
+ map_element "label", to: :label, cdata: true
28
+ end
29
+ end
30
+
31
+ class Plain < Lutaml::Model::Serializable
32
+ attribute :label, :string
33
+
34
+ xml do
35
+ element "plain"
36
+ map_element "label", to: :label
37
+ end
38
+ end
39
+ end
40
+
41
+ RSpec.describe "CDATA emission" do
42
+ %i[leptris nokogiri].each do |engine|
43
+ context "on the #{engine} lane" do
44
+ around do |example|
45
+ previous = Lutaml::Model::Config.xml_adapter_type
46
+ Lutaml::Model::Config.xml_adapter_type = engine
47
+ example.run
48
+ ensure
49
+ Lutaml::Model::Config.xml_adapter_type = previous
50
+ end
51
+
52
+ it "emits a CDATA section for a content rule with cdata: true" do
53
+ expect(CdataEmissionSpec::Content.new(id: "i", label: "<x>").to_xml)
54
+ .to include("<item id=\"i\"><![CDATA[<x>]]></item>")
55
+ end
56
+
57
+ it "emits a CDATA section for an element rule with cdata: true" do
58
+ expect(CdataEmissionSpec::Element.new(label: "<x>").to_xml)
59
+ .to include("<label><![CDATA[<x>]]></label>")
60
+ end
61
+
62
+ it "keeps escaped text for a rule without cdata" do
63
+ expect(CdataEmissionSpec::Plain.new(label: "<x>").to_xml)
64
+ .to include("<label>&lt;x&gt;</label>")
65
+ end
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
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+
5
+ # Named fixtures for LML any_attribute (map_all_attributes) specs.
6
+ module LmlAnyAttributeFixtures
7
+ class Tag < Lutaml::Model::Serializable
8
+ attribute :id, :string
9
+ attribute :other_attributes, :hash
10
+
11
+ xml do
12
+ element "tag"
13
+ map_attribute "id", to: :id
14
+ map_all_attributes to: :other_attributes
15
+ end
16
+ end
17
+
18
+ class Filtered < Lutaml::Model::Serializable
19
+ attribute :other_attributes, :hash
20
+
21
+ xml do
22
+ element "filtered"
23
+ map_all_attributes to: :other_attributes, matching: /\Adata-/
24
+ end
25
+ end
26
+ end