xsd 2.2.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +1 -0
- data/lib/xsd/base_object.rb +26 -17
- data/lib/xsd/generator.rb +18 -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: f913aa0733e739fe2c7bad6f4c3969f9ee3e5a14df34bb240f3b188b3fbc77e0
|
4
|
+
data.tar.gz: 5a3b4c6bdd94e8c7223b3d5b7d6c29d010689a36d5efd014129e7d1d8b23ff7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 898d2ccca42898cef0a4017e37e3b8f0f4da86a462552aa971c3268abbf675eeceb5e5022364d755f40f09714245eb7ac3057895a1abc45dcf4a4108f92c7b73
|
7
|
+
data.tar.gz: bfad6be19d68ef8b932cac391701f80174ea36b4883f8cad2e3be3b0e3d068755b8bbda328d1de76d5ef499876e8081384e0630e6931535e688e79958210f19c
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [2.4.0] - 2023-11-13
|
4
|
+
|
5
|
+
- Fixed reading of SimpleType in List
|
6
|
+
- Add support for Restriction without base
|
7
|
+
- Support Any generation in simple scenarios
|
8
|
+
|
9
|
+
## [2.3.0] - 2023-09-06
|
10
|
+
|
11
|
+
- Fixed reading of SimpleType in List
|
12
|
+
- Add support for Restriction without base
|
13
|
+
- Support Any generation in simple scenarios
|
14
|
+
|
3
15
|
## [2.2.0] - 2023-07-10
|
4
16
|
|
5
17
|
- 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,10 +75,14 @@ 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
|
82
|
+
elsif attribute.required?
|
83
|
+
raise Error, "Attribute #{attribute.name} is required, but no data in provided for it" if attribute.fixed.nil?
|
84
|
+
|
85
|
+
attributes[attribute.name] = attribute.fixed
|
82
86
|
else
|
83
87
|
attributes.delete(attribute.name)
|
84
88
|
end
|
@@ -88,13 +92,18 @@ module XSD
|
|
88
92
|
if element.complex?
|
89
93
|
# generate tag recursively
|
90
94
|
xml.tag!("#{prefix}:#{element.name}", attributes) do
|
91
|
-
|
95
|
+
all_elements.each do |elem|
|
92
96
|
build_element(xml, elem, item, namespaces.dup)
|
93
97
|
end
|
94
98
|
end
|
95
99
|
else
|
96
|
-
|
97
|
-
|
100
|
+
has_text = item.is_a?(Hash)
|
101
|
+
if has_text && element.complex_type&.nodes(:any, true)&.any?
|
102
|
+
xml.tag!("#{prefix}:#{element.name}", attributes) { |res| res << item['#text'].to_s }
|
103
|
+
else
|
104
|
+
value = has_text ? item['#text'] : item
|
105
|
+
xml.tag!("#{prefix}:#{element.name}", attributes, (value == '' ? nil : value))
|
106
|
+
end
|
98
107
|
end
|
99
108
|
end
|
100
109
|
end
|
@@ -108,7 +117,7 @@ module XSD
|
|
108
117
|
namespace = (element.referenced? ? element.reference : element).target_namespace
|
109
118
|
prefix = namespaces.key(namespace)
|
110
119
|
unless prefix
|
111
|
-
prefix = "
|
120
|
+
prefix = "n#{@namespace_index += 1}"
|
112
121
|
namespaces[prefix] = attributes["xmlns:#{prefix}"] = namespace
|
113
122
|
end
|
114
123
|
|
@@ -119,7 +128,7 @@ module XSD
|
|
119
128
|
# @param [String, Array<String>, nil] lookup
|
120
129
|
def find_root_element(lookup)
|
121
130
|
if lookup
|
122
|
-
element =
|
131
|
+
element = self[*lookup]
|
123
132
|
raise Error, "Cant find start element #{lookup}" unless element.is_a?(Element)
|
124
133
|
|
125
134
|
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.4.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-11-13 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.
|