Skip to content

Commit 5f7e7ee

Browse files
bfrearsonczechboy0
andauthored
Update uri decoder for non encoded comma strings (#52)
### Motivation Makes decoding of non-percent encoded strings containing commas more permissive as per discussion in [278](apple/swift-openapi-generator#278). This is not strictly necessary to adhere to the Openapi spec, but encompasses header values that are not percent encoded. ### Modifications Update the simple unexploded decoder to return a single string of comma separated values. ### Result Simple, unexploded nodes containing commas should be decoded correctly. e.g. `foo, bar` should be a single string, rather than an array. ### Test Plan Add tests for non percent encoded comma separated strings and percent encoded strings for additional coverage. --------- Co-authored-by: bfrearson <> Co-authored-by: Honza Dvorsky <[email protected]>
1 parent f4f5963 commit 5f7e7ee

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Sources/OpenAPIRuntime/URICoder/Decoding/URIValueFromNodeDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ extension URIValueFromNodeDecoder {
294294
array = try rootValue(in: values)
295295
}
296296
guard array.count == 1 else {
297+
if style == .simple {
298+
return Substring(array.joined(separator: ","))
299+
}
297300
let reason = array.isEmpty ? "an empty node" : "a node with multiple values"
298301
try throwMismatch("Cannot parse a value from \(reason).")
299302
}

Tests/OpenAPIRuntimeTests/URICoder/Decoder/Test_URIDecoder.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,30 @@ final class Test_URIDecoder: Test_Runtime {
8080
XCTAssertEqual(decodedValue, nil)
8181
}
8282
}
83+
84+
func testDecoding_percentEncodedCommaToString() throws {
85+
let decoder = URIDecoder(configuration: .simpleUnexplode)
86+
87+
do {
88+
let decodedValue = try decoder.decode(
89+
String.self,
90+
forKey: "",
91+
from: "foo%2C%20bar"
92+
)
93+
XCTAssertEqual(decodedValue, "foo, bar")
94+
}
95+
}
96+
97+
func testDecoding_nonPercentEncodedCommaToString() throws {
98+
let decoder = URIDecoder(configuration: .simpleUnexplode)
99+
100+
do {
101+
let decodedValue = try decoder.decode(
102+
String.self,
103+
forKey: "",
104+
from: "foo, bar"
105+
)
106+
XCTAssertEqual(decodedValue, "foo, bar")
107+
}
108+
}
83109
}

0 commit comments

Comments
 (0)