Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CookiePartitionKeyConverter doesn't support deserialization of string properly #2825

Open
danbopes opened this issue Nov 17, 2024 · 0 comments

Comments

@danbopes
Copy link

danbopes commented Nov 17, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant