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,390 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "json"
5
+ require "open3"
6
+ require "rbconfig"
7
+
8
+ # lutaml-model#1033 final review (Ruby, #1116 @ 7e23cd7), import side:
9
+ # M1 parents before children, M2 transactional install, M5 attribute
10
+ # names, L1 mapping scalars, L2 no descent into existing constants,
11
+ # L7 duplicate JSON keys on any json gem, L8 bounded nesting.
12
+ RSpec.describe "LML import hardening (review of #1116)" do
13
+ let(:lml) { Lutaml::Model::Lml }
14
+ let(:rejected) { lml::RejectedError }
15
+
16
+ def string_attr(name)
17
+ { "name" => name, "type" => { "kind" => "builtin", "name" => "string" } }
18
+ end
19
+
20
+ def model_doc(name, attributes = [], mappings: nil)
21
+ doc = { "lml" => 2, "model" => name, "register" => "default", "attributes" => attributes }
22
+ doc["mappings"] = mappings if mappings
23
+ doc
24
+ end
25
+
26
+ def bundle_of(*docs, value_types: {}, roots: nil)
27
+ models = docs.to_h { |doc| ["#{doc['model']}@#{doc['register']}", doc] }
28
+ formats = docs.flat_map { |doc| (doc["mappings"] || {}).keys }.uniq.sort
29
+ { "lml_bundle" => 1, "roots" => roots || [models.keys.min], "capabilities" => formats,
30
+ "value_types" => value_types, "models" => models }
31
+ end
32
+
33
+ def refusal_of
34
+ yield
35
+ nil
36
+ rescue rejected => e
37
+ e
38
+ end
39
+
40
+ describe "M1: a parent installs before its nested children" do
41
+ let(:bundle) do
42
+ bundle_of(model_doc("Para", [string_attr("t")]),
43
+ model_doc("Para::Inner", [string_attr("u")]))
44
+ end
45
+
46
+ before { stub_const("ImportHolder", Module.new) }
47
+
48
+ it "imports a model and a model nested under it into a namespace" do
49
+ classes = lml.import_bundle(bundle, namespace: ImportHolder)
50
+
51
+ expect(ImportHolder::Para).to be(classes["Para@default"])
52
+ expect(ImportHolder::Para::Inner).to be(classes["Para::Inner@default"])
53
+ expect(classes["Para::Inner@default"].name).to eq("ImportHolder::Para::Inner")
54
+ end
55
+
56
+ it "verifies the materialization of an exported parent and nested child" do
57
+ stub_const("VerifySrc", Module.new)
58
+ parent = Class.new(Lutaml::Model::Serializable) do
59
+ attribute :t, :string
60
+ xml { root "para" }
61
+ end
62
+ VerifySrc.const_set(:Para, parent)
63
+ child = Class.new(Lutaml::Model::Serializable) do
64
+ attribute :u, :string
65
+ xml { root "inner" }
66
+ end
67
+ parent.const_set(:Inner, child)
68
+
69
+ exported = lml.export_bundle([parent, child])
70
+ expect(exported.models.keys.first).to include("Inner")
71
+ expect { lml.verify_materialization(exported) }.not_to raise_error
72
+ end
73
+
74
+ it "gives the same result whichever way the ids sort" do
75
+ reversed = bundle_of(model_doc("Aa::Bb::Cc"), model_doc("Aa::Bb"), model_doc("Aa"))
76
+ classes = lml.import_bundle(reversed, namespace: ImportHolder)
77
+
78
+ expect(ImportHolder::Aa::Bb::Cc).to be(classes["Aa::Bb::Cc@default"])
79
+ end
80
+
81
+ it "builds a module holder for a path no model declares" do
82
+ classes = lml.import_bundle(bundle_of(model_doc("Pkg::Deep::Leaf")), namespace: ImportHolder)
83
+
84
+ expect(ImportHolder::Pkg).to be_a(Module).and(satisfy { |m| !m.is_a?(Class) })
85
+ expect(ImportHolder::Pkg::Deep::Leaf).to be(classes["Pkg::Deep::Leaf@default"])
86
+ end
87
+
88
+ it "refuses two registers that claim one constant" do
89
+ twin = bundle_of(model_doc("Twin"), model_doc("Twin").merge("register" => "other"))
90
+ error = refusal_of { lml.import_bundle(twin, namespace: ImportHolder) }
91
+
92
+ expect(error.reason).to eq(:duplicate_id)
93
+ expect(ImportHolder.constants).to be_empty
94
+ end
95
+ end
96
+
97
+ describe "M2: import is all or nothing" do
98
+ let(:bundle) do
99
+ bundle_of(model_doc("Fine", [string_attr("x")]),
100
+ model_doc("Needy", [{ "name" => "y", "type" => { "kind" => "value", "id" => "V" } }]),
101
+ value_types: { "V" => { "base" => "string", "facets" => {} } })
102
+ end
103
+
104
+ before { stub_const("ImportHolder", Module.new) }
105
+
106
+ it "leaves no constant behind when linking fails, and a retry succeeds" do
107
+ error = refusal_of { lml.import_bundle(bundle, namespace: ImportHolder) }
108
+
109
+ expect(error.reason).to eq(:missing_type_binding)
110
+ expect(ImportHolder.constants).to be_empty
111
+
112
+ classes = lml.import_bundle(bundle, namespace: ImportHolder,
113
+ type_bindings: { "V" => Lutaml::Model::Type::String })
114
+ expect(classes.keys).to eq(%w[Fine@default Needy@default])
115
+ end
116
+
117
+ it "removes the installed constants when the mapping compile refuses" do
118
+ rule = { "kind" => "element", "name" => ["n"], "to" => "nope" }
119
+ mapped = bundle_of(model_doc("Ok1", [string_attr("a")]),
120
+ model_doc("Mapped", [string_attr("a")],
121
+ mappings: { "xml" => { "rules" => [rule] } }))
122
+ error = refusal_of { lml.import_bundle(mapped, namespace: ImportHolder) }
123
+
124
+ expect(error.reason).to eq(:dangling_reference)
125
+ expect(ImportHolder.constants).to be_empty
126
+
127
+ rule["to"] = "a"
128
+ expect(lml.import_bundle(mapped, namespace: ImportHolder).keys).to eq(%w[Mapped@default Ok1@default])
129
+ end
130
+ end
131
+
132
+ describe "M5: attribute names" do
133
+ def import_attribute(name)
134
+ lml.import_bundle(bundle_of(model_doc("A", [string_attr(name)])))["A@default"]
135
+ end
136
+
137
+ ["@x", "a b", "foo=", "1", "x-y", "Name", "ok?", "go!", ""].each do |name|
138
+ it "refuses the non-identifier #{name.inspect}" do
139
+ error = refusal_of { import_attribute(name) }
140
+
141
+ expect(error).to be_a(rejected)
142
+ expect(error.reason).to eq(:invalid_document)
143
+ expect(error.location).to eq("/models/A@default/attributes/0/name")
144
+ end
145
+ end
146
+
147
+ %w[initialize class element_order attribute_order to_json to_xml
148
+ lutaml_register public_send encoding].each do |name|
149
+ it "refuses #{name}, which breaks construction or serialization" do
150
+ error = refusal_of { import_attribute(name) }
151
+
152
+ expect(error&.reason).to eq(:reserved_member)
153
+ expect(error.attribute).to eq(name)
154
+ end
155
+ end
156
+
157
+ %w[display hash schema_location send freeze to_s object_id title
158
+ _private snake_case_2 value].each do |name|
159
+ it "accepts #{name} and builds a class that round-trips it" do
160
+ klass = import_attribute(name)
161
+ instance = klass.new(name => "v")
162
+
163
+ expect(instance.public_send(name)).to eq("v")
164
+ expect(klass.from_json(instance.to_json).public_send(name)).to eq("v")
165
+ end
166
+ end
167
+
168
+ it "re-derives the reserved attribute names by measurement (every broken name is denied)" do
169
+ skip "needs fork to isolate each probe" unless Process.respond_to?(:fork)
170
+
171
+ serializable = Lutaml::Model::Serializable
172
+ candidates = (Object.public_instance_methods + Object.protected_instance_methods +
173
+ serializable.public_instance_methods + serializable.protected_instance_methods).map(&:to_s) +
174
+ %w[initialize initialize_copy initialize_dup initialize_clone method_missing]
175
+ candidates = candidates.uniq.grep(lml::DocumentValidator::ATTRIBUTE_NAME).sort
176
+
177
+ broken = candidates.reject { |name| isolated_attribute_works?(name) }
178
+
179
+ expect(candidates.size).to be > 50
180
+ reserved = lml::DocumentValidator::RESERVED_ATTRIBUTE_NAMES
181
+ expect(broken - reserved).to be_empty
182
+ expect(reserved - candidates).to be_empty
183
+ end
184
+
185
+ # One forked process per probe: a broken name must not poison the
186
+ # next measurement.
187
+ def isolated_attribute_works?(name)
188
+ reader, writer = IO.pipe
189
+ pid = fork do
190
+ reader.close
191
+ $stderr.reopen(File::NULL)
192
+ writer.write(attribute_works?(name) ? "ok" : "broken")
193
+ writer.close
194
+ exit!(0)
195
+ end
196
+ writer.close
197
+ result = reader.read
198
+ Process.wait(pid)
199
+ result == "ok"
200
+ end
201
+
202
+ def attribute_works?(name)
203
+ sym = name.to_sym
204
+ klass = Class.new(Lutaml::Model::Serializable) do
205
+ attribute sym, :string
206
+ xml do
207
+ root "r"
208
+ map_element "e", to: sym
209
+ end
210
+ key_value { map "e", to: sym }
211
+ end
212
+ Object.const_set(:MeasuredProbe, klass)
213
+ klass.new
214
+ instance = klass.new(name => "v")
215
+ instance.public_send(name) == "v" &&
216
+ klass.from_json(instance.to_json).public_send(name) == "v" &&
217
+ klass.from_xml(instance.to_xml).public_send(name) == "v" &&
218
+ klass.from_yaml(instance.to_yaml).public_send(name) == "v"
219
+ rescue StandardError, SystemStackError
220
+ false
221
+ end
222
+ end
223
+
224
+ describe "L1: mapping scalars" do
225
+ def xml_bundle(mapping)
226
+ bundle_of(model_doc("A", [string_attr("x")], mappings: { "xml" => mapping }))
227
+ end
228
+
229
+ def namespace_mapping(prefix)
230
+ { "namespace" => { "mode" => "uri", "uri" => "http://x", "prefix" => prefix }, "rules" => [] }
231
+ end
232
+
233
+ ["str", 5, [1], true].each do |root|
234
+ it "refuses root #{root.inspect} with a named code" do
235
+ error = refusal_of { lml.import_bundle(xml_bundle("root" => root, "rules" => [])) }
236
+
237
+ expect(error&.reason).to eq(:invalid_document)
238
+ end
239
+ end
240
+
241
+ [5, ["a"], ""].each do |element|
242
+ it "refuses root.element #{element.inspect}" do
243
+ error = refusal_of { lml.import_bundle(xml_bundle("root" => { "element" => element }, "rules" => [])) }
244
+
245
+ expect(error&.reason).to eq(:invalid_document)
246
+ end
247
+ end
248
+
249
+ [5, { "a" => 1 }, [], "", "a b"].each do |prefix|
250
+ it "refuses namespace prefix #{prefix.inspect}" do
251
+ error = refusal_of { lml.import_bundle(xml_bundle(namespace_mapping(prefix))) }
252
+
253
+ expect(error&.reason).to eq(:invalid_document)
254
+ end
255
+ end
256
+
257
+ it "refuses a rule child_mappings that is not an object" do
258
+ rule = { "key" => ["k"], "to" => "x", "child_mappings" => "oops" }
259
+ bundle = bundle_of(model_doc("A", [string_attr("x")], mappings: { "json" => { "rules" => [rule] } }))
260
+
261
+ expect(refusal_of { lml.import_bundle(bundle) }&.reason).to eq(:invalid_document)
262
+ end
263
+
264
+ it "still imports a well-formed root, prefix and rule" do
265
+ mapping = namespace_mapping("p").merge("root" => { "element" => "a" })
266
+ classes = lml.import_bundle(xml_bundle(mapping))
267
+
268
+ expect(classes["A@default"].new(x: "1").to_xml).to include("http://x")
269
+ end
270
+ end
271
+
272
+ describe "L2: existing constants are never descended into" do
273
+ before { stub_const("ImportHolder", Module.new) }
274
+
275
+ it "refuses a name whose first segment is a non-module value" do
276
+ ImportHolder.const_set(:Foo, 5)
277
+ error = refusal_of { lml.import_bundle(bundle_of(model_doc("Foo::Bar")), namespace: ImportHolder) }
278
+
279
+ expect(error&.reason).to eq(:duplicate_id)
280
+ expect(ImportHolder::Foo).to eq(5)
281
+ end
282
+
283
+ it "refuses a pre-existing class as an intermediate and injects nothing into it" do
284
+ config = Class.new
285
+ ImportHolder.const_set(:Config, config)
286
+ error = refusal_of { lml.import_bundle(bundle_of(model_doc("Config::Evil")), namespace: ImportHolder) }
287
+
288
+ expect(error&.reason).to eq(:duplicate_id)
289
+ expect(config.constants).to be_empty
290
+ end
291
+
292
+ it "refuses a pre-existing module as an intermediate and leaves it untouched" do
293
+ existing = Module.new
294
+ ImportHolder.const_set(:Shared, existing)
295
+ error = refusal_of { lml.import_bundle(bundle_of(model_doc("Shared::Evil")), namespace: ImportHolder) }
296
+
297
+ expect(error&.reason).to eq(:duplicate_id)
298
+ expect(existing.constants).to be_empty
299
+ end
300
+ end
301
+
302
+ describe "L7: duplicate JSON keys" do
303
+ let(:text) do
304
+ a1 = '{"name":"x","type":{"kind":"builtin","name":"string"}}'
305
+ a2 = '{"name":"x","type":{"kind":"builtin","name":"integer"}}'
306
+ %({"lml_bundle":1,"roots":["A@default"],"capabilities":[],"value_types":{},) +
307
+ %("models":{"A@default":{"lml":2,"model":"A","register":"default",) +
308
+ %("attributes":[#{a1}],"attributes":[#{a2}]}}})
309
+ end
310
+
311
+ it "is refused" do
312
+ error = refusal_of { lml::Importer.bundle(text) }
313
+
314
+ expect(error&.reason).to eq(:invalid_document)
315
+ expect(error.message).to include("duplicate key")
316
+ end
317
+
318
+ it "is refused on a json gem that keeps the last value (json < 3.1)" do
319
+ older = Gem::Specification.find_all_by_name("json", "< 3.1").max_by(&:version)
320
+ skip "no json < 3.1 installed" unless older
321
+
322
+ script = <<~RUBY
323
+ gem "json", "= #{older.version}"
324
+ $LOAD_PATH.unshift(File.expand_path("lib", #{Dir.pwd.inspect}))
325
+ require "lutaml/model"
326
+ require "lutaml/model/lml"
327
+ text = #{text.inspect}
328
+ begin
329
+ Lutaml::Model::Lml::Importer.bundle(text)
330
+ puts "ACCEPTED"
331
+ rescue Lutaml::Model::Lml::RejectedError => e
332
+ puts "REFUSED \#{e.reason}"
333
+ end
334
+ RUBY
335
+ out, status = Open3.capture2e(RbConfig.ruby, "-e", script)
336
+
337
+ expect(status).to be_success
338
+ expect(out.lines.last.to_s.strip).to eq("REFUSED invalid_document")
339
+ end
340
+
341
+ it "refuses a repeated key spelled with an escape, in a nested object" do
342
+ nested = '{"lml_bundle":1,"provenance":{"a":{"k":1,"\\u006b":2}}}'
343
+
344
+ expect(refusal_of { lml::Importer.bundle(nested) }&.reason).to eq(:invalid_document)
345
+ end
346
+
347
+ it "allows one key in sibling objects and a value equal to a key" do
348
+ sibling = '{"lml_bundle":1,"roots":[],"capabilities":[],"value_types":{},"models":{},' \
349
+ '"provenance":{"a":{"k":"k"},"b":{"k":"k"},"c":["k","k"]}}'
350
+
351
+ expect(lml::Importer.bundle(sibling).provenance.keys).to eq(%w[a b c])
352
+ end
353
+
354
+ it "still accepts a document with distinct keys" do
355
+ distinct = text.sub('"attributes":[{"name":"x","type":{"kind":"builtin","name":"integer"}}]', '"mappings":{}')
356
+
357
+ expect(lml::Importer.bundle(distinct).models.keys).to eq(["A@default"])
358
+ end
359
+ end
360
+
361
+ describe "L8: bounded nesting" do
362
+ def deep(levels)
363
+ root = {}
364
+ cursor = root
365
+ levels.times do
366
+ node = {}
367
+ cursor["k"] = node
368
+ cursor = node
369
+ end
370
+ root
371
+ end
372
+
373
+ def bundle_with_provenance(tree)
374
+ { "lml_bundle" => 1, "roots" => [], "models" => {}, "value_types" => {},
375
+ "capabilities" => [], "provenance" => tree }
376
+ end
377
+
378
+ it "refuses a 12,000-deep Hash with a named code, not SystemStackError" do
379
+ error = refusal_of { lml::Importer.bundle(bundle_with_provenance(deep(12_000))) }
380
+
381
+ expect(error&.reason).to eq(:invalid_document)
382
+ expect(error.message).to include("nesting deeper than")
383
+ end
384
+
385
+ it "accepts nesting up to the limit" do
386
+ limit = lml::DocumentValidator::MAX_DEPTH
387
+ expect { lml::Importer.bundle(bundle_with_provenance(deep(limit - 2))) }.not_to raise_error
388
+ end
389
+ end
390
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "fileutils"
5
+ require "open3"
6
+ require "tmpdir"
7
+ require "yaml"
8
+ require "lutaml/model/lml"
9
+
10
+ RSpec.describe "LML review round-3 misc fixes" do
11
+ let(:lml) { Lutaml::Model::Lml }
12
+ let(:fixtures) { File.expand_path("../../fixtures/lml_artifacts", __dir__) }
13
+
14
+ describe "L3: Bundle owns private copies of its inputs" do
15
+ it "does not change identity or freeze the caller's strings" do
16
+ root = +"A@default"
17
+ capability = +"json"
18
+ name = +"A"
19
+ doc = { "lml" => 2, "model" => name, "register" => "default", "attributes" => [] }
20
+ bundle = lml::Bundle.new(roots: [root], models: { "A@default" => doc },
21
+ capabilities: [capability])
22
+ before = bundle.checksum
23
+
24
+ root << "x"
25
+ capability << "y"
26
+ name << "z"
27
+
28
+ expect(bundle.roots).to eq(["A@default"])
29
+ expect(bundle.capabilities).to eq(["json"])
30
+ expect(bundle.checksum).to eq(before)
31
+ expect(name.frozen?).to be(false)
32
+ expect(root.frozen?).to be(false)
33
+ expect(bundle.roots).to all(be_frozen)
34
+ expect(bundle.models["A@default"]["model"]).to be_frozen
35
+ end
36
+ end
37
+
38
+ describe "L9: OrderedContent.apply refuses tokens serialization overrides" do
39
+ let(:klass) do
40
+ Class.new(Lutaml::Model::Serializable) do
41
+ attribute :text, :string, collection: true
42
+ attribute :em, :string, collection: true
43
+ xml do
44
+ root "mixed"
45
+ mixed_content
46
+ map_content to: :text
47
+ map_element "em", to: :em
48
+ end
49
+
50
+ def self.name = "ReviewMisc::Mixed"
51
+ end
52
+ end
53
+ let(:descriptor) do
54
+ lml::OrderedContent.descriptor_for(klass, mapping: klass.mappings_for(:xml))
55
+ end
56
+ let(:text0) { { "kind" => "text", "attribute" => "text", "index" => 0 } }
57
+ let(:em0) { { "kind" => "element", "attribute" => "em", "index" => 0 } }
58
+ let(:literal) { { "kind" => "text", "literal" => "ZZ" } }
59
+
60
+ def apply(attrs, tokens)
61
+ instance = klass.new(**attrs)
62
+ lml::OrderedContent.apply(descriptor, tokens, instance)
63
+ instance
64
+ end
65
+
66
+ it "rejects a literal that a remaining content value would override" do
67
+ expect { apply({ text: ["a"], em: ["x"] }, [literal, em0]) }
68
+ .to raise_error(lml::RejectedError) { |e| expect(e.message).to include("invalid_ordered_content") }
69
+ expect { apply({ text: %w[a b], em: [] }, [text0, literal]) }
70
+ .to raise_error(lml::RejectedError, /invalid_ordered_content/)
71
+ end
72
+
73
+ it "rejects a literal CDATA token the content values would replace" do
74
+ expect { apply({ text: ["a"], em: [] }, [{ "kind" => "cdata", "literal" => "C" }]) }
75
+ .to raise_error(lml::RejectedError, /invalid_ordered_content.*would be overridden/)
76
+ end
77
+
78
+ it "still accepts literals the serializer keeps" do
79
+ cdata = { "kind" => "cdata", "literal" => "C" }
80
+ expect(apply({ text: ["a"], em: [] }, [text0, cdata]).to_xml).to include("C")
81
+
82
+ instance = apply({ text: ["a"], em: ["x"] }, [text0, literal, em0])
83
+ expect(instance.to_xml).to include("ZZ")
84
+ expect(apply({ text: [], em: ["x"] }, [literal, em0]).to_xml).to include("ZZ")
85
+ end
86
+ end
87
+
88
+ describe "L10: ArtifactVerifier" do
89
+ it "is autoloaded from Lutaml::Model::Lml" do
90
+ out, status = Open3.capture2e(RbConfig.ruby, "-Ilib", "-e",
91
+ 'require "lutaml/model"; require "lutaml/model/lml"; ' \
92
+ "puts Lutaml::Model::Lml::ArtifactVerifier.name")
93
+ expect(status.success?).to be(true), out
94
+ expect(out).to include("Lutaml::Model::Lml::ArtifactVerifier")
95
+ end
96
+
97
+ it "keeps digest and pathname out of top-level requires" do
98
+ source = File.read(File.expand_path("../../../lib/lutaml/model/lml/artifact_verifier.rb", __dir__))
99
+ expect(source).not_to match(/^require ["'](digest|pathname)["']/)
100
+ end
101
+
102
+ context "with a manifest lacking files" do
103
+ let(:manifest) do
104
+ JSON.parse(File.read(File.join(fixtures, "valid.json"))).tap { |m| m.delete("files") }
105
+ end
106
+ let(:pin) { lml::ArtifactVerifier.checksum(manifest) }
107
+
108
+ it "raises the named error, not KeyError" do
109
+ verifier = lml::ArtifactVerifier.new(root: fixtures, expected_sha256: pin)
110
+ expect { verifier.verify(manifest) }
111
+ .to raise_error(lml::ArtifactVerifier::Error, %r{invalid_document: /files})
112
+ end
113
+
114
+ it "makes the CLI exit 1 with a JSON verdict" do
115
+ Dir.mktmpdir do |dir|
116
+ path = File.join(dir, "manifest.json")
117
+ File.write(path, JSON.generate(manifest))
118
+ out, err, status = Open3.capture3(RbConfig.ruby, "scripts/lml/verify_artifacts.rb",
119
+ "--manifest", path, "--expected-sha256", pin)
120
+ expect(status.exitstatus).to eq(1), "#{out}#{err}"
121
+ expect(JSON.parse(err)).to include("verified" => false)
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "L10: CI fixtures-unchanged step" do
128
+ let(:step) do
129
+ workflow = YAML.load_file(File.expand_path("../../../.github/workflows/lml-conformance.yml", __dir__))
130
+ workflow["jobs"].values.flat_map { |job| job["steps"] }
131
+ .find { |s| s["name"] == "Ensure verification leaves fixtures unchanged" }
132
+ end
133
+
134
+ it "fails on untracked files as well as modifications" do
135
+ Dir.mktmpdir do |dir|
136
+ env = { "GIT_AUTHOR_NAME" => "t", "GIT_AUTHOR_EMAIL" => "t@e", "GIT_COMMITTER_NAME" => "t",
137
+ "GIT_COMMITTER_EMAIL" => "t@e" }
138
+ FileUtils.mkdir_p(File.join(dir, "scripts/lml"))
139
+ FileUtils.mkdir_p(File.join(dir, "spec/fixtures"))
140
+ File.write(File.join(dir, "scripts/lml/a.rb"), "1")
141
+ [%w[git init -q], %w[git add .], %w[git commit -qm x]].each do |cmd|
142
+ system(env, *cmd, chdir: dir, exception: true)
143
+ end
144
+ run = -> { Open3.capture2e("sh", "-ec", step["run"], chdir: dir)[1].exitstatus }
145
+
146
+ expect(run.call).to eq(0)
147
+ File.write(File.join(dir, "spec/fixtures/stray.json"), "{}")
148
+ expect(run.call).to eq(1)
149
+ end
150
+ end
151
+ end
152
+ end