/Newtonsoft.Json.Tests/Schema/JsonSchemaTests.cs

https://github.com/ayoung/Newtonsoft.Json · C# · 426 lines · 342 code · 62 blank · 22 comment · 0 complexity · 1d9d1d77ce228c11643f2712ead5c93c 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.Generic;
  27. using System.IO;
  28. using System.Linq;
  29. using System.Text;
  30. using Newtonsoft.Json.Schema;
  31. using NUnit.Framework;
  32. namespace Newtonsoft.Json.Tests.Schema
  33. {
  34. public class JsonSchemaTests : TestFixtureBase
  35. {
  36. [Test]
  37. public void Extends()
  38. {
  39. string json;
  40. JsonSchemaResolver resolver = new JsonSchemaResolver();
  41. json = @"{
  42. ""id"":""first"",
  43. ""type"":""object"",
  44. ""additionalProperties"":{}
  45. }";
  46. JsonSchema first = JsonSchema.Parse(json, resolver);
  47. json =
  48. @"{
  49. ""id"":""second"",
  50. ""type"":""object"",
  51. ""extends"":{""$ref"":""first""},
  52. ""additionalProperties"":{""type"":""string""}
  53. }";
  54. JsonSchema second = JsonSchema.Parse(json, resolver);
  55. Assert.AreEqual(first, second.Extends);
  56. json =
  57. @"{
  58. ""id"":""third"",
  59. ""type"":""object"",
  60. ""extends"":{""$ref"":""second""},
  61. ""additionalProperties"":false
  62. }";
  63. JsonSchema third = JsonSchema.Parse(json, resolver);
  64. Assert.AreEqual(second, third.Extends);
  65. Assert.AreEqual(first, third.Extends.Extends);
  66. StringWriter writer = new StringWriter();
  67. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  68. jsonWriter.Formatting = Formatting.Indented;
  69. third.WriteTo(jsonWriter, resolver);
  70. string writtenJson = writer.ToString();
  71. Assert.AreEqual(@"{
  72. ""id"": ""third"",
  73. ""type"": ""object"",
  74. ""additionalProperties"": false,
  75. ""extends"": {
  76. ""$ref"": ""second""
  77. }
  78. }", writtenJson);
  79. StringWriter writer1 = new StringWriter();
  80. JsonTextWriter jsonWriter1 = new JsonTextWriter(writer1);
  81. jsonWriter1.Formatting = Formatting.Indented;
  82. third.WriteTo(jsonWriter1);
  83. writtenJson = writer1.ToString();
  84. Assert.AreEqual(@"{
  85. ""id"": ""third"",
  86. ""type"": ""object"",
  87. ""additionalProperties"": false,
  88. ""extends"": {
  89. ""id"": ""second"",
  90. ""type"": ""object"",
  91. ""additionalProperties"": {
  92. ""type"": ""string""
  93. },
  94. ""extends"": {
  95. ""id"": ""first"",
  96. ""type"": ""object"",
  97. ""additionalProperties"": {}
  98. }
  99. }
  100. }", writtenJson);
  101. }
  102. [Test]
  103. public void WriteTo_AdditionalProperties()
  104. {
  105. StringWriter writer = new StringWriter();
  106. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  107. jsonWriter.Formatting = Formatting.Indented;
  108. JsonSchema schema = JsonSchema.Parse(@"{
  109. ""description"":""AdditionalProperties"",
  110. ""type"":[""string"", ""integer""],
  111. ""additionalProperties"":{""type"":[""object"", ""boolean""]}
  112. }");
  113. schema.WriteTo(jsonWriter);
  114. string json = writer.ToString();
  115. Assert.AreEqual(@"{
  116. ""description"": ""AdditionalProperties"",
  117. ""type"": [
  118. ""string"",
  119. ""integer""
  120. ],
  121. ""additionalProperties"": {
  122. ""type"": [
  123. ""boolean"",
  124. ""object""
  125. ]
  126. }
  127. }", json);
  128. }
  129. [Test]
  130. public void WriteTo_Properties()
  131. {
  132. JsonSchema schema = JsonSchema.Parse(@"{
  133. ""description"":""A person"",
  134. ""type"":""object"",
  135. ""properties"":
  136. {
  137. ""name"":{""type"":""string""},
  138. ""hobbies"":
  139. {
  140. ""type"":""array"",
  141. ""items"": {""type"":""string""}
  142. }
  143. }
  144. }");
  145. StringWriter writer = new StringWriter();
  146. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  147. jsonWriter.Formatting = Formatting.Indented;
  148. schema.WriteTo(jsonWriter);
  149. string json = writer.ToString();
  150. Assert.AreEqual(@"{
  151. ""description"": ""A person"",
  152. ""type"": ""object"",
  153. ""properties"": {
  154. ""name"": {
  155. ""type"": ""string""
  156. },
  157. ""hobbies"": {
  158. ""type"": ""array"",
  159. ""items"": {
  160. ""type"": ""string""
  161. }
  162. }
  163. }
  164. }", json);
  165. }
  166. [Test]
  167. public void WriteTo_Enum()
  168. {
  169. JsonSchema schema = JsonSchema.Parse(@"{
  170. ""description"":""Type"",
  171. ""type"":[""string"",""array""],
  172. ""items"":{},
  173. ""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
  174. }");
  175. StringWriter writer = new StringWriter();
  176. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  177. jsonWriter.Formatting = Formatting.Indented;
  178. schema.WriteTo(jsonWriter);
  179. string json = writer.ToString();
  180. Assert.AreEqual(@"{
  181. ""description"": ""Type"",
  182. ""type"": [
  183. ""string"",
  184. ""array""
  185. ],
  186. ""items"": {},
  187. ""enum"": [
  188. ""string"",
  189. ""object"",
  190. ""array"",
  191. ""boolean"",
  192. ""number"",
  193. ""integer"",
  194. ""null"",
  195. ""any""
  196. ]
  197. }", json);
  198. }
  199. [Test]
  200. public void WriteTo_CircularReference()
  201. {
  202. string json = @"{
  203. ""id"":""CircularReferenceArray"",
  204. ""description"":""CircularReference"",
  205. ""type"":[""array""],
  206. ""items"":{""$ref"":""CircularReferenceArray""}
  207. }";
  208. JsonSchema schema = JsonSchema.Parse(json);
  209. StringWriter writer = new StringWriter();
  210. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  211. jsonWriter.Formatting = Formatting.Indented;
  212. schema.WriteTo(jsonWriter);
  213. string writtenJson = writer.ToString();
  214. Assert.AreEqual(@"{
  215. ""id"": ""CircularReferenceArray"",
  216. ""description"": ""CircularReference"",
  217. ""type"": ""array"",
  218. ""items"": {
  219. ""$ref"": ""CircularReferenceArray""
  220. }
  221. }", writtenJson);
  222. }
  223. [Test]
  224. public void WriteTo_DisallowMultiple()
  225. {
  226. JsonSchema schema = JsonSchema.Parse(@"{
  227. ""description"":""Type"",
  228. ""type"":[""string"",""array""],
  229. ""items"":{},
  230. ""disallow"":[""string"",""object"",""array""]
  231. }");
  232. StringWriter writer = new StringWriter();
  233. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  234. jsonWriter.Formatting = Formatting.Indented;
  235. schema.WriteTo(jsonWriter);
  236. string json = writer.ToString();
  237. Assert.AreEqual(@"{
  238. ""description"": ""Type"",
  239. ""type"": [
  240. ""string"",
  241. ""array""
  242. ],
  243. ""items"": {},
  244. ""disallow"": [
  245. ""string"",
  246. ""object"",
  247. ""array""
  248. ]
  249. }", json);
  250. }
  251. [Test]
  252. public void WriteTo_DisallowSingle()
  253. {
  254. JsonSchema schema = JsonSchema.Parse(@"{
  255. ""description"":""Type"",
  256. ""type"":[""string"",""array""],
  257. ""items"":{},
  258. ""disallow"":""any""
  259. }");
  260. StringWriter writer = new StringWriter();
  261. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  262. jsonWriter.Formatting = Formatting.Indented;
  263. schema.WriteTo(jsonWriter);
  264. string json = writer.ToString();
  265. Assert.AreEqual(@"{
  266. ""description"": ""Type"",
  267. ""type"": [
  268. ""string"",
  269. ""array""
  270. ],
  271. ""items"": {},
  272. ""disallow"": ""any""
  273. }", json);
  274. }
  275. [Test]
  276. public void WriteTo_MultipleItems()
  277. {
  278. JsonSchema schema = JsonSchema.Parse(@"{
  279. ""items"":[{},{}]
  280. }");
  281. StringWriter writer = new StringWriter();
  282. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  283. jsonWriter.Formatting = Formatting.Indented;
  284. schema.WriteTo(jsonWriter);
  285. string json = writer.ToString();
  286. Assert.AreEqual(@"{
  287. ""items"": [
  288. {},
  289. {}
  290. ]
  291. }", json);
  292. }
  293. [Test]
  294. public void ReadOptions()
  295. {
  296. JsonSchema schema = JsonSchema.Parse(@"{
  297. ""type"": ""object"",
  298. ""properties"": {
  299. ""x"": {
  300. ""type"": ""integer"",
  301. ""enum"": [
  302. 0,
  303. 1,
  304. -1
  305. ],
  306. ""options"": [
  307. {
  308. ""value"": 0,
  309. ""label"": ""No""
  310. },
  311. {
  312. ""value"": 1,
  313. ""label"": ""Asc""
  314. },
  315. {
  316. ""value"": -1,
  317. ""label"": ""Desc""
  318. }
  319. ]
  320. }
  321. }
  322. }");
  323. Assert.AreEqual(schema.Properties["x"].Options.Count, 3);
  324. Assert.AreEqual(schema.Properties["x"].Options[0], "No");
  325. Assert.AreEqual(schema.Properties["x"].Options[1], "Asc");
  326. Assert.AreEqual(schema.Properties["x"].Options[-1], "Desc");
  327. }
  328. [Test]
  329. public void WriteTo_ExclusiveMinimum_ExclusiveMaximum()
  330. {
  331. JsonSchema schema = new JsonSchema();
  332. schema.ExclusiveMinimum = true;
  333. schema.ExclusiveMaximum = true;
  334. StringWriter writer = new StringWriter();
  335. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  336. jsonWriter.Formatting = Formatting.Indented;
  337. schema.WriteTo(jsonWriter);
  338. string json = writer.ToString();
  339. Assert.AreEqual(@"{
  340. ""exclusiveMinimum"": true,
  341. ""exclusiveMaximum"": true
  342. }", json);
  343. }
  344. [Test]
  345. public void WriteTo_PatternProperties()
  346. {
  347. JsonSchema schema = new JsonSchema();
  348. schema.PatternProperties = new Dictionary<string, JsonSchema>
  349. {
  350. { "[abc]", new JsonSchema() }
  351. };
  352. StringWriter writer = new StringWriter();
  353. JsonTextWriter jsonWriter = new JsonTextWriter(writer);
  354. jsonWriter.Formatting = Formatting.Indented;
  355. schema.WriteTo(jsonWriter);
  356. string json = writer.ToString();
  357. Assert.AreEqual(@"{
  358. ""patternProperties"": {
  359. ""[abc]"": {}
  360. }
  361. }", json);
  362. }
  363. }
  364. }