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,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "lutaml/model/lml"
|
|
4
|
+
require_relative "../../support/lml_model_choices_fixtures"
|
|
5
|
+
|
|
6
|
+
RSpec.describe Lutaml::Model::Lml do
|
|
7
|
+
let(:fixtures) { LmlModelChoicesFixtures }
|
|
8
|
+
|
|
9
|
+
def copy(tree)
|
|
10
|
+
JSON.parse(JSON.generate(tree))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
LmlModelChoicesFixtures::CASES.each do |name, cases|
|
|
14
|
+
cases.each_with_index do |input, index|
|
|
15
|
+
it "preserves #{name} choice validation and deferred I/O for case #{index}" do
|
|
16
|
+
source = LmlModelChoicesFixtures.const_get(name)
|
|
17
|
+
bundle = described_class.export_bundle(source)
|
|
18
|
+
imported = described_class.import_bundle(bundle).fetch("#{source.name}@default")
|
|
19
|
+
original = source.new(input)
|
|
20
|
+
actual = imported.new(input)
|
|
21
|
+
expected = original.validate.map(&:class)
|
|
22
|
+
expect(actual.validate.map(&:class)).to eq(expected)
|
|
23
|
+
expect(actual.to_json).to eq(original.to_json)
|
|
24
|
+
source_read = begin
|
|
25
|
+
source.from_json(JSON.generate(input)).validate.map(&:class)
|
|
26
|
+
rescue Lutaml::Model::CollectionCountOutOfRangeError => e
|
|
27
|
+
e.class
|
|
28
|
+
end
|
|
29
|
+
imported_read = begin
|
|
30
|
+
imported.from_json(JSON.generate(input)).validate.map(&:class)
|
|
31
|
+
rescue Lutaml::Model::CollectionCountOutOfRangeError => e
|
|
32
|
+
e.class
|
|
33
|
+
end
|
|
34
|
+
expect(imported_read).to eq(source_read)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "carries nested groups, their XML render policy scope and exact bounds" do
|
|
40
|
+
document = described_class.export_bundle(fixtures::Nested).to_h.fetch("models").values.first
|
|
41
|
+
choices = document.fetch("choices")
|
|
42
|
+
expect(choices.values_at("version", "validation")).to eq([1, "explicit"])
|
|
43
|
+
root = choices.fetch("groups").first
|
|
44
|
+
expect(root.values_at("min", "max", "format")).to eq([1, 1, "xml"])
|
|
45
|
+
expect(root.fetch("members").first.fetch("choice").fetch("members")).to eq([
|
|
46
|
+
{ "attribute" => "a" }, { "attribute" => "b" }
|
|
47
|
+
])
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "requires the explicit choice capability" do
|
|
51
|
+
tree = copy(described_class.export_bundle(fixtures::Plain).to_h)
|
|
52
|
+
tree["capabilities"].delete("model.choices.v1")
|
|
53
|
+
expect { described_class.import_bundle(tree) }.to raise_error(described_class::RejectedError) { |error| expect(error.reason).to eq(:capability_missing) }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "refuses unknown metadata versions and validation phases" do
|
|
57
|
+
tree = copy(described_class.export_bundle(fixtures::Plain).to_h)
|
|
58
|
+
metadata = tree.fetch("models").values.first.fetch("choices")
|
|
59
|
+
metadata["version"] = 2
|
|
60
|
+
expect { described_class.import_bundle(tree) }.to raise_error(described_class::RejectedError) { |error| expect(error.reason).to eq(:unsupported_version) }
|
|
61
|
+
metadata["version"] = 1
|
|
62
|
+
metadata["validation"] = "constructor"
|
|
63
|
+
expect { described_class.import_bundle(tree) }.to raise_error(described_class::RejectedError) { |error| expect(error.reason).to eq(:invalid_document) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "refuses dangling or ambiguous member references" do
|
|
67
|
+
tree = copy(described_class.export_bundle(fixtures::Plain).to_h)
|
|
68
|
+
member = tree.fetch("models").values.first.fetch("choices").fetch("groups").first.fetch("members").first
|
|
69
|
+
member["attribute"] = "missing"
|
|
70
|
+
expect { described_class.import_bundle(tree) }.to raise_error(described_class::RejectedError) { |error| expect(error.reason).to eq(:dangling_reference) }
|
|
71
|
+
member["attribute"] = "a"
|
|
72
|
+
member["choice"] = {}
|
|
73
|
+
expect { described_class.import_bundle(tree) }.to raise_error(described_class::RejectedError) { |error| expect(error.reason).to eq(:invalid_document) }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "covers choices in canonical identity without adding metadata to ordinary models" do
|
|
77
|
+
original = described_class.export_bundle(fixtures::Plain)
|
|
78
|
+
tree = copy(original.to_h)
|
|
79
|
+
tree.fetch("models").values.first.fetch("choices").fetch("groups").first["min"] = 0
|
|
80
|
+
expect(described_class::Importer.bundle(tree).checksum).not_to eq(original.checksum)
|
|
81
|
+
plain = Class.new(Lutaml::Model::Serializable)
|
|
82
|
+
stub_const("LmlNoChoiceSnapshot", plain)
|
|
83
|
+
plain.attribute(:value, :string)
|
|
84
|
+
ordinary = described_class.export_bundle(plain).to_h
|
|
85
|
+
expect(ordinary.fetch("models").values.first).not_to have_key("choices")
|
|
86
|
+
expect(ordinary.fetch("capabilities")).not_to include("model.choices.v1")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "json"
|
|
5
|
+
require "lutaml/model/lml/ordered_content"
|
|
6
|
+
require_relative "../../support/lml_order_fixtures"
|
|
7
|
+
|
|
8
|
+
# lutaml-model#1033 card 02c: per-instance element order as portable data.
|
|
9
|
+
RSpec.describe Lutaml::Model::Lml::OrderedContent do
|
|
10
|
+
let(:fx) { LmlOrderFixtures }
|
|
11
|
+
let(:rejected) { Lutaml::Model::Lml::RejectedError }
|
|
12
|
+
|
|
13
|
+
def descriptor(klass)
|
|
14
|
+
described_class.descriptor_for(klass, mapping: klass.mappings_for(:xml))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Rebuild an instance from member values only (no recorded order).
|
|
18
|
+
def rebuild(klass, source)
|
|
19
|
+
klass.new(klass.attributes.keys.to_h { |k| [k, source.public_send(k)] })
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def json_cycle(tokens)
|
|
23
|
+
JSON.parse(JSON.generate(tokens))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe "mixed content round trip" do
|
|
27
|
+
let(:para) { fx::Para.from_xml(fx::PARA_XML) }
|
|
28
|
+
let(:desc) { descriptor(fx::Para) }
|
|
29
|
+
|
|
30
|
+
it "captures interleaved elements, text and CDATA as tokens" do
|
|
31
|
+
expect(described_class.capture(desc, para)).to eq(
|
|
32
|
+
[
|
|
33
|
+
{ "kind" => "text", "attribute" => "text", "index" => 0 },
|
|
34
|
+
{ "kind" => "element", "attribute" => "em", "index" => 0 },
|
|
35
|
+
{ "kind" => "text", "attribute" => "text", "index" => 1 },
|
|
36
|
+
{ "kind" => "cdata", "literal" => "cd" },
|
|
37
|
+
{ "kind" => "text", "attribute" => "text", "index" => 2 },
|
|
38
|
+
{ "kind" => "element", "attribute" => "bold", "index" => 0 },
|
|
39
|
+
{ "kind" => "element", "attribute" => "em", "index" => 1 },
|
|
40
|
+
{ "kind" => "text", "attribute" => "text", "index" => 3 },
|
|
41
|
+
{ "kind" => "element", "attribute" => "note", "index" => 0 },
|
|
42
|
+
{ "kind" => "text", "attribute" => "text", "index" => 4 },
|
|
43
|
+
],
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "reproduces the original document from tokens alone" do
|
|
48
|
+
tokens = json_cycle(described_class.capture(desc, para))
|
|
49
|
+
fresh = rebuild(fx::Para, para)
|
|
50
|
+
expect(fresh.element_order).to be_nil
|
|
51
|
+
|
|
52
|
+
described_class.apply(desc, tokens, fresh)
|
|
53
|
+
|
|
54
|
+
expect(fresh.to_xml).to eq(para.to_xml)
|
|
55
|
+
expect(fresh.to_xml).to include("<em>a</em> mid")
|
|
56
|
+
expect(fresh.to_xml.index("<b>x</b>")).to be < fresh.to_xml.index("<em>b</em>")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "lets tokens, not member grouping, decide interleaving" do
|
|
60
|
+
fresh = rebuild(fx::Para, para)
|
|
61
|
+
described_class.apply(desc, [
|
|
62
|
+
{ "kind" => "element", "attribute" => "note", "index" => 0 },
|
|
63
|
+
{ "kind" => "element", "attribute" => "em", "index" => 0 },
|
|
64
|
+
{ "kind" => "text", "literal" => "-" },
|
|
65
|
+
{ "kind" => "element", "attribute" => "em", "index" => 1 },
|
|
66
|
+
{ "kind" => "element", "attribute" => "bold", "index" => 0 },
|
|
67
|
+
], fresh)
|
|
68
|
+
|
|
69
|
+
expect(fresh.to_xml).to eq("<p><note>n</note><em>a</em>-<em>b</em><b>x</b></p>")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "refuses to apply references that would need the member reordered" do
|
|
73
|
+
fresh = rebuild(fx::Para, para)
|
|
74
|
+
expect do
|
|
75
|
+
described_class.apply(desc, [
|
|
76
|
+
{ "kind" => "element", "attribute" => "em", "index" => 1 },
|
|
77
|
+
{ "kind" => "element", "attribute" => "em", "index" => 0 },
|
|
78
|
+
], fresh)
|
|
79
|
+
end.to raise_error(rejected) { |e|
|
|
80
|
+
expect(e.reason).to eq(:invalid_ordered_content)
|
|
81
|
+
expect(e.detail).to include("must ascend")
|
|
82
|
+
}
|
|
83
|
+
expect(fresh.element_order).to be_nil
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
describe "element-only ordered models" do
|
|
88
|
+
let(:transport) { fx::Transport.from_xml(fx::TRANSPORT_XML) }
|
|
89
|
+
let(:desc) { descriptor(fx::Transport) }
|
|
90
|
+
|
|
91
|
+
it "preserves repeated interleaved elements" do
|
|
92
|
+
tokens = described_class.capture(desc, transport)
|
|
93
|
+
expect(tokens.map { |t| [t["attribute"], t["index"]] }).to eq(
|
|
94
|
+
[["car", 0], ["boat", 0], ["car", 1], ["boat", 1], ["car", 2]],
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
fresh = rebuild(fx::Transport, transport)
|
|
98
|
+
described_class.apply(desc, json_cycle(tokens), fresh)
|
|
99
|
+
expect(fresh.to_xml).to eq(transport.to_xml)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it "ignores indentation between elements" do
|
|
103
|
+
pretty = fx::Transport.from_xml(
|
|
104
|
+
"<transportation>\n <car>1</car>\n <boat>2</boat>\n</transportation>",
|
|
105
|
+
)
|
|
106
|
+
expect(described_class.capture(desc, pretty).map { |t| t["kind"] }).to eq(%w[element element])
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
describe "namespace identity" do
|
|
111
|
+
let(:ns) { fx::Namespaced.from_xml(fx::NAMESPACED_XML) }
|
|
112
|
+
let(:desc) { descriptor(fx::Namespaced) }
|
|
113
|
+
|
|
114
|
+
it "tells same-named elements apart by namespace and restores it" do
|
|
115
|
+
tokens = described_class.capture(desc, ns)
|
|
116
|
+
expect(tokens.map { |t| [t["attribute"], t["index"]] }).to eq(
|
|
117
|
+
[["a_items", 0], ["b_items", 0], ["a_items", 1], ["b_items", 1]],
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
fresh = rebuild(fx::Namespaced, ns)
|
|
121
|
+
entries = described_class.apply(desc, json_cycle(tokens), fresh)
|
|
122
|
+
expect(entries.map(&:namespace_uri)).to eq(
|
|
123
|
+
["urn:lml-order:a", "urn:lml-order:b", "urn:lml-order:a", "urn:lml-order:b"],
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
# The serialized form interleaves 1,2,3,4 and puts 2 and 4 in B's namespace.
|
|
127
|
+
xml = fresh.to_xml
|
|
128
|
+
expect(xml.scan(/<item[^>]*>(\d)</).flatten).to eq(%w[1 2 3 4])
|
|
129
|
+
expect(xml.scan(/<item xmlns="urn:lml-order:b">(\d)</).flatten).to eq(%w[2 4])
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
describe "instances without a recorded order" do
|
|
134
|
+
it "captures nil and applies nil" do
|
|
135
|
+
desc = descriptor(fx::Transport)
|
|
136
|
+
built = fx::Transport.new(car: %w[1], boat: %w[2])
|
|
137
|
+
expect(described_class.capture(desc, built)).to be_nil
|
|
138
|
+
expect(described_class.apply(desc, nil, built)).to be_nil
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it "appends members added after parsing, in declaration order" do
|
|
142
|
+
desc = descriptor(fx::Transport)
|
|
143
|
+
parsed = fx::Transport.from_xml(fx::TRANSPORT_XML)
|
|
144
|
+
parsed.boat << "6"
|
|
145
|
+
tokens = described_class.capture(desc, parsed)
|
|
146
|
+
expect(tokens.last).to eq("kind" => "element", "attribute" => "boat", "index" => 2)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
describe "rejections" do
|
|
151
|
+
it "refuses a custom-method element rule as imperative order" do
|
|
152
|
+
expect { descriptor(fx::ImperativeRule) }.to raise_error(rejected) { |e|
|
|
153
|
+
expect(e.reason).to eq(:imperative_element_order)
|
|
154
|
+
}
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
it "refuses a class that generates its own element_order" do
|
|
158
|
+
expect { descriptor(fx::ImperativeOrder) }.to raise_error(rejected) { |e|
|
|
159
|
+
expect(e.reason).to eq(:imperative_element_order)
|
|
160
|
+
expect(e.detail).to include("element_order")
|
|
161
|
+
}
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it "refuses a source member named __lml_content" do
|
|
165
|
+
expect { descriptor(fx::ReservedMember) }.to raise_error(rejected) { |e|
|
|
166
|
+
expect(e.reason).to eq(:reserved_member)
|
|
167
|
+
}
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
it "returns no descriptor for a mapping without per-instance order" do
|
|
171
|
+
expect(descriptor(fx::Plain)).to be_nil
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
describe "invalid tokens" do
|
|
175
|
+
let(:desc) { descriptor(fx::Transport) }
|
|
176
|
+
let(:instance) { fx::Transport.new(car: %w[1 2], boat: %w[3]) }
|
|
177
|
+
|
|
178
|
+
def reason_of(tokens, instance: nil)
|
|
179
|
+
described_class.validate!(desc, tokens, instance: instance)
|
|
180
|
+
nil
|
|
181
|
+
rescue rejected => e
|
|
182
|
+
[e.reason, e.detail]
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def el(attribute, index)
|
|
186
|
+
{ "kind" => "element", "attribute" => attribute, "index" => index }
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
it "accepts tokens that reference existing indices" do
|
|
190
|
+
tokens = [el("car", 1), el("boat", 0), el("car", 0)]
|
|
191
|
+
expect(described_class.validate!(desc, tokens, instance: instance).size).to eq(3)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it "rejects an index past the member's values" do
|
|
195
|
+
reason, detail = reason_of([el("car", 2)], instance: instance)
|
|
196
|
+
expect(reason).to eq(:invalid_ordered_content)
|
|
197
|
+
expect(detail).to include("car[2] does not exist")
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
it "rejects duplicate references" do
|
|
201
|
+
reason, detail = reason_of([el("car", 0), el("car", 0)])
|
|
202
|
+
expect(reason).to eq(:invalid_ordered_content)
|
|
203
|
+
expect(detail).to include("duplicate")
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
it "rejects unknown members, bad kinds, bad shapes and negative indices" do
|
|
207
|
+
[
|
|
208
|
+
[el("nope", 0)],
|
|
209
|
+
[{ "kind" => "comment", "literal" => "x" }],
|
|
210
|
+
[{ "kind" => "element", "attribute" => "car", "index" => 0, "literal" => "x" }],
|
|
211
|
+
[{ "kind" => "text" }],
|
|
212
|
+
[{ "kind" => "text", "attribute" => "car", "index" => 0 }],
|
|
213
|
+
[el("car", -1)],
|
|
214
|
+
[el("car", "0")],
|
|
215
|
+
["car"],
|
|
216
|
+
].each do |tokens|
|
|
217
|
+
expect(reason_of(tokens)&.first).to eq(:invalid_ordered_content), tokens.inspect
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
it "rejects a non-zero index on a scalar member" do
|
|
222
|
+
para_desc = descriptor(fx::Para)
|
|
223
|
+
expect do
|
|
224
|
+
described_class.validate!(para_desc, [el("note", 1)])
|
|
225
|
+
end.to raise_error(rejected) { |e| expect(e.detail).to include("only has index 0") }
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
it "does not touch the instance when apply fails" do
|
|
229
|
+
before = instance.element_order
|
|
230
|
+
expect { described_class.apply(desc, [el("car", 5)], instance) }.to raise_error(rejected)
|
|
231
|
+
expect(instance.element_order).to eq(before)
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
it "refuses comments in element_order rather than dropping them" do
|
|
236
|
+
desc = descriptor(fx::Transport)
|
|
237
|
+
parsed = fx::Transport.from_xml("<transportation><car>1</car><!--c--></transportation>")
|
|
238
|
+
expect { described_class.capture(desc, parsed) }.to raise_error(rejected) { |e|
|
|
239
|
+
expect(e.reason).to eq(:invalid_ordered_content)
|
|
240
|
+
}
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
end
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "bigdecimal"
|
|
5
|
+
|
|
6
|
+
# lutaml-model#1033: the opt-in writer for the portable-exact-json-v1 wire
|
|
7
|
+
# profile that the TS and Rust codecs read. Native to_json stays
|
|
8
|
+
# ruby-native-v1.
|
|
9
|
+
RSpec.describe Lutaml::Model::Lml::PortableJson do
|
|
10
|
+
let(:tag_class) do
|
|
11
|
+
Class.new(Lutaml::Model::Serializable) do
|
|
12
|
+
def self.name = "PortableJsonSpec::Tag"
|
|
13
|
+
attribute :name, :string
|
|
14
|
+
attribute :weight, :float
|
|
15
|
+
json do
|
|
16
|
+
map "name", to: :name
|
|
17
|
+
map "weight", to: :weight
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
let(:record_class) do
|
|
23
|
+
tag = tag_class
|
|
24
|
+
Class.new(Lutaml::Model::Serializable) do
|
|
25
|
+
def self.name = "PortableJsonSpec::Record"
|
|
26
|
+
attribute :label, :string
|
|
27
|
+
attribute :count, :integer
|
|
28
|
+
attribute :counts, :integer, collection: true
|
|
29
|
+
attribute :price, :decimal
|
|
30
|
+
attribute :ratio, :float
|
|
31
|
+
attribute :open, :boolean
|
|
32
|
+
attribute :meta, :hash
|
|
33
|
+
attribute :tag, tag
|
|
34
|
+
attribute :tags, tag, collection: true
|
|
35
|
+
attribute :note, :string
|
|
36
|
+
json do
|
|
37
|
+
map %w[label lbl], to: :label
|
|
38
|
+
map "count", to: :count
|
|
39
|
+
map "counts", to: :counts
|
|
40
|
+
map "price", to: :price
|
|
41
|
+
map "ratio", to: :ratio
|
|
42
|
+
map "open", to: :open
|
|
43
|
+
map "meta", to: :meta
|
|
44
|
+
map "tag", to: :tag
|
|
45
|
+
map "tags", to: :tags
|
|
46
|
+
map "note", to: :note, render_nil: :as_nil
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def portable(instance)
|
|
52
|
+
JSON.parse(described_class.generate(instance))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "names the profile" do
|
|
56
|
+
expect(described_class::PROFILE).to eq("portable-exact-json-v1")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "keeps the default to_json output unchanged (ruby-native-v1)" do
|
|
60
|
+
record = record_class.new(label: "a", count: 2**60, price: BigDecimal("1.5"), ratio: 1e20)
|
|
61
|
+
|
|
62
|
+
expect(record.to_json).to eq(
|
|
63
|
+
'{"label":"a","count":1152921504606846976,"price":"0.15e1","ratio":1e+20,"note":null}',
|
|
64
|
+
)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
context "with a scalar alias in the model register" do
|
|
68
|
+
let(:model) do
|
|
69
|
+
Class.new(Lutaml::Model::Serializable) do
|
|
70
|
+
def self.name = "PortableJsonSpec::Registered"
|
|
71
|
+
def self.lutaml_default_register = :portable_json_scalar_register
|
|
72
|
+
|
|
73
|
+
attribute :price, :registered_decimal
|
|
74
|
+
attribute :amounts, :registered_decimal, collection: true
|
|
75
|
+
json do
|
|
76
|
+
map "price", to: :price
|
|
77
|
+
map "amounts", to: :amounts
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
before do
|
|
83
|
+
registry = Lutaml::Model::TypeRegistry.new
|
|
84
|
+
Lutaml::Model::Type.register_builtin_types_in(registry)
|
|
85
|
+
registry.register(:registered_decimal, Class.new(Lutaml::Model::Type::Decimal))
|
|
86
|
+
Lutaml::Model::GlobalContext.create_context(id: :portable_json_scalar_register, registry: registry)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
after do
|
|
90
|
+
Lutaml::Model::GlobalContext.unregister_context(:portable_json_scalar_register)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it "writes registered scalar and collection fields using their exact type" do
|
|
94
|
+
record = model.new(price: "0.50", amounts: ["1.20", "-0.25"])
|
|
95
|
+
expect(portable(record)).to eq("price" => "0.5", "amounts" => ["1.2", "-0.25"])
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "writes integers beyond 2**53 as decimal strings, exactly" do
|
|
100
|
+
record = record_class.new(count: 9_007_199_254_740_993, counts: [-7, 0, 2**70])
|
|
101
|
+
|
|
102
|
+
expect(described_class.generate(record)).to eq(
|
|
103
|
+
'{"count":"9007199254740993","counts":["-7","0","1180591620717411303424"],"note":null}',
|
|
104
|
+
)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "writes decimals as plain strings without an exponent" do
|
|
108
|
+
{
|
|
109
|
+
"1.5" => "1.5", "0.15e1" => "1.5", "-0.0" => "-0.0", "1e-7" => "0.0000001",
|
|
110
|
+
"12345678901234567890.5" => "12345678901234567890.5", "2" => "2.0", "1e20" => "100000000000000000000.0"
|
|
111
|
+
}.each do |input, wire|
|
|
112
|
+
expect(portable(record_class.new(price: BigDecimal(input)))["price"]).to eq(wire)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it "writes floats in the shared canonical float wire, including negative zero" do
|
|
117
|
+
vectors = JSON.parse(File.read(File.join(__dir__, "../../fixtures/lml_conformance/float_vectors.json")))
|
|
118
|
+
vectors.each do |vector|
|
|
119
|
+
float = [vector.fetch("bits")].pack("H*").unpack1("G")
|
|
120
|
+
expect(portable(record_class.new(ratio: float))["ratio"]).to eq(vector.fetch("wire")), vector["bits"]
|
|
121
|
+
end
|
|
122
|
+
expect(portable(record_class.new(ratio: -0.0))["ratio"]).to eq("-0.0")
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it "keeps strings, booleans, nil, hashes and key order native" do
|
|
126
|
+
record = record_class.new(label: "café", open: false, meta: { "k" => [1, nil] }, count: 3)
|
|
127
|
+
|
|
128
|
+
expect(described_class.generate(record)).to eq(
|
|
129
|
+
'{"label":"café","count":"3","open":false,"meta":{"k":[1,null]},"note":null}',
|
|
130
|
+
)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
describe "hash attribute numbers" do
|
|
134
|
+
def spelled(meta)
|
|
135
|
+
described_class.generate(record_class.new(meta: meta))
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
it "spells floats the way ECMAScript Number::toString does" do
|
|
139
|
+
meta = { "k" => [1, nil], "f" => 0.1, "e" => 1e21, "s" => 1.5e-7, "one" => 1.0,
|
|
140
|
+
"n" => -2.5e-5, "m" => 123.456, "z" => 1e-6, "big" => (2.0**53) - 1 }
|
|
141
|
+
|
|
142
|
+
expect(spelled(meta)).to include(
|
|
143
|
+
'"meta":{"k":[1,null],"f":0.1,"e":1e+21,"s":1.5e-7,"one":1,' \
|
|
144
|
+
'"n":-0.000025,"m":123.456,"z":0.000001,"big":9007199254740991}',
|
|
145
|
+
)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it "spells positive zero as 0, the ECMAScript spelling" do
|
|
149
|
+
expect(spelled({ "z" => 0.0 })).to include('"meta":{"z":0}')
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
it "spells symbol keys and values as strings" do
|
|
153
|
+
expect(spelled({ a: :b })).to include('"meta":{"a":"b"}')
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it "agrees with ECMAScript on the digit/exponent switch points" do
|
|
157
|
+
meta = { "a" => 1.5e-6, "b" => 1.5e-7, "c" => 1.2345e22, "d" => 4.9e-324, "e" => Float::MAX }
|
|
158
|
+
|
|
159
|
+
expect(spelled(meta)).to include(
|
|
160
|
+
'"meta":{"a":0.0000015,"b":1.5e-7,"c":1.2345e+22,"d":5e-324,"e":1.7976931348623157e+308}',
|
|
161
|
+
)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
{
|
|
165
|
+
"an integer beyond 2**53-1" => 2**60,
|
|
166
|
+
"a BigDecimal" => BigDecimal("1.5"),
|
|
167
|
+
"-0.0" => -0.0,
|
|
168
|
+
"NaN" => Float::NAN,
|
|
169
|
+
"Infinity" => Float::INFINITY,
|
|
170
|
+
"an integral float beyond 2**53-1 (1e20)" => 1e20,
|
|
171
|
+
"an object with no JSON form" => Time.at(0),
|
|
172
|
+
"a nested bad number" => [{ "x" => [2**53] }],
|
|
173
|
+
}.each do |name, value|
|
|
174
|
+
it "refuses #{name}" do
|
|
175
|
+
expect { spelled({ "v" => value }) }.to raise_error(Lutaml::Model::Lml::RejectedError)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it "refuses non-string keys and keys that collide" do
|
|
180
|
+
expect { spelled({ 1 => 2 }) }.to raise_error(Lutaml::Model::Lml::RejectedError)
|
|
181
|
+
expect { spelled({ "a" => 1, a: 2 }) }.to raise_error(Lutaml::Model::Lml::RejectedError, /duplicate key/)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
it "refuses invalid UTF-8 strings" do
|
|
185
|
+
expect { spelled({ "v" => "\xED\xA0\x80" }) }.to raise_error(Lutaml::Model::Lml::RejectedError)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
it "refuses hash nesting beyond the json gem's limit, and 201 levels deep" do
|
|
189
|
+
nest = ->(levels) { levels.times.inject(1) { |inner, _| [inner] } }
|
|
190
|
+
|
|
191
|
+
expect { spelled({ "v" => nest.call(50) }) }.not_to raise_error
|
|
192
|
+
expect { spelled({ "v" => nest.call(200) }) }.to raise_error(Lutaml::Model::Lml::RejectedError)
|
|
193
|
+
expect { spelled({ "v" => nest.call(201) }) }.to raise_error(Lutaml::Model::Lml::RejectedError)
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
it "types nested models and collections of models" do
|
|
198
|
+
record = record_class.new(
|
|
199
|
+
tag: tag_class.new(name: "t", weight: 0.5),
|
|
200
|
+
tags: [tag_class.new(weight: 1e-5), tag_class.new(weight: 2.0)],
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
expect(portable(record)).to eq(
|
|
204
|
+
"tag" => { "name" => "t", "weight" => "0.5" },
|
|
205
|
+
"tags" => [{ "weight" => "1.0e-5" }, { "weight" => "2.0" }],
|
|
206
|
+
"note" => nil,
|
|
207
|
+
)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
it "is reachable as Lml.portable_json, optionally pretty" do
|
|
211
|
+
record = record_class.new(count: 1)
|
|
212
|
+
|
|
213
|
+
expect(Lutaml::Model::Lml.portable_json(record)).to eq('{"count":"1","note":null}')
|
|
214
|
+
expect(Lutaml::Model::Lml.portable_json(record, pretty: true)).to eq(
|
|
215
|
+
"{\n \"count\": \"1\",\n \"note\": null\n}",
|
|
216
|
+
)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
it "writes the conformance Record exactly as the portable-exact case expects" do
|
|
220
|
+
require_relative "../../support/lml_conformance"
|
|
221
|
+
record = LmlConformance::Record.new(label: "beyond binary64", count: 9_007_199_254_740_993, enabled: false)
|
|
222
|
+
entry = JSON.parse(File.read(File.join(__dir__, "../../fixtures/lml_conformance/core/index.json")))
|
|
223
|
+
.fetch("cases").find { |c| c["id"] == "core-json-portable-exact" }
|
|
224
|
+
|
|
225
|
+
expect(described_class.generate(record)).to eq(entry.fetch("expected_output"))
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
describe "refusals" do
|
|
229
|
+
def refusal(&block)
|
|
230
|
+
expect(&block).to raise_error(Lutaml::Model::Lml::RejectedError) { |error| return error }
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
it "refuses NaN and Infinity floats, which have no wire form" do
|
|
234
|
+
[Float::NAN, Float::INFINITY, -Float::INFINITY].each do |value|
|
|
235
|
+
error = refusal { described_class.generate(record_class.new(ratio: value)) }
|
|
236
|
+
expect(error.reason).to eq(:not_serializable)
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it "refuses non-finite decimals" do
|
|
241
|
+
error = refusal { described_class.generate(record_class.new(price: BigDecimal("NaN"))) }
|
|
242
|
+
expect(error.reason).to eq(:not_serializable)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
it "refuses a decimal whose plain spelling exceeds the readers' 10,000 digits" do
|
|
246
|
+
expect(described_class.generate(record_class.new(price: BigDecimal("1e9998")))).to include('"price":"1')
|
|
247
|
+
error = refusal { described_class.generate(record_class.new(price: BigDecimal("1e100000000"))) }
|
|
248
|
+
expect(error.reason).to eq(:not_serializable)
|
|
249
|
+
expect(refusal { described_class.generate(record_class.new(price: BigDecimal("1e-10000"))) }.reason)
|
|
250
|
+
.to eq(:not_serializable)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
it "refuses a hash attribute with an export transform rather than drop it" do
|
|
254
|
+
klass = Class.new(Lutaml::Model::Serializable) do
|
|
255
|
+
def self.name = "PortableJsonSpec::Transformed"
|
|
256
|
+
attribute :h, :hash
|
|
257
|
+
json { map "h", to: :h, transform: { export: ->(_) { { "a" => 2**60 } } } }
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
expect(refusal { described_class.generate(klass.new(h: { "a" => 1 })) }.reason).to eq(:unsupported_mapping)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
it "refuses a cyclic model graph instead of overflowing the stack" do
|
|
264
|
+
klass = Class.new(Lutaml::Model::Serializable) do
|
|
265
|
+
def self.name = "PortableJsonSpec::Node"
|
|
266
|
+
attribute :n, :integer
|
|
267
|
+
attribute :child, self
|
|
268
|
+
json do
|
|
269
|
+
map "n", to: :n
|
|
270
|
+
map "child", to: :child
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
node = klass.new(n: 1)
|
|
274
|
+
node.child = node
|
|
275
|
+
|
|
276
|
+
expect(refusal { described_class.generate(node) }.reason).to eq(:not_serializable)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
it "refuses a non-Serializable argument" do
|
|
280
|
+
expect(refusal { described_class.generate({}) }.reason).to eq(:not_serializable)
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
it "refuses a mapping with custom methods" do
|
|
284
|
+
klass = Class.new(Lutaml::Model::Serializable) do
|
|
285
|
+
def self.name = "PortableJsonSpec::Custom"
|
|
286
|
+
attribute :n, :integer
|
|
287
|
+
json { map "n", to: :n, with: { to: :n_to, from: :n_from } }
|
|
288
|
+
def n_to(model, doc) = doc["n"] = model.n
|
|
289
|
+
def n_from(model, value) = model.n = value
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
expect(refusal { described_class.generate(klass.new(n: 1)) }.reason).to eq(:unsupported_mapping)
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
it "refuses a value type whose native output is a JSON number" do
|
|
296
|
+
numeric = Class.new(Lutaml::Model::Type::Value) do
|
|
297
|
+
def self.name = "PortableJsonSpec::Numeric"
|
|
298
|
+
def self.cast(value, *) = value
|
|
299
|
+
def to_json(*) = 5
|
|
300
|
+
end
|
|
301
|
+
klass = Class.new(Lutaml::Model::Serializable) do
|
|
302
|
+
def self.name = "PortableJsonSpec::Holder"
|
|
303
|
+
attribute :n, numeric
|
|
304
|
+
json { map "n", to: :n }
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
expect(refusal { described_class.generate(klass.new(n: 5)) }.reason).to eq(:not_serializable)
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
end
|