mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
cdp: handle nullable Type for params
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
@@ -112,7 +112,7 @@ fn getWindowForTarget(
|
|||||||
const Params = struct {
|
const Params = struct {
|
||||||
targetId: ?[]const u8 = null,
|
targetId: ?[]const u8 = null,
|
||||||
};
|
};
|
||||||
const input = try Input(Params).get(alloc, msg);
|
const input = try Input(?Params).get(alloc, msg);
|
||||||
defer input.deinit();
|
defer input.deinit();
|
||||||
std.debug.assert(input.sessionId != null);
|
std.debug.assert(input.sessionId != null);
|
||||||
log.debug("Req > id {d}, method {s}", .{ input.id, "browser.getWindowForTarget" });
|
log.debug("Req > id {d}, method {s}", .{ input.id, "browser.getWindowForTarget" });
|
||||||
|
|||||||
@@ -146,7 +146,15 @@ pub const IncomingMessage = struct {
|
|||||||
return error.SkippedParams;
|
return error.SkippedParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
try self.scanUntil("params");
|
self.scanUntil("params") catch |err| {
|
||||||
|
// handle nullable type
|
||||||
|
if (@typeInfo(T) == .Optional) {
|
||||||
|
if (err == error.InvalidToken or err == error.EndOfDocument) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
};
|
||||||
|
|
||||||
// parse "params"
|
// parse "params"
|
||||||
const options = std.json.ParseOptions{
|
const options = std.json.ParseOptions{
|
||||||
@@ -250,3 +258,34 @@ test "read incoming message with null session id" {
|
|||||||
try std.testing.expectEqual(1, try msg.getId());
|
try std.testing.expectEqual(1, try msg.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "message with nullable params" {
|
||||||
|
const T = struct {
|
||||||
|
bar: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
// nullable type, params is present => value
|
||||||
|
const not_null =
|
||||||
|
\\{"id": 1,"method":"foo","params":{"bar":"baz"}}
|
||||||
|
;
|
||||||
|
var msg = IncomingMessage.init(std.testing.allocator, not_null);
|
||||||
|
defer msg.deinit();
|
||||||
|
const input = try Input(?T).get(std.testing.allocator, &msg);
|
||||||
|
defer input.deinit();
|
||||||
|
try std.testing.expectEqualStrings(input.params.?.bar, "baz");
|
||||||
|
|
||||||
|
// nullable type, params is not present => null
|
||||||
|
const is_null =
|
||||||
|
\\{"id": 1,"method":"foo","sessionId":"AAA"}
|
||||||
|
;
|
||||||
|
var msg_null = IncomingMessage.init(std.testing.allocator, is_null);
|
||||||
|
defer msg_null.deinit();
|
||||||
|
const input_null = try Input(?T).get(std.testing.allocator, &msg_null);
|
||||||
|
defer input_null.deinit();
|
||||||
|
try std.testing.expectEqual(null, input_null.params);
|
||||||
|
try std.testing.expectEqualStrings("AAA", input_null.sessionId.?);
|
||||||
|
|
||||||
|
// not nullable type, params is not present => error
|
||||||
|
const params_or_error = msg_null.getParams(std.testing.allocator, T);
|
||||||
|
try std.testing.expectError(error.EndOfDocument, params_or_error);
|
||||||
|
}
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ fn getTargetInfo(
|
|||||||
const Params = struct {
|
const Params = struct {
|
||||||
targetId: ?[]const u8 = null,
|
targetId: ?[]const u8 = null,
|
||||||
};
|
};
|
||||||
const input = try Input(Params).get(alloc, msg);
|
const input = try Input(?Params).get(alloc, msg);
|
||||||
defer input.deinit();
|
defer input.deinit();
|
||||||
log.debug("Req > id {d}, method {s}", .{ input.id, "target.getTargetInfo" });
|
log.debug("Req > id {d}, method {s}", .{ input.id, "target.getTargetInfo" });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user