lutaml-model 0.8.176 → 0.8.178
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/lib/lutaml/json/generator_options.rb +10 -3
- data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +3 -1
- data/lib/lutaml/model/schema/xml_compiler/supported_data_types.rb +23 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/xsd_validator.rb +67 -22
- data/spec/lutaml/json/adapter/standard_adapter_spec.rb +11 -1
- data/spec/lutaml/model/generator_defaults_spec.rb +9 -2
- data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +37 -0
- data/spec/lutaml/xml/validate_xml_with_constructs_spec.rb +10 -0
- data/spec/lutaml/xml/validate_xml_with_spec.rb +4 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2992312b17ca6365a073615318db447cca51e3aa2c7f7c9279565faa370cbdc3
|
|
4
|
+
data.tar.gz: 7b41def8578756a2e1e36a693a5b36172359ddbc170007a26f0848ed1a30eb85
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 21cf0a02683ad8ccefce980343448195a40fdf48c49ec7420b606d3ee8543366d3e5e4cefef62381c6a5b9a9e341c0d8f67b27c75a976618a9db4f54996d6b80
|
|
7
|
+
data.tar.gz: 40a5bb7ec659bda3d112b710146ad4a6b9e678dab938bc76dbcaadc2a970cfbb8b2f464edf8579a2e21ef82ea2f8d84f55fc09923dd3d661ee4bf3debe59f911
|
|
@@ -13,11 +13,18 @@ module Lutaml
|
|
|
13
13
|
# JSON.generate.
|
|
14
14
|
module GeneratorOptions
|
|
15
15
|
# Options the generator accepts on both json 2.x and 3.x. Measured
|
|
16
|
-
# against 2.9.1, 2.15.2, 2.19.9, 2.20.0, 2.21.1, 2.21.2
|
|
17
|
-
# :escape_slash is NOT here because json 3.0 removed it
|
|
18
|
-
#
|
|
16
|
+
# against 2.9.1, 2.15.2, 2.19.9, 2.20.0, 2.21.1, 2.21.2, 3.0.0 and
|
|
17
|
+
# 3.2.0. :escape_slash is NOT here because json 3.0 removed it (and
|
|
18
|
+
# json 3.2 raises on it); on 2.x it still arrives via
|
|
19
|
+
# DERIVED_PERMITTED. :allow_duplicate_key IS here because the
|
|
19
20
|
# generator accepts it on every version while JSON::State never exposed
|
|
20
21
|
# it as an accessor, so the derived half alone would miss it.
|
|
22
|
+
#
|
|
23
|
+
# json 3.2 changed script_safe semantics: <, > and the line
|
|
24
|
+
# separators escape as \uXXXX (ECMAScript-conformant, same XSS
|
|
25
|
+
# protection) and the historical \/ slash escaping is dropped. The
|
|
26
|
+
# escape_slash translation stays load-bearing regardless - raw
|
|
27
|
+
# escape_slash raises on json 3.0+.
|
|
21
28
|
BASE_PERMITTED = %i[
|
|
22
29
|
allow_duplicate_key allow_nan array_nl as_json ascii_only
|
|
23
30
|
buffer_initial_length depth indent max_nesting object_nl script_safe
|
|
@@ -219,7 +219,9 @@ module Lutaml
|
|
|
219
219
|
when NilClass then 1
|
|
220
220
|
else max_occurs.to_i
|
|
221
221
|
end
|
|
222
|
-
|
|
222
|
+
# Optional singletons (minOccurs=0, maxOccurs=1) stay scalar:
|
|
223
|
+
# collections are for genuinely repeating members only.
|
|
224
|
+
return false if max == 1
|
|
223
225
|
|
|
224
226
|
(min..max)
|
|
225
227
|
end
|
|
@@ -53,6 +53,29 @@ module Lutaml
|
|
|
53
53
|
int: { skippable: true, class_name: TC[:integer] },
|
|
54
54
|
id: { skippable: false, class_name: TC[:string],
|
|
55
55
|
validations: { pattern: /\A[a-zA-Z_][\w.-]*\z/ } },
|
|
56
|
+
gYear: { skippable: true, class_name: TC[:string] },
|
|
57
|
+
gYearMonth: { skippable: true, class_name: TC[:string] },
|
|
58
|
+
gMonth: { skippable: true, class_name: TC[:string] },
|
|
59
|
+
gMonthDay: { skippable: true, class_name: TC[:string] },
|
|
60
|
+
gDay: { skippable: true, class_name: TC[:string] },
|
|
61
|
+
duration: { skippable: true, class_name: TC[:duration] },
|
|
62
|
+
dayTimeDuration: { skippable: true, class_name: TC[:day_time_duration] },
|
|
63
|
+
yearMonthDuration: { skippable: true, class_name: TC[:year_month_duration] },
|
|
64
|
+
negativeInteger: { skippable: false, class_name: TC[:integer],
|
|
65
|
+
validations: { max_inclusive: -1 } },
|
|
66
|
+
nonPositiveInteger: { skippable: false, class_name: TC[:integer],
|
|
67
|
+
validations: { max_inclusive: 0 } },
|
|
68
|
+
short: { skippable: false, class_name: TC[:integer],
|
|
69
|
+
validations: { min_inclusive: -32768, max_inclusive: 32767 } },
|
|
70
|
+
Name: { skippable: false, class_name: TC[:string],
|
|
71
|
+
validations: { pattern: /\A[a-zA-Z_:][\w.:-]*\z/ } },
|
|
72
|
+
QName: { skippable: true, class_name: TC[:qname] },
|
|
73
|
+
IDREF: { skippable: false, class_name: TC[:string],
|
|
74
|
+
validations: { pattern: /\A[a-zA-Z_][\w.-]*\z/ } },
|
|
75
|
+
IDREFS: { skippable: true, class_name: TC[:string] },
|
|
76
|
+
NMTOKEN: { skippable: false, class_name: TC[:string],
|
|
77
|
+
validations: { pattern: /\A[\w.:-]+\z/ } },
|
|
78
|
+
NMTOKENS: { skippable: true, class_name: TC[:string] },
|
|
56
79
|
}.freeze
|
|
57
80
|
|
|
58
81
|
module_function
|
data/lib/lutaml/model/version.rb
CHANGED
|
@@ -6,9 +6,18 @@ module Lutaml
|
|
|
6
6
|
module Xml
|
|
7
7
|
# Validates a serialized XML string against an XSD schema file.
|
|
8
8
|
#
|
|
9
|
-
# Backs the `validate_xml_with` class macro (issue #264).
|
|
10
|
-
#
|
|
11
|
-
# on
|
|
9
|
+
# Backs the `validate_xml_with` class macro (issue #264). Routing is
|
|
10
|
+
# engine-agnostic through moxml (#1184): schemas compile and validate
|
|
11
|
+
# on the CONFIGURED engine — `Moxml::XSD.supported?` answers without
|
|
12
|
+
# raising, and engines without a validator (ox, oga, rexml) fall back
|
|
13
|
+
# to the historical Nokogiri path, keeping its requirement error when
|
|
14
|
+
# the gem is absent. The leptris engine carries a validator, with the
|
|
15
|
+
# pending targetNamespace-enforcement gap tracked upstream
|
|
16
|
+
# (leptris/leptris-ruby#679).
|
|
17
|
+
#
|
|
18
|
+
# Strict parsing: libxml2 recovery would otherwise repair malformed
|
|
19
|
+
# input and report it as schema-valid. Malformed input raises
|
|
20
|
+
# Moxml::ParseError (the normalized class; line and column carried).
|
|
12
21
|
#
|
|
13
22
|
# Compiled schemas are memoized by absolute path: the XSD files are
|
|
14
23
|
# constant for the life of the process, so each is read and compiled once
|
|
@@ -17,10 +26,8 @@ module Lutaml
|
|
|
17
26
|
# Not to be confused with Schema::Xsd::SchemaValidator, which checks
|
|
18
27
|
# that an XSD document itself is well-formed XSD.
|
|
19
28
|
module XsdValidator
|
|
20
|
-
# Concurrent (and a working Nokogiri) are unavailable under Opal, so
|
|
21
|
-
# the memoization store is only built off-Opal. validate raises a clear
|
|
22
|
-
# NotImplementedError under Opal before this store is ever consulted.
|
|
23
29
|
@schemas = ::Concurrent::Map.new unless RUBY_ENGINE == "opal"
|
|
30
|
+
@nokogiri_schemas = ::Concurrent::Map.new unless RUBY_ENGINE == "opal"
|
|
24
31
|
|
|
25
32
|
# @param xml [String] an XML document
|
|
26
33
|
# @param schema_paths [Array<String>] paths to XSD schema files
|
|
@@ -31,36 +38,74 @@ module Lutaml
|
|
|
31
38
|
|
|
32
39
|
if RUBY_ENGINE == "opal"
|
|
33
40
|
raise NotImplementedError,
|
|
34
|
-
"XSD schema validation (validate_xml_with) requires
|
|
35
|
-
"
|
|
41
|
+
"XSD schema validation (validate_xml_with) requires an " \
|
|
42
|
+
"XML engine with schema validation, which is not " \
|
|
43
|
+
"available under Opal."
|
|
36
44
|
end
|
|
37
45
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
if engine_supports_xsd?
|
|
47
|
+
document = context.parse(xml, strict: true)
|
|
48
|
+
paths.flat_map do |path|
|
|
49
|
+
schema_for(path)
|
|
50
|
+
.validate(document)
|
|
51
|
+
.map { |error| Error::SchemaValidationError.new(error.message, path) }
|
|
52
|
+
end
|
|
53
|
+
else
|
|
54
|
+
ensure_nokogiri!
|
|
55
|
+
begin
|
|
56
|
+
document = ::Nokogiri::XML(xml, &:strict)
|
|
57
|
+
rescue ::Nokogiri::XML::SyntaxError => e
|
|
58
|
+
# One error contract across lanes: the fallback normalizes
|
|
59
|
+
# to the same Moxml::ParseError the engine path raises.
|
|
60
|
+
raise Moxml::ParseError.new(e.message, line: e.line,
|
|
61
|
+
column: e.column)
|
|
62
|
+
end
|
|
63
|
+
paths.flat_map do |path|
|
|
64
|
+
nokogiri_schema_for(path)
|
|
65
|
+
.validate(document)
|
|
66
|
+
.map { |error| Error::SchemaValidationError.new(error.message, path) }
|
|
67
|
+
end
|
|
46
68
|
end
|
|
47
69
|
end
|
|
48
70
|
|
|
71
|
+
# The configured engine as a moxml backend symbol. Config's plain
|
|
72
|
+
# symbol (nokogiri, leptris, ...) — NOT the AdapterResolver's
|
|
73
|
+
# configured type, whose FIXED_ADAPTER_TYPE sentinel is internal
|
|
74
|
+
# and must never reach Moxml (the closed #1183 leak).
|
|
75
|
+
def self.backend
|
|
76
|
+
Lutaml::Model::Config.xml_adapter_type || :nokogiri
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.engine_supports_xsd?
|
|
80
|
+
Moxml::XSD.supported?(context)
|
|
81
|
+
rescue Moxml::AdapterError, LoadError
|
|
82
|
+
false
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def self.context
|
|
86
|
+
@context ||= Moxml.new(backend)
|
|
87
|
+
end
|
|
88
|
+
|
|
49
89
|
def self.schema_for(path)
|
|
50
|
-
@schemas.compute_if_absent(path)
|
|
51
|
-
::Nokogiri::XML::Schema(File.read(path))
|
|
52
|
-
end
|
|
90
|
+
@schemas.compute_if_absent(path) { context.xsd_file(path) }
|
|
53
91
|
end
|
|
54
92
|
|
|
55
93
|
def self.ensure_nokogiri!
|
|
56
94
|
require "nokogiri"
|
|
57
95
|
rescue LoadError
|
|
58
96
|
raise Error::XmlConfigurationError,
|
|
59
|
-
"XSD schema validation (validate_xml_with) requires
|
|
60
|
-
"
|
|
97
|
+
"XSD schema validation (validate_xml_with) requires an " \
|
|
98
|
+
"XML engine with schema validation; add `gem \"nokogiri\"` " \
|
|
99
|
+
"to your Gemfile"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def self.nokogiri_schema_for(path)
|
|
103
|
+
@nokogiri_schemas.compute_if_absent(path) do
|
|
104
|
+
::Nokogiri::XML::Schema(File.read(path))
|
|
105
|
+
end
|
|
61
106
|
end
|
|
62
107
|
|
|
63
|
-
private_class_method :ensure_nokogiri!, :
|
|
108
|
+
private_class_method :ensure_nokogiri!, :nokogiri_schema_for
|
|
64
109
|
end
|
|
65
110
|
end
|
|
66
111
|
end
|
|
@@ -45,9 +45,19 @@ RSpec.describe Lutaml::Json::Adapter::StandardAdapter do
|
|
|
45
45
|
|
|
46
46
|
# json 3.0 removed :escape_slash. Dropping it would silently stop escaping
|
|
47
47
|
# slashes; it is the old alias of :script_safe, so it is translated.
|
|
48
|
+
# json 3.2's script_safe (the translation target) no longer escapes
|
|
49
|
+
# slashes - it escapes <, > and the line separators as \uXXXX, which
|
|
50
|
+
# is the same XSS protection in ECMAScript form. The translation to
|
|
51
|
+
# script_safe stays load-bearing: raw escape_slash raises on json
|
|
52
|
+
# 3.0+.
|
|
48
53
|
it "honours escape_slash on every json version" do
|
|
54
|
+
expected = if Gem::Version.new(JSON::VERSION) >= Gem::Version.new("3.2.0")
|
|
55
|
+
'{"a":"x/y"}'
|
|
56
|
+
else
|
|
57
|
+
'{"a":"x\\/y"}'
|
|
58
|
+
end
|
|
49
59
|
expect(described_class.new({ "a" => "x/y" }).to_json(escape_slash: true))
|
|
50
|
-
.to eq(
|
|
60
|
+
.to eq(expected)
|
|
51
61
|
end
|
|
52
62
|
|
|
53
63
|
# The filter is an allowlist, not a passthrough: genuine generator options
|
|
@@ -22,9 +22,16 @@ RSpec.describe "generator options reaching a nested model" do
|
|
|
22
22
|
let(:model) { EscapeProbe.new(s: "</script>é") }
|
|
23
23
|
|
|
24
24
|
describe "the stdlib adapter" do
|
|
25
|
+
# json 3.2 changed script_safe semantics: <, > and the line
|
|
26
|
+
# separators escape as \uXXXX (ECMAScript-conformant, same XSS
|
|
27
|
+
# protection) and the historical \/ slash escaping is dropped.
|
|
25
28
|
it "honours script_safe" do
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
expected = if Gem::Version.new(JSON::VERSION) >= Gem::Version.new("3.2.0")
|
|
30
|
+
'{"m":{"s":"\\u003c/script>é"}}'
|
|
31
|
+
else
|
|
32
|
+
'{"m":{"s":"<\\/script>é"}}'
|
|
33
|
+
end
|
|
34
|
+
expect(JSON.generate({ "m" => model }, script_safe: true)).to eq(expected)
|
|
28
35
|
end
|
|
29
36
|
|
|
30
37
|
it "honours ascii_only" do
|
|
@@ -130,4 +130,41 @@ RSpec.describe "runtime XSD class loading" do
|
|
|
130
130
|
end
|
|
131
131
|
end
|
|
132
132
|
end
|
|
133
|
+
|
|
134
|
+
describe "builtin types and cardinality" do
|
|
135
|
+
let(:xsd) do
|
|
136
|
+
<<~XSD
|
|
137
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
138
|
+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
|
139
|
+
<xs:complexType name="ItemType">
|
|
140
|
+
<xs:sequence>
|
|
141
|
+
<xs:element name="title" type="xs:string"/>
|
|
142
|
+
<xs:element name="issued" type="xs:gYearMonth" minOccurs="0"/>
|
|
143
|
+
<xs:element name="note" type="xs:string" minOccurs="0"/>
|
|
144
|
+
</xs:sequence>
|
|
145
|
+
</xs:complexType>
|
|
146
|
+
<xs:element name="item" type="ItemType"/>
|
|
147
|
+
</xs:schema>
|
|
148
|
+
XSD
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it "maps XSD builtins absent from the table (gYearMonth) and keeps
|
|
152
|
+
optional singletons scalar" do
|
|
153
|
+
sources = Lutaml::Model::Schema.from_xml(
|
|
154
|
+
xsd, output_dir: "builtin_types_models", load_classes: true
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
model_name = sources.keys.find { |n| n.include?("Item") }
|
|
158
|
+
doc = Object.const_get("BuiltinTypesModels::#{model_name}")
|
|
159
|
+
|
|
160
|
+
parsed = doc.from_xml("<item><title>T1</title><issued>2026-11</issued></item>")
|
|
161
|
+
expect(parsed.title).to eq("T1")
|
|
162
|
+
expect(parsed.issued).to eq("2026-11")
|
|
163
|
+
|
|
164
|
+
minimal = doc.from_xml("<item><title>T2</title></item>")
|
|
165
|
+
expect(minimal.title).to eq("T2")
|
|
166
|
+
# minOccurs=0, maxOccurs=1: scalar optional, not a collection
|
|
167
|
+
expect(minimal.note).to be_nil
|
|
168
|
+
end
|
|
169
|
+
end
|
|
133
170
|
end
|
|
@@ -185,6 +185,16 @@ RSpec.describe "validate_xml_with XSD constructs" do
|
|
|
185
185
|
end
|
|
186
186
|
|
|
187
187
|
it "rejects a document missing the target namespace" do
|
|
188
|
+
# leptris's XSD compiler does not yet enforce targetNamespace
|
|
189
|
+
# qualification (leptris/leptris-ruby#679); the cell gates on the
|
|
190
|
+
# engine until the engine fix lands. The default nokogiri lane
|
|
191
|
+
# enforces it and runs the assertion.
|
|
192
|
+
engine = Lutaml::Model::Config.xml_adapter_type&.to_s
|
|
193
|
+
if engine == "leptris"
|
|
194
|
+
skip "leptris targetNamespace enforcement pending " \
|
|
195
|
+
"leptris/leptris-ruby#679"
|
|
196
|
+
end
|
|
197
|
+
|
|
188
198
|
xml = "<address><city>Berlin</city></address>"
|
|
189
199
|
|
|
190
200
|
expect(XsdNamespacedDoc.validate_xml(xml)).not_to be_empty
|
|
@@ -84,10 +84,13 @@ RSpec.describe "validate_xml_with" do
|
|
|
84
84
|
end.to raise_error(Lutaml::Model::ValidationError, /age/)
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
+
# Malformed input raises instead of recovering; the error class is
|
|
88
|
+
# moxml's normalized ParseError (engine-independent, line and column
|
|
89
|
+
# carried) now that validation routes through the moxml layer (#1184).
|
|
87
90
|
it "raises a parse error for malformed XML instead of recovering" do
|
|
88
91
|
expect do
|
|
89
92
|
XsdValidatedPerson.validate_xml("<person><name>Alice</name>")
|
|
90
|
-
end.to raise_error(
|
|
93
|
+
end.to raise_error(Moxml::ParseError, /Line: 1/)
|
|
91
94
|
end
|
|
92
95
|
end
|
|
93
96
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lutaml-model
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.178
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: base64
|