Skip to content

Commit 6b691d3

Browse files
committed
cherry-pick(release-1.41): fix: rewrite json channel converters
PR: #2831
1 parent 6c57fcc commit 6b691d3

File tree

3 files changed

+23
-63
lines changed

3 files changed

+23
-63
lines changed

src/Playwright/Transport/Connection.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ public Connection(LocalUtils localUtils = null)
5858
JsonSerializerOptions NewJsonSerializerOptions(bool keepNulls)
5959
{
6060
var options = JsonExtensions.GetNewDefaultSerializerOptions(keepNulls);
61-
options.Converters.Add(new ChannelToGuidConverter(this));
62-
options.Converters.Add(new ChannelOwnerToGuidConverter<JSHandle>(this));
63-
options.Converters.Add(new ChannelOwnerToGuidConverter<ElementHandle>(this));
6461

62+
// Workaround for https://github.com/dotnet/runtime/issues/46522
63+
options.Converters.Add(new ChannelOwnerConverterFactory(this));
6564
// Workaround for https://github.com/dotnet/runtime/issues/46522
6665
options.Converters.Add(new ChannelOwnerListToGuidListConverter<WritableStream>(this));
6766
return options;

src/Playwright/Transport/Converters/ChannelOwnerToGuidConverter.cs src/Playwright/Transport/Converters/ChannelOwnerConverterFactory.cs

+21-5
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,32 @@
2323
*/
2424

2525
using System;
26+
using System.Runtime.InteropServices.ComTypes;
2627
using System.Text.Json;
2728
using System.Text.Json.Serialization;
2829

2930
namespace Microsoft.Playwright.Transport.Converters;
3031

31-
internal class ChannelOwnerToGuidConverter<T>
32-
: JsonConverter<T>
32+
internal class ChannelOwnerConverterFactory : JsonConverterFactory
33+
{
34+
private readonly Connection _connection;
35+
36+
public ChannelOwnerConverterFactory(Connection connection)
37+
{
38+
_connection = connection;
39+
}
40+
41+
public override bool CanConvert(Type typeToConvert) => typeof(ChannelOwner).IsAssignableFrom(typeToConvert);
42+
43+
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
44+
{
45+
return (JsonConverter)Activator.CreateInstance(
46+
typeof(ChannelOwnerToGuidConverter<>).MakeGenericType(new Type[] { typeToConvert }),
47+
new object[] { _connection });
48+
}
49+
}
50+
51+
internal class ChannelOwnerToGuidConverter<T> : JsonConverter<T>
3352
where T : ChannelOwner
3453
{
3554
private readonly Connection _connection;
@@ -39,9 +58,6 @@ public ChannelOwnerToGuidConverter(Connection connection)
3958
_connection = connection;
4059
}
4160

42-
public override bool CanConvert(Type type)
43-
=> (typeof(T) == typeof(ChannelOwner) && typeof(T).IsAssignableFrom(type)) || type == typeof(T);
44-
4561
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
4662
{
4763
using JsonDocument document = JsonDocument.ParseValue(ref reader);

src/Playwright/Transport/Converters/ChannelToGuidConverter.cs

-55
This file was deleted.

0 commit comments

Comments
 (0)