forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeSerializationTests.cs
More file actions
345 lines (294 loc) · 11.9 KB
/
Copy pathRuntimeSerializationTests.cs
File metadata and controls
345 lines (294 loc) · 11.9 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml;
using NUnit.Framework;
using ServiceStack.Auth;
using ServiceStack.Messaging;
using ServiceStack.Templates;
using ServiceStack.Text.Json;
namespace ServiceStack.Text.Tests
{
public class RuntimeObject
{
public object Object { get; set; }
}
public class AType {}
public class JsonType
{
public int Int { get; set; }
public string String { get; set; }
public bool Bool { get; set; }
public string Null { get; set; }
public List<object> List { get; set; }
public Dictionary<string, object> Dictionary { get; set; }
}
[DataContract]
public class DtoType { }
[RuntimeSerializable]
public class RuntimeSerializableType { }
public class MetaType : IMeta
{
public Dictionary<string, string> Meta { get; set; }
}
public class RequestDto : IReturn<RequestDto> {}
#if NETFX
[Serializable]
public class SerialiazableType { }
#endif
public class RuntimeInterface
{
public IObject Object { get; set; }
}
public interface IObject { }
public class AInterface : IObject, IRuntimeSerializable { }
public class RuntimeObjects
{
public object[] Objects { get; set; }
}
public class RuntimeSerializationTests
{
string CreateJson(Type type) => CreateJson(type.AssemblyQualifiedName);
string CreateJson(string typeInfo) => "{\"Object\":{\"__type\":\"" + typeInfo + "\"}}";
[Test]
public void Does_allow_builtin_DataTypes_in_Object()
{
var dto = "{\"Object\":1}".FromJson<RuntimeObject>();
Assert.That(dto.Object, Is.EqualTo("1"));
dto = "{\"Object\":\"foo\"}".FromJson<RuntimeObject>();
Assert.That(dto.Object, Is.EqualTo("foo"));
}
[Test]
public void Does_not_allow_UnknownTypes_in_Object()
{
var types = new[]
{
typeof(AType),
typeof(XmlReaderSettings)
};
foreach (var type in types)
{
var json = CreateJson(type);
try
{
var instance = json.FromJson<RuntimeObject>();
Assert.Fail("Should throw " + type.Name);
}
catch (NotSupportedException ex)
{
ex.Message.Print();
}
}
}
[Test]
public void Can_bypass_RuntimeType_validation()
{
JsConfig.AllowRuntimeType = type => true;
var json = CreateJson(typeof(AType));
var instance = json.FromJson<RuntimeObject>();
Assert.That(instance.Object, Is.TypeOf<AType>());
JsConfig.AllowRuntimeType = null;
}
[Test]
public void Does_Serialize_Allowed_Types()
{
var allowTypes = new[]
{
typeof(DtoType),
typeof(RuntimeSerializableType),
typeof(MetaType),
typeof(RequestDto),
typeof(UserAuth),
typeof(UserAuthDetails),
typeof(Message),
};
foreach (var allowType in allowTypes)
{
var json = CreateJson(allowType);
var instance = json.FromJson<RuntimeObject>();
Assert.That(instance.Object.GetType(), Is.EqualTo(allowType));
}
}
[Test]
public void Does_allow_Unknown_Types_in_Interface()
{
var allowTypes = new[]
{
typeof(AInterface),
};
foreach (var allowType in allowTypes)
{
var json = CreateJson(allowType);
var instance = json.FromJson<RuntimeInterface>();
Assert.That(instance.Object.GetType(), Is.EqualTo(allowType));
}
}
string CreateJsonArray(Type type) => CreateJsonArray(type.AssemblyQualifiedName);
string CreateJsonArray(string typeInfo) => "{\"Objects\":[{\"__type\":\"" + typeInfo + "\"}]}";
[Test]
public void Does_not_allow_UnknownTypes_in_Objects_Array()
{
var types = new[]
{
typeof(AType),
typeof(XmlReaderSettings)
};
foreach (var type in types)
{
var json = CreateJsonArray(type);
try
{
var instance = json.FromJson<RuntimeObjects>();
Assert.Fail("Should throw " + type.Name);
}
catch (NotSupportedException ex)
{
ex.Message.Print();
}
}
}
[Test]
public void Does_Serialize_Allowed_Types_in_Objects_Array()
{
var allowTypes = new[]
{
typeof(DtoType),
typeof(RuntimeSerializableType),
typeof(MetaType),
typeof(RequestDto),
typeof(UserAuth),
typeof(UserAuthDetails),
typeof(Message),
};
foreach (var allowType in allowTypes)
{
var json = CreateJsonArray(allowType);
var instance = json.FromJson<RuntimeObjects>();
Assert.That(instance.Objects.Length, Is.EqualTo(1));
Assert.That(instance.Objects[0].GetType(), Is.EqualTo(allowType));
}
}
[Test]
public void Does_allow_Unknown_Type_in_MQ_Messages()
{
JsConfig.AllowRuntimeTypeInTypes.Add(typeof(Message).FullName);
var mqMessage = new Message<AType> //Internal Type used for ServiceStack MQ
{
Body = new AType()
};
var json = mqMessage.ToJson();
var fromJson = json.FromJson<Message>();
Assert.That(fromJson.Body.GetType(), Is.EqualTo(typeof(AType)));
}
[Test]
public void Does_allow_Unknown_Type_in_RequestLogEntry()
{
JsConfig.AllowRuntimeTypeInTypes.Add(typeof(RequestLogEntry).FullName);
var logEntry = new RequestLogEntry //Internal Type used for ServiceStack LogEntry
{
RequestDto = new AType()
};
var json = logEntry.ToJson();
var fromJson = json.FromJson<RequestLogEntry>();
Assert.That(fromJson.RequestDto.GetType(), Is.EqualTo(typeof(AType)));
}
[Test]
public void Can_deserialize_object_with_unknown_JSON_into_object_type()
{
using (JsConfig.With(new Config { ExcludeTypeInfo = true }))
{
JS.Configure();
var dto = new RuntimeObject
{
Object = new JsonType
{
Int = 1,
String = "foo",
Bool = true,
List = new List<object> { new JsonType{Int = 1, String = "foo", Bool = true} },
Dictionary = new Dictionary<string, object> {{ "key", new JsonType{Int = 1, String = "foo", Bool = true} }},
}
};
var json = dto.ToJson();
Assert.That(json, Is.EqualTo(@"{""Object"":{""Int"":1,""String"":""foo"",""Bool"":true," +
@"""List"":[{""Int"":1,""String"":""foo"",""Bool"":true}],""Dictionary"":{""key"":{""Int"":1,""String"":""foo"",""Bool"":true}}}}"));
// into object
var fromJson = json.FromJson<object>();
var jsonObj = (Dictionary<string, object>)fromJson;
var jsonType = (Dictionary<string, object>)jsonObj["Object"];
Assert.That(jsonType["Int"], Is.EqualTo(1));
Assert.That(jsonType["String"], Is.EqualTo("foo"));
Assert.That(jsonType["Bool"], Is.EqualTo(true));
var jsonList = (List<object>)jsonType["List"];
Assert.That(((Dictionary<string, object>)jsonList[0])["Int"], Is.EqualTo(1));
var jsonDict = (Dictionary<string, object>)jsonType["Dictionary"];
Assert.That(((Dictionary<string, object>)jsonDict["key"])["Int"], Is.EqualTo(1));
// into DTO with Object property
var dtoFromJson = json.FromJson<RuntimeObject>();
jsonType = (Dictionary<string, object>) dtoFromJson.Object;
Assert.That(jsonType["Int"], Is.EqualTo(1));
Assert.That(jsonType["String"], Is.EqualTo("foo"));
Assert.That(jsonType["Bool"], Is.EqualTo(true));
jsonList = (List<object>)jsonType["List"];
Assert.That(((Dictionary<string, object>)jsonList[0])["Int"], Is.EqualTo(1));
jsonDict = (Dictionary<string, object>)jsonType["Dictionary"];
Assert.That(((Dictionary<string, object>)jsonDict["key"])["Int"], Is.EqualTo(1));
JS.UnConfigure();
}
}
[Test]
public void Can_serialize_JS_literal_into_DTO()
{
JS.Configure();
var js = @"{""Object"":{ Int:1,String:'foo',Bool:true,List:[{Int:1,String:`foo`,Bool:true}],Dictionary:{key:{Int:1,String:""foo"",Bool:true}}}}";
// into DTO with Object property
var dtoFromJson = js.FromJson<RuntimeObject>();
var jsonType = (Dictionary<string, object>)dtoFromJson.Object;
Assert.That(jsonType["Int"], Is.EqualTo(1));
Assert.That(jsonType["Int"], Is.EqualTo(1));
Assert.That(jsonType["String"], Is.EqualTo("foo"));
Assert.That(jsonType["Bool"], Is.EqualTo(true));
var jsonList = (List<object>)jsonType["List"];
Assert.That(((Dictionary<string, object>)jsonList[0])["Int"], Is.EqualTo(1));
var jsonDict = (Dictionary<string, object>)jsonType["Dictionary"];
Assert.That(((Dictionary<string, object>)jsonDict["key"])["Int"], Is.EqualTo(1));
JS.UnConfigure();
}
[Test]
public void ServiceStack_AllowRuntimeType()
{
// Initialize static delegate to allow all types to be deserialized with the type attribute
JsConfig.AllowRuntimeType = _ => true;
JsConfig.TypeAttr = "$type";
var example = new Example { Property = new MyProperty { Value = "Hello serializer" } };
var serialized = JsonSerializer.SerializeToString(example);
var deserialized = JsonSerializer.DeserializeFromString<Example>(serialized);
Assert.IsNotNull(deserialized?.Property);
// Now the same process with a config scope that has a TypeAttr that differs from the global TypeAttr value
using var scope = JsConfig.With(new Config { TypeAttr = "_type" });
serialized = JsonSerializer.SerializeToString(example);
deserialized = JsonSerializer.DeserializeFromString<Example>(serialized);
Assert.IsNotNull(deserialized?.Property);
JsConfig.Reset();
}
private class Example
{
public IProperty Property { get; set; }
}
private interface IProperty
{
}
private class MyProperty : IProperty
{
public string Value { get; set; }
}
[Test]
public void Can_deserialize_object_into_string_dictionary()
{
var json = "{\"__type\":\"System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib],[System.String, System.Private.CoreLib]], System.Private.CoreLib\",\"Key\":\"A\",\"Value\":\"B\"}";
var dto = json.FromJson<KeyValuePair<string, string>>();
Assert.That(dto.Key, Is.EqualTo("A"));
Assert.That(dto.Value, Is.EqualTo("B"));
}
}
}