Code Coverage Statistics for Source File
Newtonsoft.Json.Tests\Schema\JsonSchemaGeneratorTests.cs
Symbol Coverage: 97.99% (146 of 149)
Branch Coverage: 95.83% (23 of 24)
Cyclomatic Complexity Avg: 1.08 Max:2
Code Lines: 416
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#region License |
|
2 |
// Copyright (c) 2007 James Newton-King |
|
3 |
// |
|
4 |
// Permission is hereby granted, free of charge, to any person |
|
5 |
// obtaining a copy of this software and associated documentation |
|
6 |
// files (the "Software"), to deal in the Software without |
|
7 |
// restriction, including without limitation the rights to use, |
|
8 |
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
// copies of the Software, and to permit persons to whom the |
|
10 |
// Software is furnished to do so, subject to the following |
|
11 |
// conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be |
|
14 |
// included in all copies or substantial portions of the Software. |
|
15 |
// |
|
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
18 |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
21 |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
23 |
// OTHER DEALINGS IN THE SOFTWARE. |
|
24 |
#endregion |
|
25 |
||
26 |
using System; |
|
27 |
using System.Collections; |
|
28 |
using System.Collections.Generic; |
|
29 |
using System.Globalization; |
|
30 |
using Newtonsoft.Json.Converters; |
|
31 |
using Newtonsoft.Json.Serialization; |
|
32 |
using Newtonsoft.Json.Tests.TestObjects; |
|
33 |
using Newtonsoft.Json.Utilities; |
|
34 |
using NUnit.Framework; |
|
35 |
using Newtonsoft.Json.Schema; |
|
36 |
using System.IO; |
|
37 |
using System.Linq; |
|
38 |
using Newtonsoft.Json.Linq; |
|
39 |
using System.Text; |
|
40 |
using Extensions=Newtonsoft.Json.Schema.Extensions; |
|
41 |
||
42 |
namespace Newtonsoft.Json.Tests.Schema |
|
43 |
{ |
|
44 |
public class JsonSchemaGeneratorTests : TestFixtureBase |
|
45 |
{ |
|
46 |
[Test] |
|
47 |
public void Generate_GenericDictionary() |
|
48 |
{ |
|
49 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
50 |
1 |
JsonSchema schema = generator.Generate(typeof (Dictionary<string, List<string>>));
|
51 |
||
52 |
1 |
string json = schema.ToString();
|
53 |
||
54 |
1 |
Assert.AreEqual(@"{
|
55 |
1 |
""type"": ""object"",
|
56 |
1 |
""additionalProperties"": {
|
57 |
1 |
""type"": [
|
58 |
1 |
""array"",
|
59 |
1 |
""null""
|
60 |
1 |
],
|
61 |
1 |
""items"": {
|
62 |
1 |
""type"": [
|
63 |
1 |
""string"",
|
64 |
1 |
""null""
|
65 |
1 |
]
|
66 |
1 |
}
|
67 |
1 |
}
|
68 |
1 |
}", json);
|
69 |
||
70 |
1 |
Dictionary<string, List<string>> value = new Dictionary<string, List<string>>
|
71 |
1 |
{
|
72 |
1 |
{"HasValue", new List<string>() { "first", "second", null }},
|
73 |
1 |
{"NoValue", null}
|
74 |
1 |
};
|
75 |
||
76 |
1 |
string valueJson = JsonConvert.SerializeObject(value, Formatting.Indented);
|
77 |
1 |
JObject o = JObject.Parse(valueJson);
|
78 |
||
79 |
1 |
Assert.IsTrue(o.IsValid(schema));
|
80 |
1 |
}
|
81 |
||
82 |
#if !PocketPC |
|
83 |
[Test] |
|
84 |
public void Generate_DefaultValueAttributeTestClass() |
|
85 |
{ |
|
86 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
87 |
1 |
JsonSchema schema = generator.Generate(typeof(DefaultValueAttributeTestClass));
|
88 |
||
89 |
1 |
string json = schema.ToString();
|
90 |
||
91 |
1 |
Assert.AreEqual(@"{
|
92 |
1 |
""description"": ""DefaultValueAttributeTestClass description!"",
|
93 |
1 |
""type"": ""object"",
|
94 |
1 |
""additionalProperties"": false,
|
95 |
1 |
""properties"": {
|
96 |
1 |
""TestField1"": {
|
97 |
1 |
""type"": ""integer"",
|
98 |
1 |
""default"": 21
|
99 |
1 |
},
|
100 |
1 |
""TestProperty1"": {
|
101 |
1 |
""type"": [
|
102 |
1 |
""string"",
|
103 |
1 |
""null""
|
104 |
1 |
],
|
105 |
1 |
""default"": ""TestProperty1Value""
|
106 |
1 |
}
|
107 |
1 |
}
|
108 |
1 |
}", json);
|
109 |
1 |
}
|
110 |
#endif |
|
111 |
||
112 |
[Test] |
|
113 |
public void Generate_Person() |
|
114 |
{ |
|
115 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
116 |
1 |
JsonSchema schema = generator.Generate(typeof(Person));
|
117 |
||
118 |
1 |
string json = schema.ToString();
|
119 |
||
120 |
1 |
Assert.AreEqual(@"{
|
121 |
1 |
""id"": ""Person"",
|
122 |
1 |
""title"": ""Title!"",
|
123 |
1 |
""description"": ""JsonObjectAttribute description!"",
|
124 |
1 |
""type"": ""object"",
|
125 |
1 |
""properties"": {
|
126 |
1 |
""Name"": {
|
127 |
1 |
""type"": [
|
128 |
1 |
""string"",
|
129 |
1 |
""null""
|
130 |
1 |
]
|
131 |
1 |
},
|
132 |
1 |
""BirthDate"": {
|
133 |
1 |
""type"": ""string""
|
134 |
1 |
},
|
135 |
1 |
""LastModified"": {
|
136 |
1 |
""type"": ""string""
|
137 |
1 |
}
|
138 |
1 |
}
|
139 |
1 |
}", json);
|
140 |
1 |
}
|
141 |
||
142 |
[Test] |
|
143 |
public void Generate_UserNullable() |
|
144 |
{ |
|
145 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
146 |
1 |
JsonSchema schema = generator.Generate(typeof(UserNullable));
|
147 |
||
148 |
1 |
string json = schema.ToString();
|
149 |
||
150 |
1 |
Assert.AreEqual(@"{
|
151 |
1 |
""type"": ""object"",
|
152 |
1 |
""properties"": {
|
153 |
1 |
""Id"": {
|
154 |
1 |
""type"": ""string""
|
155 |
1 |
},
|
156 |
1 |
""FName"": {
|
157 |
1 |
""type"": [
|
158 |
1 |
""string"",
|
159 |
1 |
""null""
|
160 |
1 |
]
|
161 |
1 |
},
|
162 |
1 |
""LName"": {
|
163 |
1 |
""type"": [
|
164 |
1 |
""string"",
|
165 |
1 |
""null""
|
166 |
1 |
]
|
167 |
1 |
},
|
168 |
1 |
""RoleId"": {
|
169 |
1 |
""type"": ""integer""
|
170 |
1 |
},
|
171 |
1 |
""NullableRoleId"": {
|
172 |
1 |
""type"": [
|
173 |
1 |
""integer"",
|
174 |
1 |
""null""
|
175 |
1 |
]
|
176 |
1 |
},
|
177 |
1 |
""NullRoleId"": {
|
178 |
1 |
""type"": [
|
179 |
1 |
""integer"",
|
180 |
1 |
""null""
|
181 |
1 |
]
|
182 |
1 |
},
|
183 |
1 |
""Active"": {
|
184 |
1 |
""type"": [
|
185 |
1 |
""boolean"",
|
186 |
1 |
""null""
|
187 |
1 |
]
|
188 |
1 |
}
|
189 |
1 |
}
|
190 |
1 |
}", json);
|
191 |
1 |
}
|
192 |
||
193 |
[Test] |
|
194 |
public void Generate_RequiredMembersClass() |
|
195 |
{ |
|
196 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
197 |
1 |
JsonSchema schema = generator.Generate(typeof(RequiredMembersClass));
|
198 |
||
199 |
1 |
Assert.AreEqual(JsonSchemaType.String, schema.Properties["FirstName"].Type);
|
200 |
1 |
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["MiddleName"].Type);
|
201 |
1 |
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["LastName"].Type);
|
202 |
1 |
Assert.AreEqual(JsonSchemaType.String, schema.Properties["BirthDate"].Type);
|
203 |
1 |
}
|
204 |
||
205 |
[Test] |
|
206 |
public void Generate_Store() |
|
207 |
{ |
|
208 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
209 |
1 |
JsonSchema schema = generator.Generate(typeof(Store));
|
210 |
||
211 |
1 |
Assert.AreEqual(11, schema.Properties.Count);
|
212 |
||
213 |
1 |
JsonSchema productArraySchema = schema.Properties["product"];
|
214 |
1 |
JsonSchema productSchema = productArraySchema.Items[0];
|
215 |
||
216 |
1 |
Assert.AreEqual(4, productSchema.Properties.Count);
|
217 |
1 |
}
|
218 |
||
219 |
[Test] |
|
220 |
public void MissingSchemaIdHandlingTest() |
|
221 |
{ |
|
222 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
223 |
||
224 |
1 |
JsonSchema schema = generator.Generate(typeof(Store));
|
225 |
1 |
Assert.AreEqual(null, schema.Id);
|
226 |
||
227 |
1 |
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
228 |
1 |
schema = generator.Generate(typeof (Store));
|
229 |
1 |
Assert.AreEqual(typeof(Store).FullName, schema.Id);
|
230 |
||
231 |
1 |
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseAssemblyQualifiedName;
|
232 |
1 |
schema = generator.Generate(typeof(Store));
|
233 |
1 |
Assert.AreEqual(typeof(Store).AssemblyQualifiedName, schema.Id);
|
234 |
1 |
}
|
235 |
||
236 |
[Test] |
|
237 |
public void Generate_NumberFormatInfo() |
|
238 |
{ |
|
239 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
240 |
1 |
JsonSchema schema = generator.Generate(typeof(NumberFormatInfo));
|
241 |
||
242 |
1 |
string json = schema.ToString();
|
243 |
||
244 |
1 |
Console.WriteLine(json);
|
245 |
||
246 |
// Assert.AreEqual(@"{ |
|
247 |
// ""type"": ""object"", |
|
248 |
// ""additionalProperties"": { |
|
249 |
// ""type"": ""array"", |
|
250 |
// ""items"": { |
|
251 |
// ""type"": ""string"" |
|
252 |
// } |
|
253 |
// } |
|
254 |
//}", json); |
|
255 |
1 |
}
|
256 |
||
257 |
[Test] |
|
258 |
[ExpectedException(typeof(Exception), ExpectedMessage = @"Unresolved circular reference for type 'Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.")] |
|
259 |
public void CircularReferenceError() |
|
260 |
{ |
|
261 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
262 |
1 |
generator.Generate(typeof(CircularReferenceClass));
|
263 |
0 |
}
|
264 |
||
265 |
[Test] |
|
266 |
public void CircularReferenceWithTypeNameId() |
|
267 |
{ |
|
268 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
269 |
1 |
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
270 |
||
271 |
1 |
JsonSchema schema = generator.Generate(typeof(CircularReferenceClass), true);
|
272 |
||
273 |
1 |
Assert.AreEqual(JsonSchemaType.String, schema.Properties["Name"].Type);
|
274 |
1 |
Assert.AreEqual(typeof(CircularReferenceClass).FullName, schema.Id);
|
275 |
1 |
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
|
276 |
1 |
Assert.AreEqual(schema, schema.Properties["Child"]);
|
277 |
1 |
}
|
278 |
||
279 |
[Test] |
|
280 |
public void CircularReferenceWithExplicitId() |
|
281 |
{ |
|
282 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
283 |
||
284 |
1 |
JsonSchema schema = generator.Generate(typeof(CircularReferenceWithIdClass));
|
285 |
||
286 |
1 |
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["Name"].Type);
|
287 |
1 |
Assert.AreEqual("MyExplicitId", schema.Id);
|
288 |
1 |
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
|
289 |
1 |
Assert.AreEqual(schema, schema.Properties["Child"]);
|
290 |
1 |
}
|
291 |
||
292 |
[Test] |
|
293 |
public void GenerateSchemaForType() |
|
294 |
{ |
|
295 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
296 |
1 |
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
297 |
||
298 |
1 |
JsonSchema schema = generator.Generate(typeof(Type));
|
299 |
||
300 |
1 |
Assert.AreEqual(JsonSchemaType.String, schema.Type);
|
301 |
||
302 |
1 |
string json = JsonConvert.SerializeObject(typeof(Version), Formatting.Indented);
|
303 |
||
304 |
1 |
JValue v = new JValue(json);
|
305 |
1 |
Assert.IsTrue(v.IsValid(schema));
|
306 |
1 |
}
|
307 |
||
308 |
#if !SILVERLIGHT && !PocketPC |
|
309 |
[Test] |
|
310 |
public void GenerateSchemaForISerializable() |
|
311 |
{ |
|
312 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
313 |
1 |
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
314 |
||
315 |
1 |
JsonSchema schema = generator.Generate(typeof(Exception));
|
316 |
||
317 |
1 |
Assert.AreEqual(JsonSchemaType.Object, schema.Type);
|
318 |
1 |
Assert.AreEqual(true, schema.AllowAdditionalProperties);
|
319 |
1 |
Assert.AreEqual(null, schema.Properties);
|
320 |
1 |
}
|
321 |
#endif |
|
322 |
||
323 |
[Test] |
|
324 |
public void GenerateSchemaForDBNull() |
|
325 |
{ |
|
326 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
327 |
1 |
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
328 |
||
329 |
1 |
JsonSchema schema = generator.Generate(typeof(DBNull));
|
330 |
||
331 |
1 |
Assert.AreEqual(JsonSchemaType.Null, schema.Type);
|
332 |
1 |
}
|
333 |
||
334 |
public class CustomDirectoryInfoMapper : DefaultContractResolver |
|
335 |
{ |
|
336 |
2 |
public CustomDirectoryInfoMapper()
|
337 |
2 |
: base(true)
|
338 |
{ |
|
339 |
2 |
}
|
340 |
||
341 |
protected override JsonContract CreateContract(Type objectType) |
|
342 |
{ |
|
343 |
5 |
if (objectType == typeof(DirectoryInfo))
|
344 |
1 |
return base.CreateObjectContract(objectType);
|
345 |
||
346 |
4 |
return base.CreateContract(objectType);
|
347 |
5 |
}
|
348 |
||
349 |
protected override IList<JsonProperty> CreateProperties(JsonObjectContract contract) |
|
350 |
{ |
|
351 |
1 |
IList<JsonProperty> properties = base.CreateProperties(contract);
|
352 |
||
353 |
1 |
JsonPropertyCollection c = new JsonPropertyCollection(contract);
|
354 |
1 |
CollectionUtils.AddRange(c, (IEnumerable)properties.Where(m => m.PropertyName != "Root"));
|
355 |
||
356 |
1 |
return c;
|
357 |
1 |
}
|
358 |
} |
|
359 |
||
360 |
[Test] |
|
361 |
public void GenerateSchemaForDirectoryInfo() |
|
362 |
{ |
|
363 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
364 |
1 |
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
365 |
1 |
generator.ContractResolver = new CustomDirectoryInfoMapper();
|
366 |
||
367 |
1 |
JsonSchema schema = generator.Generate(typeof(DirectoryInfo), true);
|
368 |
||
369 |
1 |
string json = schema.ToString();
|
370 |
|
|
371 |
1 |
Assert.AreEqual(@"{
|
372 |
1 |
""id"": ""System.IO.DirectoryInfo"",
|
373 |
1 |
""type"": [
|
374 |
1 |
""object"",
|
375 |
1 |
""null""
|
376 |
1 |
],
|
377 |
1 |
""additionalProperties"": false,
|
378 |
1 |
""properties"": {
|
379 |
1 |
""Name"": {
|
380 |
1 |
""type"": [
|
381 |
1 |
""string"",
|
382 |
1 |
""null""
|
383 |
1 |
]
|
384 |
1 |
},
|
385 |
1 |
""Parent"": {
|
386 |
1 |
""$ref"": ""System.IO.DirectoryInfo""
|
387 |
1 |
},
|
388 |
1 |
""Exists"": {
|
389 |
1 |
""type"": ""boolean""
|
390 |
1 |
},
|
391 |
1 |
""FullName"": {
|
392 |
1 |
""type"": [
|
393 |
1 |
""string"",
|
394 |
1 |
""null""
|
395 |
1 |
]
|
396 |
1 |
},
|
397 |
1 |
""Extension"": {
|
398 |
1 |
""type"": [
|
399 |
1 |
""string"",
|
400 |
1 |
""null""
|
401 |
1 |
]
|
402 |
1 |
},
|
403 |
1 |
""CreationTime"": {
|
404 |
1 |
""type"": ""string""
|
405 |
1 |
},
|
406 |
1 |
""CreationTimeUtc"": {
|
407 |
1 |
""type"": ""string""
|
408 |
1 |
},
|
409 |
1 |
""LastAccessTime"": {
|
410 |
1 |
""type"": ""string""
|
411 |
1 |
},
|
412 |
1 |
""LastAccessTimeUtc"": {
|
413 |
1 |
""type"": ""string""
|
414 |
1 |
},
|
415 |
1 |
""LastWriteTime"": {
|
416 |
1 |
""type"": ""string""
|
417 |
1 |
},
|
418 |
1 |
""LastWriteTimeUtc"": {
|
419 |
1 |
""type"": ""string""
|
420 |
1 |
},
|
421 |
1 |
""Attributes"": {
|
422 |
1 |
""type"": ""integer""
|
423 |
1 |
}
|
424 |
1 |
}
|
425 |
1 |
}", json);
|
426 |
||
427 |
1 |
DirectoryInfo temp = new DirectoryInfo(@"c:\temp");
|
428 |
||
429 |
1 |
JTokenWriter jsonWriter = new JTokenWriter();
|
430 |
1 |
JsonSerializer serializer = new JsonSerializer();
|
431 |
1 |
serializer.Converters.Add(new IsoDateTimeConverter());
|
432 |
1 |
serializer.ContractResolver = new CustomDirectoryInfoMapper();
|
433 |
1 |
serializer.Serialize(jsonWriter, temp);
|
434 |
||
435 |
1 |
List<string> errors = new List<string>();
|
436 |
0 |
jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message)); |
437 |
||
438 |
1 |
Assert.AreEqual(0, errors.Count);
|
439 |
1 |
}
|
440 |
||
441 |
[Test] |
|
442 |
public void GenerateSchemaCamelCase() |
|
443 |
{ |
|
444 |
1 |
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
445 |
1 |
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
446 |
1 |
generator.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
447 |
||
448 |
1 |
JsonSchema schema = generator.Generate(typeof (Version), true);
|
449 |
||
450 |
1 |
string json = schema.ToString();
|
451 |
||
452 |
1 |
Assert.AreEqual(@"{
|
453 |
1 |
""id"": ""System.Version"",
|
454 |
1 |
""type"": [
|
455 |
1 |
""object"",
|
456 |
1 |
""null""
|
457 |
1 |
],
|
458 |
1 |
""additionalProperties"": false,
|
459 |
1 |
""properties"": {
|
460 |
1 |
""major"": {
|
461 |
1 |
""type"": ""integer""
|
462 |
1 |
},
|
463 |
1 |
""minor"": {
|
464 |
1 |
""type"": ""integer""
|
465 |
1 |
},
|
466 |
1 |
""build"": {
|
467 |
1 |
""type"": ""integer""
|
468 |
1 |
},
|
469 |
1 |
""revision"": {
|
470 |
1 |
""type"": ""integer""
|
471 |
1 |
},
|
472 |
1 |
""majorRevision"": {
|
473 |
1 |
""type"": ""integer""
|
474 |
1 |
},
|
475 |
1 |
""minorRevision"": {
|
476 |
1 |
""type"": ""integer""
|
477 |
1 |
}
|
478 |
1 |
}
|
479 |
1 |
}", json);
|
480 |
1 |
}
|
481 |
||
482 |
public enum SortTypeFlag |
|
483 |
{ |
|
484 |
No = 0, |
|
485 |
Asc = 1, |
|
486 |
Desc = -1 |
|
487 |
} |
|
488 |
||
489 |
public class X |
|
490 |
{ |
|
491 |
public SortTypeFlag x; |
|
492 |
} |
|
493 |
||
494 |
[Test] |
|
495 |
public void GenerateSchemaWithNegativeEnum() |
|
496 |
{ |
|
497 |
1 |
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
498 |
1 |
JsonSchema schema = jsonSchemaGenerator.Generate(typeof(X));
|
499 |
||
500 |
1 |
string json = schema.ToString();
|
501 |
||
502 |
1 |
Assert.AreEqual(@"{
|
503 |
1 |
""type"": ""object"",
|
504 |
1 |
""properties"": {
|
505 |
1 |
""x"": {
|
506 |
1 |
""type"": ""integer"",
|
507 |
1 |
""enum"": [
|
508 |
1 |
0,
|
509 |
1 |
1,
|
510 |
1 |
-1
|
511 |
1 |
],
|
512 |
1 |
""options"": [
|
513 |
1 |
{
|
514 |
1 |
""value"": 0,
|
515 |
1 |
""value"": ""No""
|
516 |
1 |
},
|
517 |
1 |
{
|
518 |
1 |
""value"": 1,
|
519 |
1 |
""value"": ""Asc""
|
520 |
1 |
},
|
521 |
1 |
{
|
522 |
1 |
""value"": -1,
|
523 |
1 |
""value"": ""Desc""
|
524 |
1 |
}
|
525 |
1 |
]
|
526 |
1 |
}
|
527 |
1 |
}
|
528 |
1 |
}", json);
|
529 |
1 |
}
|
530 |
||
531 |
[Test] |
|
532 |
public void CircularCollectionReferences() |
|
533 |
{ |
|
534 |
1 |
Type type = typeof (Workspace);
|
535 |
1 |
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
536 |
||
537 |
1 |
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
538 |
1 |
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(type);
|
539 |
||
540 |
// should succeed |
|
541 |
1 |
Assert.IsNotNull(jsonSchema);
|
542 |
1 |
}
|
543 |
||
544 |
[Test] |
|
545 |
public void CircularReferenceWithMixedRequires() |
|
546 |
{ |
|
547 |
1 |
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
548 |
||
549 |
1 |
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
550 |
1 |
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(CircularReferenceClass));
|
551 |
1 |
string json = jsonSchema.ToString();
|
552 |
||
553 |
1 |
Assert.AreEqual(@"{
|
554 |
1 |
""id"": ""Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass"",
|
555 |
1 |
""optional"": true,
|
556 |
1 |
""type"": [
|
557 |
1 |
""object"",
|
558 |
1 |
""null""
|
559 |
1 |
],
|
560 |
1 |
""properties"": {
|
561 |
1 |
""Name"": {
|
562 |
1 |
""type"": ""string""
|
563 |
1 |
},
|
564 |
1 |
""Child"": {
|
565 |
1 |
""$ref"": ""Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass""
|
566 |
1 |
}
|
567 |
1 |
}
|
568 |
1 |
}", json);
|
569 |
1 |
}
|
570 |
||
571 |
[Test] |
|
572 |
public void JsonPropertyWithHandlingValues() |
|
573 |
{ |
|
574 |
1 |
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
575 |
||
576 |
1 |
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
577 |
1 |
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(JsonPropertyWithHandlingValues));
|
578 |
1 |
string json = jsonSchema.ToString();
|
579 |
||
580 |
1 |
Assert.AreEqual(@"{
|
581 |
1 |
""id"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues"",
|
582 |
1 |
""type"": [
|
583 |
1 |
""object"",
|
584 |
1 |
""null""
|
585 |
1 |
],
|
586 |
1 |
""properties"": {
|
587 |
1 |
""DefaultValueHandlingIgnoreProperty"": {
|
588 |
1 |
""optional"": true,
|
589 |
1 |
""type"": [
|
590 |
1 |
""string"",
|
591 |
1 |
""null""
|
592 |
1 |
],
|
593 |
1 |
""default"": ""Default!""
|
594 |
1 |
},
|
595 |
1 |
""DefaultValueHandlingIncludeProperty"": {
|
596 |
1 |
""type"": [
|
597 |
1 |
""string"",
|
598 |
1 |
""null""
|
599 |
1 |
],
|
600 |
1 |
""default"": ""Default!""
|
601 |
1 |
},
|
602 |
1 |
""NullValueHandlingIgnoreProperty"": {
|
603 |
1 |
""optional"": true,
|
604 |
1 |
""type"": [
|
605 |
1 |
""string"",
|
606 |
1 |
""null""
|
607 |
1 |
]
|
608 |
1 |
},
|
609 |
1 |
""NullValueHandlingIncludeProperty"": {
|
610 |
1 |
""type"": [
|
611 |
1 |
""string"",
|
612 |
1 |
""null""
|
613 |
1 |
]
|
614 |
1 |
},
|
615 |
1 |
""ReferenceLoopHandlingErrorProperty"": {
|
616 |
1 |
""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
|
617 |
1 |
},
|
618 |
1 |
""ReferenceLoopHandlingIgnoreProperty"": {
|
619 |
1 |
""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
|
620 |
1 |
},
|
621 |
1 |
""ReferenceLoopHandlingSerializeProperty"": {
|
622 |
1 |
""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
|
623 |
1 |
}
|
624 |
1 |
}
|
625 |
1 |
}", json);
|
626 |
1 |
}
|
627 |
} |
|
628 |
||
629 |
public class DMDSLBase |
|
630 |
{ |
|
631 |
public String Comment; |
|
632 |
} |
|
633 |
||
634 |
public class Workspace : DMDSLBase |
|
635 |
{ |
|
636 |
0 |
public ControlFlowItemCollection Jobs = new ControlFlowItemCollection();
|
637 |
} |
|
638 |
||
639 |
public class ControlFlowItemBase : DMDSLBase |
|
640 |
{ |
|
641 |
public String Name; |
|
642 |
} |
|
643 |
||
644 |
public class ControlFlowItem : ControlFlowItemBase//A Job |
|
645 |
{ |
|
646 |
0 |
public TaskCollection Tasks = new TaskCollection();
|
647 |
0 |
public ContainerCollection Containers = new ContainerCollection();
|
648 |
} |
|
649 |
||
650 |
public class ControlFlowItemCollection : List<ControlFlowItem> |
|
651 |
{ |
|
652 |
} |
|
653 |
||
654 |
public class Task : ControlFlowItemBase |
|
655 |
{ |
|
656 |
0 |
public DataFlowTaskCollection DataFlowTasks = new DataFlowTaskCollection();
|
657 |
0 |
public BulkInsertTaskCollection BulkInsertTask = new BulkInsertTaskCollection();
|
658 |
} |
|
659 |
||
660 |
public class TaskCollection : List<Task> |
|
661 |
{ |
|
662 |
} |
|
663 |
||
664 |
public class Container : ControlFlowItemBase |
|
665 |
{ |
|
666 |
0 |
public ControlFlowItemCollection ContainerJobs = new ControlFlowItemCollection();
|
667 |
} |
|
668 |
||
669 |
public class ContainerCollection : List<Container> |
|
670 |
{ |
|
671 |
} |
|
672 |
||
673 |
public class DataFlowTask_DSL : ControlFlowItemBase |
|
674 |
{ |
|
675 |
} |
|
676 |
||
677 |
public class DataFlowTaskCollection : List<DataFlowTask_DSL> |
|
678 |
{ |
|
679 |
} |
|
680 |
||
681 |
public class SequenceContainer_DSL : Container |
|
682 |
{ |
|
683 |
} |
|
684 |
||
685 |
public class BulkInsertTaskCollection : List<BulkInsertTask_DSL> |
|
686 |
{ |
|
687 |
} |
|
688 |
||
689 |
public class BulkInsertTask_DSL |
|
690 |
{ |
|
691 |
} |
|
692 |
} |