Skip to content

Commit d0eaf65

Browse files
committed
Add WSDL.dump as symmetric counterpart to WSDL.load
WSDL.load(hash) restores a Definition, but there was no top-level method to serialize one. Now there is one besides Definition#to_h.
1 parent e5d654c commit d0eaf65

6 files changed

Lines changed: 89 additions & 6 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ WSDL toolkit for Ruby. Turn WSDL 1.1 documents into inspectable definitions and
1717

1818
## Key Concepts
1919

20-
- **Definition** — Frozen IR of a parsed WSDL service (`lib/wsdl/definition.rb`). Created by `WSDL.parse`, restored by `WSDL.load`. Provides discovery, introspection, provenance, and serialization. Everything downstream operates on the Definition.
20+
- **Definition** — Frozen IR of a parsed WSDL service (`lib/wsdl/definition.rb`). Created by `WSDL.parse`, serialized by `WSDL.dump`, restored by `WSDL.load`. Provides discovery, introspection, provenance, and serialization. Everything downstream operates on the Definition.
2121
- **Client** — Wraps a Definition with an HTTP client for calling operations (`lib/wsdl/client.rb`)
2222
- **Operation** — Callable SOAP operation with body, headers, and security
2323
- **Parser** — Parses WSDL and XSD documents into intermediate structures consumed by the Definition Builder

docs/core/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ When an error is raised due to a strictness check, the error message tells you e
100100

101101
## Caching
102102

103-
The built-in parser cache has been removed. `Definition` is serializable via `to_h`/`to_json` and restorable via `WSDL.load` — cache at the Definition level instead:
103+
The built-in parser cache has been removed. `Definition` is serializable via `WSDL.dump`/`to_h`/`to_json` and restorable via `WSDL.load` — cache at the Definition level instead:
104104

105105
```ruby
106106
# Parse once

lib/wsdl.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,31 @@ def parse(source, http: nil, strictness: nil, sandbox_paths: nil, limits: nil)
109109
)
110110
end
111111

112+
# Serializes a {Definition} to a plain Hash.
113+
#
114+
# The returned hash is suitable for JSON serialization and can be
115+
# restored via {.load}. This is the inverse of {.load}.
116+
#
117+
# @param definition [Definition] the definition to serialize
118+
# @return [Hash{String => Object}] serializable hash with string keys
119+
#
120+
# @example Save to file
121+
# definition = WSDL.parse('http://example.com/service?wsdl')
122+
# File.write('service.json', JSON.generate(WSDL.dump(definition)))
123+
#
124+
# @example Round-trip
125+
# hash = WSDL.dump(definition)
126+
# restored = WSDL.load(hash)
127+
#
128+
def dump(definition)
129+
definition.to_h
130+
end
131+
112132
# Restores a {Definition} from a serialized Hash.
113133
#
114-
# The hash must have been produced by {Definition#to_h} or parsed
115-
# from {Definition#to_json}. Raises if the schema version doesn't
116-
# match the current library version.
134+
# The hash must have been produced by {.dump}, {Definition#to_h},
135+
# or parsed from {Definition#to_json}. Raises if the schema version
136+
# doesn't match the current library version.
117137
#
118138
# @param hash [Hash{String => Object}] serialized definition hash
119139
# @return [Definition] the restored definition

lib/wsdl/definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def resolve_service_and_port
292292
# Serializes this Definition to a plain Hash.
293293
#
294294
# The hash is suitable for JSON serialization and can be restored
295-
# via {WSDL.load} or {.from_h}.
295+
# via {WSDL.load} or {.from_h}. Equivalent to calling {WSDL.dump}.
296296
#
297297
# @return [Hash{String => Object}] serializable hash with string keys
298298
def to_h

spec/wsdl/parse_load_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,44 @@
109109
end
110110
end
111111
end
112+
113+
describe '.dump' do
114+
let(:definition) { described_class.parse(fixture('wsdl/authentication'), http: http_mock) }
115+
116+
it 'returns a serializable hash' do
117+
hash = described_class.dump(definition)
118+
119+
expect(hash).to be_a(Hash)
120+
expect(hash.keys).to all(be_a(String))
121+
end
122+
123+
it 'produces the same hash as Definition#to_h' do
124+
expect(described_class.dump(definition)).to eq(definition.to_h)
125+
end
126+
127+
it 'is the inverse of .load' do
128+
restored = described_class.load(described_class.dump(definition))
129+
130+
expect(restored.service_name).to eq(definition.service_name)
131+
expect(restored.fingerprint).to eq(definition.fingerprint)
132+
expect(restored.to_h).to eq(definition.to_h)
133+
end
134+
135+
it 'round-trips through JSON with .load' do
136+
json = JSON.generate(described_class.dump(definition))
137+
restored = described_class.load(JSON.parse(json))
138+
139+
expect(restored.service_name).to eq(definition.service_name)
140+
expect(restored.fingerprint).to eq(definition.fingerprint)
141+
end
142+
143+
it 'round-trips every standard fixture' do
144+
%w[authentication temperature blz_service bronto].each do |name|
145+
original = described_class.parse(fixture("wsdl/#{name}"), http: http_mock)
146+
restored = described_class.load(described_class.dump(original))
147+
148+
expect(restored.to_h).to eq(original.to_h), "#{name}: dump/load round-trip mismatch"
149+
end
150+
end
151+
end
112152
end

spec/wsdl/wsdl_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,27 @@ def config
7777
}.to raise_error(ArgumentError, /schema version mismatch/)
7878
end
7979
end
80+
81+
describe '.dump' do
82+
let(:definition) { described_class.parse(fixture('wsdl/authentication')) }
83+
84+
it 'serializes a Definition to a hash' do
85+
hash = described_class.dump(definition)
86+
87+
expect(hash).to be_a(Hash)
88+
expect(hash).to have_key('schema_version')
89+
expect(hash).to have_key('service_name')
90+
end
91+
92+
it 'produces the same hash as Definition#to_h' do
93+
expect(described_class.dump(definition)).to eq(definition.to_h)
94+
end
95+
96+
it 'round-trips through load' do
97+
restored = described_class.load(described_class.dump(definition))
98+
99+
expect(restored.service_name).to eq(definition.service_name)
100+
expect(restored.fingerprint).to eq(definition.fingerprint)
101+
end
102+
end
80103
end

0 commit comments

Comments
 (0)