-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
UnsupportedTypesTests.cs
302 lines (247 loc) · 12.9 KB
/
UnsupportedTypesTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.Text.Json.Serialization.Tests
{
public abstract class UnsupportedTypesTests : SerializerTests
{
private bool SupportsJsonPathOnSerialize { get; init; }
public UnsupportedTypesTests(
JsonSerializerWrapper serializerWrapper,
bool supportsJsonPathOnSerialize) : base(serializerWrapper)
{
SupportsJsonPathOnSerialize = supportsJsonPathOnSerialize;
}
[Theory]
[MemberData(nameof(GetUnsupportedValues))]
public async Task DeserializeUnsupportedType<T>(ValueWrapper<T> wrapper)
{
_ = wrapper; // only used to instantiate T
Assert.NotNull(Serializer.GetTypeInfo(typeof(T))); // It should be possible to obtain metadata for the type.
string json = @"""Some string"""; // Any test payload is fine.
Type type = GetNullableOfTUnderlyingType(typeof(T), out bool isNullableOfT);
string fullName = type.FullName;
NotSupportedException ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.DeserializeWrapper<T>(json));
string exAsStr = ex.ToString();
Assert.Contains(fullName, exAsStr);
Assert.Contains("$", exAsStr);
json = $@"{{""Prop"":{json}}}";
ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.DeserializeWrapper<ClassWithType<T>>(json));
exAsStr = ex.ToString();
Assert.Contains(fullName, exAsStr);
Assert.Contains("$.Prop", exAsStr);
// Verify Nullable<> semantics. NSE is not thrown because the serializer handles null.
if (isNullableOfT)
{
Assert.Null(await Serializer.DeserializeWrapper<T>("null"));
json = $@"{{""Prop"":null}}";
ClassWithType<T> obj = await Serializer.DeserializeWrapper<ClassWithType<T>>(json);
Assert.Null(obj.Prop);
}
}
[Theory]
[MemberData(nameof(GetUnsupportedValues))]
public async Task SerializeUnsupportedType<T>(ValueWrapper<T> wrapper)
{
T value = wrapper.value;
Assert.NotNull(Serializer.GetTypeInfo(typeof(T))); // It should be possible to obtain metadata for the type.
Type type = GetNullableOfTUnderlyingType(typeof(T), out bool isNullableOfT);
string fullName = type.FullName;
NotSupportedException ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.SerializeWrapper(value));
string exAsStr = ex.ToString();
Assert.Contains(fullName, exAsStr);
Assert.Contains("$", exAsStr);
ClassWithType<T> obj = new ClassWithType<T> { Prop = value };
ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.SerializeWrapper(obj));
exAsStr = ex.ToString();
Assert.Contains(fullName, exAsStr);
if (SupportsJsonPathOnSerialize)
{
Assert.Contains("$.Prop", exAsStr);
}
else
{
Assert.Contains("$.", exAsStr);
Assert.DoesNotContain("$.Prop", exAsStr);
}
// Verify null semantics. NSE is not thrown because the serializer handles null.
if (!type.IsValueType || isNullableOfT)
{
string serialized = await Serializer.SerializeWrapper<T>((T)(object)null);
Assert.Equal("null", serialized);
obj.Prop = (T)(object)null;
serialized = await Serializer.SerializeWrapper(obj);
Assert.Equal(@"{""Prop"":null}", serialized);
serialized = await Serializer.SerializeWrapper(obj, new JsonSerializerOptions { IgnoreNullValues = true });
Assert.Equal(@"{}", serialized);
}
#if !BUILDING_SOURCE_GENERATOR_TESTS
// The reflection-based serializer will report the runtime type and not the declared type.
fullName = GetNullableOfTUnderlyingType(value.GetType(), out bool _).FullName;
#endif
ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.SerializeWrapper<object>(value));
exAsStr = ex.ToString();
Assert.Contains(fullName, exAsStr);
Assert.Contains("$", exAsStr);
ClassWithType<object> polyObj = new ClassWithType<object> { Prop = value };
ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.SerializeWrapper(polyObj));
exAsStr = ex.ToString();
Assert.Contains(fullName, exAsStr);
}
public static IEnumerable<object[]> GetUnsupportedValues()
{
yield return WrapArgs(new int[,] { { 1, 0 }, { 0, 1 } });
yield return WrapArgs(new bool[,,,] { { { { false } } } });
yield return WrapArgs(typeof(int));
yield return WrapArgs(typeof(ClassWithExtensionProperty).GetConstructor(Array.Empty<Type>()));
yield return WrapArgs(typeof(ClassWithExtensionProperty).GetProperty(nameof(ClassWithExtensionProperty.MyInt)));
yield return WrapArgs(new SerializationInfo(typeof(Type), new FormatterConverter()));
yield return WrapArgs((IntPtr)123);
yield return WrapArgs<IntPtr?>(new IntPtr(123)); // One nullable variation.
yield return WrapArgs((UIntPtr)123);
static object[] WrapArgs<T>(T value) => new object[] { new ValueWrapper<T>(value) };
}
// Helper record used to path both value & type information to generic theories.
// This is needed e.g. when passing System.Type instances whose runtime type
// actually is System.Reflection.RuntimeType.
public record ValueWrapper<T>(T value)
{
public override string ToString() => value.ToString();
}
public class ClassWithIntPtr
{
public IntPtr MyIntPtr { get; set; }
}
public class ClassWithIntPtrConverter
{
[JsonConverter(typeof(IntPtrConverter))]
public IntPtr MyIntPtr { get; set; }
}
public class IntPtrConverter : JsonConverter<IntPtr>
{
public override IntPtr Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
int value = reader.GetInt32();
return new IntPtr(value);
}
public override void Write(Utf8JsonWriter writer, IntPtr value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.ToInt32());
}
}
#if !BUILDING_SOURCE_GENERATOR_TESTS
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp, "NullableContextAttribute is public in corelib in .NET 8+")]
public async Task TypeWithNullConstructorParameterName_ThrowsNotSupportedException()
{
// Regression test for https://github.com/dotnet/runtime/issues/58690
Type type = Assembly.GetExecutingAssembly().GetType("System.Runtime.CompilerServices.NullableContextAttribute")!;
ConstructorInfo ctorInfo = type.GetConstructor(new Type[] { typeof(byte) });
Assert.True(string.IsNullOrEmpty(ctorInfo.GetParameters()[0].Name));
object value = ctorInfo.Invoke(new object[] { (byte)0 });
await Assert.ThrowsAnyAsync<NotSupportedException>(() => Serializer.SerializeWrapper(value));
await Assert.ThrowsAnyAsync<NotSupportedException>(() => Serializer.DeserializeWrapper("{}", type));
}
#endif
[Fact]
public async Task RuntimeConverterIsSupported_IntPtr()
{
const string Json = "{\"MyIntPtr\":42}";
string serialized;
JsonSerializerOptions options = new();
options.Converters.Add(new IntPtrConverter());
serialized = await Serializer.SerializeWrapper(new IntPtr(42), options);
Assert.Equal("42", serialized);
IntPtr intPtr = await Serializer.DeserializeWrapper<IntPtr>("42", options);
Assert.Equal(42, intPtr.ToInt32());
ClassWithIntPtr obj = new() { MyIntPtr = new IntPtr(42) };
serialized = await Serializer.SerializeWrapper(obj, options);
Assert.Equal(Json, serialized);
obj = await Serializer.DeserializeWrapper<ClassWithIntPtr>(Json, options);
Assert.Equal(42, obj.MyIntPtr.ToInt32());
}
[Fact]
public async Task CompileTimeConverterIsSupported_IntPtr()
{
const string Json = "{\"MyIntPtr\":42}";
ClassWithIntPtrConverter obj = new() { MyIntPtr = new IntPtr(42) };
string serialized = await Serializer.SerializeWrapper(obj);
Assert.Equal(Json, serialized);
obj = await Serializer.DeserializeWrapper<ClassWithIntPtrConverter>(Json);
Assert.Equal(42, obj.MyIntPtr.ToInt32());
}
public class ClassWithAsyncEnumerableConverter
{
[JsonConverter(typeof(AsyncEnumerableConverter))]
public ClassThatImplementsIAsyncEnumerable MyAsyncEnumerable { get; set; }
}
public class ClassThatImplementsIAsyncEnumerable : IAsyncEnumerable<int>
{
public string Status { get; set; } = "Created";
// Should not be called.
IAsyncEnumerator<int> IAsyncEnumerable<int>.GetAsyncEnumerator(CancellationToken cancellationToken) => throw new NotImplementedException();
}
public class AsyncEnumerableConverter : JsonConverter<ClassThatImplementsIAsyncEnumerable>
{
public override ClassThatImplementsIAsyncEnumerable Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Assert.Equal(JsonTokenType.StartArray, reader.TokenType);
reader.Read();
Assert.Equal(JsonTokenType.EndArray, reader.TokenType);
return new ClassThatImplementsIAsyncEnumerable { Status = "Read" };
}
public override void Write(Utf8JsonWriter writer, ClassThatImplementsIAsyncEnumerable value, JsonSerializerOptions options)
{
writer.WriteStartArray();
writer.WriteEndArray();
value.Status = "Write";
}
}
[Fact]
public async Task RuntimeConverterIsSupported_AsyncEnumerable()
{
const string Json = "{\"MyAsyncEnumerable\":[]}";
string serialized;
JsonSerializerOptions options = new();
options.Converters.Add(new AsyncEnumerableConverter());
ClassThatImplementsIAsyncEnumerable obj = new();
Assert.Equal("Created", obj.Status);
serialized = await Serializer.SerializeWrapper(obj, options);
Assert.Equal("[]", serialized);
Assert.Equal("Write", obj.Status);
obj = await Serializer.DeserializeWrapper<ClassThatImplementsIAsyncEnumerable>("[]", options);
Assert.Equal("Read", obj.Status);
ClassWithAsyncEnumerableConverter poco = new();
poco.MyAsyncEnumerable = new ClassThatImplementsIAsyncEnumerable();
Assert.Equal("Created", poco.MyAsyncEnumerable.Status);
serialized = await Serializer.SerializeWrapper(poco, options);
Assert.Equal(Json, serialized);
Assert.Equal("Write", poco.MyAsyncEnumerable.Status);
poco = await Serializer.DeserializeWrapper<ClassWithAsyncEnumerableConverter>(Json, options);
Assert.Equal("Read", poco.MyAsyncEnumerable.Status);
}
[Fact]
public async Task CompileTimeConverterIsSupported_AsyncEnumerable()
{
const string Json = "{\"MyAsyncEnumerable\":[]}";
ClassWithAsyncEnumerableConverter obj = new();
obj.MyAsyncEnumerable = new ClassThatImplementsIAsyncEnumerable();
Assert.Equal("Created", obj.MyAsyncEnumerable.Status);
string serialized = await Serializer.SerializeWrapper(obj);
Assert.Equal(Json, serialized);
Assert.Equal("Write", obj.MyAsyncEnumerable.Status);
obj = await Serializer.DeserializeWrapper<ClassWithAsyncEnumerableConverter>(Json);
Assert.Equal("Read", obj.MyAsyncEnumerable.Status);
}
public static Type GetNullableOfTUnderlyingType(Type type, out bool isNullableOfT)
{
isNullableOfT = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
return isNullableOfT ? type.GetGenericArguments()[0] : type;
}
}
}