lutaml-model 0.8.185 → 0.8.187

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b26ea4574929a6043881ef30b2d06dda2f92c1d45ec4c3fd02ccaf6fd960106e
4
- data.tar.gz: bfdd0d769e016a8b2bb81c5e501e7c4e6da1cf209da9085d5200fab95d21ae20
3
+ metadata.gz: c99faff9d9708ef506066511872124dc5a3b0c8fb81b8754e9994597855d4ad1
4
+ data.tar.gz: 89639853feb6000854c88e471077aa4287afe099a7101f7fe52325c764f59e72
5
5
  SHA512:
6
- metadata.gz: 687960e7428c5b0da2ffe6acba59f08a8ccc5e120fb87cd2311f00e1eec8de3f49985046a0411f29736c935e0135189630d68f692b79b6efc58c43d0606ba5ad
7
- data.tar.gz: 1af00fce805b3d9982a25a8f79b9cac56ee5dbb697284c5080ef15c1e81d1e5759e0b1da8b97206776e852eb7456f245a11634781c0440b6023646af04b95c72
6
+ metadata.gz: 49377837acaeb059625140e4d02712c5b6cf4fe64abb94999c835b4ee80265debf60e669f5417816f1b1d82477e4483dc8971436358a0f3e427151b218c18240
7
+ data.tar.gz: 2c650806f8322434d3cae7943bd406e52c02ed3f23e48dc1244e614cc4dd70bb058050e25d01847957e0d6d5a0a92422c8d8ea9f58d03b361d9d004bf204cdab
@@ -25,7 +25,7 @@ module Lutaml
25
25
  # carry the owning element's name as `root_name` so the
26
26
  # generated class serializes the element, not the internal
27
27
  # CT_ alias.
28
- def build(complex_type, root_name: nil)
28
+ def build(complex_type, root_name: nil, schema: nil)
29
29
  model = Definitions::Model.new(
30
30
  class_name: Utils.camel_case(complex_type.name),
31
31
  xml_root: Definitions::XmlRoot.new(
@@ -33,7 +33,8 @@ module Lutaml
33
33
  name: root_name || complex_type.name,
34
34
  ),
35
35
  mixed: !!complex_type.mixed,
36
- namespace_class_name: @parent.namespace_class_name,
36
+ namespace_class_name:
37
+ @parent.namespace_class_name_for(schema),
37
38
  )
38
39
 
39
40
  ElementOrder.resolved(complex_type).each do |element|
@@ -61,6 +61,11 @@ module Lutaml
61
61
  )
62
62
  @namespace_classes[ns.class_name] = ns
63
63
  end
64
+ # Per-unit binding (#1212): each schema unit's models bind the
65
+ # unit's own target namespace, so generated children match
66
+ # their declaring schema's elements exactly on every lane.
67
+ @namespace_classes_by_uri = @namespace_classes.values
68
+ .to_h { |ns| [ns.uri, ns.class_name] }
64
69
  # Resolve the namespace_class_name once. When the caller
65
70
  # passed :namespace, every generated complex type gets this
66
71
  # name. Mirrors XmlCompiler::ComplexType#setup_options on main.
@@ -70,6 +75,40 @@ module Lutaml
70
75
  end&.class_name
71
76
  end
72
77
 
78
+ # The namespace class for a schema unit's models: the unit's own
79
+ # target namespace when it declares one, else the caller's
80
+ # requested namespace (#1212 per-unit binding). Imported units
81
+ # surface during the walk, after collect_namespaces ran, so the
82
+ # unit's namespace class is created on first encounter and rides
83
+ # the output's namespace entries like the seeded ones.
84
+ def namespace_class_name_for(schema)
85
+ # Per-unit binding rides an explicit namespace request: the
86
+ # caller's option unlocks per-unit resolution for imported
87
+ # units (#1212). Without it, models stay unbound - the
88
+ # legacy bare-emission/parse behavior the compile corpus
89
+ # depends on.
90
+ return @namespace_class_name if @namespace_class_name.nil?
91
+
92
+ uri = schema&.target_namespace
93
+ return @namespace_class_name if uri.nil? || uri.empty?
94
+
95
+ @namespace_classes_by_uri ||= {}
96
+ @namespace_classes_by_uri[uri] ||= begin
97
+ found = @namespace_classes.values.find { |ns| ns.uri == uri }
98
+ if found
99
+ found.class_name
100
+ else
101
+ ns = Definitions::Namespace.new(
102
+ class_name: NamespaceNaming.class_name_for(uri),
103
+ uri: uri,
104
+ prefix_default: NamespaceNaming.prefix_for(uri),
105
+ )
106
+ @namespace_classes[ns.class_name] = ns
107
+ ns.class_name
108
+ end
109
+ end
110
+ end
111
+
73
112
  def walk_schemas(schemas)
74
113
  return if schemas.empty?
75
114
 
@@ -111,10 +150,11 @@ module Lutaml
111
150
  end
112
151
  end
113
152
 
114
- def dispatch_complex(item, _schema)
153
+ def dispatch_complex(item, schema)
115
154
  case item
116
155
  when Lutaml::Xml::Schema::Xsd::ComplexType
117
- @complex_types[item.name] = @complex_types_builder.build(item)
156
+ @complex_types[item.name] =
157
+ @complex_types_builder.build(item, schema: schema)
118
158
  when Lutaml::Xml::Schema::Xsd::Group
119
159
  @group_types[item.name] = @complex_types_builder.build_group(item)
120
160
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.185"
5
+ VERSION = "0.8.187"
6
6
  end
7
7
  end
@@ -337,6 +337,13 @@ module Lutaml
337
337
  ec.each do |child|
338
338
  child_names_set.add(child.namespaced_name)
339
339
  child_names_set.add(child.unprefixed_name)
340
+ # URI-qualified spelling: prefix-independent exact matching
341
+ # against the rule's URI-form namespaced name — derived
342
+ # models bind per schema unit while instances spell
343
+ # arbitrary prefixes (#1212).
344
+ if (c_uri = child.namespace_uri)
345
+ child_names_set.add("#{c_uri}:#{child.unprefixed_name}")
346
+ end
340
347
  end
341
348
  end
342
349
  end
@@ -1285,6 +1292,18 @@ _effective_register)
1285
1292
  # This handles the case where elements have prefixed names but are in blank namespace
1286
1293
  # e.g., <GML:ApplicationSchema xmlns=""> should match the rule expecting GML namespace
1287
1294
  child_ns_prefix = child.namespace_prefix
1295
+ # The element declaration lives in the DECLARING model's
1296
+ # namespace (XSD: an element's namespace is its declaring
1297
+ # schema's), even when the rule's spelling follows the
1298
+ # referenced type's namespace unit (b:Limit-type rules spell
1299
+ # the type's target namespace). Accept the parent-bound
1300
+ # spelling by (URI, local) so XmlCompiler-derived children
1301
+ # parse identically on every lane (#1212).
1302
+ if child_uri && child.unprefixed_name == rule_name_str
1303
+ parent_ns_class = session.xml_mapping&.namespace_class
1304
+ parent_uri = parent_ns_class&.uri
1305
+ next true if parent_uri && child_uri == parent_uri
1306
+ end
1288
1307
  # Match by prefix AND local name to avoid matching unrelated elements
1289
1308
  # with the same namespace prefix (e.g., xsd:attributeGroup should not
1290
1309
  # match a rule for xsd:attribute)
@@ -29,7 +29,7 @@ module Lutaml
29
29
  attribute :simple_content, :simple_content
30
30
  attribute :complex_content, :complex_content
31
31
  attribute :assert, :assert, collection: true,
32
- initialize_empty: true
32
+ initialize_empty: true
33
33
  attribute :open_content, :open_content
34
34
  attribute :attribute, :attribute, collection: true,
35
35
  initialize_empty: true
@@ -13,7 +13,7 @@ module Lutaml
13
13
  attribute :sequence, :sequence
14
14
  attribute :annotation, :annotation
15
15
  attribute :assert, :assert, collection: true,
16
- initialize_empty: true
16
+ initialize_empty: true
17
17
  attribute :any_attribute, :any_attribute
18
18
  attribute :attribute, :attribute, collection: true,
19
19
  initialize_empty: true
@@ -9,7 +9,7 @@ module Lutaml
9
9
  attribute :base, :string
10
10
  attribute :annotation, :annotation
11
11
  attribute :assert, :assert, collection: true,
12
- initialize_empty: true
12
+ initialize_empty: true
13
13
  attribute :any_attribute, :any_attribute
14
14
  attribute :attribute, :attribute, collection: true,
15
15
  initialize_empty: true
@@ -10,6 +10,7 @@ module Lutaml
10
10
  class OpenContent < Base
11
11
  attribute :id, :string
12
12
  attribute :mode, :string
13
+ attribute :any, :any
13
14
  attribute :annotation, :annotation
14
15
 
15
16
  xml do
@@ -19,6 +20,7 @@ module Lutaml
19
20
 
20
21
  map_attribute :id, to: :id
21
22
  map_attribute :mode, to: :mode
23
+ map_element :any, to: :any
22
24
  map_element :annotation, to: :annotation
23
25
  end
24
26
 
@@ -13,7 +13,7 @@ module Lutaml
13
13
  attribute :sequence, :sequence
14
14
  attribute :annotation, :annotation
15
15
  attribute :assert, :assert, collection: true,
16
- initialize_empty: true
16
+ initialize_empty: true
17
17
  attribute :any_attribute, :any_attribute
18
18
  attribute :attribute, :attribute, collection: true,
19
19
  initialize_empty: true
@@ -9,7 +9,7 @@ module Lutaml
9
9
  attribute :base, :string
10
10
  attribute :annotation, :annotation
11
11
  attribute :assert, :assert, collection: true,
12
- initialize_empty: true
12
+ initialize_empty: true
13
13
  attribute :simple_type, :simple_type
14
14
  attribute :any_attribute, :any_attribute
15
15
  attribute :length, :length, collection: true, initialize_empty: true
@@ -31,6 +31,8 @@ module Lutaml
31
31
  initialize_empty: true
32
32
  attribute :fraction_digits, :fraction_digits, collection: true,
33
33
  initialize_empty: true
34
+ attribute :assertion, :assert, collection: true,
35
+ initialize_empty: true
34
36
 
35
37
  xml do
36
38
  element "restriction"
@@ -53,6 +55,7 @@ module Lutaml
53
55
  map_element :maxInclusive, to: :max_inclusive
54
56
  map_element :minInclusive, to: :min_inclusive
55
57
  map_element :fractionDigits, to: :fraction_digits
58
+ map_element :assertion, to: :assertion
56
59
  end
57
60
 
58
61
  Lutaml::Xml::Schema::Xsd.register_model(self,
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "open3"
5
+ require "rbconfig"
6
+ require "tmpdir"
7
+
8
+ # XmlCompiler-derived models bind one schema namespace at the model
9
+ # level while children legally live in the imported schemas'
10
+ # namespaces. Every adapter must bind such cross-namespace children
11
+ # identically (#1212).
12
+ ADAPTERS = %w[default nokogiri].freeze
13
+
14
+ RSpec.describe "adapter parity for cross-namespace child binding (#1212)" do
15
+ let(:base_xsd) do
16
+ <<~XSD
17
+ <?xml version="1.0" encoding="UTF-8"?>
18
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
19
+ targetNamespace="http://example.com/p1212-base"
20
+ elementFormDefault="qualified">
21
+ <xs:complexType name="Upper">
22
+ <xs:simpleContent>
23
+ <xs:extension base="xs:string"/>
24
+ </xs:simpleContent>
25
+ </xs:complexType>
26
+ <xs:complexType name="Limit">
27
+ <xs:sequence>
28
+ <xs:element name="lower" type="xs:string"/>
29
+ <xs:element name="upper" type="Upper"/>
30
+ </xs:sequence>
31
+ </xs:complexType>
32
+ </xs:schema>
33
+ XSD
34
+ end
35
+
36
+ let(:main_xsd) do
37
+ <<~XSD
38
+ <?xml version="1.0" encoding="UTF-8"?>
39
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
40
+ xmlns:b="http://example.com/p1212-base"
41
+ targetNamespace="http://example.com/p1212-main"
42
+ elementFormDefault="qualified">
43
+ <xs:import namespace="http://example.com/p1212-base"
44
+ schemaLocation="base.xsd"/>
45
+ <xs:complexType name="Holder">
46
+ <xs:sequence>
47
+ <xs:element name="limit" type="b:Limit"/>
48
+ </xs:sequence>
49
+ </xs:complexType>
50
+ <xs:element name="root">
51
+ <xs:complexType>
52
+ <xs:sequence>
53
+ <xs:element name="holder" type="Holder"/>
54
+ </xs:sequence>
55
+ </xs:complexType>
56
+ </xs:element>
57
+ </xs:schema>
58
+ XSD
59
+ end
60
+
61
+ let(:instance_xml) do
62
+ <<~XML
63
+ <m:root xmlns:m="http://example.com/p1212-main"
64
+ xmlns:b="http://example.com/p1212-base">
65
+ <m:holder>
66
+ <m:limit>
67
+ <b:lower>0</b:lower>
68
+ <b:upper>5</b:upper>
69
+ </m:limit>
70
+ </m:holder>
71
+ </m:root>
72
+ XML
73
+ end
74
+
75
+ ADAPTERS.each do |adapter|
76
+ it "binds imported-schema children on the #{adapter} lane" do
77
+ Dir.mktmpdir do |dir|
78
+ File.write(File.join(dir, "base.xsd"), base_xsd)
79
+ File.write(File.join(dir, "main.xsd"), main_xsd)
80
+ File.write(File.join(dir, "instance.xml"), instance_xml)
81
+
82
+ configure = if adapter == "default"
83
+ ""
84
+ else
85
+ "Lutaml::Model::Config.configure do |c|\n " \
86
+ "c.xml_adapter_type = :#{adapter}\n" \
87
+ "end\n"
88
+ end
89
+
90
+ probe = <<~RUBY
91
+ require "lutaml/model"
92
+ #{configure}
93
+ Lutaml::Model::Schema.from_xml(
94
+ File.read("#{dir}/main.xsd"),
95
+ output_dir: "#{dir}/p1212_models",
96
+ load_classes: true,
97
+ namespace: "http://example.com/p1212-main",
98
+ module_namespace: "P1212Models",
99
+ register_id: "p1212_reg",
100
+ location: "#{dir}/main.xsd",
101
+ )
102
+ parsed = Object.const_get("P1212Models::CTRoot").from_xml(
103
+ File.read("#{dir}/instance.xml"),
104
+ )
105
+ limit = parsed.holder.limit
106
+ puts "lower=\#{limit.lower.inspect}"
107
+ puts "upper=\#{limit.upper&.content.inspect}"
108
+ RUBY
109
+
110
+ _stdout, stderr, status = Open3.capture3(RbConfig.ruby, "-I", "lib",
111
+ "-e", probe)
112
+ expect(status).to be_success, stderr
113
+
114
+ expect(_stdout).to include('lower="0"')
115
+ expect(_stdout).to include('upper="5"')
116
+ end
117
+ end
118
+ end
119
+ end
@@ -117,4 +117,44 @@ RSpec.describe "XSD 1.1 constructs (#1203)" do
117
117
  expect(emitted).to include("<xs:alternative")
118
118
  expect(emitted).to include('type="T"')
119
119
  end
120
+
121
+ it "maps xs:assertion on a simpleType restriction" do
122
+ xsd = <<~XSD
123
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
124
+ <xs:simpleType name="num">
125
+ <xs:restriction base="xs:string">
126
+ <xs:assertion test="$value castable as xs:double"/>
127
+ </xs:restriction>
128
+ </xs:simpleType>
129
+ </xs:schema>
130
+ XSD
131
+
132
+ schema = Lutaml::Xml::Schema::Xsd.parse(xsd)
133
+ assertion = schema.simple_type.first.restriction.assertion.first
134
+
135
+ expect(assertion).not_to be_nil
136
+ expect(assertion.test).to eq("$value castable as xs:double")
137
+ end
138
+
139
+ it "keeps the xs:any wildcard on xs:openContent" do
140
+ xsd = <<~XSD
141
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
142
+ <xs:complexType name="t">
143
+ <xs:sequence>
144
+ <xs:element name="a"/>
145
+ </xs:sequence>
146
+ <xs:openContent mode="interleave">
147
+ <xs:any namespace="http://open.example/" processContents="lax"/>
148
+ </xs:openContent>
149
+ </xs:complexType>
150
+ </xs:schema>
151
+ XSD
152
+
153
+ schema = Lutaml::Xml::Schema::Xsd.parse(xsd)
154
+ any = schema.complex_type.first.open_content.any
155
+
156
+ expect(any).not_to be_nil
157
+ expect(any.namespace).to eq("http://open.example/")
158
+ expect(any.process_contents).to eq("lax")
159
+ end
120
160
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.185
4
+ version: 0.8.187
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -2009,6 +2009,7 @@ files:
2009
2009
  - spec/lutaml/xml/adapter/xml_namespace_spec.rb
2010
2010
  - spec/lutaml/xml/adapter_loader_spec.rb
2011
2011
  - spec/lutaml/xml/adapter_metadata_default_spec.rb
2012
+ - spec/lutaml/xml/adapter_parity_cross_namespace_spec.rb
2012
2013
  - spec/lutaml/xml/attribute_ambiguity_surface_spec.rb
2013
2014
  - spec/lutaml/xml/attribute_element_namespace_disjoint_spec.rb
2014
2015
  - spec/lutaml/xml/attribute_namespace_disjointness_spec.rb