Skip to content

Commit 53dd0a5

Browse files
cdp: handle nullable Type for params
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 4b8c3cb commit 53dd0a5

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/cdp/browser.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn getWindowForTarget(
112112
const Params = struct {
113113
targetId: ?[]const u8 = null,
114114
};
115-
const input = try Input(Params).get(alloc, msg);
115+
const input = try Input(?Params).get(alloc, msg);
116116
defer input.deinit();
117117
std.debug.assert(input.sessionId != null);
118118
log.debug("Req > id {d}, method {s}", .{ input.id, "browser.getWindowForTarget" });

src/cdp/msg.zig

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,15 @@ pub const IncomingMessage = struct {
146146
return error.SkippedParams;
147147
}
148148

149-
try self.scanUntil("params");
149+
self.scanUntil("params") catch |err| {
150+
// handle nullable type
151+
if (@typeInfo(T) == .Optional) {
152+
if (err == error.InvalidToken or err == error.EndOfDocument) {
153+
return null;
154+
}
155+
}
156+
return err;
157+
};
150158

151159
// parse "params"
152160
const options = std.json.ParseOptions{
@@ -250,3 +258,34 @@ test "read incoming message with null session id" {
250258
try std.testing.expectEqual(1, try msg.getId());
251259
}
252260
}
261+
262+
test "message with nullable params" {
263+
const T = struct {
264+
bar: []const u8,
265+
};
266+
267+
// nullable type, params is present => value
268+
const not_null =
269+
\\{"id": 1,"method":"foo","params":{"bar":"baz"}}
270+
;
271+
var msg = IncomingMessage.init(std.testing.allocator, not_null);
272+
defer msg.deinit();
273+
const input = try Input(?T).get(std.testing.allocator, &msg);
274+
defer input.deinit();
275+
try std.testing.expectEqualStrings(input.params.?.bar, "baz");
276+
277+
// nullable type, params is not present => null
278+
const is_null =
279+
\\{"id": 1,"method":"foo","sessionId":"AAA"}
280+
;
281+
var msg_null = IncomingMessage.init(std.testing.allocator, is_null);
282+
defer msg_null.deinit();
283+
const input_null = try Input(?T).get(std.testing.allocator, &msg_null);
284+
defer input_null.deinit();
285+
try std.testing.expectEqual(null, input_null.params);
286+
try std.testing.expectEqualStrings("AAA", input_null.sessionId.?);
287+
288+
// not nullable type, params is not present => error
289+
const params_or_error = msg_null.getParams(std.testing.allocator, T);
290+
try std.testing.expectError(error.EndOfDocument, params_or_error);
291+
}

src/cdp/target.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn getTargetInfo(
184184
const Params = struct {
185185
targetId: ?[]const u8 = null,
186186
};
187-
const input = try Input(Params).get(alloc, msg);
187+
const input = try Input(?Params).get(alloc, msg);
188188
defer input.deinit();
189189
log.debug("Req > id {d}, method {s}", .{ input.id, "target.getTargetInfo" });
190190

0 commit comments

Comments
 (0)