Skip to content

Commit

Permalink
Fix deserialization exception when retrieving cookie with PartitionKe…
Browse files Browse the repository at this point in the history
…y set (#2783)

* Fix incorrect CookieParam.PartitionKey type which caused deserialization exceptions

* Reimplement fix based on upstream solution

* Delete CookiePartitionKey

* remove nullable and bump version

---------

Co-authored-by: Darío Kondratiuk <dariokondratiuk@gmail.com>
  • Loading branch information
AlexCSDev and kblok authored Sep 19, 2024
1 parent a22adb0 commit e14f59d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
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

0 comments on commit e14f59d

Please sign in to comment.