Skip to content

Commit a1dbd15

Browse files
authored
Use JSON (de)serializers instead of custom ones for tests (#2162)
In some tests, we were using a custom serializer/deserializer to deal with the `PeerInfo` type. We can just reuse the JSON (de)serialiser we have. Had to copy it over as it was in a different target.
1 parent ebcac53 commit a1dbd15

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

Diff for: Tests/GRPCCoreTests/Test Utilities/Coding+JSON.swift

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import GRPCCore
1718

1819
import struct Foundation.Data

Diff for: Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift

+2-18
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct InProcessTransportTests {
8080
request: ClientRequest(message: ()),
8181
descriptor: .peerInfo,
8282
serializer: VoidSerializer(),
83-
deserializer: PeerInfoDeserializer(),
83+
deserializer: JSONDeserializer<PeerInfo>(),
8484
options: .defaults
8585
) {
8686
try $0.message
@@ -142,7 +142,7 @@ private struct TestService: RegistrableRPCService {
142142
router.registerHandler(
143143
forMethod: .peerInfo,
144144
deserializer: VoidDeserializer(),
145-
serializer: PeerInfoSerializer(),
145+
serializer: JSONSerializer<PeerInfo>(),
146146
handler: {
147147
let response = try await self.peerInfo(
148148
request: ServerRequest<Void>(stream: $0),
@@ -171,22 +171,6 @@ private struct PeerInfo: Codable {
171171
var remote: String
172172
}
173173

174-
private struct PeerInfoSerializer: MessageSerializer {
175-
func serialize<Bytes: GRPCContiguousBytes>(_ message: PeerInfo) throws -> Bytes {
176-
Bytes("\(message.local) \(message.remote)".utf8)
177-
}
178-
}
179-
180-
private struct PeerInfoDeserializer: MessageDeserializer {
181-
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> PeerInfo {
182-
let stringPeerInfo = serializedMessageBytes.withUnsafeBytes {
183-
String(decoding: $0, as: UTF8.self)
184-
}
185-
let peerInfoComponents = stringPeerInfo.split(separator: " ")
186-
return PeerInfo(local: String(peerInfoComponents[0]), remote: String(peerInfoComponents[1]))
187-
}
188-
}
189-
190174
private struct UTF8Serializer: MessageSerializer {
191175
func serialize<Bytes: GRPCContiguousBytes>(_ message: String) throws -> Bytes {
192176
Bytes(message.utf8)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2025, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import GRPCCore
18+
19+
import struct Foundation.Data
20+
import class Foundation.JSONDecoder
21+
import class Foundation.JSONEncoder
22+
23+
struct JSONSerializer<Message: Codable>: MessageSerializer {
24+
func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes {
25+
do {
26+
let jsonEncoder = JSONEncoder()
27+
let data = try jsonEncoder.encode(message)
28+
return Bytes(data)
29+
} catch {
30+
throw RPCError(code: .internalError, message: "Can't serialize message to JSON. \(error)")
31+
}
32+
}
33+
}
34+
35+
struct JSONDeserializer<Message: Codable>: MessageDeserializer {
36+
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message {
37+
do {
38+
let jsonDecoder = JSONDecoder()
39+
let data = serializedMessageBytes.withUnsafeBytes { Data($0) }
40+
return try jsonDecoder.decode(Message.self, from: data)
41+
} catch {
42+
throw RPCError(code: .internalError, message: "Can't deserialze message from JSON. \(error)")
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)