PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Json45r7/Source/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs

https://bitbucket.org/wantstudios/bitbucketclient
C# | 846 lines | 720 code | 103 blank | 23 comment | 15 complexity | 48591c06f002fb24229270160c46658c MD5 | raw file
  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. using System;
  26. using System.Collections;
  27. using System.Collections.Generic;
  28. using System.Globalization;
  29. using Newtonsoft.Json.Converters;
  30. using Newtonsoft.Json.Serialization;
  31. using Newtonsoft.Json.Tests.TestObjects;
  32. using Newtonsoft.Json.Utilities;
  33. #if !NETFX_CORE
  34. using NUnit.Framework;
  35. #else
  36. using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
  37. using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
  38. using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
  39. #endif
  40. using Newtonsoft.Json.Schema;
  41. using System.IO;
  42. using Newtonsoft.Json.Linq;
  43. using System.Text;
  44. using Extensions=Newtonsoft.Json.Schema.Extensions;
  45. #if NET20
  46. using Newtonsoft.Json.Utilities.LinqBridge;
  47. #else
  48. using System.Linq;
  49. #endif
  50. namespace Newtonsoft.Json.Tests.Schema
  51. {
  52. [TestFixture]
  53. public class JsonSchemaGeneratorTests : TestFixtureBase
  54. {
  55. [Test]
  56. public void Generate_GenericDictionary()
  57. {
  58. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  59. JsonSchema schema = generator.Generate(typeof (Dictionary<string, List<string>>));
  60. string json = schema.ToString();
  61. Assert.AreEqual(@"{
  62. ""type"": ""object"",
  63. ""additionalProperties"": {
  64. ""type"": [
  65. ""array"",
  66. ""null""
  67. ],
  68. ""items"": {
  69. ""type"": [
  70. ""string"",
  71. ""null""
  72. ]
  73. }
  74. }
  75. }", json);
  76. Dictionary<string, List<string>> value = new Dictionary<string, List<string>>
  77. {
  78. {"HasValue", new List<string>() { "first", "second", null }},
  79. {"NoValue", null}
  80. };
  81. string valueJson = JsonConvert.SerializeObject(value, Formatting.Indented);
  82. JObject o = JObject.Parse(valueJson);
  83. Assert.IsTrue(o.IsValid(schema));
  84. }
  85. #if !(NETFX_CORE || PORTABLE)
  86. [Test]
  87. public void Generate_DefaultValueAttributeTestClass()
  88. {
  89. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  90. JsonSchema schema = generator.Generate(typeof(DefaultValueAttributeTestClass));
  91. string json = schema.ToString();
  92. Assert.AreEqual(@"{
  93. ""description"": ""DefaultValueAttributeTestClass description!"",
  94. ""type"": ""object"",
  95. ""additionalProperties"": false,
  96. ""properties"": {
  97. ""TestField1"": {
  98. ""required"": true,
  99. ""type"": ""integer"",
  100. ""default"": 21
  101. },
  102. ""TestProperty1"": {
  103. ""required"": true,
  104. ""type"": [
  105. ""string"",
  106. ""null""
  107. ],
  108. ""default"": ""TestProperty1Value""
  109. }
  110. }
  111. }", json);
  112. }
  113. #endif
  114. [Test]
  115. public void Generate_Person()
  116. {
  117. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  118. JsonSchema schema = generator.Generate(typeof(Person));
  119. string json = schema.ToString();
  120. Assert.AreEqual(@"{
  121. ""id"": ""Person"",
  122. ""title"": ""Title!"",
  123. ""description"": ""JsonObjectAttribute description!"",
  124. ""type"": ""object"",
  125. ""properties"": {
  126. ""Name"": {
  127. ""required"": true,
  128. ""type"": [
  129. ""string"",
  130. ""null""
  131. ]
  132. },
  133. ""BirthDate"": {
  134. ""required"": true,
  135. ""type"": ""string""
  136. },
  137. ""LastModified"": {
  138. ""required"": true,
  139. ""type"": ""string""
  140. }
  141. }
  142. }", json);
  143. }
  144. [Test]
  145. public void Generate_UserNullable()
  146. {
  147. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  148. JsonSchema schema = generator.Generate(typeof(UserNullable));
  149. string json = schema.ToString();
  150. Assert.AreEqual(@"{
  151. ""type"": ""object"",
  152. ""properties"": {
  153. ""Id"": {
  154. ""required"": true,
  155. ""type"": ""string""
  156. },
  157. ""FName"": {
  158. ""required"": true,
  159. ""type"": [
  160. ""string"",
  161. ""null""
  162. ]
  163. },
  164. ""LName"": {
  165. ""required"": true,
  166. ""type"": [
  167. ""string"",
  168. ""null""
  169. ]
  170. },
  171. ""RoleId"": {
  172. ""required"": true,
  173. ""type"": ""integer""
  174. },
  175. ""NullableRoleId"": {
  176. ""required"": true,
  177. ""type"": [
  178. ""integer"",
  179. ""null""
  180. ]
  181. },
  182. ""NullRoleId"": {
  183. ""required"": true,
  184. ""type"": [
  185. ""integer"",
  186. ""null""
  187. ]
  188. },
  189. ""Active"": {
  190. ""required"": true,
  191. ""type"": [
  192. ""boolean"",
  193. ""null""
  194. ]
  195. }
  196. }
  197. }", json);
  198. }
  199. [Test]
  200. public void Generate_RequiredMembersClass()
  201. {
  202. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  203. JsonSchema schema = generator.Generate(typeof(RequiredMembersClass));
  204. Assert.AreEqual(JsonSchemaType.String, schema.Properties["FirstName"].Type);
  205. Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["MiddleName"].Type);
  206. Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["LastName"].Type);
  207. Assert.AreEqual(JsonSchemaType.String, schema.Properties["BirthDate"].Type);
  208. }
  209. [Test]
  210. public void Generate_Store()
  211. {
  212. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  213. JsonSchema schema = generator.Generate(typeof(Store));
  214. Assert.AreEqual(11, schema.Properties.Count);
  215. JsonSchema productArraySchema = schema.Properties["product"];
  216. JsonSchema productSchema = productArraySchema.Items[0];
  217. Assert.AreEqual(4, productSchema.Properties.Count);
  218. }
  219. [Test]
  220. public void MissingSchemaIdHandlingTest()
  221. {
  222. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  223. JsonSchema schema = generator.Generate(typeof(Store));
  224. Assert.AreEqual(null, schema.Id);
  225. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  226. schema = generator.Generate(typeof (Store));
  227. Assert.AreEqual(typeof(Store).FullName, schema.Id);
  228. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseAssemblyQualifiedName;
  229. schema = generator.Generate(typeof(Store));
  230. Assert.AreEqual(typeof(Store).AssemblyQualifiedName, schema.Id);
  231. }
  232. [Test]
  233. public void CircularReferenceError()
  234. {
  235. ExceptionAssert.Throws<Exception>(@"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.",
  236. () =>
  237. {
  238. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  239. generator.Generate(typeof(CircularReferenceClass));
  240. });
  241. }
  242. [Test]
  243. public void CircularReferenceWithTypeNameId()
  244. {
  245. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  246. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  247. JsonSchema schema = generator.Generate(typeof(CircularReferenceClass), true);
  248. Assert.AreEqual(JsonSchemaType.String, schema.Properties["Name"].Type);
  249. Assert.AreEqual(typeof(CircularReferenceClass).FullName, schema.Id);
  250. Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
  251. Assert.AreEqual(schema, schema.Properties["Child"]);
  252. }
  253. [Test]
  254. public void CircularReferenceWithExplicitId()
  255. {
  256. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  257. JsonSchema schema = generator.Generate(typeof(CircularReferenceWithIdClass));
  258. Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["Name"].Type);
  259. Assert.AreEqual("MyExplicitId", schema.Id);
  260. Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
  261. Assert.AreEqual(schema, schema.Properties["Child"]);
  262. }
  263. [Test]
  264. public void GenerateSchemaForType()
  265. {
  266. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  267. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  268. JsonSchema schema = generator.Generate(typeof(Type));
  269. Assert.AreEqual(JsonSchemaType.String, schema.Type);
  270. string json = JsonConvert.SerializeObject(typeof(Version), Formatting.Indented);
  271. JValue v = new JValue(json);
  272. Assert.IsTrue(v.IsValid(schema));
  273. }
  274. #if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
  275. [Test]
  276. public void GenerateSchemaForISerializable()
  277. {
  278. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  279. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  280. JsonSchema schema = generator.Generate(typeof(Exception));
  281. Assert.AreEqual(JsonSchemaType.Object, schema.Type);
  282. Assert.AreEqual(true, schema.AllowAdditionalProperties);
  283. Assert.AreEqual(null, schema.Properties);
  284. }
  285. #endif
  286. #if !(NETFX_CORE || PORTABLE)
  287. [Test]
  288. public void GenerateSchemaForDBNull()
  289. {
  290. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  291. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  292. JsonSchema schema = generator.Generate(typeof(DBNull));
  293. Assert.AreEqual(JsonSchemaType.Null, schema.Type);
  294. }
  295. public class CustomDirectoryInfoMapper : DefaultContractResolver
  296. {
  297. public CustomDirectoryInfoMapper()
  298. : base(true)
  299. {
  300. }
  301. protected override JsonContract CreateContract(Type objectType)
  302. {
  303. if (objectType == typeof(DirectoryInfo))
  304. return base.CreateObjectContract(objectType);
  305. return base.CreateContract(objectType);
  306. }
  307. protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
  308. {
  309. IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
  310. JsonPropertyCollection c = new JsonPropertyCollection(type);
  311. CollectionUtils.AddRange(c, (IEnumerable)properties.Where(m => m.PropertyName != "Root"));
  312. return c;
  313. }
  314. }
  315. [Test]
  316. public void GenerateSchemaForDirectoryInfo()
  317. {
  318. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  319. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  320. generator.ContractResolver = new CustomDirectoryInfoMapper
  321. {
  322. #if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
  323. IgnoreSerializableAttribute = true
  324. #endif
  325. };
  326. JsonSchema schema = generator.Generate(typeof(DirectoryInfo), true);
  327. string json = schema.ToString();
  328. Assert.AreEqual(@"{
  329. ""id"": ""System.IO.DirectoryInfo"",
  330. ""required"": true,
  331. ""type"": [
  332. ""object"",
  333. ""null""
  334. ],
  335. ""additionalProperties"": false,
  336. ""properties"": {
  337. ""Name"": {
  338. ""required"": true,
  339. ""type"": [
  340. ""string"",
  341. ""null""
  342. ]
  343. },
  344. ""Parent"": {
  345. ""$ref"": ""System.IO.DirectoryInfo""
  346. },
  347. ""Exists"": {
  348. ""required"": true,
  349. ""type"": ""boolean""
  350. },
  351. ""FullName"": {
  352. ""required"": true,
  353. ""type"": [
  354. ""string"",
  355. ""null""
  356. ]
  357. },
  358. ""Extension"": {
  359. ""required"": true,
  360. ""type"": [
  361. ""string"",
  362. ""null""
  363. ]
  364. },
  365. ""CreationTime"": {
  366. ""required"": true,
  367. ""type"": ""string""
  368. },
  369. ""CreationTimeUtc"": {
  370. ""required"": true,
  371. ""type"": ""string""
  372. },
  373. ""LastAccessTime"": {
  374. ""required"": true,
  375. ""type"": ""string""
  376. },
  377. ""LastAccessTimeUtc"": {
  378. ""required"": true,
  379. ""type"": ""string""
  380. },
  381. ""LastWriteTime"": {
  382. ""required"": true,
  383. ""type"": ""string""
  384. },
  385. ""LastWriteTimeUtc"": {
  386. ""required"": true,
  387. ""type"": ""string""
  388. },
  389. ""Attributes"": {
  390. ""required"": true,
  391. ""type"": ""integer""
  392. }
  393. }
  394. }", json);
  395. DirectoryInfo temp = new DirectoryInfo(@"c:\temp");
  396. JTokenWriter jsonWriter = new JTokenWriter();
  397. JsonSerializer serializer = new JsonSerializer();
  398. serializer.Converters.Add(new IsoDateTimeConverter());
  399. serializer.ContractResolver = new CustomDirectoryInfoMapper
  400. {
  401. #if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
  402. IgnoreSerializableInterface = true
  403. #endif
  404. };
  405. serializer.Serialize(jsonWriter, temp);
  406. List<string> errors = new List<string>();
  407. jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));
  408. Assert.AreEqual(0, errors.Count);
  409. }
  410. #endif
  411. [Test]
  412. public void GenerateSchemaCamelCase()
  413. {
  414. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  415. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  416. generator.ContractResolver = new CamelCasePropertyNamesContractResolver()
  417. {
  418. #if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
  419. IgnoreSerializableAttribute = true
  420. #endif
  421. };
  422. JsonSchema schema = generator.Generate(typeof(Version), true);
  423. string json = schema.ToString();
  424. Assert.AreEqual(@"{
  425. ""id"": ""System.Version"",
  426. ""type"": [
  427. ""object"",
  428. ""null""
  429. ],
  430. ""additionalProperties"": false,
  431. ""properties"": {
  432. ""major"": {
  433. ""required"": true,
  434. ""type"": ""integer""
  435. },
  436. ""minor"": {
  437. ""required"": true,
  438. ""type"": ""integer""
  439. },
  440. ""build"": {
  441. ""required"": true,
  442. ""type"": ""integer""
  443. },
  444. ""revision"": {
  445. ""required"": true,
  446. ""type"": ""integer""
  447. },
  448. ""majorRevision"": {
  449. ""required"": true,
  450. ""type"": ""integer""
  451. },
  452. ""minorRevision"": {
  453. ""required"": true,
  454. ""type"": ""integer""
  455. }
  456. }
  457. }", json);
  458. }
  459. #if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
  460. [Test]
  461. public void GenerateSchemaSerializable()
  462. {
  463. JsonSchemaGenerator generator = new JsonSchemaGenerator();
  464. generator.ContractResolver = new DefaultContractResolver
  465. {
  466. IgnoreSerializableAttribute = false
  467. };
  468. generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  469. JsonSchema schema = generator.Generate(typeof (Version), true);
  470. string json = schema.ToString();
  471. Assert.AreEqual(@"{
  472. ""id"": ""System.Version"",
  473. ""type"": [
  474. ""object"",
  475. ""null""
  476. ],
  477. ""additionalProperties"": false,
  478. ""properties"": {
  479. ""_Major"": {
  480. ""required"": true,
  481. ""type"": ""integer""
  482. },
  483. ""_Minor"": {
  484. ""required"": true,
  485. ""type"": ""integer""
  486. },
  487. ""_Build"": {
  488. ""required"": true,
  489. ""type"": ""integer""
  490. },
  491. ""_Revision"": {
  492. ""required"": true,
  493. ""type"": ""integer""
  494. }
  495. }
  496. }", json);
  497. JTokenWriter jsonWriter = new JTokenWriter();
  498. JsonSerializer serializer = new JsonSerializer();
  499. serializer.ContractResolver = new DefaultContractResolver
  500. {
  501. IgnoreSerializableAttribute = false
  502. };
  503. serializer.Serialize(jsonWriter, new Version(1, 2, 3, 4));
  504. List<string> errors = new List<string>();
  505. jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));
  506. Assert.AreEqual(0, errors.Count);
  507. Assert.AreEqual(@"{
  508. ""_Major"": 1,
  509. ""_Minor"": 2,
  510. ""_Build"": 3,
  511. ""_Revision"": 4
  512. }", jsonWriter.Token.ToString());
  513. Version version = jsonWriter.Token.ToObject<Version>(serializer);
  514. Assert.AreEqual(1, version.Major);
  515. Assert.AreEqual(2, version.Minor);
  516. Assert.AreEqual(3, version.Build);
  517. Assert.AreEqual(4, version.Revision);
  518. }
  519. #endif
  520. public enum SortTypeFlag
  521. {
  522. No = 0,
  523. Asc = 1,
  524. Desc = -1
  525. }
  526. public class X
  527. {
  528. public SortTypeFlag x;
  529. }
  530. [Test]
  531. public void GenerateSchemaWithNegativeEnum()
  532. {
  533. JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
  534. JsonSchema schema = jsonSchemaGenerator.Generate(typeof(X));
  535. string json = schema.ToString();
  536. Assert.AreEqual(@"{
  537. ""type"": ""object"",
  538. ""properties"": {
  539. ""x"": {
  540. ""required"": true,
  541. ""type"": ""integer"",
  542. ""enum"": [
  543. 0,
  544. 1,
  545. -1
  546. ],
  547. ""options"": [
  548. {
  549. ""value"": 0,
  550. ""label"": ""No""
  551. },
  552. {
  553. ""value"": 1,
  554. ""label"": ""Asc""
  555. },
  556. {
  557. ""value"": -1,
  558. ""label"": ""Desc""
  559. }
  560. ]
  561. }
  562. }
  563. }", json);
  564. }
  565. [Test]
  566. public void CircularCollectionReferences()
  567. {
  568. Type type = typeof (Workspace);
  569. JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
  570. jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  571. JsonSchema jsonSchema = jsonSchemaGenerator.Generate(type);
  572. // should succeed
  573. Assert.IsNotNull(jsonSchema);
  574. }
  575. [Test]
  576. public void CircularReferenceWithMixedRequires()
  577. {
  578. JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
  579. jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  580. JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(CircularReferenceClass));
  581. string json = jsonSchema.ToString();
  582. Assert.AreEqual(@"{
  583. ""id"": ""Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass"",
  584. ""type"": [
  585. ""object"",
  586. ""null""
  587. ],
  588. ""properties"": {
  589. ""Name"": {
  590. ""required"": true,
  591. ""type"": ""string""
  592. },
  593. ""Child"": {
  594. ""$ref"": ""Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass""
  595. }
  596. }
  597. }", json);
  598. }
  599. [Test]
  600. public void JsonPropertyWithHandlingValues()
  601. {
  602. JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
  603. jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
  604. JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(JsonPropertyWithHandlingValues));
  605. string json = jsonSchema.ToString();
  606. Assert.AreEqual(@"{
  607. ""id"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues"",
  608. ""required"": true,
  609. ""type"": [
  610. ""object"",
  611. ""null""
  612. ],
  613. ""properties"": {
  614. ""DefaultValueHandlingIgnoreProperty"": {
  615. ""type"": [
  616. ""string"",
  617. ""null""
  618. ],
  619. ""default"": ""Default!""
  620. },
  621. ""DefaultValueHandlingIncludeProperty"": {
  622. ""required"": true,
  623. ""type"": [
  624. ""string"",
  625. ""null""
  626. ],
  627. ""default"": ""Default!""
  628. },
  629. ""DefaultValueHandlingPopulateProperty"": {
  630. ""required"": true,
  631. ""type"": [
  632. ""string"",
  633. ""null""
  634. ],
  635. ""default"": ""Default!""
  636. },
  637. ""DefaultValueHandlingIgnoreAndPopulateProperty"": {
  638. ""type"": [
  639. ""string"",
  640. ""null""
  641. ],
  642. ""default"": ""Default!""
  643. },
  644. ""NullValueHandlingIgnoreProperty"": {
  645. ""type"": [
  646. ""string"",
  647. ""null""
  648. ]
  649. },
  650. ""NullValueHandlingIncludeProperty"": {
  651. ""required"": true,
  652. ""type"": [
  653. ""string"",
  654. ""null""
  655. ]
  656. },
  657. ""ReferenceLoopHandlingErrorProperty"": {
  658. ""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
  659. },
  660. ""ReferenceLoopHandlingIgnoreProperty"": {
  661. ""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
  662. },
  663. ""ReferenceLoopHandlingSerializeProperty"": {
  664. ""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
  665. }
  666. }
  667. }", json);
  668. }
  669. [Test]
  670. public void GenerateForNullableInt32()
  671. {
  672. JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
  673. JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(NullableInt32TestClass));
  674. string json = jsonSchema.ToString();
  675. Assert.AreEqual(@"{
  676. ""type"": ""object"",
  677. ""properties"": {
  678. ""Value"": {
  679. ""required"": true,
  680. ""type"": [
  681. ""integer"",
  682. ""null""
  683. ]
  684. }
  685. }
  686. }", json);
  687. }
  688. }
  689. public class NullableInt32TestClass
  690. {
  691. public int? Value { get; set; }
  692. }
  693. public class DMDSLBase
  694. {
  695. public String Comment;
  696. }
  697. public class Workspace : DMDSLBase
  698. {
  699. public ControlFlowItemCollection Jobs = new ControlFlowItemCollection();
  700. }
  701. public class ControlFlowItemBase : DMDSLBase
  702. {
  703. public String Name;
  704. }
  705. public class ControlFlowItem : ControlFlowItemBase//A Job
  706. {
  707. public TaskCollection Tasks = new TaskCollection();
  708. public ContainerCollection Containers = new ContainerCollection();
  709. }
  710. public class ControlFlowItemCollection : List<ControlFlowItem>
  711. {
  712. }
  713. public class Task : ControlFlowItemBase
  714. {
  715. public DataFlowTaskCollection DataFlowTasks = new DataFlowTaskCollection();
  716. public BulkInsertTaskCollection BulkInsertTask = new BulkInsertTaskCollection();
  717. }
  718. public class TaskCollection : List<Task>
  719. {
  720. }
  721. public class Container : ControlFlowItemBase
  722. {
  723. public ControlFlowItemCollection ContainerJobs = new ControlFlowItemCollection();
  724. }
  725. public class ContainerCollection : List<Container>
  726. {
  727. }
  728. public class DataFlowTask_DSL : ControlFlowItemBase
  729. {
  730. }
  731. public class DataFlowTaskCollection : List<DataFlowTask_DSL>
  732. {
  733. }
  734. public class SequenceContainer_DSL : Container
  735. {
  736. }
  737. public class BulkInsertTaskCollection : List<BulkInsertTask_DSL>
  738. {
  739. }
  740. public class BulkInsertTask_DSL
  741. {
  742. }
  743. }