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

Fix deserialization exception when retrieving cookie with PartitionKey set #2783

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/PuppeteerSharp/CookieParam.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Text.Json.Serialization;
using PuppeteerSharp.Helpers.Json;

namespace PuppeteerSharp
{
/// <summary>
Expand Down Expand Up @@ -91,7 +94,9 @@ public class CookieParam
/// <summary>
/// Cookie partition key. The site of the top-level URL the browser was visiting at the
/// start of the request to the endpoint that set the cookie. Supported only in Chrome.
/// TODO: a breaking change is needed to support other partition keys.
/// </summary>
[JsonConverter(typeof(CookiePartitionKeyConverter))]
public string PartitionKey { get; set; }

/// <summary>
Expand Down
41 changes: 41 additions & 0 deletions lib/PuppeteerSharp/Helpers/Json/CookiePartitionKeyConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#nullable enable

using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace PuppeteerSharp.Helpers.Json
{
internal sealed class CookiePartitionKeyConverter : JsonConverter<string>
{
/// <inheritdoc cref="JsonConverter"/>
public override bool CanConvert(Type objectType) => typeof(string).IsAssignableFrom(objectType);

/// <inheritdoc cref="JsonConverter"/>
public override string? Read(
ref Utf8JsonReader reader,
Type objectType,
JsonSerializerOptions options)
{
JsonNode? node = JsonNode.Parse(ref reader);

return node?["topLevelSite"]?.GetValue<string>() ?? null;
}

/// <inheritdoc cref="JsonConverter"/>
public override void Write(
Utf8JsonWriter writer,
string value,
JsonSerializerOptions options)
{
if (value != null && writer != null)
{
writer.WriteStartObject("partitionKey");
writer.WriteString("topLevelSite", value);
writer.WriteBoolean("hasCrossSiteAncestor", false);
writer.WriteEndObject();
}
}
}
}
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp/PuppeteerSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<Description>Headless Browser .NET API</Description>
<PackageId>PuppeteerSharp</PackageId>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageVersion>20.0.1-beta1</PackageVersion>
<ReleaseVersion>20.0.1</ReleaseVersion>
<AssemblyVersion>20.0.1</AssemblyVersion>
<FileVersion>20.0.1</FileVersion>
<PackageVersion>20.0.2</PackageVersion>
<ReleaseVersion>20.0.2</ReleaseVersion>
<AssemblyVersion>20.0.2</AssemblyVersion>
<FileVersion>20.0.2</FileVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
<DebugType>embedded</DebugType>
Expand Down
Loading