Skip to content

Commit e14f59d

Browse files
AlexCSDevkblok
andauthored
Fix deserialization exception when retrieving cookie with PartitionKey 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 <[email protected]>
1 parent a22adb0 commit e14f59d

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

lib/PuppeteerSharp/CookieParam.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System.Text.Json.Serialization;
2+
using PuppeteerSharp.Helpers.Json;
3+
14
namespace PuppeteerSharp
25
{
36
/// <summary>
@@ -91,7 +94,9 @@ public class CookieParam
9194
/// <summary>
9295
/// Cookie partition key. The site of the top-level URL the browser was visiting at the
9396
/// start of the request to the endpoint that set the cookie. Supported only in Chrome.
97+
/// TODO: a breaking change is needed to support other partition keys.
9498
/// </summary>
99+
[JsonConverter(typeof(CookiePartitionKeyConverter))]
95100
public string PartitionKey { get; set; }
96101

97102
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#nullable enable
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Nodes;
6+
using System.Text.Json.Serialization;
7+
8+
namespace PuppeteerSharp.Helpers.Json
9+
{
10+
internal sealed class CookiePartitionKeyConverter : JsonConverter<string>
11+
{
12+
/// <inheritdoc cref="JsonConverter"/>
13+
public override bool CanConvert(Type objectType) => typeof(string).IsAssignableFrom(objectType);
14+
15+
/// <inheritdoc cref="JsonConverter"/>
16+
public override string? Read(
17+
ref Utf8JsonReader reader,
18+
Type objectType,
19+
JsonSerializerOptions options)
20+
{
21+
JsonNode? node = JsonNode.Parse(ref reader);
22+
23+
return node?["topLevelSite"]?.GetValue<string>() ?? null;
24+
}
25+
26+
/// <inheritdoc cref="JsonConverter"/>
27+
public override void Write(
28+
Utf8JsonWriter writer,
29+
string value,
30+
JsonSerializerOptions options)
31+
{
32+
if (value != null && writer != null)
33+
{
34+
writer.WriteStartObject("partitionKey");
35+
writer.WriteString("topLevelSite", value);
36+
writer.WriteBoolean("hasCrossSiteAncestor", false);
37+
writer.WriteEndObject();
38+
}
39+
}
40+
}
41+
}

lib/PuppeteerSharp/PuppeteerSharp.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
<Description>Headless Browser .NET API</Description>
1313
<PackageId>PuppeteerSharp</PackageId>
1414
<PackageReleaseNotes></PackageReleaseNotes>
15-
<PackageVersion>20.0.1-beta1</PackageVersion>
16-
<ReleaseVersion>20.0.1</ReleaseVersion>
17-
<AssemblyVersion>20.0.1</AssemblyVersion>
18-
<FileVersion>20.0.1</FileVersion>
15+
<PackageVersion>20.0.2</PackageVersion>
16+
<ReleaseVersion>20.0.2</ReleaseVersion>
17+
<AssemblyVersion>20.0.2</AssemblyVersion>
18+
<FileVersion>20.0.2</FileVersion>
1919
<SynchReleaseVersion>false</SynchReleaseVersion>
2020
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
2121
<DebugType>embedded</DebugType>

0 commit comments

Comments
 (0)