Skip to content

Commit

Permalink
Update uri decoder for non encoded comma strings (#52)
Browse files Browse the repository at this point in the history
### 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]>
  • Loading branch information
bfrearson and czechboy0 authored Sep 19, 2023
1 parent f4f5963 commit 5f7e7ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ extension URIValueFromNodeDecoder {
array = try rootValue(in: values)
}
guard array.count == 1 else {
if style == .simple {
return Substring(array.joined(separator: ","))
}
let reason = array.isEmpty ? "an empty node" : "a node with multiple values"
try throwMismatch("Cannot parse a value from \(reason).")
}
Expand Down
26 changes: 26 additions & 0 deletions Tests/OpenAPIRuntimeTests/URICoder/Decoder/Test_URIDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,30 @@ final class Test_URIDecoder: Test_Runtime {
XCTAssertEqual(decodedValue, nil)
}
}

func testDecoding_percentEncodedCommaToString() throws {
let decoder = URIDecoder(configuration: .simpleUnexplode)

do {
let decodedValue = try decoder.decode(
String.self,
forKey: "",
from: "foo%2C%20bar"
)
XCTAssertEqual(decodedValue, "foo, bar")
}
}

func testDecoding_nonPercentEncodedCommaToString() throws {
let decoder = URIDecoder(configuration: .simpleUnexplode)

do {
let decodedValue = try decoder.decode(
String.self,
forKey: "",
from: "foo, bar"
)
XCTAssertEqual(decodedValue, "foo, bar")
}
}
}

0 comments on commit 5f7e7ee

Please sign in to comment.