xsd 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab9eaf4eb6b8aacde5379acdc08cc16c3d85e3dc9d75a5887b2f1db05ea6ed58
4
- data.tar.gz: 8c5fdbf108a6867689ff3292a5e208a33160f232b4a3d3a96eb55e42e9b6da7e
3
+ metadata.gz: dce377d21632c6a26b8cf654ec519d7fa7664a6518b136280433a1411580159c
4
+ data.tar.gz: 57ff6cd7eb787f93d1ab51b8ebcc170da4f9726105a584a3a9d69f24711fdd0d
5
5
  SHA512:
6
- metadata.gz: d75af5531e7763eb3bc9359b2992e03704d963cfbc394a863022a3ff8db87d97db368409666bc7540f2fc772b78e2b25ab59905301e19d370827750050823462
7
- data.tar.gz: 2b06fd643f69e41017683b689e614dbef7c42dca4fefbdec753f7f8edcc0978caf80eefe8c7d68e4acdc86aadd2777160a5cd7105a776d13ebfc2c57fc5af45c
6
+ metadata.gz: 95c494a1699eee86c90989c1de42ef8f1532ea805273e1f35b2b7f2f6d68cc74f4a056ba1c71b5b3f3e9b7b80d9500b206fc684d4bddf4cb3563dda59d21fc48
7
+ data.tar.gz: d58a3a37ebd8104cbcd58eb416f127bd862f2c9e510bafafc83ffbefb257ce5ef1093d56db8a6ad0682137fbe9de04d67fa8d0e5d2b32f38a56e1b1a3fe0e0cc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [2.3.0] - 2023-09-06
4
+
5
+ - Fixed reading of SimpleType in List
6
+ - Add support for Restriction without base
7
+ - Support Any generation in simple scenarios
8
+
3
9
  ## [2.2.0] - 2023-07-10
4
10
 
5
11
  - Fixed built-in types detection for unprefixed schema namespace
data/Gemfile CHANGED
@@ -12,4 +12,5 @@ group :development do
12
12
  gem 'rubocop-performance', '~> 1.17'
13
13
  gem 'rubocop-rake', '~> 0.6'
14
14
  gem 'rubocop-rspec', '~> 2.10'
15
+ gem 'wasabi', '~> 4.0'
15
16
  end
@@ -32,14 +32,6 @@ module XSD
32
32
  end
33
33
  end
34
34
 
35
- # Optional. Specifies a unique ID for the element
36
- # @!attribute id
37
- # @return String
38
- # property :id, :string
39
- def id
40
- node['id']
41
- end
42
-
43
35
  def initialize(options = {})
44
36
  @options = options
45
37
  @cache = {}
@@ -53,6 +45,20 @@ module XSD
53
45
  options[:node]
54
46
  end
55
47
 
48
+ # Get object string representation
49
+ # @return String
50
+ def inspect
51
+ "#<#{self.class.name} path=#{node.path}>"
52
+ end
53
+
54
+ # Optional. Specifies a unique ID for the element
55
+ # @!attribute id
56
+ # @return String
57
+ # property :id, :string
58
+ def id
59
+ node['id']
60
+ end
61
+
56
62
  # Get current namespaces
57
63
  # @return Hash
58
64
  def namespaces
@@ -62,20 +68,20 @@ module XSD
62
68
  # Get child nodes
63
69
  # @param [Symbol] name
64
70
  # @return Nokogiri::XML::NodeSet
65
- def nodes(name = :*)
66
- node.xpath("./xs:#{name}", { 'xs' => XML_SCHEMA })
71
+ def nodes(name = :*, deep = false)
72
+ node.xpath("./#{deep ? '/' : ''}xs:#{name}", { 'xs' => XML_SCHEMA })
67
73
  end
68
74
 
69
- # Get schema by namespace or namespace prefix
70
- # @param [String, nil] namespace
75
+ # Get schemas by namespace or prefix
76
+ # @param [String, nil] ns_or_prefix
71
77
  # @return Array<Schema>
72
- def schemas_for_namespace(namespace)
73
- if schema.targets_namespace?(namespace)
78
+ def schemas_for_namespace(ns_or_prefix)
79
+ if schema.targets_namespace?(ns_or_prefix)
74
80
  [schema, *schema.includes.map(&:imported_schema)]
75
- elsif (import = schema.import_by_namespace(namespace))
81
+ elsif (import = schema.import_by_namespace(ns_or_prefix))
76
82
  [import.imported_schema]
77
83
  else
78
- raise Error, "Schema not found for namespace '#{namespace}' in '#{schema.id || schema.target_namespace}'"
84
+ raise Error, "Schema not found for namespace '#{ns_or_prefix}' in '#{schema.id || schema.target_namespace}'"
79
85
  end
80
86
  end
81
87
 
@@ -187,7 +193,7 @@ module XSD
187
193
  # @param [Nokogiri::XML::Node] node
188
194
  # @return Array<String>
189
195
  def documentation_for(node)
190
- node.xpath('./xs:annotation/xs:documentation/text()', { 'xs' => XML_SCHEMA }).map(&:to_s).map(&:strip)
196
+ node.xpath('./xs:annotation/xs:documentation/text()', { 'xs' => XML_SCHEMA }).map { |x| x.to_s.strip }
191
197
  end
192
198
 
193
199
  # Get all available elements on the current stack level
@@ -315,6 +321,9 @@ module XSD
315
321
  name = link[:property] ? send(link[:property]) : nil
316
322
  if name
317
323
  return @cache[method] = object_by_name(link[:type], name)
324
+ elsif is_a?(Restriction) && method == :base_simple_type
325
+ # handle restriction without base
326
+ return nil
318
327
  end
319
328
  end
320
329
 
data/lib/xsd/generator.rb CHANGED
@@ -59,13 +59,13 @@ module XSD
59
59
  # configure namespaces
60
60
  # TODO: попытаться использовать collect_namespaces?
61
61
  attributes = {}
62
- collect_attributes = element.collect_attributes
62
+ all_attributes = element.collect_attributes
63
63
  if element.complex?
64
- collect_elements = element.collect_elements
64
+ all_elements = element.collect_elements
65
65
 
66
66
  # get namespaces for current element and it's children
67
67
  prefix = nil
68
- [*collect_elements, element].each do |elem|
68
+ [*all_elements, element].each do |elem|
69
69
  prefix = get_namespace_prefix(elem, attributes, namespaces)
70
70
  end
71
71
  else
@@ -75,7 +75,7 @@ module XSD
75
75
  # iterate through each item
76
76
  data.each do |item|
77
77
  # prepare attributes
78
- collect_attributes.each do |attribute|
78
+ all_attributes.each do |attribute|
79
79
  value = item["@#{attribute.name}"]
80
80
  if value
81
81
  attributes[attribute.name] = value
@@ -88,13 +88,18 @@ module XSD
88
88
  if element.complex?
89
89
  # generate tag recursively
90
90
  xml.tag!("#{prefix}:#{element.name}", attributes) do
91
- collect_elements.each do |elem|
91
+ all_elements.each do |elem|
92
92
  build_element(xml, elem, item, namespaces.dup)
93
93
  end
94
94
  end
95
95
  else
96
- value = item.is_a?(Hash) ? item['#text'] : item
97
- xml.tag!("#{prefix}:#{element.name}", attributes, (value == '' ? nil : value))
96
+ has_text = item.is_a?(Hash)
97
+ if has_text && element.complex_type&.nodes(:any, true)&.any?
98
+ xml.tag!("#{prefix}:#{element.name}", attributes) { |res| res << item['#text'].to_s }
99
+ else
100
+ value = has_text ? item['#text'] : item
101
+ xml.tag!("#{prefix}:#{element.name}", attributes, (value == '' ? nil : value))
102
+ end
98
103
  end
99
104
  end
100
105
  end
@@ -108,7 +113,7 @@ module XSD
108
113
  namespace = (element.referenced? ? element.reference : element).target_namespace
109
114
  prefix = namespaces.key(namespace)
110
115
  unless prefix
111
- prefix = "tns#{@namespace_index += 1}"
116
+ prefix = "n#{@namespace_index += 1}"
112
117
  namespaces[prefix] = attributes["xmlns:#{prefix}"] = namespace
113
118
  end
114
119
 
@@ -119,7 +124,7 @@ module XSD
119
124
  # @param [String, Array<String>, nil] lookup
120
125
  def find_root_element(lookup)
121
126
  if lookup
122
- element = schema[*lookup]
127
+ element = self[*lookup]
123
128
  raise Error, "Cant find start element #{lookup}" unless element.is_a?(Element)
124
129
 
125
130
  element
@@ -5,7 +5,7 @@ module XSD
5
5
  # Parent elements: simpleType
6
6
  # https://www.w3schools.com/xml/el_list.asp
7
7
  class List < BaseObject
8
- TYPE_PROPERTY = :itemType
8
+ TYPE_PROPERTY = :item_type
9
9
 
10
10
  include SimpleTyped
11
11
 
@@ -154,10 +154,15 @@ module XSD
154
154
  end.compact.flatten
155
155
  end
156
156
 
157
- # Get import by namespace
157
+ # Get import by namespace or prefix
158
+ # @param [String, nil] ns_or_prefix
158
159
  # @return Import
159
- def import_by_namespace(ns)
160
- aliases = [ns, namespaces["xmlns:#{(ns || '').gsub(/^xmlns:/, '')}"], reader.namespace_prefixes[ns]].compact
160
+ def import_by_namespace(ns_or_prefix)
161
+ aliases = [
162
+ ns_or_prefix,
163
+ namespaces["xmlns:#{(ns_or_prefix || '').gsub(/^xmlns:/, '')}"]
164
+ ].compact
165
+
161
166
  imports.find { |import| aliases.include?(import.namespace) }
162
167
  end
163
168
 
@@ -224,12 +229,25 @@ module XSD
224
229
  # @param [Set] processed
225
230
  def recursive_import_xsd(schema, file, processed, &block)
226
231
  # handle recursion
227
- namespace = schema.target_namespace
228
- return if processed.include?(namespace)
232
+ return if processed.include?(schema.target_namespace)
233
+
234
+ processed.add(schema.target_namespace)
235
+
236
+ # prepare schema XML with all namespaces included, clone node to avoid mutating original schema
237
+ node = schema.node
238
+ if node.namespaces.size != node.namespace_definitions.size
239
+ prefixes = node.namespace_definitions.map(&:prefix)
240
+ node = schema.node.dup
229
241
 
230
- processed.add(namespace)
242
+ schema.node.namespaces.each do |attr, ns|
243
+ prefix = attr == 'xmlns' ? nil : attr.sub('xmlns:', '')
244
+ # does not work!
245
+ # node.add_namespace_definition(prefix, ns) unless prefixes.include?(prefix)
246
+ node[attr] = ns unless prefixes.include?(prefix)
247
+ end
248
+ end
231
249
 
232
- data = schema.node.to_xml
250
+ data = node.to_xml
233
251
 
234
252
  schema.imports.each do |import|
235
253
  name = "#{::SecureRandom.urlsafe_base64}.xsd"
data/lib/xsd/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module XSD
4
- VERSION = '2.2.0'
4
+ VERSION = '2.3.0'
5
5
  end
data/lib/xsd/xml.rb CHANGED
@@ -7,7 +7,7 @@ module XSD
7
7
  class XML
8
8
  include Generator
9
9
 
10
- attr_reader :options, :object_cache, :schemas, :namespace_prefixes
10
+ attr_reader :options, :object_cache, :schemas
11
11
 
12
12
  DEFAULT_RESOURCE_RESOLVER = proc do |location, namespace|
13
13
  if location =~ /^https?:/
@@ -74,10 +74,9 @@ module XSD
74
74
  end
75
75
 
76
76
  def initialize(**options)
77
- @options = options
78
- @object_cache = {}
79
- @schemas = []
80
- @namespace_prefixes = {}
77
+ @options = options
78
+ @object_cache = {}
79
+ @schemas = []
81
80
  end
82
81
 
83
82
  def logger
@@ -115,24 +114,16 @@ module XSD
115
114
  new_schema
116
115
  end
117
116
 
118
- # Add prefixes defined outside of processed schemas, for example in WSDL document
119
- # @param [String] prefix
120
- # @param [String] namespace
121
- def add_namespace_prefix(prefix, namespace)
122
- @namespace_prefixes[prefix] = namespace
123
- end
124
-
125
117
  # Get first added (considered primary) schema
126
118
  # @return Schema, nil
127
119
  def schema
128
120
  schemas.first
129
121
  end
130
122
 
131
- # Get schema by namespace or namespace prefix
123
+ # Get schemas by namespace
132
124
  # @param [String, nil] namespace
133
125
  # @return Array<Schema>
134
126
  def schemas_for_namespace(namespace)
135
- namespace = namespace_prefixes[namespace] if namespace_prefixes.key?(namespace)
136
127
  schemas.select { |schema| schema.target_namespace == namespace }
137
128
  end
138
129
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xsd
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - d.arkhipov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-10 00:00:00.000000000 Z
11
+ date: 2023-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  requirements: []
137
- rubygems_version: 3.1.6
137
+ rubygems_version: 3.4.10
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: The Ruby XSD library is an XML Schema implementation for Ruby.