From 33e014803b4407147d8193b2547eb1981ca9060d Mon Sep 17 00:00:00 2001 From: AlexCSDev <942091+AlexCSDev@users.noreply.github.com> Date: Wed, 18 Sep 2024 22:52:11 +0300 Subject: [PATCH 1/4] Fix incorrect CookieParam.PartitionKey type which caused deserialization exceptions --- lib/PuppeteerSharp/CookieParam.cs | 7 ++++--- lib/PuppeteerSharp/CookiePartitionKey.cs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 lib/PuppeteerSharp/CookiePartitionKey.cs diff --git a/lib/PuppeteerSharp/CookieParam.cs b/lib/PuppeteerSharp/CookieParam.cs index 45bfaed4a..69862e819 100644 --- a/lib/PuppeteerSharp/CookieParam.cs +++ b/lib/PuppeteerSharp/CookieParam.cs @@ -88,11 +88,12 @@ public class CookieParam /// public CookieSourceScheme? SourceScheme { get; set; } +#nullable enable /// - /// 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. + /// Cookie partition key. Supported only in Chrome. /// - public string PartitionKey { get; set; } + public CookiePartitionKey? PartitionKey { get; set; } +#nullable disable /// /// True if cookie partition key is opaque. Supported only in Chrome. diff --git a/lib/PuppeteerSharp/CookiePartitionKey.cs b/lib/PuppeteerSharp/CookiePartitionKey.cs new file mode 100644 index 000000000..5070947ea --- /dev/null +++ b/lib/PuppeteerSharp/CookiePartitionKey.cs @@ -0,0 +1,18 @@ +namespace PuppeteerSharp +{ + /// + /// Cookie's Partition Key data. + /// + public class CookiePartitionKey + { + /// + /// The site of the top-level URL the browser was visiting at the start of the request to the endpoint that set the cookie. + /// + public string TopLevelSite { get; set; } + + /// + /// Indicates if the cookie has any ancestors that are cross-site to the TopLevelSite. + /// + public bool HasCrossSiteAncestor { get; set; } + } +} From 8d945e2b68c6502c1225bf6012089741905ec1eb Mon Sep 17 00:00:00 2001 From: AlexCSDev <942091+AlexCSDev@users.noreply.github.com> Date: Wed, 18 Sep 2024 23:34:57 +0300 Subject: [PATCH 2/4] Reimplement fix based on upstream solution --- lib/PuppeteerSharp/CookieParam.cs | 10 ++++- .../Json/CookiePartitionKeyConverter.cs | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 lib/PuppeteerSharp/Helpers/Json/CookiePartitionKeyConverter.cs diff --git a/lib/PuppeteerSharp/CookieParam.cs b/lib/PuppeteerSharp/CookieParam.cs index 69862e819..419dbfe68 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 { /// @@ -90,9 +93,12 @@ public class CookieParam #nullable enable /// - /// Cookie partition key. Supported only in Chrome. + /// 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. /// - public CookiePartitionKey? PartitionKey { get; set; } + [JsonConverter(typeof(CookiePartitionKeyConverter))] + public string? PartitionKey { get; set; } #nullable disable /// 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(); + } + } + } +} From d2670aaa796b58ea84bd2bdcc95aef3f402238c4 Mon Sep 17 00:00:00 2001 From: AlexCSDev <942091+AlexCSDev@users.noreply.github.com> Date: Wed, 18 Sep 2024 23:43:47 +0300 Subject: [PATCH 3/4] Delete CookiePartitionKey --- lib/PuppeteerSharp/CookiePartitionKey.cs | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 lib/PuppeteerSharp/CookiePartitionKey.cs diff --git a/lib/PuppeteerSharp/CookiePartitionKey.cs b/lib/PuppeteerSharp/CookiePartitionKey.cs deleted file mode 100644 index 5070947ea..000000000 --- a/lib/PuppeteerSharp/CookiePartitionKey.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace PuppeteerSharp -{ - /// - /// Cookie's Partition Key data. - /// - public class CookiePartitionKey - { - /// - /// The site of the top-level URL the browser was visiting at the start of the request to the endpoint that set the cookie. - /// - public string TopLevelSite { get; set; } - - /// - /// Indicates if the cookie has any ancestors that are cross-site to the TopLevelSite. - /// - public bool HasCrossSiteAncestor { get; set; } - } -} From c1c6db415b4f0f940690fb68f541012516240066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Wed, 18 Sep 2024 22:01:49 -0300 Subject: [PATCH 4/4] remove nullable and bump version --- lib/PuppeteerSharp/CookieParam.cs | 4 +--- lib/PuppeteerSharp/PuppeteerSharp.csproj | 8 ++++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/PuppeteerSharp/CookieParam.cs b/lib/PuppeteerSharp/CookieParam.cs index 419dbfe68..a3739fab0 100644 --- a/lib/PuppeteerSharp/CookieParam.cs +++ b/lib/PuppeteerSharp/CookieParam.cs @@ -91,15 +91,13 @@ public class CookieParam /// public CookieSourceScheme? SourceScheme { get; set; } -#nullable enable /// /// 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; } -#nullable disable + public string PartitionKey { get; set; } /// /// True if cookie partition key is opaque. Supported only in Chrome. 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