diff --git a/lib/PuppeteerSharp/CookieParam.cs b/lib/PuppeteerSharp/CookieParam.cs index 45bfaed4a..a3739fab0 100644 --- a/lib/PuppeteerSharp/CookieParam.cs +++ b/lib/PuppeteerSharp/CookieParam.cs @@ -1,3 +1,6 @@ +using System.Text.Json.Serialization; +using PuppeteerSharp.Helpers.Json; + namespace PuppeteerSharp { /// @@ -91,7 +94,9 @@ public class CookieParam /// /// 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. /// + [JsonConverter(typeof(CookiePartitionKeyConverter))] public string PartitionKey { get; set; } /// diff --git a/lib/PuppeteerSharp/Helpers/Json/CookiePartitionKeyConverter.cs b/lib/PuppeteerSharp/Helpers/Json/CookiePartitionKeyConverter.cs new file mode 100644 index 000000000..5a4c2584a --- /dev/null +++ b/lib/PuppeteerSharp/Helpers/Json/CookiePartitionKeyConverter.cs @@ -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 + { + /// + public override bool CanConvert(Type objectType) => typeof(string).IsAssignableFrom(objectType); + + /// + public override string? Read( + ref Utf8JsonReader reader, + Type objectType, + JsonSerializerOptions options) + { + JsonNode? node = JsonNode.Parse(ref reader); + + return node?["topLevelSite"]?.GetValue() ?? null; + } + + /// + 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(); + } + } + } +} diff --git a/lib/PuppeteerSharp/PuppeteerSharp.csproj b/lib/PuppeteerSharp/PuppeteerSharp.csproj index 15fe51990..1ce826f32 100644 --- a/lib/PuppeteerSharp/PuppeteerSharp.csproj +++ b/lib/PuppeteerSharp/PuppeteerSharp.csproj @@ -12,10 +12,10 @@ Headless Browser .NET API PuppeteerSharp - 20.0.1-beta1 - 20.0.1 - 20.0.1 - 20.0.1 + 20.0.2 + 20.0.2 + 20.0.2 + 20.0.2 false false embedded