Skip to content

Commit 7936174

Browse files
committed
Allow padding to be omitted from binary metadata values
Motivation: Binary metadata values are encoded as base64 strings. The gRPC spec doesn't require that the values are padded. Currently gRPC Swift requires values to be padded otherwise decoding will fail. Modifications: - Allow padding characters to be omitted when decoding base64 Result: Can decode unpadded binary metadata values
1 parent 369172a commit 7936174

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Sources/GRPCCore/Metadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ extension Metadata {
423423
switch value {
424424
case .string(let stringValue):
425425
do {
426-
return try Base64.decode(string: stringValue)
426+
return try Base64.decode(string: stringValue, options: [.omitPaddingCharacter])
427427
} catch {
428428
continue
429429
}

Tests/GRPCCoreTests/MetadataTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ struct MetadataTests {
221221
#expect(Array(metadata[binaryValues: "key-bin"]) == expected)
222222
}
223223

224+
@Test("Iterate over unpadded base64 encoded binary values for a key")
225+
@available(gRPCSwift 2.0, *)
226+
func iterateOverUnpaddedBase64BinaryEncodedValuesForKey() {
227+
let metadata: Metadata = [
228+
"key-bin": "YQ==",
229+
"key-bin": "YQ",
230+
]
231+
232+
let expected: [[UInt8]] = [[UInt8(ascii: "a")], [UInt8(ascii: "a")]]
233+
#expect(Array(metadata[binaryValues: "key-bin"]) == expected)
234+
}
235+
224236
@Test("Subscripts are case-insensitive")
225237
@available(gRPCSwift 2.0, *)
226238
func subscriptIsCaseInsensitive() {

0 commit comments

Comments
 (0)