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.
- checksums.yaml +4 -4
- data/.gitattributes +5 -0
- data/.github/workflows/lml-conformance.yml +71 -0
- data/.gitignore +7 -0
- data/README.adoc +1 -0
- data/docs/_guides/lml-cross-language.adoc +128 -0
- data/docs/_guides/native-engines.adoc +10 -0
- data/docs/_references/lml-conformance.adoc +78 -0
- data/docs/_references/lml-contract.adoc +290 -0
- data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
- data/lib/lutaml/model/lml/bundle.rb +171 -0
- data/lib/lutaml/model/lml/document.rb +46 -10
- data/lib/lutaml/model/lml/document_validator.rb +840 -0
- data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
- data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
- data/lib/lutaml/model/lml/exporter.rb +223 -87
- data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
- data/lib/lutaml/model/lml/importer.rb +180 -0
- data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
- data/lib/lutaml/model/lml/materialization.rb +257 -0
- data/lib/lutaml/model/lml/ordered_content.rb +577 -0
- data/lib/lutaml/model/lml/portable_json.rb +385 -0
- data/lib/lutaml/model/lml/rejected_error.rb +64 -15
- data/lib/lutaml/model/lml/round_trip.rb +86 -0
- data/lib/lutaml/model/lml/value_declarations.rb +84 -0
- data/lib/lutaml/model/lml.rb +61 -0
- data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
- data/lib/lutaml/model/schema/registry_generator.rb +1 -1
- data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
- data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
- data/lib/lutaml/model/sequence.rb +7 -0
- data/lib/lutaml/model/serialize/format_conversion.rb +22 -8
- data/lib/lutaml/model/serialize/initialization.rb +2 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/leptris/native_emission.rb +85 -3
- data/lib/lutaml/xml/leptris/plan_compiler.rb +190 -45
- data/lib/lutaml/xml/leptris/plan_hydrator.rb +209 -56
- data/lib/lutaml/xml/leptris/plan_serializer.rb +533 -102
- data/lib/lutaml/xml/leptris/plan_walk.rb +1 -0
- data/lib/lutaml/xml/schema/builder/moxml.rb +7 -3
- data/lib/lutaml/xml/transformation/order_reconciler.rb +4 -0
- data/lib/lutaml/xml/transformation/ordered_applier.rb +8 -7
- data/schema/lml/document.schema.json +179 -0
- data/scripts/lml/export_choices.rb +57 -0
- data/scripts/lml/export_conformance.rb +23 -0
- data/scripts/lml/export_mml.rb +253 -0
- data/scripts/lml/export_preservation.rb +421 -0
- data/scripts/lml/verify_artifacts.rb +31 -0
- data/spec/fixtures/lml_artifacts/model.json +1 -0
- data/spec/fixtures/lml_artifacts/valid.json +1 -0
- data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
- data/spec/fixtures/lml_conformance/core/index.json +1 -0
- data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
- data/spec/fixtures/lml_conformance/core/schema.json +1 -0
- data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
- data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
- data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
- data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
- data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
- data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
- data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
- data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
- data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
- data/spec/fixtures/lml_contract/bundle.json +81 -0
- data/spec/fixtures/lml_contract/manifest.json +30 -0
- data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
- data/spec/fixtures/lml_contract/v1_unit.json +1 -0
- data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
- data/spec/fixtures/lml_contract/v2_node.json +32 -0
- data/spec/fixtures/lml_mml/inventory.json +1 -0
- data/spec/fixtures/lml_mml/manifest.json +1 -0
- data/spec/fixtures/lml_mml/oracle.json +1 -0
- data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
- data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
- data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
- data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
- data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
- data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
- data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
- data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
- data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
- data/spec/lutaml/model/lml_document_spec.rb +279 -0
- data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
- data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
- data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
- data/spec/lutaml/model/lml_importer_spec.rb +290 -0
- data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
- data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
- data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
- data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
- data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
- data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
- data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
- data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
- data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
- data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
- data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
- data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
- data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
- data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
- data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
- data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
- data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
- data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
- data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
- data/spec/lutaml/model/sequence_spec.rb +48 -0
- data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
- data/spec/lutaml/xml/production_lane_parity_spec.rb +632 -0
- data/spec/support/lml_any_attribute_fixtures.rb +26 -0
- data/spec/support/lml_conformance.rb +292 -0
- data/spec/support/lml_corpus.rb +75 -0
- data/spec/support/lml_export_fixtures.rb +190 -0
- data/spec/support/lml_graph_fixtures.rb +108 -0
- data/spec/support/lml_import_fixtures.rb +52 -0
- data/spec/support/lml_mapping_fixtures.rb +170 -0
- data/spec/support/lml_materialization_fixtures.rb +53 -0
- data/spec/support/lml_model_choices_fixtures.rb +150 -0
- data/spec/support/lml_order_fixtures.rb +151 -0
- metadata +94 -3
- data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model"
|
|
4
|
+
require "lutaml/model/lml/document_validator"
|
|
5
|
+
require "json"
|
|
6
|
+
require "yaml"
|
|
7
|
+
|
|
8
|
+
# lutaml-model#1033 card 01: the agreed document contract. The validator
|
|
9
|
+
# accepts v1 unchanged and strict v2 documents/bundles/manifests, and
|
|
10
|
+
# refuses everything else with a named code and a JSON-pointer location.
|
|
11
|
+
RSpec.describe Lutaml::Model::Lml::DocumentValidator do
|
|
12
|
+
let(:fixtures) { File.expand_path("../../fixtures/lml_contract", __dir__) }
|
|
13
|
+
let(:rejected) { Lutaml::Model::Lml::RejectedError }
|
|
14
|
+
let(:legacy_doc) { fixture("v1_unit.json") }
|
|
15
|
+
let(:v2) { fixture("v2_node.json") }
|
|
16
|
+
let(:bundle) { fixture("bundle.json") }
|
|
17
|
+
let(:manifest) { fixture("manifest.json") }
|
|
18
|
+
let(:model_scoped_codes) { %i[lambda_default reserved_member duplicate_id] }
|
|
19
|
+
|
|
20
|
+
def fixture(name)
|
|
21
|
+
JSON.parse(File.read(File.join(fixtures, name)))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def deep_copy(value)
|
|
25
|
+
JSON.parse(JSON.generate(value))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def validate(hash, kind: :document)
|
|
29
|
+
described_class.validate!(hash, kind: kind)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def refusal(hash, kind: :document)
|
|
33
|
+
validate(hash, kind: kind)
|
|
34
|
+
nil
|
|
35
|
+
rescue Lutaml::Model::Lml::RejectedError => e
|
|
36
|
+
e
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
describe "valid input" do
|
|
40
|
+
it "accepts a v1 document, a v2 document, a bundle and a manifest" do
|
|
41
|
+
expect(validate(legacy_doc)["lml"]).to eq(1)
|
|
42
|
+
expect(validate(v2)["lml"]).to eq(2)
|
|
43
|
+
expect(validate(bundle, kind: :bundle)["lml_bundle"]).to eq(1)
|
|
44
|
+
expect(validate(manifest, kind: :manifest)["manifest"]).to eq(1)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "returns a deeply immutable tree and leaves the input untouched" do
|
|
48
|
+
tree = validate(v2)
|
|
49
|
+
expect(tree).to be_frozen
|
|
50
|
+
expect(tree["attributes"].first["type"]).to be_frozen
|
|
51
|
+
expect(tree["attributes"].first["name"]).to be_frozen
|
|
52
|
+
expect(v2).not_to be_frozen
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "ignores unknown v1 keys (legacy tolerance) but never in v2" do
|
|
56
|
+
expect(refusal(legacy_doc.merge("extra" => 1))).to be_nil
|
|
57
|
+
expect(refusal(v2.merge("extra" => 1)).reason).to eq(:invalid_document)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
describe "version handling" do
|
|
62
|
+
it "refuses an unknown document version instead of rewriting it" do
|
|
63
|
+
error = refusal(v2.merge("lml" => 999))
|
|
64
|
+
expect(error.reason).to eq(:unsupported_version)
|
|
65
|
+
expect(error.location).to eq("/lml")
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "refuses unknown bundle and manifest versions" do
|
|
69
|
+
expect(refusal(bundle.merge("lml_bundle" => 2), kind: :bundle).reason).to eq(:unsupported_version)
|
|
70
|
+
expect(refusal(manifest.merge("manifest" => 2), kind: :manifest).reason).to eq(:unsupported_version)
|
|
71
|
+
expect(refusal(manifest.merge("schema_version" => 3), kind: :manifest).reason).to eq(:unsupported_version)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "refuses a v1 document inside a v2 bundle" do
|
|
75
|
+
b = deep_copy(bundle)
|
|
76
|
+
b["models"]["Example::Node@default"]["lml"] = 1
|
|
77
|
+
expect(refusal(b, kind: :bundle).reason).to eq(:unsupported_version)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
describe "Document.from_hash" do
|
|
82
|
+
# old-red: before this card from_hash rewrote any supplied version to 1,
|
|
83
|
+
# so an unsupported `lml` was silently dropped and a v1 document came back.
|
|
84
|
+
it "does not silently rewrite an unsupported lml version" do
|
|
85
|
+
expect { Lutaml::Model::Lml::Document.from_hash(legacy_doc.merge("lml" => 999)) }
|
|
86
|
+
.to raise_error(rejected, /unsupported_version/)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it "keeps v1 identity: same canonical form and checksum" do
|
|
90
|
+
doc = Lutaml::Model::Lml::Document.from_hash(legacy_doc)
|
|
91
|
+
expect(doc.to_h["lml"]).to eq(1)
|
|
92
|
+
expect(doc.canonical_json).to eq(File.read(File.join(fixtures, "v1_unit.canonical.json")))
|
|
93
|
+
expect(doc.checksum).to eq("sha256:04b66d5ff963c422758bce40377498c7f466645c215f19bccc5936d78468911b")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "keeps the v2 version and pins canonical JSON and checksum" do
|
|
97
|
+
doc = Lutaml::Model::Lml::Document.from_hash(v2)
|
|
98
|
+
canonical = File.binread(File.join(fixtures, "v2_node.canonical.json"))
|
|
99
|
+
expect(doc.to_h["lml"]).to eq(2)
|
|
100
|
+
expect(doc.canonical_json.b).to eq(canonical)
|
|
101
|
+
expect(canonical.dup.force_encoding("UTF-8")).to include("café/x")
|
|
102
|
+
expect(doc.checksum).to eq("sha256:b4b3f994b6069fe2982f8de18a40df0f044d85d000912dc97044201c7785bf43")
|
|
103
|
+
expect(doc.checksum).to eq("sha256:#{Digest::SHA256.file(File.join(fixtures, 'v2_node.canonical.json')).hexdigest}")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it "gives v2 a key-order independent canonical form" do
|
|
107
|
+
shuffled = v2.to_a.reverse.to_h
|
|
108
|
+
shuffled["attributes"] = shuffled["attributes"].map { |a| a.to_a.reverse.to_h }
|
|
109
|
+
expect(Lutaml::Model::Lml::Document.from_hash(shuffled).checksum)
|
|
110
|
+
.to eq(Lutaml::Model::Lml::Document.from_hash(v2).checksum)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "round-trips v2 through YAML to the same tree" do
|
|
114
|
+
doc = Lutaml::Model::Lml::Document.from_hash(v2)
|
|
115
|
+
reloaded = YAML.safe_load(doc.to_yaml, permitted_classes: [], aliases: false)
|
|
116
|
+
expect(Lutaml::Model::Lml::Document.from_hash(reloaded).checksum).to eq(doc.checksum)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "reports a missing model as invalid_document" do
|
|
120
|
+
expect { Lutaml::Model::Lml::Document.from_hash("lml" => 1) }
|
|
121
|
+
.to raise_error(rejected, /invalid_document/)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
describe "portable float" do
|
|
126
|
+
it "pins the canonical wire spelling" do
|
|
127
|
+
expect([0.71, 1.0e-05, 1e20, 3.0, -0.0, 1e16, 123456789.125, 5e-324, 1.7976931348623157e308]
|
|
128
|
+
.map { |value| described_class.float_wire(value) })
|
|
129
|
+
.to eq(%w[0.71 1.0e-5 1.0e20 3.0 -0.0 1.0e16 123456789.125 5.0e-324 1.7976931348623157e308])
|
|
130
|
+
expect { described_class.float_wire(Float::NAN) }.to raise_error(ArgumentError)
|
|
131
|
+
expect { described_class.float_wire(-Float::INFINITY) }.to raise_error(ArgumentError)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
it "round-trips every canonical string to the identical binary64 value" do
|
|
135
|
+
[0.1, 0.71, 1.0e-05, 2.2250738585072014e-308, 9007199254740993.0, -1.5e-7, 1e22].each do |value|
|
|
136
|
+
wire = described_class.float_wire(value)
|
|
137
|
+
expect(described_class.canonical_float?(wire)).to be(true)
|
|
138
|
+
expect(Float(wire)).to eq(value)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it "accepts a canonical float default and refuses spellings that are not canonical" do
|
|
143
|
+
doc = deep_copy(v2)
|
|
144
|
+
doc["attributes"][1].merge!("type" => { "kind" => "builtin", "name" => "float" },
|
|
145
|
+
"default" => { "present" => true, "value" => "0.71" })
|
|
146
|
+
expect(refusal(doc)).to be_nil
|
|
147
|
+
%w[0.710 +0.71 .71 1e-5 1.0E-5 1.0e+20 1.0e-05 0x1p3 NaN Infinity -Infinity].each do |spelling|
|
|
148
|
+
doc["attributes"][1]["default"]["value"] = spelling
|
|
149
|
+
expect(refusal(doc)&.reason).to eq(:invalid_document), spelling
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
describe "model errors" do
|
|
155
|
+
def with_attribute(extra)
|
|
156
|
+
doc = deep_copy(v2)
|
|
157
|
+
doc["attributes"] << { "name" => "extra", "type" => { "kind" => "builtin", "name" => "string" } }.merge(extra)
|
|
158
|
+
doc
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
{
|
|
162
|
+
"lambda default" => [->(d) { d["attributes"][1]["default"] = { "lambda" => "-> { 1 }" } }, :lambda_default, "/attributes/1/default"],
|
|
163
|
+
"present default without value" => [->(d) { d["attributes"][1]["default"] = { "present" => true } }, :invalid_document, "/attributes/1/default/value"],
|
|
164
|
+
"custom initialize" => [->(d) { d["custom_initialize"] = true }, :custom_initialize, "/custom_initialize"],
|
|
165
|
+
"imperative order" => [->(d) { d["imperative_element_order"] = true }, :imperative_element_order, "/imperative_element_order"],
|
|
166
|
+
"unknown builtin type" => [->(d) { d["attributes"][1]["type"] = { "kind" => "builtin", "name" => "nope" } }, :unknown_type, "/attributes/1/type/name"],
|
|
167
|
+
"unknown type kind" => [->(d) { d["attributes"][1]["type"] = { "kind" => "weird" } }, :unknown_type, "/attributes/1/type/kind"],
|
|
168
|
+
"double alias" => [->(d) { d["attributes"][1]["type"] = { "kind" => "builtin", "name" => "double" } }, :unsupported_type, "/attributes/1/type/name"],
|
|
169
|
+
"float default as a JSON number" => [->(d) { d["attributes"][1].merge!("type" => { "kind" => "builtin", "name" => "float" }, "default" => { "present" => true, "value" => 1.5 }) }, :invalid_document, "/attributes/1/default/value"],
|
|
170
|
+
"float default non-canonical" => [->(d) { d["attributes"][1].merge!("type" => { "kind" => "builtin", "name" => "float" }, "default" => { "present" => true, "value" => "1.0e-05" }) }, :invalid_document, "/attributes/1/default/value"],
|
|
171
|
+
"float default NaN" => [->(d) { d["attributes"][1].merge!("type" => { "kind" => "builtin", "name" => "float" }, "default" => { "present" => true, "value" => "NaN" }) }, :invalid_document, "/attributes/1/default/value"],
|
|
172
|
+
"float default Infinity" => [->(d) { d["attributes"][1].merge!("type" => { "kind" => "builtin", "name" => "float" }, "default" => { "present" => true, "value" => "Infinity" }) }, :invalid_document, "/attributes/1/default/value"],
|
|
173
|
+
"duplicate attribute" => [->(d) { d["attributes"][1]["name"] = "children" }, :duplicate_id, "/attributes/1/name"],
|
|
174
|
+
"reserved member" => [->(d) { d["attributes"][1]["name"] = "__lml_content" }, :reserved_member, "/attributes/1/name"],
|
|
175
|
+
"unknown format" => [->(d) { d["mappings"]["csv"] = {} }, :unsupported_format, "/mappings/csv"],
|
|
176
|
+
"executable rule" => [->(d) { d["mappings"]["xml"]["rules"][0]["with"] = { "to" => "x" } }, :unsupported_mapping, "/mappings/xml/rules/0/with"],
|
|
177
|
+
"unknown rule key" => [->(d) { d["mappings"]["xml"]["rules"][0]["mystery"] = 1 }, :unsupported_mapping, "/mappings/xml/rules/0/mystery"],
|
|
178
|
+
"unknown namespace mode" => [->(d) { d["mappings"]["xml"]["namespace"] = { "mode" => "guess" } }, :unsupported_mapping, "/mappings/xml/namespace/mode"],
|
|
179
|
+
"bad ordered content" => [->(d) { d["ordered_content"] = { "member" => "other", "version" => 1 } }, :invalid_ordered_content, "/ordered_content"],
|
|
180
|
+
"float in a value" => [->(d) { d["attributes"][1]["default"] = { "present" => true, "value" => 1.5 } }, :invalid_document, "/attributes/1/default/value"],
|
|
181
|
+
"oversized integer" => [->(d) { d["attributes"][1]["default"] = { "present" => true, "value" => 2**60 } }, :invalid_document, "/attributes/1/default/value"],
|
|
182
|
+
"invalid register" => [->(d) { d["register"] = "has space" }, :invalid_document, "/register"],
|
|
183
|
+
}.each do |name, (mutate, code, location)|
|
|
184
|
+
it "refuses #{name} with #{code} at #{location}" do
|
|
185
|
+
doc = with_attribute({})
|
|
186
|
+
mutate.call(doc)
|
|
187
|
+
error = refusal(doc)
|
|
188
|
+
expect([error&.reason, error&.location]).to eq([code, location])
|
|
189
|
+
expect(error.model_id).to eq("Example::Node@default") if model_scoped_codes.include?(code)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
it "keeps legacy reason/klass/detail on exporter errors" do
|
|
194
|
+
error = rejected.new(:custom_initialize, "Foo", "has initialize")
|
|
195
|
+
expect([error.reason, error.klass, error.detail, error.location]).to eq([:custom_initialize, "Foo", "has initialize", nil])
|
|
196
|
+
expect(error.message).to eq("cannot export Foo to LML: custom_initialize (has initialize)")
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
describe "bundle graph" do
|
|
201
|
+
it "refuses a dangling type reference" do
|
|
202
|
+
b = deep_copy(bundle)
|
|
203
|
+
b["models"]["Example::Node@default"]["attributes"][0]["type"]["id"] = "Missing@default"
|
|
204
|
+
error = refusal(b, kind: :bundle)
|
|
205
|
+
expect([error.reason, error.location]).to eq([:dangling_reference, "/models/Example::Node@default/attributes/0/type/id"])
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it "refuses a dangling root and duplicate roots" do
|
|
209
|
+
b = deep_copy(bundle)
|
|
210
|
+
b["roots"] = ["A@default"]
|
|
211
|
+
expect(refusal(b, kind: :bundle).reason).to eq(:dangling_reference)
|
|
212
|
+
b["roots"] = ["Example::Node@default", "Example::Node@default"]
|
|
213
|
+
expect(refusal(b, kind: :bundle).reason).to eq(:duplicate_id)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
it "refuses a model key that disagrees with its ID" do
|
|
217
|
+
b = deep_copy(bundle)
|
|
218
|
+
b["models"]["Example::Other@default"] = b["models"].delete("Example::Node@default")
|
|
219
|
+
b["roots"] = []
|
|
220
|
+
expect(refusal(b, kind: :bundle).reason).to eq(:invalid_document)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
it "refuses a format missing from capabilities" do
|
|
224
|
+
b = deep_copy(bundle)
|
|
225
|
+
b["capabilities"] = ["json"]
|
|
226
|
+
expect(refusal(b, kind: :bundle).reason).to eq(:capability_missing)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
it "checks discriminator targets and their attribute" do
|
|
230
|
+
b = deep_copy(bundle)
|
|
231
|
+
node = b["models"]["Example::Node@default"]
|
|
232
|
+
node["attributes"][1]["discriminator"] = { "attribute" => "label", "cases" => { "a" => "Example::Node@default" } }
|
|
233
|
+
expect(refusal(b, kind: :bundle)).to be_nil
|
|
234
|
+
node["attributes"][1]["discriminator"] = { "attribute" => "nope", "cases" => { "a" => "Example::Node@default" } }
|
|
235
|
+
expect(refusal(b, kind: :bundle).reason).to eq(:unknown_discriminator)
|
|
236
|
+
node["attributes"][1]["discriminator"] = { "attribute" => "label", "cases" => { "a" => "Gone@default" } }
|
|
237
|
+
expect(refusal(b, kind: :bundle).reason).to eq(:dangling_reference)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it "refuses an unknown value type base" do
|
|
241
|
+
b = deep_copy(bundle)
|
|
242
|
+
b["value_types"]["V@default"] = { "base" => "double", "facets" => {} }
|
|
243
|
+
expect(refusal(b, kind: :bundle).reason).to eq(:unsupported_type)
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
describe "manifest" do
|
|
248
|
+
it "refuses path traversal, absolute paths and unsorted files" do
|
|
249
|
+
%w[../x /abs a/../b].each do |path|
|
|
250
|
+
m = deep_copy(manifest)
|
|
251
|
+
m["files"][0]["path"] = path
|
|
252
|
+
expect(refusal(m, kind: :manifest).reason).to eq(:invalid_document)
|
|
253
|
+
end
|
|
254
|
+
m = deep_copy(manifest)
|
|
255
|
+
m["files"] = [{ "path" => "b", "sha256" => "sha256:#{'a' * 64}" }, { "path" => "a", "sha256" => "sha256:#{'a' * 64}" }]
|
|
256
|
+
expect(refusal(m, kind: :manifest).location).to eq("/files")
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
it "refuses a malformed digest and an unknown target" do
|
|
260
|
+
m = deep_copy(manifest)
|
|
261
|
+
m["lml_checksum"] = "sha256:ABC"
|
|
262
|
+
expect(refusal(m, kind: :manifest).location).to eq("/lml_checksum")
|
|
263
|
+
m = deep_copy(manifest)
|
|
264
|
+
m["target"] = "go"
|
|
265
|
+
expect(refusal(m, kind: :manifest).location).to eq("/target")
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
describe "schema file" do
|
|
270
|
+
let(:schema) { JSON.parse(File.read(File.expand_path("../../../schema/lml/document.schema.json", __dir__))) }
|
|
271
|
+
|
|
272
|
+
it "lists the same codes, builtin types and formats as the validator" do
|
|
273
|
+
defs = schema["$defs"]
|
|
274
|
+
expect(defs["errorCodes"]["enum"]).to eq(rejected::CODES.map(&:to_s))
|
|
275
|
+
expect(defs["builtinType"]["enum"]).to eq(described_class::BUILTIN_TYPES)
|
|
276
|
+
expect(defs["formats"]["enum"]).to eq(described_class::FORMATS)
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model"
|
|
4
|
+
require "lutaml/model/lml/document_validator"
|
|
5
|
+
require_relative "../../support/lml_export_fixtures"
|
|
6
|
+
require_relative "../../support/lml_order_fixtures"
|
|
7
|
+
require_relative "../../support/lml_graph_fixtures"
|
|
8
|
+
|
|
9
|
+
# Drift guard: whatever the portable exporter emits, the v2
|
|
10
|
+
# DocumentValidator (and so every reader mirroring it) must accept, and
|
|
11
|
+
# whatever the exporter cannot express it must refuse by name.
|
|
12
|
+
RSpec.describe "LML exporter output vs DocumentValidator" do
|
|
13
|
+
let(:validator) { Lutaml::Model::Lml::DocumentValidator }
|
|
14
|
+
|
|
15
|
+
it "covers a non-trivial set of fixture models" do
|
|
16
|
+
expect(LmlExportFixtures.exportable_models.size).to be >= 15
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
LmlExportFixtures.exportable_models.each do |klass|
|
|
20
|
+
if LmlExportFixtures::REFUSED.key?(klass.name)
|
|
21
|
+
it "#{klass.name}: refused by name, never emitted" do
|
|
22
|
+
expect { Lutaml::Model::Lml::Exporter.project(klass) }
|
|
23
|
+
.to raise_error(Lutaml::Model::Lml::RejectedError) { |e|
|
|
24
|
+
expect(e.reason).to eq(LmlExportFixtures::REFUSED.fetch(klass.name))
|
|
25
|
+
}
|
|
26
|
+
end
|
|
27
|
+
else
|
|
28
|
+
it "#{klass.name}: projected document validates as v2" do
|
|
29
|
+
document = Lutaml::Model::Lml::Exporter.project(klass)
|
|
30
|
+
expect { validator.validate!(document, kind: :document) }.not_to raise_error
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
describe "graph fixtures" do
|
|
36
|
+
it "every projected graph document validates as v2" do
|
|
37
|
+
projector = LmlGraphFixtures.projector
|
|
38
|
+
[LmlGraphFixtures::Node, LmlGraphFixtures::Left, LmlGraphFixtures::Right,
|
|
39
|
+
LmlGraphFixtures::Leaf, LmlGraphFixtures::Shape, LmlGraphFixtures::Circle,
|
|
40
|
+
LmlGraphFixtures::Square].each do |klass|
|
|
41
|
+
document = projector.call(klass, "default")
|
|
42
|
+
expect { validator.validate!(document, kind: :document) }.not_to raise_error
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe "contract decisions" do
|
|
48
|
+
def project(klass) = Lutaml::Model::Lml::Exporter.project(klass)
|
|
49
|
+
|
|
50
|
+
it "carries format-level ordered, mixed_content and sequence" do
|
|
51
|
+
expect(project(LmlOrderFixtures::Transport).dig("mappings", "xml", "ordered")).to be(true)
|
|
52
|
+
expect(project(LmlOrderFixtures::Para).dig("mappings", "xml", "mixed_content")).to be(true)
|
|
53
|
+
sequence = project(LmlExportFixtures::SequencedShape).dig("mappings", "xml", "sequence")
|
|
54
|
+
expect(sequence.map { |g| [g["kind"], g["members"].map { |m| m["name"] }] })
|
|
55
|
+
.to eq([["sequence", [["a"], ["b"]]]])
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "projects a choice nested in a sequence with its bounds" do
|
|
59
|
+
sequence = project(LmlExportFixtures::ChoiceSequence).dig("mappings", "xml", "sequence")
|
|
60
|
+
kinds = sequence.first["members"].map { |m| m["kind"] }
|
|
61
|
+
choice = sequence.first["members"].find { |m| m["kind"] == "choice" }
|
|
62
|
+
expect(kinds).to eq(%w[element choice])
|
|
63
|
+
expect([choice["min"], choice["max"], choice["members"].map { |m| m["to"] }])
|
|
64
|
+
.to eq([1, 1, %w[b c]])
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "keeps child_mappings, root_mappings and polymorphic data (not dropped)" do
|
|
68
|
+
expect(project(LmlExportFixtures::ChildMapped).dig("mappings", "json", "rules", 0, "child_mappings"))
|
|
69
|
+
.to eq("id" => "key", "path" => %w[path link])
|
|
70
|
+
expect(project(LmlExportFixtures::RootMapped).dig("mappings", "json", "rules", 0, "root_mappings"))
|
|
71
|
+
.to eq("name" => "key", "styles" => "value")
|
|
72
|
+
expect(project(LmlExportFixtures::PolyBase).dig("mappings", "xml", "rules", 0, "polymorphic_map"))
|
|
73
|
+
.to eq("basic" => "LmlExportFixtures::PolyBase")
|
|
74
|
+
expect(project(LmlExportFixtures::PolyHolder).dig("mappings", "xml", "rules", 0, "discriminator"))
|
|
75
|
+
.to eq("attribute" => "type", "cases" => { "base" => "LmlExportFixtures::PolyBase@default" })
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "does not emit the in-memory hash format for key_value models" do
|
|
79
|
+
mappings = project(LmlExportFixtures::KeyValueOnly)["mappings"]
|
|
80
|
+
expect(mappings.keys).to include("json", "yaml")
|
|
81
|
+
expect(mappings.keys).not_to include("hash")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "rejects a non-boolean ordered flag and a malformed sequence" do
|
|
85
|
+
doc = project(LmlOrderFixtures::Transport)
|
|
86
|
+
bad = Marshal.load(Marshal.dump(doc))
|
|
87
|
+
bad["mappings"]["xml"]["ordered"] = "yes"
|
|
88
|
+
expect { validator.validate!(bad, kind: :document) }
|
|
89
|
+
.to raise_error(Lutaml::Model::Lml::RejectedError)
|
|
90
|
+
bad = Marshal.load(Marshal.dump(doc))
|
|
91
|
+
bad["mappings"]["xml"]["sequence"] = [1]
|
|
92
|
+
expect { validator.validate!(bad, kind: :document) }
|
|
93
|
+
.to raise_error(Lutaml::Model::Lml::RejectedError)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "lutaml/model"
|
|
4
4
|
require "json"
|
|
5
5
|
require "yaml"
|
|
6
|
+
require_relative "../../support/lml_export_fixtures"
|
|
6
7
|
|
|
7
8
|
# lutaml-model#1033 Phase 1: the LML declaration exporter. Ruby model →
|
|
8
9
|
# neutral LML document (attributes + per-format mappings), checksummed
|
|
@@ -90,6 +91,40 @@ RSpec.describe Lutaml::Model::Lml do
|
|
|
90
91
|
)
|
|
91
92
|
end
|
|
92
93
|
|
|
94
|
+
describe "v1 sequence determinism" do
|
|
95
|
+
let(:script) do
|
|
96
|
+
<<~RUBY
|
|
97
|
+
require "lutaml/model"
|
|
98
|
+
require "json"
|
|
99
|
+
require "./spec/support/lml_export_fixtures"
|
|
100
|
+
d = Lutaml::Model::Lml::Exporter.export(LmlExportFixtures::ChoiceSequence)
|
|
101
|
+
puts JSON.generate([d.mappings["xml"]["sequence"], d.checksum])
|
|
102
|
+
RUBY
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def run_fresh
|
|
106
|
+
out = IO.popen([RbConfig.ruby, "-Ilib", "-Ispec", "-e", script], err: %i[child out], &:read)
|
|
107
|
+
JSON.parse(out.lines.last)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it "gives identical v1 sequence bytes and checksum in separate processes" do
|
|
111
|
+
first = run_fresh
|
|
112
|
+
second = run_fresh
|
|
113
|
+
expect(second).to eq(first)
|
|
114
|
+
expect(first.first.to_s).not_to match(/#<|0x[0-9a-f]{6,}/)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it "projects the sequence structurally" do
|
|
118
|
+
sequence = export(LmlExportFixtures::ChoiceSequence).mappings["xml"]["sequence"]
|
|
119
|
+
expect(sequence.first).to include("kind" => "sequence")
|
|
120
|
+
expect(sequence.first["members"].map { |m| m["kind"] }).to eq(%w[element choice])
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "leaves models without a sequence unchanged" do
|
|
124
|
+
expect(export(unit_class).mappings["xml"]).not_to have_key("sequence")
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
93
128
|
it "pins a checksum over the canonical form" do
|
|
94
129
|
document = export(unit_class)
|
|
95
130
|
expect(document.checksum).to start_with("sha256:")
|
|
@@ -134,11 +169,98 @@ RSpec.describe Lutaml::Model::Lml do
|
|
|
134
169
|
)
|
|
135
170
|
end
|
|
136
171
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
172
|
+
describe "ordered and mixed content" do
|
|
173
|
+
let(:descriptor) { { "member" => "__lml_content", "version" => 1 } }
|
|
174
|
+
|
|
175
|
+
it "exports declarative ordered content (Mml::V4::Math shape)" do
|
|
176
|
+
document = export(LmlExportFixtures::MathShape)
|
|
177
|
+
expect(document.mappings["xml"]).to include(
|
|
178
|
+
"ordered" => true, "ordered_content" => descriptor,
|
|
179
|
+
)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
it "exports declarative mixed content" do
|
|
183
|
+
document = export(LmlExportFixtures::MixedShape)
|
|
184
|
+
expect(document.mappings["xml"]).to include("ordered_content" => descriptor)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
it "exports the v1 ordered fixture" do
|
|
188
|
+
expect(export(LmlSpecFixtures::OrderedMixed).mappings["xml"])
|
|
189
|
+
.to include("ordered_content" => descriptor)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
it "still rejects order driven by an executable callback" do
|
|
193
|
+
expect { export(LmlExportFixtures::OrderedWithCallback) }.to raise_error(
|
|
194
|
+
Lutaml::Model::Lml::RejectedError, /imperative_element_order/
|
|
195
|
+
)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it "rejects a source member named __lml_content" do
|
|
199
|
+
expect { export(LmlExportFixtures::ReservedMember) }.to raise_error(
|
|
200
|
+
Lutaml::Model::Lml::RejectedError, /reserved_member/
|
|
201
|
+
)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it "declares the descriptor on the portable document" do
|
|
205
|
+
projected = Lutaml::Model::Lml::Exporter.project(LmlExportFixtures::MathShape)
|
|
206
|
+
expect(projected["ordered_content"]).to eq(descriptor)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
describe "render policies" do
|
|
211
|
+
it "keeps render_empty and value_map on JSON rules" do
|
|
212
|
+
rules = export(LmlExportFixtures::RenderPolicies).mappings["json"]["rules"]
|
|
213
|
+
expect(rules).to include({ "key" => ["x"], "to" => "x", "render_empty" => true })
|
|
214
|
+
y_rule = rules.find { |rule| rule["key"] == ["y"] }
|
|
215
|
+
expect(y_rule["value_map"]["to"]["empty"]).to eq("nil")
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
it "keeps render_empty and render_default on XML rules" do
|
|
219
|
+
rule = export(LmlExportFixtures::PortableRender).mappings["xml"]["rules"].first
|
|
220
|
+
expect(rule).to include("render_empty" => true, "render_default" => true)
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
describe "portable v2 projection" do
|
|
225
|
+
let(:projected) { Lutaml::Model::Lml::Exporter.project(LmlExportFixtures::PortableRender) }
|
|
226
|
+
|
|
227
|
+
it "tags types, defaults, enums and collection bounds" do
|
|
228
|
+
by_name = projected["attributes"].to_h { |attr| [attr["name"], attr] }
|
|
229
|
+
expect(projected).to include("lml" => 2, "register" => "default")
|
|
230
|
+
expect(by_name["x"]).to include("type" => { "kind" => "builtin", "name" => "string" },
|
|
231
|
+
"default" => { "present" => false }, "collection" => false)
|
|
232
|
+
expect(by_name["count"]["default"]).to eq({ "present" => true, "value" => 3 })
|
|
233
|
+
expect(by_name["kind"]["values"]).to eq(%w[a b])
|
|
234
|
+
expect(by_name["items"]["collection"]).to eq({ "min" => 1, "max" => 4 })
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
it "resolves render policies and namespace modes per rule" do
|
|
238
|
+
rule = projected["mappings"]["xml"]["rules"].first
|
|
239
|
+
expect(rule).to include(
|
|
240
|
+
"render" => { "nil" => "as_blank", "empty" => "as_empty", "default" => true },
|
|
241
|
+
"namespace" => { "mode" => "inherit" },
|
|
242
|
+
)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
it "names unsupported constructs" do
|
|
246
|
+
expect { Lutaml::Model::Lml::Exporter.project(LmlExportFixtures::TransformRule) }
|
|
247
|
+
.to raise_error(Lutaml::Model::Lml::RejectedError, /unsupported_mapping/)
|
|
248
|
+
expect { Lutaml::Model::Lml::Exporter.project(LmlExportFixtures::NonFiniteFloatDefault) }
|
|
249
|
+
.to raise_error(Lutaml::Model::Lml::RejectedError, /non_serializable_default/)
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
it "exports float as the builtin with a canonical shortest-round-trip string default" do
|
|
253
|
+
document = Lutaml::Model::Lml::Exporter.project(LmlExportFixtures::FloatAttr)
|
|
254
|
+
by_name = document["attributes"].to_h { |attribute| [attribute["name"], attribute] }
|
|
255
|
+
expect(by_name.values.map { |attribute| attribute["type"] }).to all(eq("kind" => "builtin", "name" => "float"))
|
|
256
|
+
expect(by_name["f"]["default"]).to eq("present" => false)
|
|
257
|
+
expect(by_name.slice("ratio", "tiny", "whole").transform_values { |a| a["default"] }).to eq(
|
|
258
|
+
"ratio" => { "present" => true, "value" => "0.71" },
|
|
259
|
+
"tiny" => { "present" => true, "value" => "1.0e-5" },
|
|
260
|
+
"whole" => { "present" => true, "value" => "3.0" },
|
|
261
|
+
)
|
|
262
|
+
expect { Lutaml::Model::Lml::DocumentValidator.validate!(document, kind: :document) }.not_to raise_error
|
|
263
|
+
end
|
|
142
264
|
end
|
|
143
265
|
|
|
144
266
|
it "rejects a non-serializable class" do
|