lutaml-model 0.8.184 → 0.8.186

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: 7512cd09dea291c7f8ef58266e2fb60aa313701ed7bb0df1843e8c7a194dd092
4
- data.tar.gz: 720c91d86520af12e609cef406ae88b9d865450ca04c3e0fd3741114abe1b4da
3
+ metadata.gz: ea63d75c561923cb1286edc3d320fb2e24aa39ed9318a19cabbcf73659f3b9c1
4
+ data.tar.gz: 7dda5b3ee35853aae9dada687a762641bddb5ced2e55dc606c22a8c87b44f451
5
5
  SHA512:
6
- metadata.gz: b5bf83dd31ae2e125c9b886325074b4ae36a333ac637dae2ce364d2dc96850c370e457a0ecb073ece1b2ab247f8e584c000437dfe18c39ed43b4b90bf442a2c9
7
- data.tar.gz: ebb1d2c13fe1a3725ed78b64dccdfe6993b918a9969121b3569b56a877f3d9bf07f389569ab838f3a3a0ca866928f6fe5cb06bc9ef948d3ef1ff93f34b030686
6
+ metadata.gz: 78e413d5a78c52d788e6bcea704b4365afb081671bf2886f84deac7b9ff9e44b574ef38256632c894aa4bfb675aadf692f2b260540d3901bc3a868e39a843624
7
+ data.tar.gz: 95f5e0d1466f68e79287c797b0dba6421d045df2806449e179dba73eb6d395c7eb590c031031cc842917f39f74cf80d89adf0871c4554848c680013a4efaee5d
@@ -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
@@ -201,6 +201,12 @@ module Lutaml
201
201
  return nil if options.key?(:adapter) || options.key?(:only) ||
202
202
  options.key?(:except) || options.key?(:mappings) ||
203
203
  options.key?(:register)
204
+ # A declared model class (the `model` macro) is hydrated by the
205
+ # interpretive transformer, which instantiates the declared
206
+ # class and writes through its accessors; the plan hydrator's
207
+ # construction machinery is Serializable-bound and would build
208
+ # the declaring class instead (#1208).
209
+ return nil if model && model != self
204
210
 
205
211
  adapter_name = Lutaml::Model::Config.adapter_for(:xml)
206
212
  adapter_name = adapter_name&.name
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lutaml
4
4
  module Model
5
- VERSION = "0.8.184"
5
+ VERSION = "0.8.186"
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
@@ -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
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "`model` declaration under every XML adapter (#1208)" do
6
+ let(:xml) { "<book><title>Hi</title></book>" }
7
+
8
+ # Mirrors the issue repro: the Serializable class declares `model
9
+ # BookData`; the helpers live on the plain model class.
10
+ let(:build_classes) do
11
+ stub_const("BookData", Class.new do
12
+ attr_accessor :title
13
+
14
+ def shout = title.upcase
15
+ end)
16
+ stub_const("Book", Class.new(Lutaml::Model::Serializable) do
17
+ model BookData
18
+
19
+ attribute :title, :string
20
+
21
+ xml do
22
+ element "book"
23
+ map_element "title", to: :title
24
+ end
25
+ end)
26
+ end
27
+
28
+ it "returns the declared model class on the configured adapter" do
29
+ build_classes
30
+
31
+ book = Book.from_xml(xml)
32
+
33
+ expect(book).to be_a(BookData)
34
+ expect(book.shout).to eq("HI")
35
+ expect(book.title).to eq("Hi")
36
+ end
37
+
38
+ it "returns the declared model class on the leptris lane", if: defined?(Leptris) do
39
+ build_classes
40
+ previous = Lutaml::Model::Config.xml_adapter_type
41
+ Lutaml::Model::Config.xml_adapter_type = :leptris
42
+
43
+ book = Book.from_xml(xml)
44
+
45
+ expect(book).to be_a(BookData)
46
+ expect(book.shout).to eq("HI")
47
+ ensure
48
+ Lutaml::Model::Config.xml_adapter_type = previous
49
+ end
50
+
51
+ it "returns the declared model class on the nokogiri lane", if: defined?(Nokogiri) do
52
+ build_classes
53
+ previous = Lutaml::Model::Config.xml_adapter_type
54
+ Lutaml::Model::Config.xml_adapter_type = :nokogiri
55
+
56
+ book = Book.from_xml(xml)
57
+
58
+ expect(book).to be_a(BookData)
59
+ expect(book.shout).to eq("HI")
60
+ ensure
61
+ Lutaml::Model::Config.xml_adapter_type = previous
62
+ end
63
+
64
+ it "round-trips a model-class instance through to_xml" do
65
+ build_classes
66
+
67
+ data = BookData.new
68
+ data.title = "Hi"
69
+
70
+ emitted = Book.to_xml(data)
71
+ expect(emitted).to include("<title>Hi</title>")
72
+
73
+ reparsed = Book.from_xml(emitted)
74
+ expect(reparsed).to be_a(BookData)
75
+ expect(reparsed.title).to eq("Hi")
76
+ end
77
+ end
@@ -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
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.184
4
+ version: 0.8.186
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -1852,6 +1852,7 @@ files:
1852
1852
  - spec/lutaml/model/mapping_type_validation_spec.rb
1853
1853
  - spec/lutaml/model/metaschema_types_spec.rb
1854
1854
  - spec/lutaml/model/mixed_content_spec.rb
1855
+ - spec/lutaml/model/model_declaration_from_xml_spec.rb
1855
1856
  - spec/lutaml/model/model_import_reentry_spec.rb
1856
1857
  - spec/lutaml/model/model_transformer_spec.rb
1857
1858
  - spec/lutaml/model/multiple_mapping_spec.rb
@@ -2008,6 +2009,7 @@ files:
2008
2009
  - spec/lutaml/xml/adapter/xml_namespace_spec.rb
2009
2010
  - spec/lutaml/xml/adapter_loader_spec.rb
2010
2011
  - spec/lutaml/xml/adapter_metadata_default_spec.rb
2012
+ - spec/lutaml/xml/adapter_parity_cross_namespace_spec.rb
2011
2013
  - spec/lutaml/xml/attribute_ambiguity_surface_spec.rb
2012
2014
  - spec/lutaml/xml/attribute_element_namespace_disjoint_spec.rb
2013
2015
  - spec/lutaml/xml/attribute_namespace_disjointness_spec.rb