Skip to content

Commit 828e8ab

Browse files
Added STJ tests for deserializing null values (#3224)
1 parent f938c17 commit 828e8ab

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/StripeTests/Infrastructure/SerializationTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public void Serialize()
4444
JsonConvert.DeserializeObject<Event>(serialized),
4545
Formatting.Indented);
4646
Assert.Equal(serialized, reserialized);
47+
48+
json = GetResourceAsString("api_fixtures.customer_with_expansions.json");
49+
var customer = JsonConvert.DeserializeObject<Customer>(json);
50+
serialized = JsonConvert.SerializeObject(customer, Formatting.Indented);
51+
reserialized = JsonConvert.SerializeObject(
52+
JsonConvert.DeserializeObject<Customer>(serialized),
53+
Formatting.Indented);
54+
Assert.Equal(serialized, reserialized);
4755
}
4856
}
4957
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#if NET6_0_OR_GREATER
2+
namespace StripeTests
3+
{
4+
using System;
5+
using System.Text.Json;
6+
using System.Xml;
7+
using Stripe;
8+
using StripeTests.Infrastructure.TestData;
9+
using Xunit;
10+
11+
public class StjDeserializationTest : BaseStripeTest
12+
{
13+
[Fact]
14+
public void RoundtripDatetime()
15+
{
16+
var date = DateTime.UtcNow;
17+
var roundedDate = date.AddTicks(-1 * (date.Ticks % TimeSpan.TicksPerSecond));
18+
var obj = new TestObjectDateTime
19+
{
20+
Date = roundedDate,
21+
};
22+
23+
var reloaded = JsonSerializer.Deserialize<TestObjectDateTime>(JsonSerializer.Serialize(obj));
24+
Assert.Equal(reloaded.Date, obj.Date);
25+
}
26+
27+
[Fact]
28+
public void HandleNull()
29+
{
30+
var obj = new TestObjectDateTime
31+
{
32+
Date = null,
33+
};
34+
35+
var reloaded = JsonSerializer.Deserialize<TestObjectDateTime>(JsonSerializer.Serialize(obj));
36+
Assert.Null(reloaded.Date);
37+
}
38+
39+
[Fact(Skip="System.Text.JSON Deserialization does not work correctly currently for handling non-nullable null values")]
40+
public void Serialize()
41+
{
42+
var json = GetResourceAsString("api_fixtures.customer_with_expansions.json");
43+
var customer = JsonSerializer.Deserialize<Customer>(json);
44+
var serialized = JsonSerializer.Serialize(customer);
45+
var reserialized = JsonSerializer.Serialize(
46+
JsonSerializer.Deserialize<Customer>(serialized));
47+
Assert.Equal(serialized, reserialized);
48+
}
49+
}
50+
}
51+
#endif

0 commit comments

Comments
 (0)