puppeteer-sharp icon indicating copy to clipboard operation
puppeteer-sharp copied to clipboard

CookiePartitionKeyConverter doesn't support deserialization of string properly

Open danbopes opened this issue 1 year ago • 0 comments

Description

The current CookiePartitionKeyConverter doesn't currently support deserialization if the key is already a string. Essentially, trying to serialize these back out to a file, and then read back in again doesn't currently work, because the current writer writes as a string, and the reader will attempt to read an object. This currently works in puppeteer, because it's expecting an object from CDP, but it should still be able to deserialize if the cookies are offloaded to a file/string.

Two simple functions:

private async Task TryLoadCookiesAsync()
{
    if (!File.Exists(_cookieFile))
        return;

    try
    {
        using var fs = File.OpenRead(_cookieFile);
        var currentCookies = JsonSerializer.Deserialize<CookieParam[]>(fs);
        await _page.SetCookieAsync(currentCookies);
    }
    catch (Exception ex)
    {
        _log.Warning(ex, "Exception loading cookies.");
    }
}

private async Task TrySaveCookiesAsync()
{
    try
    {
        // Attempt to get cookies from browser:
        var cookies = await _page.GetCookiesAsync();

        using var fs = File.Create(_cookieFile);
        await JsonSerializer.SerializeAsync(fs, cookies);
    }
    catch (Exception ex)
    {
        _log.Warning(ex, "Exception saving cookies");
    }
}

Expected behavior:

TryLoadCookiesAsync() to be able to be loaded.

Actual behavior:

Throws an exception "The node must be of type 'JsonObject'".

Versions

  • Which version of PuppeteerSharp are you using? 20.0.0.5
  • Which .NET runtime and version are you targeting? .NET 8

danbopes avatar Nov 17 '24 00:11 danbopes