Skip to content

Commit 3b61afe

Browse files
[dotnet] Handle negative zero BiDi response (#15898)
* [dotnet] Handle negative zero BiDi response * Rename `BiDiDoubleConverter` to `SpecialNumberConverter` * Fix merge * alter null check message * Fix special handling * Work around AI limitations * Update dotnet/src/webdriver/BiDi/Communication/Json/Converters/SpecialNumberConverter.cs Co-authored-by: Nikolay Borisenko <[email protected]> --------- Co-authored-by: Nikolay Borisenko <[email protected]>
1 parent 3ce647f commit 3b61afe

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// <copyright file="SpecialNumberConverter.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using System;
21+
using System.Text.Json;
22+
using System.Text.Json.Serialization;
23+
24+
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;
25+
26+
// Serializes and deserializes double into a BiDi spec-compliant number value.
27+
// See https://w3c.github.io/webdriver-bidi/#type-script-PrimitiveProtocolValue
28+
internal sealed class SpecialNumberConverter : JsonConverter<double>
29+
{
30+
public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
31+
{
32+
switch (reader.TokenType)
33+
{
34+
case JsonTokenType.Number:
35+
return reader.GetDouble();
36+
37+
case JsonTokenType.String:
38+
var str = reader.GetString()!;
39+
if (str.Equals("-0", StringComparison.Ordinal))
40+
{
41+
return -0.0;
42+
}
43+
44+
if (str.Equals("NaN", StringComparison.Ordinal))
45+
{
46+
return double.NaN;
47+
}
48+
49+
if (str.Equals("Infinity", StringComparison.Ordinal))
50+
{
51+
return double.PositiveInfinity;
52+
}
53+
54+
if (str.Equals("-Infinity", StringComparison.Ordinal))
55+
{
56+
return double.NegativeInfinity;
57+
}
58+
59+
throw new JsonException($"JSON '{str}' string could not be parsed to a special number");
60+
61+
default:
62+
throw new JsonException($"JSON type not a number or string: {reader.TokenType}");
63+
}
64+
}
65+
66+
public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options)
67+
{
68+
if (double.IsNaN(value))
69+
{
70+
writer.WriteStringValue("NaN");
71+
}
72+
else if (double.IsPositiveInfinity(value))
73+
{
74+
writer.WriteStringValue("Infinity");
75+
}
76+
else if (double.IsNegativeInfinity(value))
77+
{
78+
writer.WriteStringValue("-Infinity");
79+
}
80+
else if (IsNegativeZero(value))
81+
{
82+
writer.WriteStringValue("-0");
83+
}
84+
else
85+
{
86+
writer.WriteNumberValue(value);
87+
}
88+
89+
static bool IsNegativeZero(double x)
90+
{
91+
// Negative zero is less trivial to test, because 0 == -0 is true
92+
// We need to do a bit pattern comparison
93+
94+
return BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(-0.0);
95+
}
96+
}
97+
}

dotnet/src/webdriver/BiDi/Script/LocalValue.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Numerics;
2525
using System.Text.Json.Serialization;
2626
using System.Text.RegularExpressions;
27+
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
2728

2829
namespace OpenQA.Selenium.BiDi.Script;
2930

@@ -282,7 +283,7 @@ private static LocalValue ReflectionBasedConvertFrom(object? value)
282283

283284
public abstract record PrimitiveProtocolLocalValue : LocalValue;
284285

285-
public sealed record NumberLocalValue(double Value) : PrimitiveProtocolLocalValue
286+
public sealed record NumberLocalValue([property: JsonConverter(typeof(SpecialNumberConverter))] double Value) : PrimitiveProtocolLocalValue
286287
{
287288
public static explicit operator NumberLocalValue(double n) => new NumberLocalValue(n);
288289
}

dotnet/src/webdriver/BiDi/Script/RemoteValue.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Numerics;
2424
using System.Text.Json;
2525
using System.Text.Json.Serialization;
26+
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
2627

2728
namespace OpenQA.Selenium.BiDi.Script;
2829

@@ -100,7 +101,7 @@ public abstract record RemoteValue
100101

101102
public abstract record PrimitiveProtocolRemoteValue : RemoteValue;
102103

103-
public sealed record NumberRemoteValue(double Value) : PrimitiveProtocolRemoteValue;
104+
public sealed record NumberRemoteValue([property: JsonConverter(typeof(SpecialNumberConverter))] double Value) : PrimitiveProtocolRemoteValue;
104105

105106
public sealed record BooleanRemoteValue(bool Value) : PrimitiveProtocolRemoteValue;
106107

dotnet/test/common/BiDi/Script/CallFunctionLocalValueTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ public async Task CanCallFunctionWithArgumentNumberZero()
221221
}
222222

223223
[Test]
224-
[IgnoreBrowser(Selenium.Browser.Edge, "Chromium can't handle -0 argument as a number: https://github.com/w3c/webdriver-bidi/issues/887")]
225-
[IgnoreBrowser(Selenium.Browser.Chrome, "Chromium can't handle -0 argument as a number: https://github.com/w3c/webdriver-bidi/issues/887")]
226224
public async Task CanCallFunctionWithArgumentNumberNegativeZero()
227225
{
228226
var arg = new NumberLocalValue(double.NegativeZero);

0 commit comments

Comments
 (0)