Skip to content

Commit ab9733c

Browse files
authored
[dotnet] [bidi] Make cookie expiry as TimeSpan (#16204)
1 parent 3a34d66 commit ab9733c

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ internal Broker(BiDi bidi, Uri url)
8484
new RealmConverter(_bidi),
8585
new RealmTypeConverter(),
8686
new DateTimeOffsetConverter(),
87+
new TimeSpanConverter(),
8788
new PrintPageRangeConverter(),
8889
new InputOriginConverter(),
8990
new WebExtensionConverter(_bidi),

dotnet/src/webdriver/BiDi/Communication/Json/Converters/DateTimeOffsetConverter.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ internal class DateTimeOffsetConverter : JsonConverter<DateTimeOffset>
2727
{
2828
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2929
{
30-
// Workaround: it should be Int64, chrome uses double for `expiry` like "expiry":1737379944.308351
31-
32-
if (reader.TryGetInt64(out long unixTime) is false)
33-
{
34-
var doubleValue = reader.GetDouble();
35-
36-
unixTime = Convert.ToInt64(doubleValue);
37-
}
38-
39-
return DateTimeOffset.FromUnixTimeMilliseconds(unixTime);
30+
return DateTimeOffset.FromUnixTimeMilliseconds(reader.GetInt64());
4031
}
4132

4233
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// <copyright file="TimeSpanConverter.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+
internal class TimeSpanConverter : JsonConverter<TimeSpan>
27+
{
28+
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
29+
{
30+
if (reader.TryGetInt64(out long milliseconds) is false)
31+
{
32+
var doubleValue = reader.GetDouble();
33+
34+
milliseconds = Convert.ToInt64(doubleValue);
35+
}
36+
37+
return TimeSpan.FromMilliseconds(milliseconds);
38+
}
39+
40+
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
41+
{
42+
writer.WriteNumberValue(value.TotalMilliseconds);
43+
}
44+
}

dotnet/src/webdriver/BiDi/Network/Cookie.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Text.Json.Serialization;
2221

2322
namespace OpenQA.Selenium.BiDi.Network;
2423

25-
public sealed record Cookie(string Name, BytesValue Value, string Domain, string Path, long Size, bool HttpOnly, bool Secure, SameSite SameSite)
26-
{
27-
[JsonInclude]
28-
public DateTimeOffset? Expiry { get; internal set; }
29-
}
24+
public sealed record Cookie(string Name, BytesValue Value, string Domain, string Path, long Size, bool HttpOnly, bool Secure, SameSite SameSite, TimeSpan? Expiry);
3025

3126
public enum SameSite
3227
{

0 commit comments

Comments
 (0)