Skip to content

Commit 36e1ad1

Browse files
authored
Allow padding to be omitted from binary metadata values (#2243)
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 36e1ad1

6 files changed

+17
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
{
2-
"mallocCountTotal" : 2000,
3-
"memoryLeaked" : 0,
4-
"releaseCount" : 6001,
5-
"retainCount" : 2000,
6-
"syscalls" : 0
2+
"mallocCountTotal": 1000
73
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
{
2-
"mallocCountTotal" : 2000,
3-
"memoryLeaked" : 0,
4-
"releaseCount" : 6001,
5-
"retainCount" : 2000,
6-
"syscalls" : 0
2+
"mallocCountTotal": 1000
73
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
{
2-
"mallocCountTotal" : 2000,
3-
"memoryLeaked" : 0,
4-
"releaseCount" : 7001,
5-
"retainCount" : 3000,
6-
"syscalls" : 0
2+
"mallocCountTotal": 1000
73
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
{
2-
"mallocCountTotal" : 2000,
3-
"memoryLeaked" : 0,
4-
"releaseCount" : 7001,
5-
"retainCount" : 3000,
6-
"syscalls" : 0
2+
"mallocCountTotal": 1000
73
}

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)