xsd 2.2.0 → 2.3.0
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/CHANGELOG.md +6 -0
- data/Gemfile +1 -0
- data/lib/xsd/base_object.rb +26 -17
- data/lib/xsd/generator.rb +14 -9
- data/lib/xsd/objects/list.rb +1 -1
- data/lib/xsd/objects/schema.rb +25 -7
- data/lib/xsd/version.rb +1 -1
- data/lib/xsd/xml.rb +5 -14
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dce377d21632c6a26b8cf654ec519d7fa7664a6518b136280433a1411580159c
|
4
|
+
data.tar.gz: 57ff6cd7eb787f93d1ab51b8ebcc170da4f9726105a584a3a9d69f24711fdd0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/lib/xsd/base_object.rb
CHANGED
@@ -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("
|
71
|
+
def nodes(name = :*, deep = false)
|
72
|
+
node.xpath("./#{deep ? '/' : ''}xs:#{name}", { 'xs' => XML_SCHEMA })
|
67
73
|
end
|
68
74
|
|
69
|
-
# Get
|
70
|
-
# @param [String, nil]
|
75
|
+
# Get schemas by namespace or prefix
|
76
|
+
# @param [String, nil] ns_or_prefix
|
71
77
|
# @return Array<Schema>
|
72
|
-
def schemas_for_namespace(
|
73
|
-
if schema.targets_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(
|
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 '#{
|
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
|
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
|
-
|
62
|
+
all_attributes = element.collect_attributes
|
63
63
|
if element.complex?
|
64
|
-
|
64
|
+
all_elements = element.collect_elements
|
65
65
|
|
66
66
|
# get namespaces for current element and it's children
|
67
67
|
prefix = nil
|
68
|
-
[*
|
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
|
-
|
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
|
-
|
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
|
-
|
97
|
-
|
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 = "
|
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 =
|
127
|
+
element = self[*lookup]
|
123
128
|
raise Error, "Cant find start element #{lookup}" unless element.is_a?(Element)
|
124
129
|
|
125
130
|
element
|
data/lib/xsd/objects/list.rb
CHANGED
data/lib/xsd/objects/schema.rb
CHANGED
@@ -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(
|
160
|
-
aliases = [
|
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
|
-
|
228
|
-
|
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
|
-
|
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 =
|
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
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
|
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
|
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
|
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.
|
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-
|
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.
|
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.
|