lutaml-model 0.8.203 → 0.8.205

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 (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +5 -0
  3. data/.github/workflows/lml-conformance.yml +71 -0
  4. data/.gitignore +7 -0
  5. data/README.adoc +1 -0
  6. data/docs/_guides/lml-cross-language.adoc +128 -0
  7. data/docs/_guides/native-engines.adoc +10 -0
  8. data/docs/_references/lml-conformance.adoc +78 -0
  9. data/docs/_references/lml-contract.adoc +290 -0
  10. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  11. data/lib/lutaml/model/lml/bundle.rb +171 -0
  12. data/lib/lutaml/model/lml/document.rb +46 -10
  13. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  14. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  15. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  16. data/lib/lutaml/model/lml/exporter.rb +223 -87
  17. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  18. data/lib/lutaml/model/lml/importer.rb +180 -0
  19. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  20. data/lib/lutaml/model/lml/materialization.rb +257 -0
  21. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  22. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  23. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  24. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  25. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  26. data/lib/lutaml/model/lml.rb +61 -0
  27. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  28. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  29. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  30. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  31. data/lib/lutaml/model/sequence.rb +7 -0
  32. data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
  33. data/lib/lutaml/model/serialize/initialization.rb +2 -0
  34. data/lib/lutaml/model/version.rb +1 -1
  35. data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
  36. data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
  37. data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
  38. data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
  39. data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
  40. data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
  41. data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
  42. data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
  43. data/schema/lml/document.schema.json +179 -0
  44. data/scripts/lml/export_choices.rb +57 -0
  45. data/scripts/lml/export_conformance.rb +23 -0
  46. data/scripts/lml/export_mml.rb +253 -0
  47. data/scripts/lml/export_preservation.rb +421 -0
  48. data/scripts/lml/verify_artifacts.rb +31 -0
  49. data/spec/fixtures/lml_artifacts/model.json +1 -0
  50. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  51. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  52. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  53. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  54. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  55. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  56. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  57. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  58. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  59. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  60. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  61. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  62. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  63. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  64. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  65. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  66. data/spec/fixtures/lml_contract/bundle.json +81 -0
  67. data/spec/fixtures/lml_contract/manifest.json +30 -0
  68. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  69. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  70. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  71. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  72. data/spec/fixtures/lml_mml/inventory.json +1 -0
  73. data/spec/fixtures/lml_mml/manifest.json +1 -0
  74. data/spec/fixtures/lml_mml/oracle.json +1 -0
  75. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  76. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  77. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  78. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  79. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  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/xml/cdata_emission_spec.rb +68 -0
  110. data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
  111. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  112. data/spec/support/lml_conformance.rb +292 -0
  113. data/spec/support/lml_corpus.rb +75 -0
  114. data/spec/support/lml_export_fixtures.rb +190 -0
  115. data/spec/support/lml_graph_fixtures.rb +108 -0
  116. data/spec/support/lml_import_fixtures.rb +52 -0
  117. data/spec/support/lml_mapping_fixtures.rb +170 -0
  118. data/spec/support/lml_materialization_fixtures.rb +53 -0
  119. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  120. data/spec/support/lml_order_fixtures.rb +151 -0
  121. metadata +94 -3
  122. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -0,0 +1,211 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require_relative "../../../scripts/lml/export_preservation"
5
+ require "tmpdir"
6
+ require "fileutils"
7
+ require "open3"
8
+
9
+ RSpec.describe LmlPreservation do
10
+ let(:directory) { File.expand_path("../../fixtures/lml_conformance/preservation", __dir__) }
11
+ let(:index) { JSON.parse(File.read(File.join(directory, "index.json"))) }
12
+ let(:cases) { index.fetch("cases").to_h { |entry| [entry.fetch("id"), entry] } }
13
+ let(:temporary) { Dir.mktmpdir("lml-preservation") }
14
+
15
+ after { FileUtils.remove_entry(temporary) }
16
+
17
+ it "executes all seventeen independent Ruby oracle cases" do
18
+ expect(described_class.check(directory)).to include("expected_count" => 17, "executed_count" => 17, "errors" => [])
19
+ expect(index.fetch("generation")).to eq("preservation-v1")
20
+ end
21
+
22
+ it "pins source expectations independently of Windows line endings" do
23
+ entry = described_class.source_expectations.first
24
+ path = File.expand_path("../../../#{entry.fetch('path')}", __dir__)
25
+ source = File.binread(path)
26
+ allow(File).to receive(:binread).and_call_original
27
+ allow(File).to receive(:binread).with(path).and_return(source.gsub(/\r?\n/, "\r\n"))
28
+ expect(described_class.source_expectations.first).to eq(entry)
29
+ end
30
+
31
+ it "pins source expectations without modifying the original Ruby specs" do
32
+ expect(index.fetch("source_expectations")).to eq(described_class.source_expectations)
33
+ expect(index.fetch("source_expectations").map { |entry| entry.fetch("path") }).to include(
34
+ "spec/lutaml/model/mixed_content_spec.rb", "spec/lutaml/model/cdata_spec.rb",
35
+ "spec/lutaml/model/collection_spec.rb", "spec/lutaml/model/register_spec.rb"
36
+ )
37
+ end
38
+
39
+ it "preserves qualified and unqualified namespace identities in the output tree" do
40
+ entry = cases.fetch("namespace-tree")
41
+ result = described_class.execute(entry, described_class::Namespaces)
42
+ root = REXML::Document.new(result.fetch("output")).root
43
+ expect(root.namespace).to eq("urn:lml:preservation")
44
+ expect(root.elements[1].namespace).to eq("urn:lml:preservation")
45
+ expect(root.elements[2].namespace).to eq("")
46
+ expect(result.fetch("data")).to eq("qualified" => "Q", "plain" => "P")
47
+ model = described_class.schema.fetch("models").fetch("LmlPreservation::Namespaces@default")
48
+ rules = model.fetch("mappings").fetch("xml").fetch("rules")
49
+ expect(rules.map { |rule| rule.fetch("namespace") }).to eq([{ "mode" => "inherit" }, { "mode" => "unqualified" }])
50
+ end
51
+
52
+ it "pins namespace bytes independently of tree comparison" do
53
+ entry = cases.fetch("namespace-bytes")
54
+ result = described_class.execute(entry, described_class::Namespaces)
55
+ expect(entry.fetch("comparison")).to eq("bytes")
56
+ expect(result.fetch("output")).to eq(entry.fetch("expected_output"))
57
+ expect(result.fetch("output")).to include("<plain>P</plain>")
58
+ end
59
+
60
+ it "captures actual Ruby mixed text and repeated-element order as portable tokens" do
61
+ entry = cases.fetch("mixed-order-tree")
62
+ result = described_class.execute(entry, described_class::Mixed)
63
+ expect(result.fetch("data")).to include("text" => %w[before between after], "em" => %w[one two])
64
+ expect(result.fetch("data").fetch("__lml_content")).to eq([
65
+ { "kind" => "text", "attribute" => "text", "index" => 0 },
66
+ { "kind" => "element", "attribute" => "em", "index" => 0 },
67
+ { "kind" => "text", "attribute" => "text", "index" => 1 },
68
+ { "kind" => "element", "attribute" => "em", "index" => 1 },
69
+ { "kind" => "text", "attribute" => "text", "index" => 2 },
70
+ ])
71
+ expect(result.fetch("output")).to eq(entry.fetch("input"))
72
+ end
73
+
74
+ it "preserves ordinary per-instance order instead of grouped attribute order" do
75
+ entry = cases.fetch("ordered-bytes")
76
+ result = described_class.execute(entry, described_class::Ordered)
77
+ expect(result.fetch("output")).to eq("<ordered>\n <b>one</b>\n <a>two</a>\n <b>three</b>\n</ordered>")
78
+ expect(result.fetch("data").fetch("__lml_content").map { |token| token.fetch("attribute") }).to eq(%w[b a b])
79
+ end
80
+
81
+ it "preserves CDATA lexical output and decoded content" do
82
+ entry = cases.fetch("cdata-bytes")
83
+ result = described_class.execute(entry, described_class::Cdata)
84
+ expect(result).to eq("data" => { "text" => "a < b & c" }, "output" => entry.fetch("input"))
85
+ end
86
+
87
+ it "keeps absent, null, empty and explicit default states distinct" do
88
+ expect(cases.fetch("state-absent").fetch("expected_data").fetch("value")).to eq("state" => "absent", "value" => nil)
89
+ expect(cases.fetch("state-null").fetch("expected_data").fetch("value")).to eq("state" => "present", "value" => nil)
90
+ expect(cases.fetch("state-empty").fetch("expected_data").fetch("value")).to eq("state" => "present", "value" => "")
91
+ expect(cases.fetch("state-absent").fetch("expected_data").fetch("fallback")).to eq("state" => "default", "value" => "default")
92
+ expect(cases.fetch("state-explicit-default").fetch("expected_data").fetch("fallback")).to eq("state" => "present", "value" => "default")
93
+ expect(cases.fetch("state-empty").fetch("expected_data").fetch("items")).to eq("state" => "present", "value" => [])
94
+ end
95
+
96
+ it "executes native Ruby state wire expectations without claiming portable YAML I/O" do
97
+ result = described_class.execute(cases.fetch("state-null"), described_class::States)
98
+ expect(JSON.parse(result.fetch("output"))).to eq("value" => nil, "items" => [])
99
+ expect(index.fetch("wire_profile")).to eq("ruby-native")
100
+ expect(index.fetch("data_profile")).to eq("exact-values-decimal-v1")
101
+ expect(cases.values.map { |entry| entry.fetch("format") }.uniq.sort).to eq(%w[json xml])
102
+ end
103
+
104
+ it "preserves cardinality bounds and both negative boundary results" do
105
+ expect(cases.fetch("cardinality-valid").fetch("expected_data")).to eq("items" => %w[one two])
106
+ %w[cardinality-invalid cardinality-missing].each do |id|
107
+ result = described_class.execute(cases.fetch(id), described_class::Bounded)
108
+ expect(result.fetch("error")).to include(
109
+ "reason" => "invalid_document", "model_id" => "LmlPreservation::Bounded@default",
110
+ "ruby_errors" => ["Lutaml::Model::CollectionCountOutOfRangeError"]
111
+ )
112
+ end
113
+ end
114
+
115
+ it "preserves typed integer and boolean enum declarations and exact normalized values" do
116
+ model = described_class.schema.fetch("models").fetch("LmlPreservation::TypedEnum@default")
117
+ expect(model.fetch("attributes").map { |field| field.fetch("values") }).to eq([[1, 2], [true, false]])
118
+ expect(cases.fetch("typed-enum-valid").fetch("expected_data")).to eq("number" => "2", "flag" => false)
119
+ expect(described_class.execute(cases.fetch("typed-enum-invalid"), described_class::TypedEnum).fetch("error")).to include(
120
+ "reason" => "invalid_document", "ruby_errors" => ["Lutaml::Model::InvalidValueError"],
121
+ )
122
+ end
123
+
124
+ it "executes an explicit imported context without changing the global default register" do
125
+ original = Lutaml::Model::Config.default_register
126
+ result = described_class.execute(cases.fetch("imported-context"), described_class::Imported)
127
+ expect(result.fetch("data")).to eq("name" => "contextual")
128
+ expect(result.fetch("output")).to eq('{"name":"contextual"}')
129
+ expect(described_class::Imported.attributes(:default)).not_to have_key(:name)
130
+ expect(described_class::Imported.attributes(:oracle)).to have_key(:name)
131
+ expect(Lutaml::Model::Config.default_register).to eq(original)
132
+ end
133
+
134
+ it "recreates a removed imported context even when its register survives" do
135
+ context = Lutaml::Model::GlobalContext.context(:oracle)
136
+ register = Lutaml::Model::GlobalRegister.lookup(:oracle)
137
+ Lutaml::Model::GlobalContext.unregister_context(:oracle)
138
+ expect(Lutaml::Model::GlobalRegister.lookup(:oracle)).to equal(register)
139
+
140
+ result = described_class.execute(cases.fetch("imported-context"), described_class::Imported)
141
+ expect(result).to eq("data" => { "name" => "contextual" }, "output" => '{"name":"contextual"}')
142
+ expect(described_class.check(directory)).to include("executed_count" => 17, "errors" => [])
143
+ ensure
144
+ Lutaml::Model::GlobalContext.register_context(context) if context
145
+ end
146
+
147
+ it "scopes native adapters without changing the caller's adapter overrides" do
148
+ Lutaml::Model::Config.with_adapter(xml: :ox, json: :oj) do
149
+ original = Lutaml::Model::AdapterScope.current
150
+ expect(described_class.check(directory)).to include("executed_count" => 17, "errors" => [])
151
+ expect(Lutaml::Model::AdapterScope.current).to equal(original)
152
+ end
153
+ end
154
+
155
+ it "pins equivalent JSON and safely parsed YAML schemas and actual payload digests" do
156
+ expect(YAML.safe_load_file(File.join(directory, "schema.yaml"), aliases: false)).to eq(
157
+ JSON.parse(File.read(File.join(directory, "schema.json"))),
158
+ )
159
+ manifest = JSON.parse(File.read(File.join(directory, "manifest.json")))
160
+ manifest.fetch("files").each do |entry|
161
+ expect(described_class.digest(File.binread(File.join(directory, entry.fetch("path"))))).to eq(entry.fetch("sha256"))
162
+ end
163
+ described_class.files.each { |path, bytes| expect(File.binread(File.join(directory, path))).to eq(bytes.b) }
164
+ end
165
+
166
+ it "fails a missing case by ID" do
167
+ FileUtils.cp_r("#{directory}/.", temporary)
168
+ index.fetch("cases").shift
169
+ File.write(File.join(temporary, "index.json"), JSON.generate(index))
170
+ expect(described_class.check(temporary).fetch("errors")).to include("id" => "namespace-tree", "error" => "missing case")
171
+ end
172
+
173
+ it "fails unknown, duplicate and changed cases by ID" do
174
+ FileUtils.cp_r("#{directory}/.", temporary)
175
+ index.fetch("cases") << cases.fetch("namespace-tree").merge("id" => "unknown")
176
+ index.fetch("cases") << cases.fetch("namespace-tree").dup
177
+ cases.fetch("mixed-order-tree")["input"] = "<mixed/>"
178
+ File.write(File.join(temporary, "index.json"), JSON.generate(index))
179
+ expect(described_class.check(temporary).fetch("errors")).to include(
180
+ { "id" => "unknown", "error" => "unknown case" },
181
+ { "id" => "namespace-tree", "error" => "duplicate case" },
182
+ { "id" => "mixed-order-tree", "error" => "changed case" },
183
+ )
184
+ end
185
+
186
+ it "detects byte changes even when the XML tree remains equivalent" do
187
+ entry = cases.fetch("namespace-bytes")
188
+ result = { "data" => entry.fetch("expected_data"), "output" => entry.fetch("expected_output").gsub("p:", "z:").sub("xmlns:p", "xmlns:z") }
189
+ expect(described_class.xml_tree(result.fetch("output"))).to eq(described_class.xml_tree(entry.fetch("expected_output")))
190
+ expect(described_class.compare(entry, result)).to eq("output mismatch")
191
+ end
192
+
193
+ it "detects changed ordering and values through the tree comparator" do
194
+ entry = cases.fetch("mixed-order-tree")
195
+ result = { "data" => entry.fetch("expected_data"), "output" => "<mixed>before<em>two</em>between<em>one</em>after</mixed>" }
196
+ expect(described_class.compare(entry, result)).to eq("output mismatch")
197
+ result["output"] = entry.fetch("expected_output").sub("before", "changed")
198
+ expect(described_class.compare(entry, result)).to eq("output mismatch")
199
+ end
200
+
201
+ it "returns nonzero with the changed case ID through the read-only CLI" do
202
+ FileUtils.cp_r("#{directory}/.", temporary)
203
+ cases.fetch("cdata-bytes")["expected_output"] = "<cdata>a &lt; b &amp; c</cdata>"
204
+ bytes = JSON.generate(index)
205
+ File.write(File.join(temporary, "index.json"), bytes)
206
+ output, status = Open3.capture2(RbConfig.ruby, "scripts/lml/export_preservation.rb", "--check", "--package", temporary)
207
+ expect(status.exitstatus).to eq(1)
208
+ expect(JSON.parse(output).fetch("errors")).to include("id" => "cdata-bytes", "error" => "changed case")
209
+ expect(File.read(File.join(temporary, "index.json"))).to eq(bytes)
210
+ end
211
+ end
@@ -0,0 +1,247 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+ require "date"
5
+
6
+ # Exporter-side findings of the final Ruby review of #1116 (H1, M3, M4, L4,
7
+ # L5, L6, L10): every construct the v2 contract cannot carry is refused by
8
+ # name at export, and everything carried is normalised to JSON scalars.
9
+ module LmlReviewExportFixtures # rubocop:disable Lint/ConstantDefinitionInBlock -- named fixtures: the document addresses models by class path
10
+ class Item < Lutaml::Model::Serializable
11
+ attribute :v, :string
12
+ end
13
+
14
+ class ItemList < Lutaml::Model::Collection
15
+ instances :items, Item
16
+ end
17
+
18
+ class WithCollectionClass < Lutaml::Model::Serializable
19
+ attribute :things, Item, collection: ItemList
20
+ end
21
+
22
+ class WithTransform < Lutaml::Model::Serializable
23
+ attribute :x, :string, transform: { export: ->(v) { v.to_s.upcase } }
24
+ end
25
+
26
+ class WithPattern < Lutaml::Model::Serializable
27
+ attribute :y, :string, pattern: /\Aab/
28
+ end
29
+
30
+ class WithModelChoice < Lutaml::Model::Serializable
31
+ choice(min: 1, max: 1) do
32
+ attribute :a, :string
33
+ attribute :b, :string
34
+ end
35
+ end
36
+
37
+ class WithPolymorphic < Lutaml::Model::Serializable
38
+ attribute :p, Item, polymorphic: [Item]
39
+ end
40
+
41
+ class WithMethod < Lutaml::Model::Serializable
42
+ attribute :u, :string, method: :compute
43
+ end
44
+
45
+ class WithValidations < Lutaml::Model::Serializable
46
+ attribute :t, :string, validations: { min: 3 }
47
+ end
48
+
49
+ class Target < Lutaml::Model::Serializable
50
+ attribute :id, :string
51
+ end
52
+
53
+ class WithReference < Lutaml::Model::Serializable
54
+ attribute :r, { ref: ["LmlReviewExportFixtures::Target", :id] }
55
+ end
56
+
57
+ class MyStr < Lutaml::Model::Type::String; end
58
+
59
+ class WithValueType < Lutaml::Model::Serializable
60
+ attribute :x, MyStr
61
+ end
62
+
63
+ class FloatOnInteger < Lutaml::Model::Serializable
64
+ attribute :n, :integer, default: 1.5
65
+ end
66
+
67
+ class Scalars < Lutaml::Model::Serializable
68
+ attribute :d, :date, default: Date.new(2020, 1, 2)
69
+ attribute :s, :symbol, default: :foo
70
+ attribute :c, :string, values: %i[a b]
71
+ attribute :f, :float, default: 2
72
+ attribute :h, :hash, default: { "a" => 1 }
73
+ end
74
+
75
+ class StringOnInteger < Lutaml::Model::Serializable
76
+ attribute :n, :integer, default: "7"
77
+ end
78
+
79
+ class DataDefault < Lutaml::Model::Serializable
80
+ attribute :opts, :hash, default: { "reference" => "Some.Thing", "kind" => "model", "id" => "X" }
81
+ end
82
+
83
+ class Plain < Lutaml::Model::Serializable
84
+ attribute :a, :string
85
+ end
86
+
87
+ class SeqChoice < Lutaml::Model::Serializable
88
+ attribute :a, :string
89
+ attribute :b, :string
90
+ xml do
91
+ root "sc"
92
+ sequence do
93
+ map_element "a", to: :a
94
+ choice(min: 1, max: 1) { map_element "b", to: :b }
95
+ end
96
+ end
97
+ end
98
+
99
+ class AnonColl < Lutaml::Model::Serializable
100
+ attribute :things, Item,
101
+ collection: Class.new(Lutaml::Model::Collection) { instances :items, LmlReviewExportFixtures::Item }
102
+ end
103
+ end
104
+
105
+ RSpec.describe "LML exporter review fixes (#1116 final review)" do # rubocop:disable RSpec/DescribeClass
106
+ let(:lml) { Lutaml::Model::Lml }
107
+ let(:fx) { LmlReviewExportFixtures }
108
+
109
+ def reason_of
110
+ yield
111
+ :exported
112
+ rescue Lutaml::Model::Lml::RejectedError => e
113
+ e.reason
114
+ end
115
+
116
+ def project(klass)
117
+ Lutaml::Model::Lml::Exporter.project(klass)
118
+ end
119
+
120
+ describe "H1: behaviour the contract cannot carry is refused by name" do
121
+ {
122
+ "an attribute transform" => :WithTransform,
123
+ "an attribute pattern" => :WithPattern,
124
+ "an attribute polymorphic list" => :WithPolymorphic,
125
+ "a method-backed attribute" => :WithMethod,
126
+ "attribute validations" => :WithValidations,
127
+ }.each do |label, name|
128
+ it "refuses #{label} in the document and the bundle (unsupported_mapping)" do
129
+ klass = LmlReviewExportFixtures.const_get(name)
130
+ expect(reason_of { project(klass) }).to eq(:unsupported_mapping)
131
+ expect(reason_of { lml.export_bundle(klass) }).to eq(:unsupported_mapping)
132
+ end
133
+ end
134
+
135
+ it "keeps the v1 document unchanged for such a model" do
136
+ expect(reason_of { Lutaml::Model::Lml::Exporter.export(fx::WithPattern) }).to eq(:exported)
137
+ end
138
+
139
+ it "still exports the xml sequence choice compositor (its bounds travel in the mapping)" do
140
+ expect(reason_of { project(fx::SeqChoice) }).to eq(:exported)
141
+ end
142
+
143
+ it "carries a nameable custom collection class as collection.class" do
144
+ attribute = project(fx::WithCollectionClass).fetch("attributes").first
145
+ expect(attribute.fetch("collection")).to eq("min" => 0, "max" => nil, "class" => fx::ItemList.name)
146
+ end
147
+
148
+ it "round-trips a custom collection class once the importer is given its binding" do
149
+ bundle = lml.export_bundle(fx::WithCollectionClass)
150
+ bindings = { fx::ItemList.name => fx::ItemList }
151
+ expect(lml.verify_materialization(bundle, type_bindings: bindings).checksum).to eq(bundle.checksum)
152
+ end
153
+
154
+ it "refuses an anonymous custom collection class" do
155
+ expect(reason_of { project(fx::AnonColl) }).to eq(:unsupported_mapping)
156
+ end
157
+ end
158
+
159
+ describe "M3: defaults and enum values are JSON scalars the type can express" do
160
+ let(:attributes) { project(fx::Scalars).fetch("attributes").to_h { |a| [a["name"], a] } }
161
+
162
+ it "normalises Date, Symbol, enum symbols and an integer on a float to wire scalars" do
163
+ expect(attributes.fetch("d").dig("default", "value")).to eq("2020-01-02")
164
+ expect(attributes.fetch("s").dig("default", "value")).to eq("foo")
165
+ expect(attributes.fetch("c").fetch("values")).to eq(%w[a b])
166
+ expect(attributes.fetch("f").dig("default", "value")).to eq("2.0")
167
+ end
168
+
169
+ it "produces a bundle that imports and verifies from memory, not just from text" do
170
+ bundle = lml.export_bundle(fx::Scalars)
171
+ expect(lml.import_bundle(bundle)).to have_key("#{fx::Scalars.name}@default")
172
+ expect(lml.verify_materialization(bundle).checksum).to eq(bundle.checksum)
173
+ end
174
+
175
+ it "refuses a float default on an integer attribute at export" do
176
+ expect(reason_of { lml.export_bundle(fx::FloatOnInteger) }).to eq(:non_serializable_default)
177
+ end
178
+
179
+ it "refuses a string default on an integer attribute at export" do
180
+ expect(reason_of { project(fx::StringOnInteger) }).to eq(:non_serializable_default)
181
+ end
182
+ end
183
+
184
+ describe "M4: a custom value type cannot be exported" do
185
+ it "is refused as unsupported_type (the exporter emits no value_types)" do
186
+ expect(reason_of { project(fx::WithValueType) }).to eq(:unsupported_type)
187
+ expect(reason_of { lml.export_bundle(fx::WithValueType) }).to eq(:unsupported_type)
188
+ end
189
+ end
190
+
191
+ describe "L4: data values are never walked as references" do
192
+ it "exports a hash default shaped like a model reference" do
193
+ bundle = lml.export_bundle(fx::DataDefault)
194
+ expect(bundle.models.keys).to eq(["#{fx::DataDefault.name}@default"])
195
+ end
196
+ end
197
+
198
+ describe "L5: a reference attribute is refused as an unsupported type" do
199
+ it "does not report a dangling reference" do
200
+ expect(reason_of { lml.export_bundle(fx::WithReference) }).to eq(:unsupported_type)
201
+ end
202
+ end
203
+
204
+ describe "L6: anonymous classes" do
205
+ it "Exporter.project refuses an anonymous class instead of dropping the model key" do
206
+ anon = Class.new(Lutaml::Model::Serializable) { attribute :a, :string }
207
+ expect(reason_of { project(anon) }).to eq(:invalid_document)
208
+ end
209
+ end
210
+
211
+ describe "L10: registers and substitution" do
212
+ it "refuses an explicit register that is not registered" do
213
+ expect(reason_of { lml.export_bundle(fx::Plain, register: "no_such_register") }).to eq(:invalid_document)
214
+ end
215
+
216
+ it "refuses a root pair whose register is not registered" do
217
+ expect(reason_of { lml.export_bundle([[fx::Plain, "nosuch"]]) }).to eq(:invalid_document)
218
+ end
219
+
220
+ it "exports a root pair under a registered register" do
221
+ Lutaml::Model::GlobalRegister.register(Lutaml::Model::Register.new(:lml_review_pair))
222
+ expect(lml.export_bundle([[fx::Plain, "lml_review_pair"]]).roots)
223
+ .to eq(["#{fx::Plain.name}@lml_review_pair"])
224
+ ensure
225
+ Lutaml::Model::GlobalRegister.remove(:lml_review_pair)
226
+ end
227
+
228
+ it "exports under a registered register" do
229
+ Lutaml::Model::GlobalRegister.register(Lutaml::Model::Register.new(:lml_review_reg))
230
+ expect(lml.export_bundle(fx::Plain, register: "lml_review_reg").roots)
231
+ .to eq(["#{fx::Plain.name}@lml_review_reg"])
232
+ ensure
233
+ Lutaml::Model::GlobalRegister.remove(:lml_review_reg)
234
+ end
235
+
236
+ it "lets a failing register substitution propagate instead of silently using the original class" do
237
+ Lutaml::Model::GlobalRegister.register(Lutaml::Model::Register.new(:lml_review_sub))
238
+ registered = Lutaml::Model::GlobalRegister.lookup(:lml_review_sub)
239
+ allow(registered).to receive(:substitute).and_raise(RuntimeError, "substitution blew up")
240
+
241
+ expect { Lutaml::Model::Lml::GraphExporter.default_resolver(fx::Plain.name, "lml_review_sub") }
242
+ .to raise_error(RuntimeError, "substitution blew up")
243
+ ensure
244
+ Lutaml::Model::GlobalRegister.remove(:lml_review_sub)
245
+ end
246
+ end
247
+ end