2 // Copyright (c) 2007 James Newton-King
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
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
27 using System.Collections.Generic;
31 using Newtonsoft.Json.Schema;
32 using NUnit.Framework;
34 namespace Newtonsoft.Json.Tests.Schema
36 public class JsonSchemaModelBuilderTests : TestFixtureBase
39 public void ExtendedComplex()
46 ""firstproperty"":{""type"":""string""},
47 ""secondproperty"":{""type"":""string"",""maxLength"":10},
52 ""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
56 ""additionalProperties"":{}
62 ""extends"":{""$ref"":""first""},
65 ""secondproperty"":{""type"":""any""},
70 ""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
72 ""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
77 ""thirdproperty_firstproperty"":{""pattern"":""hi""}
79 ""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
81 ""fourthproperty"":{""type"":""string""}
83 ""additionalProperties"":false
86 JsonSchemaResolver resolver = new JsonSchemaResolver();
87 JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
88 JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
90 JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
92 JsonSchemaModel model = modelBuilder.Build(secondSchema);
94 Assert.AreEqual(4, model.Properties.Count);
96 Assert.AreEqual(JsonSchemaType.String, model.Properties["firstproperty"].Type);
98 Assert.AreEqual(JsonSchemaType.String, model.Properties["secondproperty"].Type);
99 Assert.AreEqual(10, model.Properties["secondproperty"].MaximumLength);
100 Assert.AreEqual(null, model.Properties["secondproperty"].Enum);
101 Assert.AreEqual(null, model.Properties["secondproperty"].Patterns);
103 Assert.AreEqual(JsonSchemaType.Object, model.Properties["thirdproperty"].Type);
104 Assert.AreEqual(3, model.Properties["thirdproperty"].AdditionalProperties.Enum.Count);
105 Assert.AreEqual("two", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[0]);
106 Assert.AreEqual("three", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[1]);
107 Assert.AreEqual("one", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[2]);
109 Assert.AreEqual(JsonSchemaType.String, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Type);
110 Assert.AreEqual(9, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MaximumLength);
111 Assert.AreEqual(7, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MinimumLength);
112 Assert.AreEqual(2, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns.Count);
113 Assert.AreEqual("hi", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[0]);
114 Assert.AreEqual("hi2u", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[1]);
115 Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Properties);
116 Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Items);
117 Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].AdditionalProperties);
121 public void CircularReference()
124 ""id"":""CircularReferenceArray"",
125 ""description"":""CircularReference"",
126 ""type"":[""array""],
127 ""items"":{""$ref"":""CircularReferenceArray""}
130 JsonSchema schema = JsonSchema.Parse(json);
132 JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
134 JsonSchemaModel model = modelBuilder.Build(schema);
136 Assert.AreEqual(JsonSchemaType.Array, model.Type);
138 Assert.AreEqual(model, model.Items[0]);
142 public void Required()
144 string schemaJson = @"{
145 ""description"":""A person"",
149 ""name"":{""type"":""string""},
150 ""hobbies"":{""type"":""string"",required:true},
151 ""age"":{""type"":""integer"",required:true}
155 JsonSchema schema = JsonSchema.Parse(schemaJson);
156 JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
157 JsonSchemaModel model = modelBuilder.Build(schema);
159 Assert.AreEqual(JsonSchemaType.Object, model.Type);
160 Assert.AreEqual(3, model.Properties.Count);
161 Assert.AreEqual(false, model.Properties["name"].Required);
162 Assert.AreEqual(true, model.Properties["hobbies"].Required);
163 Assert.AreEqual(true, model.Properties["age"].Required);