-Code reformat
This commit is contained in:
@@ -38,100 +38,101 @@ using System.IO;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
#if !(SILVERLIGHT || NETFX_CORE)
|
||||
using System.Data;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Schema
|
||||
{
|
||||
[TestFixture]
|
||||
public class ExtensionsTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void IsValid()
|
||||
[TestFixture]
|
||||
public class ExtensionsTests : TestFixtureBase
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'type':'integer'}");
|
||||
JToken stringToken = JToken.FromObject("pie");
|
||||
JToken integerToken = JToken.FromObject(1);
|
||||
[Test]
|
||||
public void IsValid()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'type':'integer'}");
|
||||
JToken stringToken = JToken.FromObject("pie");
|
||||
JToken integerToken = JToken.FromObject(1);
|
||||
|
||||
IList<string> errorMessages;
|
||||
Assert.AreEqual(true, integerToken.IsValid(schema));
|
||||
Assert.AreEqual(true, integerToken.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(0, errorMessages.Count);
|
||||
IList<string> errorMessages;
|
||||
Assert.AreEqual(true, integerToken.IsValid(schema));
|
||||
Assert.AreEqual(true, integerToken.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(0, errorMessages.Count);
|
||||
|
||||
Assert.AreEqual(false, stringToken.IsValid(schema));
|
||||
Assert.AreEqual(false, stringToken.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(1, errorMessages.Count);
|
||||
Assert.AreEqual("Invalid type. Expected Integer but got String.", errorMessages[0]);
|
||||
}
|
||||
Assert.AreEqual(false, stringToken.IsValid(schema));
|
||||
Assert.AreEqual(false, stringToken.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(1, errorMessages.Count);
|
||||
Assert.AreEqual("Invalid type. Expected Integer but got String.", errorMessages[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValidateWithEventHandler()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'pattern':'lol'}");
|
||||
JToken stringToken = JToken.FromObject("pie lol");
|
||||
[Test]
|
||||
public void ValidateWithEventHandler()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'pattern':'lol'}");
|
||||
JToken stringToken = JToken.FromObject("pie lol");
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
stringToken.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
Assert.AreEqual(0, errors.Count);
|
||||
List<string> errors = new List<string>();
|
||||
stringToken.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
Assert.AreEqual(0, errors.Count);
|
||||
|
||||
stringToken = JToken.FromObject("pie");
|
||||
stringToken = JToken.FromObject("pie");
|
||||
|
||||
stringToken.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
stringToken.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
|
||||
Assert.AreEqual("String 'pie' does not match regex pattern 'lol'.", errors[0]);
|
||||
}
|
||||
Assert.AreEqual("String 'pie' does not match regex pattern 'lol'.", errors[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValidateWithOutEventHandlerFailure()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>(@"String 'pie' does not match regex pattern 'lol'.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'pattern':'lol'}");
|
||||
JToken stringToken = JToken.FromObject("pie");
|
||||
stringToken.Validate(schema);
|
||||
});
|
||||
}
|
||||
[Test]
|
||||
public void ValidateWithOutEventHandlerFailure()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>(@"String 'pie' does not match regex pattern 'lol'.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'pattern':'lol'}");
|
||||
JToken stringToken = JToken.FromObject("pie");
|
||||
stringToken.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValidateWithOutEventHandlerSuccess()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'pattern':'lol'}");
|
||||
JToken stringToken = JToken.FromObject("pie lol");
|
||||
stringToken.Validate(schema);
|
||||
}
|
||||
[Test]
|
||||
public void ValidateWithOutEventHandlerSuccess()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'pattern':'lol'}");
|
||||
JToken stringToken = JToken.FromObject("pie lol");
|
||||
stringToken.Validate(schema);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValidateFailureWithOutLineInfoBecauseOfEndToken()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'properties':{'lol':{'required':true}}}");
|
||||
JObject o = JObject.Parse("{}");
|
||||
[Test]
|
||||
public void ValidateFailureWithOutLineInfoBecauseOfEndToken()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'properties':{'lol':{'required':true}}}");
|
||||
JObject o = JObject.Parse("{}");
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
o.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
List<string> errors = new List<string>();
|
||||
o.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
|
||||
Assert.AreEqual("Required properties are missing from object: lol.", errors[0]);
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
}
|
||||
Assert.AreEqual("Required properties are missing from object: lol.", errors[0]);
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValidateFailureWithLineInfo()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'properties':{'lol':{'type':'string'}}}");
|
||||
JObject o = JObject.Parse("{'lol':1}");
|
||||
[Test]
|
||||
public void ValidateFailureWithLineInfo()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse("{'properties':{'lol':{'type':'string'}}}");
|
||||
JObject o = JObject.Parse("{'lol':1}");
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
o.Validate(schema, (sender, args) => errors.Add(args.Path + " - " + args.Message));
|
||||
List<string> errors = new List<string>();
|
||||
o.Validate(schema, (sender, args) => errors.Add(args.Path + " - " + args.Message));
|
||||
|
||||
Assert.AreEqual("lol - Invalid type. Expected String but got Integer. Line 1, position 8.", errors[0]);
|
||||
Assert.AreEqual("1", o.SelectToken("lol").ToString());
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
}
|
||||
Assert.AreEqual("lol - Invalid type. Expected String but got Integer. Line 1, position 8.", errors[0]);
|
||||
Assert.AreEqual("1", o.SelectToken("lol").ToString());
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Blog()
|
||||
{
|
||||
string schemaJson = @"
|
||||
[Test]
|
||||
public void Blog()
|
||||
{
|
||||
string schemaJson = @"
|
||||
{
|
||||
""description"": ""A person schema"",
|
||||
""type"": ""object"",
|
||||
@@ -146,80 +147,77 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
";
|
||||
|
||||
//JsonSchema schema;
|
||||
//JsonSchema schema;
|
||||
|
||||
//using (JsonTextReader reader = new JsonTextReader(new StringReader(schemaJson)))
|
||||
//{
|
||||
// JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
// schema = builder.Parse(reader);
|
||||
//}
|
||||
//using (JsonTextReader reader = new JsonTextReader(new StringReader(schemaJson)))
|
||||
//{
|
||||
// JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
// schema = builder.Parse(reader);
|
||||
//}
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
|
||||
JObject person = JObject.Parse(@"{
|
||||
JObject person = JObject.Parse(@"{
|
||||
""name"": ""James"",
|
||||
""hobbies"": ["".NET"", ""Blogging"", ""Reading"", ""Xbox"", ""LOLCATS""]
|
||||
}");
|
||||
|
||||
bool valid = person.IsValid(schema);
|
||||
// true
|
||||
}
|
||||
bool valid = person.IsValid(schema);
|
||||
// true
|
||||
}
|
||||
|
||||
private void GenerateSchemaAndSerializeFromType<T>(T value)
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseAssemblyQualifiedName;
|
||||
JsonSchema typeSchema = generator.Generate(typeof (T));
|
||||
string schema = typeSchema.ToString();
|
||||
private void GenerateSchemaAndSerializeFromType<T>(T value)
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseAssemblyQualifiedName;
|
||||
JsonSchema typeSchema = generator.Generate(typeof(T));
|
||||
string schema = typeSchema.ToString();
|
||||
|
||||
string json = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
JToken token = JToken.ReadFrom(new JsonTextReader(new StringReader(json)));
|
||||
string json = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
JToken token = JToken.ReadFrom(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
List<string> errors = new List<string>();
|
||||
|
||||
token.Validate(typeSchema, (sender, args) =>
|
||||
{
|
||||
errors.Add(args.Message);
|
||||
});
|
||||
token.Validate(typeSchema, (sender, args) => { errors.Add(args.Message); });
|
||||
|
||||
if (errors.Count > 0)
|
||||
Assert.Fail("Schema generated for type '{0}' is not valid." + Environment.NewLine + string.Join(Environment.NewLine, errors.ToArray()), typeof(T));
|
||||
}
|
||||
if (errors.Count > 0)
|
||||
Assert.Fail("Schema generated for type '{0}' is not valid." + Environment.NewLine + string.Join(Environment.NewLine, errors.ToArray()), typeof(T));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateSchemaAndSerializeFromTypeTests()
|
||||
{
|
||||
GenerateSchemaAndSerializeFromType(new List<string> { "1", "Two", "III" });
|
||||
GenerateSchemaAndSerializeFromType(new List<int> { 1 });
|
||||
GenerateSchemaAndSerializeFromType(new Version("1.2.3.4"));
|
||||
GenerateSchemaAndSerializeFromType(new Store());
|
||||
GenerateSchemaAndSerializeFromType(new Person());
|
||||
GenerateSchemaAndSerializeFromType(new PersonRaw());
|
||||
GenerateSchemaAndSerializeFromType(new CircularReferenceClass() { Name = "I'm required" });
|
||||
GenerateSchemaAndSerializeFromType(new CircularReferenceWithIdClass());
|
||||
GenerateSchemaAndSerializeFromType(new ClassWithArray());
|
||||
GenerateSchemaAndSerializeFromType(new ClassWithGuid());
|
||||
[Test]
|
||||
public void GenerateSchemaAndSerializeFromTypeTests()
|
||||
{
|
||||
GenerateSchemaAndSerializeFromType(new List<string> { "1", "Two", "III" });
|
||||
GenerateSchemaAndSerializeFromType(new List<int> { 1 });
|
||||
GenerateSchemaAndSerializeFromType(new Version("1.2.3.4"));
|
||||
GenerateSchemaAndSerializeFromType(new Store());
|
||||
GenerateSchemaAndSerializeFromType(new Person());
|
||||
GenerateSchemaAndSerializeFromType(new PersonRaw());
|
||||
GenerateSchemaAndSerializeFromType(new CircularReferenceClass() { Name = "I'm required" });
|
||||
GenerateSchemaAndSerializeFromType(new CircularReferenceWithIdClass());
|
||||
GenerateSchemaAndSerializeFromType(new ClassWithArray());
|
||||
GenerateSchemaAndSerializeFromType(new ClassWithGuid());
|
||||
#if !NET20
|
||||
GenerateSchemaAndSerializeFromType(new NullableDateTimeTestClass());
|
||||
GenerateSchemaAndSerializeFromType(new NullableDateTimeTestClass());
|
||||
#endif
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
GenerateSchemaAndSerializeFromType(new DataSet());
|
||||
GenerateSchemaAndSerializeFromType(new DataSet());
|
||||
#endif
|
||||
GenerateSchemaAndSerializeFromType(new object());
|
||||
GenerateSchemaAndSerializeFromType(1);
|
||||
GenerateSchemaAndSerializeFromType("Hi");
|
||||
GenerateSchemaAndSerializeFromType(new DateTime(2000, 12, 29, 23, 59, 0, DateTimeKind.Utc));
|
||||
GenerateSchemaAndSerializeFromType(TimeSpan.FromTicks(1000000));
|
||||
GenerateSchemaAndSerializeFromType(new object());
|
||||
GenerateSchemaAndSerializeFromType(1);
|
||||
GenerateSchemaAndSerializeFromType("Hi");
|
||||
GenerateSchemaAndSerializeFromType(new DateTime(2000, 12, 29, 23, 59, 0, DateTimeKind.Utc));
|
||||
GenerateSchemaAndSerializeFromType(TimeSpan.FromTicks(1000000));
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
GenerateSchemaAndSerializeFromType(DBNull.Value);
|
||||
GenerateSchemaAndSerializeFromType(DBNull.Value);
|
||||
#endif
|
||||
GenerateSchemaAndSerializeFromType(new JsonPropertyWithHandlingValues());
|
||||
}
|
||||
GenerateSchemaAndSerializeFromType(new JsonPropertyWithHandlingValues());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UndefinedPropertyOnNoPropertySchema()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
[Test]
|
||||
public void UndefinedPropertyOnNoPropertySchema()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""description"": ""test"",
|
||||
""type"": ""object"",
|
||||
""additionalProperties"": false,
|
||||
@@ -227,229 +225,229 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}");
|
||||
|
||||
JObject o = JObject.Parse("{'g':1}");
|
||||
JObject o = JObject.Parse("{'g':1}");
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
o.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
List<string> errors = new List<string>();
|
||||
o.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
Assert.AreEqual("Property 'g' has not been defined and the schema does not allow additional properties. Line 1, position 5.", errors[0]);
|
||||
}
|
||||
Assert.AreEqual(1, errors.Count);
|
||||
Assert.AreEqual("Property 'g' has not been defined and the schema does not allow additional properties. Line 1, position 5.", errors[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExclusiveMaximum_Int()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 equals maximum value of 10 and exclusive maximum is true.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Maximum = 10;
|
||||
schema.ExclusiveMaximum = true;
|
||||
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExclusiveMaximum_Float()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Float 10.1 equals maximum value of 10.1 and exclusive maximum is true.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Maximum = 10.1;
|
||||
schema.ExclusiveMaximum = true;
|
||||
|
||||
JValue v = new JValue(10.1);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExclusiveMinimum_Int()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 equals minimum value of 10 and exclusive minimum is true.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Minimum = 10;
|
||||
schema.ExclusiveMinimum = true;
|
||||
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExclusiveMinimum_Float()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Float 10.1 equals minimum value of 10.1 and exclusive minimum is true.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Minimum = 10.1;
|
||||
schema.ExclusiveMinimum = true;
|
||||
|
||||
JValue v = new JValue(10.1);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DivisibleBy_Int()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 is not evenly divisible by 3.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.DivisibleBy = 3;
|
||||
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_SimpleUnique()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.UniqueItems = true;
|
||||
|
||||
JArray a = new JArray(1, 2, 3);
|
||||
Assert.IsTrue(a.IsValid(schema));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_SimpleDuplicate()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.UniqueItems = true;
|
||||
|
||||
JArray a = new JArray(1, 2, 3, 2, 2);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsFalse(a.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(2, errorMessages.Count);
|
||||
Assert.AreEqual("Non-unique array item at index 3.", errorMessages[0]);
|
||||
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_ComplexDuplicate()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.UniqueItems = true;
|
||||
|
||||
JArray a = new JArray(1, new JObject(new JProperty("value", "value!")), 3, 2, new JObject(new JProperty("value", "value!")), 4, 2, new JObject(new JProperty("value", "value!")));
|
||||
IList<string> errorMessages;
|
||||
Assert.IsFalse(a.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(3, errorMessages.Count);
|
||||
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[0]);
|
||||
Assert.AreEqual("Non-unique array item at index 6.", errorMessages[1]);
|
||||
Assert.AreEqual("Non-unique array item at index 7.", errorMessages[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_NestedDuplicate()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.UniqueItems = true;
|
||||
schema.Items = new List<JsonSchema>
|
||||
[Test]
|
||||
public void ExclusiveMaximum_Int()
|
||||
{
|
||||
new JsonSchema
|
||||
{
|
||||
UniqueItems = true
|
||||
}
|
||||
};
|
||||
schema.PositionalItemsValidation = false;
|
||||
|
||||
JArray a = new JArray(
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 1),
|
||||
new JArray(3, 4),
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 1)
|
||||
);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsFalse(a.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(4, errorMessages.Count);
|
||||
Assert.AreEqual("Non-unique array item at index 1.", errorMessages[0]);
|
||||
Assert.AreEqual("Non-unique array item at index 3.", errorMessages[1]);
|
||||
Assert.AreEqual("Non-unique array item at index 1.", errorMessages[2]);
|
||||
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[3]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Enum_Properties()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Properties = new Dictionary<string, JsonSchema>
|
||||
{
|
||||
{
|
||||
"bar",
|
||||
new JsonSchema
|
||||
{
|
||||
Enum = new List<JToken>
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 equals maximum value of 10 and exclusive maximum is true.",
|
||||
() =>
|
||||
{
|
||||
new JValue(1),
|
||||
new JValue(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Maximum = 10;
|
||||
schema.ExclusiveMaximum = true;
|
||||
|
||||
JObject o = new JObject(
|
||||
new JProperty("bar", 1)
|
||||
);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsTrue(o.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(0, errorMessages.Count);
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
o = new JObject(
|
||||
new JProperty("bar", 3)
|
||||
);
|
||||
Assert.IsFalse(o.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(1, errorMessages.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_Property()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Properties = new Dictionary<string, JsonSchema>
|
||||
[Test]
|
||||
public void ExclusiveMaximum_Float()
|
||||
{
|
||||
{
|
||||
"bar",
|
||||
new JsonSchema
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Float 10.1 equals maximum value of 10.1 and exclusive maximum is true.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Maximum = 10.1;
|
||||
schema.ExclusiveMaximum = true;
|
||||
|
||||
JValue v = new JValue(10.1);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExclusiveMinimum_Int()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 equals minimum value of 10 and exclusive minimum is true.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Minimum = 10;
|
||||
schema.ExclusiveMinimum = true;
|
||||
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExclusiveMinimum_Float()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Float 10.1 equals minimum value of 10.1 and exclusive minimum is true.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Minimum = 10.1;
|
||||
schema.ExclusiveMinimum = true;
|
||||
|
||||
JValue v = new JValue(10.1);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DivisibleBy_Int()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSchemaException>("Integer 10 is not evenly divisible by 3.",
|
||||
() =>
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.DivisibleBy = 3;
|
||||
|
||||
JValue v = new JValue(10);
|
||||
v.Validate(schema);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_SimpleUnique()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.UniqueItems = true;
|
||||
|
||||
JArray a = new JArray(1, 2, 3);
|
||||
Assert.IsTrue(a.IsValid(schema));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_SimpleDuplicate()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.UniqueItems = true;
|
||||
|
||||
JArray a = new JArray(1, 2, 3, 2, 2);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsFalse(a.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(2, errorMessages.Count);
|
||||
Assert.AreEqual("Non-unique array item at index 3.", errorMessages[0]);
|
||||
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_ComplexDuplicate()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.UniqueItems = true;
|
||||
|
||||
JArray a = new JArray(1, new JObject(new JProperty("value", "value!")), 3, 2, new JObject(new JProperty("value", "value!")), 4, 2, new JObject(new JProperty("value", "value!")));
|
||||
IList<string> errorMessages;
|
||||
Assert.IsFalse(a.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(3, errorMessages.Count);
|
||||
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[0]);
|
||||
Assert.AreEqual("Non-unique array item at index 6.", errorMessages[1]);
|
||||
Assert.AreEqual("Non-unique array item at index 7.", errorMessages[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_NestedDuplicate()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.UniqueItems = true;
|
||||
schema.Items = new List<JsonSchema>
|
||||
{
|
||||
UniqueItems = true
|
||||
}
|
||||
}
|
||||
};
|
||||
new JsonSchema
|
||||
{
|
||||
UniqueItems = true
|
||||
}
|
||||
};
|
||||
schema.PositionalItemsValidation = false;
|
||||
|
||||
JObject o = new JObject(
|
||||
new JProperty("bar", new JArray(1, 2, 3, 3))
|
||||
);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsFalse(o.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(1, errorMessages.Count);
|
||||
}
|
||||
JArray a = new JArray(
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 1),
|
||||
new JArray(3, 4),
|
||||
new JArray(1, 2),
|
||||
new JArray(1, 1)
|
||||
);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsFalse(a.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(4, errorMessages.Count);
|
||||
Assert.AreEqual("Non-unique array item at index 1.", errorMessages[0]);
|
||||
Assert.AreEqual("Non-unique array item at index 3.", errorMessages[1]);
|
||||
Assert.AreEqual("Non-unique array item at index 1.", errorMessages[2]);
|
||||
Assert.AreEqual("Non-unique array item at index 4.", errorMessages[3]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Items_Positional()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Items = new List<JsonSchema>
|
||||
[Test]
|
||||
public void Enum_Properties()
|
||||
{
|
||||
new JsonSchema { Type = JsonSchemaType.Object },
|
||||
new JsonSchema { Type = JsonSchemaType.Integer }
|
||||
};
|
||||
schema.PositionalItemsValidation = true;
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Properties = new Dictionary<string, JsonSchema>
|
||||
{
|
||||
{
|
||||
"bar",
|
||||
new JsonSchema
|
||||
{
|
||||
Enum = new List<JToken>
|
||||
{
|
||||
new JValue(1),
|
||||
new JValue(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
JArray a = new JArray(new JObject(), 1);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsTrue(a.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(0, errorMessages.Count);
|
||||
JObject o = new JObject(
|
||||
new JProperty("bar", 1)
|
||||
);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsTrue(o.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(0, errorMessages.Count);
|
||||
|
||||
o = new JObject(
|
||||
new JProperty("bar", 3)
|
||||
);
|
||||
Assert.IsFalse(o.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(1, errorMessages.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UniqueItems_Property()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Properties = new Dictionary<string, JsonSchema>
|
||||
{
|
||||
{
|
||||
"bar",
|
||||
new JsonSchema
|
||||
{
|
||||
UniqueItems = true
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
JObject o = new JObject(
|
||||
new JProperty("bar", new JArray(1, 2, 3, 3))
|
||||
);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsFalse(o.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(1, errorMessages.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Items_Positional()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Items = new List<JsonSchema>
|
||||
{
|
||||
new JsonSchema { Type = JsonSchemaType.Object },
|
||||
new JsonSchema { Type = JsonSchemaType.Integer }
|
||||
};
|
||||
schema.PositionalItemsValidation = true;
|
||||
|
||||
JArray a = new JArray(new JObject(), 1);
|
||||
IList<string> errorMessages;
|
||||
Assert.IsTrue(a.IsValid(schema, out errorMessages));
|
||||
Assert.AreEqual(0, errorMessages.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,13 +37,13 @@ using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Schema
|
||||
{
|
||||
[TestFixture]
|
||||
public class JsonSchemaBuilderTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Simple()
|
||||
[TestFixture]
|
||||
public class JsonSchemaBuilderTests : TestFixtureBase
|
||||
{
|
||||
string json = @"
|
||||
[Test]
|
||||
public void Simple()
|
||||
{
|
||||
string json = @"
|
||||
{
|
||||
""description"": ""A person"",
|
||||
""type"": ""object"",
|
||||
@@ -58,206 +58,206 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("A person", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Type);
|
||||
Assert.AreEqual("A person", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Type);
|
||||
|
||||
Assert.AreEqual(2, schema.Properties.Count);
|
||||
Assert.AreEqual(2, schema.Properties.Count);
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["name"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Array, schema.Properties["hobbies"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["hobbies"].Items[0].Type);
|
||||
}
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["name"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Array, schema.Properties["hobbies"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["hobbies"].Items[0].Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultipleTypes()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void MultipleTypes()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Age"",
|
||||
""type"":[""string"", ""integer""]
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Age", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Integer, schema.Type);
|
||||
}
|
||||
Assert.AreEqual("Age", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Integer, schema.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MultipleItems()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void MultipleItems()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""MultipleItems"",
|
||||
""type"":""array"",
|
||||
""items"": [{""type"":""string""},{""type"":""array""}]
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("MultipleItems", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Items[0].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Array, schema.Items[1].Type);
|
||||
}
|
||||
Assert.AreEqual("MultipleItems", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Items[0].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Array, schema.Items[1].Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AdditionalProperties()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void AdditionalProperties()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""AdditionalProperties"",
|
||||
""type"":[""string"", ""integer""],
|
||||
""additionalProperties"":{""type"":[""object"", ""boolean""]}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("AdditionalProperties", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Boolean, schema.AdditionalProperties.Type);
|
||||
}
|
||||
Assert.AreEqual("AdditionalProperties", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Boolean, schema.AdditionalProperties.Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Required()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Required()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Required"",
|
||||
""required"":true
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Required", schema.Description);
|
||||
Assert.AreEqual(true, schema.Required);
|
||||
}
|
||||
Assert.AreEqual("Required", schema.Description);
|
||||
Assert.AreEqual(true, schema.Required);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExclusiveMinimum_ExclusiveMaximum()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void ExclusiveMinimum_ExclusiveMaximum()
|
||||
{
|
||||
string json = @"{
|
||||
""exclusiveMinimum"":true,
|
||||
""exclusiveMaximum"":true
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual(true, schema.ExclusiveMinimum);
|
||||
Assert.AreEqual(true, schema.ExclusiveMaximum);
|
||||
}
|
||||
Assert.AreEqual(true, schema.ExclusiveMinimum);
|
||||
Assert.AreEqual(true, schema.ExclusiveMaximum);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadOnly()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void ReadOnly()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""ReadOnly"",
|
||||
""readonly"":true
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("ReadOnly", schema.Description);
|
||||
Assert.AreEqual(true, schema.ReadOnly);
|
||||
}
|
||||
Assert.AreEqual("ReadOnly", schema.Description);
|
||||
Assert.AreEqual(true, schema.ReadOnly);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Hidden()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Hidden()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Hidden"",
|
||||
""hidden"":true
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Hidden", schema.Description);
|
||||
Assert.AreEqual(true, schema.Hidden);
|
||||
}
|
||||
Assert.AreEqual("Hidden", schema.Description);
|
||||
Assert.AreEqual(true, schema.Hidden);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Id()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Id()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Id"",
|
||||
""id"":""testid""
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Id", schema.Description);
|
||||
Assert.AreEqual("testid", schema.Id);
|
||||
}
|
||||
Assert.AreEqual("Id", schema.Description);
|
||||
Assert.AreEqual("testid", schema.Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Title()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Title()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Title"",
|
||||
""title"":""testtitle""
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Title", schema.Description);
|
||||
Assert.AreEqual("testtitle", schema.Title);
|
||||
}
|
||||
Assert.AreEqual("Title", schema.Description);
|
||||
Assert.AreEqual("testtitle", schema.Title);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Pattern()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Pattern()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Pattern"",
|
||||
""pattern"":""testpattern""
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Pattern", schema.Description);
|
||||
Assert.AreEqual("testpattern", schema.Pattern);
|
||||
}
|
||||
Assert.AreEqual("Pattern", schema.Description);
|
||||
Assert.AreEqual("testpattern", schema.Pattern);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Format()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Format()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Format"",
|
||||
""format"":""testformat""
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Format", schema.Description);
|
||||
Assert.AreEqual("testformat", schema.Format);
|
||||
}
|
||||
Assert.AreEqual("Format", schema.Description);
|
||||
Assert.AreEqual("testformat", schema.Format);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Requires()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Requires()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Requires"",
|
||||
""requires"":""PurpleMonkeyDishwasher""
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Requires", schema.Description);
|
||||
Assert.AreEqual("PurpleMonkeyDishwasher", schema.Requires);
|
||||
}
|
||||
Assert.AreEqual("Requires", schema.Description);
|
||||
Assert.AreEqual("PurpleMonkeyDishwasher", schema.Requires);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MinimumMaximum()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void MinimumMaximum()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""MinimumMaximum"",
|
||||
""minimum"":1.1,
|
||||
""maximum"":1.2,
|
||||
@@ -268,204 +268,204 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""divisibleBy"":3,
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("MinimumMaximum", schema.Description);
|
||||
Assert.AreEqual(1.1, schema.Minimum);
|
||||
Assert.AreEqual(1.2, schema.Maximum);
|
||||
Assert.AreEqual(1, schema.MinimumItems);
|
||||
Assert.AreEqual(2, schema.MaximumItems);
|
||||
Assert.AreEqual(5, schema.MinimumLength);
|
||||
Assert.AreEqual(50, schema.MaximumLength);
|
||||
Assert.AreEqual(3, schema.DivisibleBy);
|
||||
}
|
||||
Assert.AreEqual("MinimumMaximum", schema.Description);
|
||||
Assert.AreEqual(1.1, schema.Minimum);
|
||||
Assert.AreEqual(1.2, schema.Maximum);
|
||||
Assert.AreEqual(1, schema.MinimumItems);
|
||||
Assert.AreEqual(2, schema.MaximumItems);
|
||||
Assert.AreEqual(5, schema.MinimumLength);
|
||||
Assert.AreEqual(50, schema.MaximumLength);
|
||||
Assert.AreEqual(3, schema.DivisibleBy);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisallowSingleType()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void DisallowSingleType()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""DisallowSingleType"",
|
||||
""disallow"":""string""
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("DisallowSingleType", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Disallow);
|
||||
}
|
||||
Assert.AreEqual("DisallowSingleType", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Disallow);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisallowMultipleTypes()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void DisallowMultipleTypes()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""DisallowMultipleTypes"",
|
||||
""disallow"":[""string"",""number""]
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("DisallowMultipleTypes", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Float, schema.Disallow);
|
||||
}
|
||||
Assert.AreEqual("DisallowMultipleTypes", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Float, schema.Disallow);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultPrimitiveType()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void DefaultPrimitiveType()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""DefaultPrimitiveType"",
|
||||
""default"":1.1
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("DefaultPrimitiveType", schema.Description);
|
||||
Assert.AreEqual(1.1, (double) schema.Default);
|
||||
}
|
||||
Assert.AreEqual("DefaultPrimitiveType", schema.Description);
|
||||
Assert.AreEqual(1.1, (double)schema.Default);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultComplexType()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void DefaultComplexType()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""DefaultComplexType"",
|
||||
""default"":{""pie"":true}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("DefaultComplexType", schema.Description);
|
||||
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(@"{""pie"":true}"), schema.Default));
|
||||
}
|
||||
Assert.AreEqual("DefaultComplexType", schema.Description);
|
||||
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(@"{""pie"":true}"), schema.Default));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Enum()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Enum()
|
||||
{
|
||||
string json = @"{
|
||||
""description"":""Type"",
|
||||
""type"":[""string"",""array""],
|
||||
""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("Type", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Array, schema.Type);
|
||||
Assert.AreEqual("Type", schema.Description);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Array, schema.Type);
|
||||
|
||||
Assert.AreEqual(8, schema.Enum.Count);
|
||||
Assert.AreEqual("string", (string) schema.Enum[0]);
|
||||
Assert.AreEqual("any", (string) schema.Enum[schema.Enum.Count - 1]);
|
||||
}
|
||||
Assert.AreEqual(8, schema.Enum.Count);
|
||||
Assert.AreEqual("string", (string)schema.Enum[0]);
|
||||
Assert.AreEqual("any", (string)schema.Enum[schema.Enum.Count - 1]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void CircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
""id"":""CircularReferenceArray"",
|
||||
""description"":""CircularReference"",
|
||||
""type"":[""array""],
|
||||
""items"":{""$ref"":""CircularReferenceArray""}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("CircularReference", schema.Description);
|
||||
Assert.AreEqual("CircularReferenceArray", schema.Id);
|
||||
Assert.AreEqual(JsonSchemaType.Array, schema.Type);
|
||||
Assert.AreEqual("CircularReference", schema.Description);
|
||||
Assert.AreEqual("CircularReferenceArray", schema.Id);
|
||||
Assert.AreEqual(JsonSchemaType.Array, schema.Type);
|
||||
|
||||
Assert.AreEqual(schema, schema.Items[0]);
|
||||
}
|
||||
Assert.AreEqual(schema, schema.Items[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnresolvedReference()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>(@"Could not resolve schema reference 'MyUnresolvedReference'.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void UnresolvedReference()
|
||||
{
|
||||
ExceptionAssert.Throws<Exception>(@"Could not resolve schema reference 'MyUnresolvedReference'.",
|
||||
() =>
|
||||
{
|
||||
string json = @"{
|
||||
""id"":""CircularReferenceArray"",
|
||||
""description"":""CircularReference"",
|
||||
""type"":[""array""],
|
||||
""items"":{""$ref"":""MyUnresolvedReference""}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
}
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PatternProperties()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void PatternProperties()
|
||||
{
|
||||
string json = @"{
|
||||
""patternProperties"": {
|
||||
""[abc]"": { ""id"":""Blah"" }
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.IsNotNull(schema.PatternProperties);
|
||||
Assert.AreEqual(1, schema.PatternProperties.Count);
|
||||
Assert.AreEqual("Blah", schema.PatternProperties["[abc]"].Id);
|
||||
}
|
||||
Assert.IsNotNull(schema.PatternProperties);
|
||||
Assert.AreEqual(1, schema.PatternProperties.Count);
|
||||
Assert.AreEqual("Blah", schema.PatternProperties["[abc]"].Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AdditionalItems()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void AdditionalItems()
|
||||
{
|
||||
string json = @"{
|
||||
""items"": [],
|
||||
""additionalItems"": {""type"": ""integer""}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.IsNotNull(schema.AdditionalItems);
|
||||
Assert.AreEqual(JsonSchemaType.Integer, schema.AdditionalItems.Type);
|
||||
Assert.AreEqual(true, schema.AllowAdditionalItems);
|
||||
}
|
||||
Assert.IsNotNull(schema.AdditionalItems);
|
||||
Assert.AreEqual(JsonSchemaType.Integer, schema.AdditionalItems.Type);
|
||||
Assert.AreEqual(true, schema.AllowAdditionalItems);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisallowAdditionalItems()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void DisallowAdditionalItems()
|
||||
{
|
||||
string json = @"{
|
||||
""items"": [],
|
||||
""additionalItems"": false
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.IsNull(schema.AdditionalItems);
|
||||
Assert.AreEqual(false, schema.AllowAdditionalItems);
|
||||
}
|
||||
Assert.IsNull(schema.AdditionalItems);
|
||||
Assert.AreEqual(false, schema.AllowAdditionalItems);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AllowAdditionalItems()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void AllowAdditionalItems()
|
||||
{
|
||||
string json = @"{
|
||||
""items"": {},
|
||||
""additionalItems"": false
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.IsNull(schema.AdditionalItems);
|
||||
Assert.AreEqual(false, schema.AllowAdditionalItems);
|
||||
}
|
||||
Assert.IsNull(schema.AdditionalItems);
|
||||
Assert.AreEqual(false, schema.AllowAdditionalItems);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Location()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Location()
|
||||
{
|
||||
string json = @"{
|
||||
""properties"":{
|
||||
""foo"":{
|
||||
""type"":""array"",
|
||||
@@ -485,50 +485,50 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual("#", schema.Location);
|
||||
Assert.AreEqual("#/properties/foo", schema.Properties["foo"].Location);
|
||||
Assert.AreEqual("#/properties/foo/items/1/properties/foo", schema.Properties["foo"].Items[1].Properties["foo"].Location);
|
||||
}
|
||||
Assert.AreEqual("#", schema.Location);
|
||||
Assert.AreEqual("#/properties/foo", schema.Properties["foo"].Location);
|
||||
Assert.AreEqual("#/properties/foo/items/1/properties/foo", schema.Properties["foo"].Items[1].Properties["foo"].Location);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Reference_BackwardsLocation()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Reference_BackwardsLocation()
|
||||
{
|
||||
string json = @"{
|
||||
""properties"": {
|
||||
""foo"": {""type"": ""integer""},
|
||||
""bar"": {""$ref"": ""#/properties/foo""}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
|
||||
}
|
||||
Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Reference_ForwardsLocation()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Reference_ForwardsLocation()
|
||||
{
|
||||
string json = @"{
|
||||
""properties"": {
|
||||
""bar"": {""$ref"": ""#/properties/foo""},
|
||||
""foo"": {""type"": ""integer""}
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
|
||||
}
|
||||
Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Reference_NonStandardLocation()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Reference_NonStandardLocation()
|
||||
{
|
||||
string json = @"{
|
||||
""properties"": {
|
||||
""bar"": {""$ref"": ""#/common/foo""},
|
||||
""foo"": {""$ref"": ""#/common/foo""}
|
||||
@@ -538,16 +538,16 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
|
||||
}
|
||||
Assert.AreEqual(schema.Properties["foo"], schema.Properties["bar"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EscapedReferences()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void EscapedReferences()
|
||||
{
|
||||
string json = @"{
|
||||
""tilda~field"": {""type"": ""integer""},
|
||||
""slash/field"": {""type"": ""object""},
|
||||
""percent%field"": {""type"": ""array""},
|
||||
@@ -558,18 +558,18 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Integer, schema.Properties["tilda"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Properties["slash"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Array, schema.Properties["percent"].Type);
|
||||
}
|
||||
Assert.AreEqual(JsonSchemaType.Integer, schema.Properties["tilda"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Properties["slash"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Array, schema.Properties["percent"].Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void References_Array()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void References_Array()
|
||||
{
|
||||
string json = @"{
|
||||
""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
|
||||
""properties"": {
|
||||
""array"": {""$ref"": ""#/array/0""},
|
||||
@@ -577,19 +577,19 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
JsonSchema schema = builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Integer, schema.Properties["array"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Properties["arrayprop"].Type);
|
||||
}
|
||||
Assert.AreEqual(JsonSchemaType.Integer, schema.Properties["array"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Properties["arrayprop"].Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void References_IndexTooBig()
|
||||
{
|
||||
// JsonException : Could not resolve schema reference '#/array/10'.
|
||||
[Test]
|
||||
public void References_IndexTooBig()
|
||||
{
|
||||
// JsonException : Could not resolve schema reference '#/array/10'.
|
||||
|
||||
string json = @"{
|
||||
string json = @"{
|
||||
""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
|
||||
""properties"": {
|
||||
""array"": {""$ref"": ""#/array/0""},
|
||||
@@ -597,19 +597,19 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Could not resolve schema reference '#/array/10'.",
|
||||
() =>
|
||||
{
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
}
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Could not resolve schema reference '#/array/10'.",
|
||||
() =>
|
||||
{
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void References_IndexNegative()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void References_IndexNegative()
|
||||
{
|
||||
string json = @"{
|
||||
""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
|
||||
""properties"": {
|
||||
""array"": {""$ref"": ""#/array/0""},
|
||||
@@ -617,19 +617,19 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Could not resolve schema reference '#/array/-1'.",
|
||||
() =>
|
||||
{
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
}
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Could not resolve schema reference '#/array/-1'.",
|
||||
() =>
|
||||
{
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void References_IndexNotInteger()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void References_IndexNotInteger()
|
||||
{
|
||||
string json = @"{
|
||||
""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
|
||||
""properties"": {
|
||||
""array"": {""$ref"": ""#/array/0""},
|
||||
@@ -637,13 +637,13 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}";
|
||||
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Could not resolve schema reference '#/array/one'.",
|
||||
() =>
|
||||
{
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
ExceptionAssert.Throws<JsonException>(
|
||||
"Could not resolve schema reference '#/array/one'.",
|
||||
() =>
|
||||
{
|
||||
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
|
||||
builder.Read(new JsonTextReader(new StringReader(json)));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,27 +42,28 @@ using Newtonsoft.Json.Schema;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Text;
|
||||
using Extensions=Newtonsoft.Json.Schema.Extensions;
|
||||
using Extensions = Newtonsoft.Json.Schema.Extensions;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Schema
|
||||
{
|
||||
[TestFixture]
|
||||
public class JsonSchemaGeneratorTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Generate_GenericDictionary()
|
||||
[TestFixture]
|
||||
public class JsonSchemaGeneratorTests : TestFixtureBase
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof (Dictionary<string, List<string>>));
|
||||
[Test]
|
||||
public void Generate_GenericDictionary()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(Dictionary<string, List<string>>));
|
||||
|
||||
string json = schema.ToString();
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""type"": ""object"",
|
||||
""additionalProperties"": {
|
||||
""type"": [
|
||||
@@ -78,28 +79,28 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}", json);
|
||||
|
||||
Dictionary<string, List<string>> value = new Dictionary<string, List<string>>
|
||||
{
|
||||
{"HasValue", new List<string>() { "first", "second", null }},
|
||||
{"NoValue", null}
|
||||
};
|
||||
Dictionary<string, List<string>> value = new Dictionary<string, List<string>>
|
||||
{
|
||||
{ "HasValue", new List<string>() { "first", "second", null } },
|
||||
{ "NoValue", null }
|
||||
};
|
||||
|
||||
string valueJson = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
JObject o = JObject.Parse(valueJson);
|
||||
string valueJson = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
JObject o = JObject.Parse(valueJson);
|
||||
|
||||
Assert.IsTrue(o.IsValid(schema));
|
||||
}
|
||||
Assert.IsTrue(o.IsValid(schema));
|
||||
}
|
||||
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
[Test]
|
||||
public void Generate_DefaultValueAttributeTestClass()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(DefaultValueAttributeTestClass));
|
||||
[Test]
|
||||
public void Generate_DefaultValueAttributeTestClass()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(DefaultValueAttributeTestClass));
|
||||
|
||||
string json = schema.ToString();
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""description"": ""DefaultValueAttributeTestClass description!"",
|
||||
""type"": ""object"",
|
||||
""additionalProperties"": false,
|
||||
@@ -119,18 +120,18 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void Generate_Person()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(Person));
|
||||
[Test]
|
||||
public void Generate_Person()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(Person));
|
||||
|
||||
string json = schema.ToString();
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""Person"",
|
||||
""title"": ""Title!"",
|
||||
""description"": ""JsonObjectAttribute description!"",
|
||||
@@ -153,17 +154,17 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Generate_UserNullable()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(UserNullable));
|
||||
[Test]
|
||||
public void Generate_UserNullable()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(UserNullable));
|
||||
|
||||
string json = schema.ToString();
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""type"": ""object"",
|
||||
""properties"": {
|
||||
""Id"": {
|
||||
@@ -211,175 +212,175 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Generate_RequiredMembersClass()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(RequiredMembersClass));
|
||||
[Test]
|
||||
public void Generate_RequiredMembersClass()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(RequiredMembersClass));
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["FirstName"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["MiddleName"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["LastName"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["BirthDate"].Type);
|
||||
}
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["FirstName"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["MiddleName"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["LastName"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["BirthDate"].Type);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Generate_Store()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(Store));
|
||||
[Test]
|
||||
public void Generate_Store()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = generator.Generate(typeof(Store));
|
||||
|
||||
Assert.AreEqual(11, schema.Properties.Count);
|
||||
Assert.AreEqual(11, schema.Properties.Count);
|
||||
|
||||
JsonSchema productArraySchema = schema.Properties["product"];
|
||||
JsonSchema productSchema = productArraySchema.Items[0];
|
||||
JsonSchema productArraySchema = schema.Properties["product"];
|
||||
JsonSchema productSchema = productArraySchema.Items[0];
|
||||
|
||||
Assert.AreEqual(4, productSchema.Properties.Count);
|
||||
}
|
||||
Assert.AreEqual(4, productSchema.Properties.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MissingSchemaIdHandlingTest()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
[Test]
|
||||
public void MissingSchemaIdHandlingTest()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(Store));
|
||||
Assert.AreEqual(null, schema.Id);
|
||||
JsonSchema schema = generator.Generate(typeof(Store));
|
||||
Assert.AreEqual(null, schema.Id);
|
||||
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
schema = generator.Generate(typeof (Store));
|
||||
Assert.AreEqual(typeof(Store).FullName, schema.Id);
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
schema = generator.Generate(typeof(Store));
|
||||
Assert.AreEqual(typeof(Store).FullName, schema.Id);
|
||||
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseAssemblyQualifiedName;
|
||||
schema = generator.Generate(typeof(Store));
|
||||
Assert.AreEqual(typeof(Store).AssemblyQualifiedName, schema.Id);
|
||||
}
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseAssemblyQualifiedName;
|
||||
schema = generator.Generate(typeof(Store));
|
||||
Assert.AreEqual(typeof(Store).AssemblyQualifiedName, schema.Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularReferenceError()
|
||||
{
|
||||
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.",
|
||||
() =>
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.Generate(typeof(CircularReferenceClass));
|
||||
});
|
||||
}
|
||||
[Test]
|
||||
public void CircularReferenceError()
|
||||
{
|
||||
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.",
|
||||
() =>
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.Generate(typeof(CircularReferenceClass));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularReferenceWithTypeNameId()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
[Test]
|
||||
public void CircularReferenceWithTypeNameId()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(CircularReferenceClass), true);
|
||||
JsonSchema schema = generator.Generate(typeof(CircularReferenceClass), true);
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["Name"].Type);
|
||||
Assert.AreEqual(typeof(CircularReferenceClass).FullName, schema.Id);
|
||||
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
|
||||
Assert.AreEqual(schema, schema.Properties["Child"]);
|
||||
}
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Properties["Name"].Type);
|
||||
Assert.AreEqual(typeof(CircularReferenceClass).FullName, schema.Id);
|
||||
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
|
||||
Assert.AreEqual(schema, schema.Properties["Child"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularReferenceWithExplicitId()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
[Test]
|
||||
public void CircularReferenceWithExplicitId()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(CircularReferenceWithIdClass));
|
||||
JsonSchema schema = generator.Generate(typeof(CircularReferenceWithIdClass));
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["Name"].Type);
|
||||
Assert.AreEqual("MyExplicitId", schema.Id);
|
||||
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
|
||||
Assert.AreEqual(schema, schema.Properties["Child"]);
|
||||
}
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["Name"].Type);
|
||||
Assert.AreEqual("MyExplicitId", schema.Id);
|
||||
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
|
||||
Assert.AreEqual(schema, schema.Properties["Child"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateSchemaForType()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
[Test]
|
||||
public void GenerateSchemaForType()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(Type));
|
||||
JsonSchema schema = generator.Generate(typeof(Type));
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Type);
|
||||
Assert.AreEqual(JsonSchemaType.String, schema.Type);
|
||||
|
||||
string json = JsonConvert.SerializeObject(typeof(Version), Formatting.Indented);
|
||||
string json = JsonConvert.SerializeObject(typeof(Version), Formatting.Indented);
|
||||
|
||||
JValue v = new JValue(json);
|
||||
Assert.IsTrue(v.IsValid(schema));
|
||||
}
|
||||
JValue v = new JValue(json);
|
||||
Assert.IsTrue(v.IsValid(schema));
|
||||
}
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
[Test]
|
||||
public void GenerateSchemaForISerializable()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
[Test]
|
||||
public void GenerateSchemaForISerializable()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(Exception));
|
||||
JsonSchema schema = generator.Generate(typeof(Exception));
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Type);
|
||||
Assert.AreEqual(true, schema.AllowAdditionalProperties);
|
||||
Assert.AreEqual(null, schema.Properties);
|
||||
}
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Type);
|
||||
Assert.AreEqual(true, schema.AllowAdditionalProperties);
|
||||
Assert.AreEqual(null, schema.Properties);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
[Test]
|
||||
public void GenerateSchemaForDBNull()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(DBNull));
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Null, schema.Type);
|
||||
}
|
||||
|
||||
public class CustomDirectoryInfoMapper : DefaultContractResolver
|
||||
{
|
||||
public CustomDirectoryInfoMapper()
|
||||
: base(true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override JsonContract CreateContract(Type objectType)
|
||||
{
|
||||
if (objectType == typeof(DirectoryInfo))
|
||||
return base.CreateObjectContract(objectType);
|
||||
|
||||
return base.CreateContract(objectType);
|
||||
}
|
||||
|
||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||
{
|
||||
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
|
||||
|
||||
JsonPropertyCollection c = new JsonPropertyCollection(type);
|
||||
c.AddRange(properties.Where(m => m.PropertyName != "Root"));
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateSchemaForDirectoryInfo()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
generator.ContractResolver = new CustomDirectoryInfoMapper
|
||||
[Test]
|
||||
public void GenerateSchemaForDBNull()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(DBNull));
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Null, schema.Type);
|
||||
}
|
||||
|
||||
public class CustomDirectoryInfoMapper : DefaultContractResolver
|
||||
{
|
||||
public CustomDirectoryInfoMapper()
|
||||
: base(true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override JsonContract CreateContract(Type objectType)
|
||||
{
|
||||
if (objectType == typeof(DirectoryInfo))
|
||||
return base.CreateObjectContract(objectType);
|
||||
|
||||
return base.CreateContract(objectType);
|
||||
}
|
||||
|
||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||
{
|
||||
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
|
||||
|
||||
JsonPropertyCollection c = new JsonPropertyCollection(type);
|
||||
c.AddRange(properties.Where(m => m.PropertyName != "Root"));
|
||||
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateSchemaForDirectoryInfo()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
generator.ContractResolver = new CustomDirectoryInfoMapper
|
||||
{
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
};
|
||||
};
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(DirectoryInfo), true);
|
||||
JsonSchema schema = generator.Generate(typeof(DirectoryInfo), true);
|
||||
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""System.IO.DirectoryInfo"",
|
||||
""required"": true,
|
||||
""type"": [
|
||||
@@ -447,43 +448,43 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}", json);
|
||||
|
||||
DirectoryInfo temp = new DirectoryInfo(@"c:\temp");
|
||||
DirectoryInfo temp = new DirectoryInfo(@"c:\temp");
|
||||
|
||||
JTokenWriter jsonWriter = new JTokenWriter();
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.Converters.Add(new IsoDateTimeConverter());
|
||||
serializer.ContractResolver = new CustomDirectoryInfoMapper
|
||||
{
|
||||
JTokenWriter jsonWriter = new JTokenWriter();
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.Converters.Add(new IsoDateTimeConverter());
|
||||
serializer.ContractResolver = new CustomDirectoryInfoMapper
|
||||
{
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
|
||||
IgnoreSerializableInterface = true
|
||||
IgnoreSerializableInterface = true
|
||||
#endif
|
||||
};
|
||||
serializer.Serialize(jsonWriter, temp);
|
||||
};
|
||||
serializer.Serialize(jsonWriter, temp);
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
List<string> errors = new List<string>();
|
||||
jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
|
||||
Assert.AreEqual(0, errors.Count);
|
||||
}
|
||||
Assert.AreEqual(0, errors.Count);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Test]
|
||||
public void GenerateSchemaCamelCase()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
generator.ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
{
|
||||
[Test]
|
||||
public void GenerateSchemaCamelCase()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
generator.ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
{
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
IgnoreSerializableAttribute = true
|
||||
IgnoreSerializableAttribute = true
|
||||
#endif
|
||||
};
|
||||
};
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof(Version), true);
|
||||
JsonSchema schema = generator.Generate(typeof(Version), true);
|
||||
|
||||
string json = schema.ToString();
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""System.Version"",
|
||||
""type"": [
|
||||
""object"",
|
||||
@@ -517,24 +518,24 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
|
||||
[Test]
|
||||
public void GenerateSchemaSerializable()
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.ContractResolver = new DefaultContractResolver
|
||||
[Test]
|
||||
public void GenerateSchemaSerializable()
|
||||
{
|
||||
IgnoreSerializableAttribute = false
|
||||
};
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
IgnoreSerializableAttribute = false
|
||||
};
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
|
||||
JsonSchema schema = generator.Generate(typeof (Version), true);
|
||||
JsonSchema schema = generator.Generate(typeof(Version), true);
|
||||
|
||||
string json = schema.ToString();
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""System.Version"",
|
||||
""type"": [
|
||||
""object"",
|
||||
@@ -561,55 +562,55 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}", json);
|
||||
|
||||
JTokenWriter jsonWriter = new JTokenWriter();
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
IgnoreSerializableAttribute = false
|
||||
};
|
||||
serializer.Serialize(jsonWriter, new Version(1, 2, 3, 4));
|
||||
JTokenWriter jsonWriter = new JTokenWriter();
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
serializer.ContractResolver = new DefaultContractResolver
|
||||
{
|
||||
IgnoreSerializableAttribute = false
|
||||
};
|
||||
serializer.Serialize(jsonWriter, new Version(1, 2, 3, 4));
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
List<string> errors = new List<string>();
|
||||
jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));
|
||||
|
||||
Assert.AreEqual(0, errors.Count);
|
||||
Assert.AreEqual(0, errors.Count);
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""_Major"": 1,
|
||||
""_Minor"": 2,
|
||||
""_Build"": 3,
|
||||
""_Revision"": 4
|
||||
}", jsonWriter.Token.ToString());
|
||||
|
||||
Version version = jsonWriter.Token.ToObject<Version>(serializer);
|
||||
Assert.AreEqual(1, version.Major);
|
||||
Assert.AreEqual(2, version.Minor);
|
||||
Assert.AreEqual(3, version.Build);
|
||||
Assert.AreEqual(4, version.Revision);
|
||||
}
|
||||
Version version = jsonWriter.Token.ToObject<Version>(serializer);
|
||||
Assert.AreEqual(1, version.Major);
|
||||
Assert.AreEqual(2, version.Minor);
|
||||
Assert.AreEqual(3, version.Build);
|
||||
Assert.AreEqual(4, version.Revision);
|
||||
}
|
||||
#endif
|
||||
|
||||
public enum SortTypeFlag
|
||||
{
|
||||
No = 0,
|
||||
Asc = 1,
|
||||
Desc = -1
|
||||
}
|
||||
public enum SortTypeFlag
|
||||
{
|
||||
No = 0,
|
||||
Asc = 1,
|
||||
Desc = -1
|
||||
}
|
||||
|
||||
public class X
|
||||
{
|
||||
public SortTypeFlag x;
|
||||
}
|
||||
public class X
|
||||
{
|
||||
public SortTypeFlag x;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateSchemaWithNegativeEnum()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = jsonSchemaGenerator.Generate(typeof(X));
|
||||
[Test]
|
||||
public void GenerateSchemaWithNegativeEnum()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
JsonSchema schema = jsonSchemaGenerator.Generate(typeof(X));
|
||||
|
||||
string json = schema.ToString();
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""type"": ""object"",
|
||||
""properties"": {
|
||||
""x"": {
|
||||
@@ -623,31 +624,31 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularCollectionReferences()
|
||||
{
|
||||
Type type = typeof (Workspace);
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
[Test]
|
||||
public void CircularCollectionReferences()
|
||||
{
|
||||
Type type = typeof(Workspace);
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
|
||||
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(type);
|
||||
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(type);
|
||||
|
||||
// should succeed
|
||||
Assert.IsNotNull(jsonSchema);
|
||||
}
|
||||
// should succeed
|
||||
Assert.IsNotNull(jsonSchema);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularReferenceWithMixedRequires()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
[Test]
|
||||
public void CircularReferenceWithMixedRequires()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
|
||||
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(CircularReferenceClass));
|
||||
string json = jsonSchema.ToString();
|
||||
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(CircularReferenceClass));
|
||||
string json = jsonSchema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass"",
|
||||
""type"": [
|
||||
""object"",
|
||||
@@ -663,18 +664,18 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonPropertyWithHandlingValues()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
[Test]
|
||||
public void JsonPropertyWithHandlingValues()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
|
||||
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(JsonPropertyWithHandlingValues));
|
||||
string json = jsonSchema.ToString();
|
||||
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(JsonPropertyWithHandlingValues));
|
||||
string json = jsonSchema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues"",
|
||||
""required"": true,
|
||||
""type"": [
|
||||
@@ -736,17 +737,17 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateForNullableInt32()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
[Test]
|
||||
public void GenerateForNullableInt32()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(NullableInt32TestClass));
|
||||
string json = jsonSchema.ToString();
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(NullableInt32TestClass));
|
||||
string json = jsonSchema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""type"": ""object"",
|
||||
""properties"": {
|
||||
""Value"": {
|
||||
@@ -758,75 +759,75 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class NullableInt32TestClass
|
||||
{
|
||||
public int? Value { get; set; }
|
||||
}
|
||||
public class NullableInt32TestClass
|
||||
{
|
||||
public int? Value { get; set; }
|
||||
}
|
||||
|
||||
public class DMDSLBase
|
||||
{
|
||||
public String Comment;
|
||||
}
|
||||
public class DMDSLBase
|
||||
{
|
||||
public String Comment;
|
||||
}
|
||||
|
||||
public class Workspace : DMDSLBase
|
||||
{
|
||||
public ControlFlowItemCollection Jobs = new ControlFlowItemCollection();
|
||||
}
|
||||
public class Workspace : DMDSLBase
|
||||
{
|
||||
public ControlFlowItemCollection Jobs = new ControlFlowItemCollection();
|
||||
}
|
||||
|
||||
public class ControlFlowItemBase : DMDSLBase
|
||||
{
|
||||
public String Name;
|
||||
}
|
||||
public class ControlFlowItemBase : DMDSLBase
|
||||
{
|
||||
public String Name;
|
||||
}
|
||||
|
||||
public class ControlFlowItem : ControlFlowItemBase//A Job
|
||||
{
|
||||
public TaskCollection Tasks = new TaskCollection();
|
||||
public ContainerCollection Containers = new ContainerCollection();
|
||||
}
|
||||
public class ControlFlowItem : ControlFlowItemBase //A Job
|
||||
{
|
||||
public TaskCollection Tasks = new TaskCollection();
|
||||
public ContainerCollection Containers = new ContainerCollection();
|
||||
}
|
||||
|
||||
public class ControlFlowItemCollection : List<ControlFlowItem>
|
||||
{
|
||||
}
|
||||
public class ControlFlowItemCollection : List<ControlFlowItem>
|
||||
{
|
||||
}
|
||||
|
||||
public class Task : ControlFlowItemBase
|
||||
{
|
||||
public DataFlowTaskCollection DataFlowTasks = new DataFlowTaskCollection();
|
||||
public BulkInsertTaskCollection BulkInsertTask = new BulkInsertTaskCollection();
|
||||
}
|
||||
public class Task : ControlFlowItemBase
|
||||
{
|
||||
public DataFlowTaskCollection DataFlowTasks = new DataFlowTaskCollection();
|
||||
public BulkInsertTaskCollection BulkInsertTask = new BulkInsertTaskCollection();
|
||||
}
|
||||
|
||||
public class TaskCollection : List<Task>
|
||||
{
|
||||
}
|
||||
public class TaskCollection : List<Task>
|
||||
{
|
||||
}
|
||||
|
||||
public class Container : ControlFlowItemBase
|
||||
{
|
||||
public ControlFlowItemCollection ContainerJobs = new ControlFlowItemCollection();
|
||||
}
|
||||
public class Container : ControlFlowItemBase
|
||||
{
|
||||
public ControlFlowItemCollection ContainerJobs = new ControlFlowItemCollection();
|
||||
}
|
||||
|
||||
public class ContainerCollection : List<Container>
|
||||
{
|
||||
}
|
||||
public class ContainerCollection : List<Container>
|
||||
{
|
||||
}
|
||||
|
||||
public class DataFlowTask_DSL : ControlFlowItemBase
|
||||
{
|
||||
}
|
||||
public class DataFlowTask_DSL : ControlFlowItemBase
|
||||
{
|
||||
}
|
||||
|
||||
public class DataFlowTaskCollection : List<DataFlowTask_DSL>
|
||||
{
|
||||
}
|
||||
public class DataFlowTaskCollection : List<DataFlowTask_DSL>
|
||||
{
|
||||
}
|
||||
|
||||
public class SequenceContainer_DSL : Container
|
||||
{
|
||||
}
|
||||
public class SequenceContainer_DSL : Container
|
||||
{
|
||||
}
|
||||
|
||||
public class BulkInsertTaskCollection : List<BulkInsertTask_DSL>
|
||||
{
|
||||
}
|
||||
public class BulkInsertTaskCollection : List<BulkInsertTask_DSL>
|
||||
{
|
||||
}
|
||||
|
||||
public class BulkInsertTask_DSL
|
||||
{
|
||||
}
|
||||
public class BulkInsertTask_DSL
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@
|
||||
using Newtonsoft.Json.Schema;
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
|
||||
#else
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||
@@ -34,13 +35,13 @@ using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAtt
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Schema
|
||||
{
|
||||
[TestFixture]
|
||||
public class JsonSchemaModelBuilderTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ExtendedComplex()
|
||||
[TestFixture]
|
||||
public class JsonSchemaModelBuilderTests : TestFixtureBase
|
||||
{
|
||||
string first = @"{
|
||||
[Test]
|
||||
public void ExtendedComplex()
|
||||
{
|
||||
string first = @"{
|
||||
""id"":""first"",
|
||||
""type"":""object"",
|
||||
""properties"":
|
||||
@@ -58,7 +59,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""additionalProperties"":{}
|
||||
}";
|
||||
|
||||
string second = @"{
|
||||
string second = @"{
|
||||
""id"":""second"",
|
||||
""type"":""object"",
|
||||
""extends"":{""$ref"":""first""},
|
||||
@@ -85,65 +86,65 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""additionalProperties"":false
|
||||
}";
|
||||
|
||||
JsonSchemaResolver resolver = new JsonSchemaResolver();
|
||||
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
|
||||
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
|
||||
JsonSchemaResolver resolver = new JsonSchemaResolver();
|
||||
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
|
||||
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
|
||||
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
|
||||
JsonSchemaModel model = modelBuilder.Build(secondSchema);
|
||||
JsonSchemaModel model = modelBuilder.Build(secondSchema);
|
||||
|
||||
Assert.AreEqual(4, model.Properties.Count);
|
||||
Assert.AreEqual(4, model.Properties.Count);
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String, model.Properties["firstproperty"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.String, model.Properties["firstproperty"].Type);
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String, model.Properties["secondproperty"].Type);
|
||||
Assert.AreEqual(10, model.Properties["secondproperty"].MaximumLength);
|
||||
Assert.AreEqual(null, model.Properties["secondproperty"].Enum);
|
||||
Assert.AreEqual(null, model.Properties["secondproperty"].Patterns);
|
||||
Assert.AreEqual(JsonSchemaType.String, model.Properties["secondproperty"].Type);
|
||||
Assert.AreEqual(10, model.Properties["secondproperty"].MaximumLength);
|
||||
Assert.AreEqual(null, model.Properties["secondproperty"].Enum);
|
||||
Assert.AreEqual(null, model.Properties["secondproperty"].Patterns);
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Object, model.Properties["thirdproperty"].Type);
|
||||
Assert.AreEqual(3, model.Properties["thirdproperty"].AdditionalProperties.Enum.Count);
|
||||
Assert.AreEqual("two", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[0]);
|
||||
Assert.AreEqual("three", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[1]);
|
||||
Assert.AreEqual("one", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[2]);
|
||||
Assert.AreEqual(JsonSchemaType.Object, model.Properties["thirdproperty"].Type);
|
||||
Assert.AreEqual(3, model.Properties["thirdproperty"].AdditionalProperties.Enum.Count);
|
||||
Assert.AreEqual("two", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[0]);
|
||||
Assert.AreEqual("three", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[1]);
|
||||
Assert.AreEqual("one", (string)model.Properties["thirdproperty"].AdditionalProperties.Enum[2]);
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Type);
|
||||
Assert.AreEqual(9, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MaximumLength);
|
||||
Assert.AreEqual(7, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MinimumLength);
|
||||
Assert.AreEqual(2, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns.Count);
|
||||
Assert.AreEqual("hi", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[0]);
|
||||
Assert.AreEqual("hi2u", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[1]);
|
||||
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Properties);
|
||||
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Items);
|
||||
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].AdditionalProperties);
|
||||
}
|
||||
Assert.AreEqual(JsonSchemaType.String, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Type);
|
||||
Assert.AreEqual(9, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MaximumLength);
|
||||
Assert.AreEqual(7, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].MinimumLength);
|
||||
Assert.AreEqual(2, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns.Count);
|
||||
Assert.AreEqual("hi", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[0]);
|
||||
Assert.AreEqual("hi2u", model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Patterns[1]);
|
||||
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Properties);
|
||||
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].Items);
|
||||
Assert.AreEqual(null, model.Properties["thirdproperty"].Properties["thirdproperty_firstproperty"].AdditionalProperties);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void CircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
""id"":""CircularReferenceArray"",
|
||||
""description"":""CircularReference"",
|
||||
""type"":[""array""],
|
||||
""items"":{""$ref"":""CircularReferenceArray""}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(json);
|
||||
JsonSchema schema = JsonSchema.Parse(json);
|
||||
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
|
||||
JsonSchemaModel model = modelBuilder.Build(schema);
|
||||
JsonSchemaModel model = modelBuilder.Build(schema);
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Array, model.Type);
|
||||
Assert.AreEqual(JsonSchemaType.Array, model.Type);
|
||||
|
||||
Assert.AreEqual(model, model.Items[0]);
|
||||
}
|
||||
Assert.AreEqual(model, model.Items[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Required()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
[Test]
|
||||
public void Required()
|
||||
{
|
||||
string schemaJson = @"{
|
||||
""description"":""A person"",
|
||||
""type"":""object"",
|
||||
""properties"":
|
||||
@@ -154,15 +155,15 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
JsonSchemaModel model = modelBuilder.Build(schema);
|
||||
JsonSchema schema = JsonSchema.Parse(schemaJson);
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
JsonSchemaModel model = modelBuilder.Build(schema);
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.Object, model.Type);
|
||||
Assert.AreEqual(3, model.Properties.Count);
|
||||
Assert.AreEqual(false, model.Properties["name"].Required);
|
||||
Assert.AreEqual(true, model.Properties["hobbies"].Required);
|
||||
Assert.AreEqual(true, model.Properties["age"].Required);
|
||||
Assert.AreEqual(JsonSchemaType.Object, model.Type);
|
||||
Assert.AreEqual(3, model.Properties.Count);
|
||||
Assert.AreEqual(false, model.Properties["name"].Required);
|
||||
Assert.AreEqual(true, model.Properties["hobbies"].Required);
|
||||
Assert.AreEqual(true, model.Properties["age"].Required);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@
|
||||
using Newtonsoft.Json.Schema;
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
|
||||
#else
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||
@@ -34,13 +35,13 @@ using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAtt
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Schema
|
||||
{
|
||||
[TestFixture]
|
||||
public class JsonSchemaNodeTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void AddSchema()
|
||||
[TestFixture]
|
||||
public class JsonSchemaNodeTests : TestFixtureBase
|
||||
{
|
||||
string first = @"{
|
||||
[Test]
|
||||
public void AddSchema()
|
||||
{
|
||||
string first = @"{
|
||||
""id"":""first"",
|
||||
""type"":""object"",
|
||||
""properties"":
|
||||
@@ -57,7 +58,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""additionalProperties"":{}
|
||||
}";
|
||||
|
||||
string second = @"{
|
||||
string second = @"{
|
||||
""id"":""second"",
|
||||
""type"":""object"",
|
||||
""extends"":{""$ref"":""first""},
|
||||
@@ -82,40 +83,39 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""additionalProperties"":false
|
||||
}";
|
||||
|
||||
JsonSchemaResolver resolver = new JsonSchemaResolver();
|
||||
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
|
||||
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
|
||||
JsonSchemaResolver resolver = new JsonSchemaResolver();
|
||||
JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
|
||||
JsonSchema secondSchema = JsonSchema.Parse(second, resolver);
|
||||
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
|
||||
JsonSchemaNode node = modelBuilder.AddSchema(null, secondSchema);
|
||||
JsonSchemaNode node = modelBuilder.AddSchema(null, secondSchema);
|
||||
|
||||
Assert.AreEqual(2, node.Schemas.Count);
|
||||
Assert.AreEqual(2, node.Properties["firstproperty"].Schemas.Count);
|
||||
Assert.AreEqual(3, node.Properties["secondproperty"].Schemas.Count);
|
||||
Assert.AreEqual(3, node.Properties["secondproperty"].Properties["secondproperty_firstproperty"].Schemas.Count);
|
||||
}
|
||||
Assert.AreEqual(2, node.Schemas.Count);
|
||||
Assert.AreEqual(2, node.Properties["firstproperty"].Schemas.Count);
|
||||
Assert.AreEqual(3, node.Properties["secondproperty"].Schemas.Count);
|
||||
Assert.AreEqual(3, node.Properties["secondproperty"].Properties["secondproperty_firstproperty"].Schemas.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void CircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
""id"":""CircularReferenceArray"",
|
||||
""description"":""CircularReference"",
|
||||
""type"":[""array""],
|
||||
""items"":{""$ref"":""CircularReferenceArray""}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(json);
|
||||
JsonSchema schema = JsonSchema.Parse(json);
|
||||
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();
|
||||
|
||||
JsonSchemaNode node = modelBuilder.AddSchema(null, schema);
|
||||
JsonSchemaNode node = modelBuilder.AddSchema(null, schema);
|
||||
|
||||
Assert.AreEqual(1, node.Schemas.Count);
|
||||
Assert.AreEqual(1, node.Schemas.Count);
|
||||
|
||||
Assert.AreEqual(node, node.Items[0]);
|
||||
Assert.AreEqual(node, node.Items[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,86 +10,86 @@ using Newtonsoft.Json.Schema;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Schema
|
||||
{
|
||||
public class JsonSchemaSpecTest
|
||||
{
|
||||
public string FileName { get; set; }
|
||||
public string TestCaseDescription { get; set; }
|
||||
public JObject Schema { get; set; }
|
||||
public string TestDescription { get; set; }
|
||||
public JToken Data { get; set; }
|
||||
public bool IsValid { get; set; }
|
||||
public int TestNumber { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
public class JsonSchemaSpecTest
|
||||
{
|
||||
return FileName + " - " + TestCaseDescription + " - " + TestDescription;
|
||||
}
|
||||
}
|
||||
public string FileName { get; set; }
|
||||
public string TestCaseDescription { get; set; }
|
||||
public JObject Schema { get; set; }
|
||||
public string TestDescription { get; set; }
|
||||
public JToken Data { get; set; }
|
||||
public bool IsValid { get; set; }
|
||||
public int TestNumber { get; set; }
|
||||
|
||||
[TestFixture]
|
||||
public class JsonSchemaSpecTests : TestFixtureBase
|
||||
{
|
||||
[TestCaseSourceAttribute("GetSpecTestDetails")]
|
||||
public void SpecTest(JsonSchemaSpecTest jsonSchemaSpecTest)
|
||||
{
|
||||
//if (jsonSchemaSpecTest.ToString() == "enum.json - simple enum validation - something else is invalid")
|
||||
{
|
||||
Console.WriteLine("Running JSON Schema test " + jsonSchemaSpecTest.TestNumber + ": " + jsonSchemaSpecTest);
|
||||
|
||||
JsonSchema s = JsonSchema.Read(jsonSchemaSpecTest.Schema.CreateReader());
|
||||
|
||||
IList<string> errorMessages;
|
||||
bool v = jsonSchemaSpecTest.Data.IsValid(s, out errorMessages);
|
||||
errorMessages = errorMessages ?? new List<string>();
|
||||
|
||||
Assert.AreEqual(jsonSchemaSpecTest.IsValid, v, jsonSchemaSpecTest.TestCaseDescription + " - " + jsonSchemaSpecTest.TestDescription + " - errors: " + string.Join(", ", errorMessages));
|
||||
}
|
||||
}
|
||||
|
||||
public IList<JsonSchemaSpecTest> GetSpecTestDetails()
|
||||
{
|
||||
IList<JsonSchemaSpecTest> specTests = new List<JsonSchemaSpecTest>();
|
||||
|
||||
// get test files location relative to the test project dll
|
||||
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
||||
string baseTestPath = Path.Combine(baseDirectory, "Schema", "Specs");
|
||||
|
||||
string[] testFiles = Directory.GetFiles(baseTestPath, "*.json", SearchOption.AllDirectories);
|
||||
|
||||
// read through each of the *.json test files and extract the test details
|
||||
foreach (string testFile in testFiles)
|
||||
{
|
||||
string testJson = System.IO.File.ReadAllText(testFile);
|
||||
|
||||
JArray a = JArray.Parse(testJson);
|
||||
|
||||
foreach (JObject testCase in a)
|
||||
public override string ToString()
|
||||
{
|
||||
foreach (JObject test in testCase["tests"])
|
||||
{
|
||||
JsonSchemaSpecTest jsonSchemaSpecTest = new JsonSchemaSpecTest();
|
||||
return FileName + " - " + TestCaseDescription + " - " + TestDescription;
|
||||
}
|
||||
}
|
||||
|
||||
jsonSchemaSpecTest.FileName = Path.GetFileName(testFile);
|
||||
jsonSchemaSpecTest.TestCaseDescription = (string) testCase["description"];
|
||||
jsonSchemaSpecTest.Schema = (JObject) testCase["schema"];
|
||||
[TestFixture]
|
||||
public class JsonSchemaSpecTests : TestFixtureBase
|
||||
{
|
||||
[TestCaseSourceAttribute("GetSpecTestDetails")]
|
||||
public void SpecTest(JsonSchemaSpecTest jsonSchemaSpecTest)
|
||||
{
|
||||
//if (jsonSchemaSpecTest.ToString() == "enum.json - simple enum validation - something else is invalid")
|
||||
{
|
||||
Console.WriteLine("Running JSON Schema test " + jsonSchemaSpecTest.TestNumber + ": " + jsonSchemaSpecTest);
|
||||
|
||||
jsonSchemaSpecTest.TestDescription = (string) test["description"];
|
||||
jsonSchemaSpecTest.Data = test["data"];
|
||||
jsonSchemaSpecTest.IsValid = (bool) test["valid"];
|
||||
jsonSchemaSpecTest.TestNumber = specTests.Count + 1;
|
||||
JsonSchema s = JsonSchema.Read(jsonSchemaSpecTest.Schema.CreateReader());
|
||||
|
||||
specTests.Add(jsonSchemaSpecTest);
|
||||
}
|
||||
IList<string> errorMessages;
|
||||
bool v = jsonSchemaSpecTest.Data.IsValid(s, out errorMessages);
|
||||
errorMessages = errorMessages ?? new List<string>();
|
||||
|
||||
Assert.AreEqual(jsonSchemaSpecTest.IsValid, v, jsonSchemaSpecTest.TestCaseDescription + " - " + jsonSchemaSpecTest.TestDescription + " - errors: " + string.Join(", ", errorMessages));
|
||||
}
|
||||
}
|
||||
|
||||
public IList<JsonSchemaSpecTest> GetSpecTestDetails()
|
||||
{
|
||||
IList<JsonSchemaSpecTest> specTests = new List<JsonSchemaSpecTest>();
|
||||
|
||||
// get test files location relative to the test project dll
|
||||
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
||||
string baseTestPath = Path.Combine(baseDirectory, "Schema", "Specs");
|
||||
|
||||
string[] testFiles = Directory.GetFiles(baseTestPath, "*.json", SearchOption.AllDirectories);
|
||||
|
||||
// read through each of the *.json test files and extract the test details
|
||||
foreach (string testFile in testFiles)
|
||||
{
|
||||
string testJson = System.IO.File.ReadAllText(testFile);
|
||||
|
||||
JArray a = JArray.Parse(testJson);
|
||||
|
||||
foreach (JObject testCase in a)
|
||||
{
|
||||
foreach (JObject test in testCase["tests"])
|
||||
{
|
||||
JsonSchemaSpecTest jsonSchemaSpecTest = new JsonSchemaSpecTest();
|
||||
|
||||
jsonSchemaSpecTest.FileName = Path.GetFileName(testFile);
|
||||
jsonSchemaSpecTest.TestCaseDescription = (string)testCase["description"];
|
||||
jsonSchemaSpecTest.Schema = (JObject)testCase["schema"];
|
||||
|
||||
jsonSchemaSpecTest.TestDescription = (string)test["description"];
|
||||
jsonSchemaSpecTest.Data = test["data"];
|
||||
jsonSchemaSpecTest.IsValid = (bool)test["valid"];
|
||||
jsonSchemaSpecTest.TestNumber = specTests.Count + 1;
|
||||
|
||||
specTests.Add(jsonSchemaSpecTest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
specTests = specTests.Where(s => s.FileName != "dependencies.json"
|
||||
&& s.TestCaseDescription != "multiple disallow subschema"
|
||||
&& s.TestCaseDescription != "types from separate schemas are merged"
|
||||
&& s.TestCaseDescription != "when types includes a schema it should fully validate the schema"
|
||||
&& s.TestCaseDescription != "types can include schemas").ToList();
|
||||
|
||||
return specTests;
|
||||
}
|
||||
}
|
||||
|
||||
specTests = specTests.Where(s => s.FileName != "dependencies.json"
|
||||
&& s.TestCaseDescription != "multiple disallow subschema"
|
||||
&& s.TestCaseDescription != "types from separate schemas are merged"
|
||||
&& s.TestCaseDescription != "when types includes a schema it should fully validate the schema"
|
||||
&& s.TestCaseDescription != "types can include schemas").ToList();
|
||||
|
||||
return specTests;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Schema;
|
||||
#if !NETFX_CORE
|
||||
using NUnit.Framework;
|
||||
|
||||
#else
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
|
||||
@@ -38,54 +39,54 @@ using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAtt
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Schema
|
||||
{
|
||||
[TestFixture]
|
||||
public class JsonSchemaTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Extends()
|
||||
[TestFixture]
|
||||
public class JsonSchemaTests : TestFixtureBase
|
||||
{
|
||||
string json;
|
||||
JsonSchemaResolver resolver = new JsonSchemaResolver();
|
||||
[Test]
|
||||
public void Extends()
|
||||
{
|
||||
string json;
|
||||
JsonSchemaResolver resolver = new JsonSchemaResolver();
|
||||
|
||||
json = @"{
|
||||
json = @"{
|
||||
""id"":""first"",
|
||||
""type"":""object"",
|
||||
""additionalProperties"":{}
|
||||
}";
|
||||
|
||||
JsonSchema first = JsonSchema.Parse(json, resolver);
|
||||
JsonSchema first = JsonSchema.Parse(json, resolver);
|
||||
|
||||
json =
|
||||
@"{
|
||||
json =
|
||||
@"{
|
||||
""id"":""second"",
|
||||
""type"":""object"",
|
||||
""extends"":{""$ref"":""first""},
|
||||
""additionalProperties"":{""type"":""string""}
|
||||
}";
|
||||
|
||||
JsonSchema second = JsonSchema.Parse(json, resolver);
|
||||
Assert.AreEqual(first, second.Extends[0]);
|
||||
JsonSchema second = JsonSchema.Parse(json, resolver);
|
||||
Assert.AreEqual(first, second.Extends[0]);
|
||||
|
||||
json =
|
||||
@"{
|
||||
json =
|
||||
@"{
|
||||
""id"":""third"",
|
||||
""type"":""object"",
|
||||
""extends"":{""$ref"":""second""},
|
||||
""additionalProperties"":false
|
||||
}";
|
||||
|
||||
JsonSchema third = JsonSchema.Parse(json, resolver);
|
||||
Assert.AreEqual(second, third.Extends[0]);
|
||||
Assert.AreEqual(first, third.Extends[0].Extends[0]);
|
||||
JsonSchema third = JsonSchema.Parse(json, resolver);
|
||||
Assert.AreEqual(second, third.Extends[0]);
|
||||
Assert.AreEqual(first, third.Extends[0].Extends[0]);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
third.WriteTo(jsonWriter, resolver);
|
||||
third.WriteTo(jsonWriter, resolver);
|
||||
|
||||
string writtenJson = writer.ToString();
|
||||
Assert.AreEqual(@"{
|
||||
string writtenJson = writer.ToString();
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""third"",
|
||||
""type"": ""object"",
|
||||
""additionalProperties"": false,
|
||||
@@ -94,14 +95,14 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}", writtenJson);
|
||||
|
||||
StringWriter writer1 = new StringWriter();
|
||||
JsonTextWriter jsonWriter1 = new JsonTextWriter(writer1);
|
||||
jsonWriter1.Formatting = Formatting.Indented;
|
||||
StringWriter writer1 = new StringWriter();
|
||||
JsonTextWriter jsonWriter1 = new JsonTextWriter(writer1);
|
||||
jsonWriter1.Formatting = Formatting.Indented;
|
||||
|
||||
third.WriteTo(jsonWriter1);
|
||||
third.WriteTo(jsonWriter1);
|
||||
|
||||
writtenJson = writer1.ToString();
|
||||
Assert.AreEqual(@"{
|
||||
writtenJson = writer1.ToString();
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""third"",
|
||||
""type"": ""object"",
|
||||
""additionalProperties"": false,
|
||||
@@ -118,26 +119,26 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", writtenJson);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Extends_Multiple()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void Extends_Multiple()
|
||||
{
|
||||
string json = @"{
|
||||
""type"":""object"",
|
||||
""extends"":{""type"":""string""},
|
||||
""additionalProperties"":{""type"":""string""}
|
||||
}";
|
||||
|
||||
JsonSchema s = JsonSchema.Parse(json);
|
||||
JsonSchema s = JsonSchema.Parse(json);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
string newJson = s.ToString();
|
||||
string newJson = s.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""type"": ""object"",
|
||||
""additionalProperties"": {
|
||||
""type"": ""string""
|
||||
@@ -146,23 +147,23 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""type"": ""string""
|
||||
}
|
||||
}", newJson);
|
||||
|
||||
|
||||
json = @"{
|
||||
|
||||
json = @"{
|
||||
""type"":""object"",
|
||||
""extends"":[{""type"":""string""}],
|
||||
""additionalProperties"":{""type"":""string""}
|
||||
}";
|
||||
|
||||
s = JsonSchema.Parse(json);
|
||||
s = JsonSchema.Parse(json);
|
||||
|
||||
writer = new StringWriter();
|
||||
jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
writer = new StringWriter();
|
||||
jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
newJson = s.ToString();
|
||||
newJson = s.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""type"": ""object"",
|
||||
""additionalProperties"": {
|
||||
""type"": ""string""
|
||||
@@ -173,21 +174,21 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}", newJson);
|
||||
|
||||
|
||||
json = @"{
|
||||
json = @"{
|
||||
""type"":""object"",
|
||||
""extends"":[{""type"":""string""},{""type"":""object""}],
|
||||
""additionalProperties"":{""type"":""string""}
|
||||
}";
|
||||
|
||||
s = JsonSchema.Parse(json);
|
||||
s = JsonSchema.Parse(json);
|
||||
|
||||
writer = new StringWriter();
|
||||
jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
writer = new StringWriter();
|
||||
jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
newJson = s.ToString();
|
||||
newJson = s.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""type"": ""object"",
|
||||
""additionalProperties"": {
|
||||
""type"": ""string""
|
||||
@@ -201,26 +202,26 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
]
|
||||
}", newJson);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_AdditionalProperties()
|
||||
{
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
[Test]
|
||||
public void WriteTo_AdditionalProperties()
|
||||
{
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""description"":""AdditionalProperties"",
|
||||
""type"":[""string"", ""integer""],
|
||||
""additionalProperties"":{""type"":[""object"", ""boolean""]}
|
||||
}");
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""description"": ""AdditionalProperties"",
|
||||
""type"": [
|
||||
""string"",
|
||||
@@ -233,12 +234,12 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
]
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_Properties()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
[Test]
|
||||
public void WriteTo_Properties()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""description"":""A person"",
|
||||
""type"":""object"",
|
||||
""properties"":
|
||||
@@ -252,15 +253,15 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}");
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""description"": ""A person"",
|
||||
""type"": ""object"",
|
||||
""properties"": {
|
||||
@@ -275,28 +276,27 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_Enum()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
[Test]
|
||||
public void WriteTo_Enum()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""description"":""Type"",
|
||||
""type"":[""string"",""array""],
|
||||
""items"":{},
|
||||
""enum"":[""string"",""object"",""array"",""boolean"",""number"",""integer"",""null"",""any""]
|
||||
}");
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""description"": ""Type"",
|
||||
""type"": [
|
||||
""string"",
|
||||
@@ -314,29 +314,29 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""any""
|
||||
]
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_CircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
[Test]
|
||||
public void WriteTo_CircularReference()
|
||||
{
|
||||
string json = @"{
|
||||
""id"":""CircularReferenceArray"",
|
||||
""description"":""CircularReference"",
|
||||
""type"":[""array""],
|
||||
""items"":{""$ref"":""CircularReferenceArray""}
|
||||
}";
|
||||
|
||||
JsonSchema schema = JsonSchema.Parse(json);
|
||||
JsonSchema schema = JsonSchema.Parse(json);
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string writtenJson = writer.ToString();
|
||||
string writtenJson = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""CircularReferenceArray"",
|
||||
""description"": ""CircularReference"",
|
||||
""type"": ""array"",
|
||||
@@ -344,27 +344,27 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""$ref"": ""CircularReferenceArray""
|
||||
}
|
||||
}", writtenJson);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_DisallowMultiple()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
[Test]
|
||||
public void WriteTo_DisallowMultiple()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""description"":""Type"",
|
||||
""type"":[""string"",""array""],
|
||||
""items"":{},
|
||||
""disallow"":[""string"",""object"",""array""]
|
||||
}");
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""description"": ""Type"",
|
||||
""type"": [
|
||||
""string"",
|
||||
@@ -377,27 +377,27 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""array""
|
||||
]
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_DisallowSingle()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
[Test]
|
||||
public void WriteTo_DisallowSingle()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""description"":""Type"",
|
||||
""type"":[""string"",""array""],
|
||||
""items"":{},
|
||||
""disallow"":""any""
|
||||
}");
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""description"": ""Type"",
|
||||
""type"": [
|
||||
""string"",
|
||||
@@ -406,160 +406,160 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""items"": {},
|
||||
""disallow"": ""any""
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_MultipleItems()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
[Test]
|
||||
public void WriteTo_MultipleItems()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""items"":[{},{}]
|
||||
}");
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""items"": [
|
||||
{},
|
||||
{}
|
||||
]
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_ExclusiveMinimum_ExclusiveMaximum()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.ExclusiveMinimum = true;
|
||||
schema.ExclusiveMaximum = true;
|
||||
[Test]
|
||||
public void WriteTo_ExclusiveMinimum_ExclusiveMaximum()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.ExclusiveMinimum = true;
|
||||
schema.ExclusiveMaximum = true;
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""exclusiveMinimum"": true,
|
||||
""exclusiveMaximum"": true
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_PatternProperties()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.PatternProperties = new Dictionary<string, JsonSchema>
|
||||
[Test]
|
||||
public void WriteTo_PatternProperties()
|
||||
{
|
||||
{ "[abc]", new JsonSchema() }
|
||||
};
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.PatternProperties = new Dictionary<string, JsonSchema>
|
||||
{
|
||||
{ "[abc]", new JsonSchema() }
|
||||
};
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""patternProperties"": {
|
||||
""[abc]"": {}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ToString_AdditionalItems()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
[Test]
|
||||
public void ToString_AdditionalItems()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""additionalItems"": {""type"": ""integer""}
|
||||
}");
|
||||
|
||||
string json = schema.ToString();
|
||||
string json = schema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""additionalItems"": {
|
||||
""type"": ""integer""
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_PositionalItemsValidation_True()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.PositionalItemsValidation = true;
|
||||
[Test]
|
||||
public void WriteTo_PositionalItemsValidation_True()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.PositionalItemsValidation = true;
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""items"": []
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_PositionalItemsValidation_TrueWithItemsSchema()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.PositionalItemsValidation = true;
|
||||
schema.Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String }};
|
||||
[Test]
|
||||
public void WriteTo_PositionalItemsValidation_TrueWithItemsSchema()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.PositionalItemsValidation = true;
|
||||
schema.Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String } };
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""items"": [
|
||||
{
|
||||
""type"": ""string""
|
||||
}
|
||||
]
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteTo_PositionalItemsValidation_FalseWithItemsSchema()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String } };
|
||||
[Test]
|
||||
public void WriteTo_PositionalItemsValidation_FalseWithItemsSchema()
|
||||
{
|
||||
JsonSchema schema = new JsonSchema();
|
||||
schema.Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String } };
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
|
||||
jsonWriter.Formatting = Formatting.Indented;
|
||||
|
||||
schema.WriteTo(jsonWriter);
|
||||
schema.WriteTo(jsonWriter);
|
||||
|
||||
string json = writer.ToString();
|
||||
string json = writer.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
Assert.AreEqual(@"{
|
||||
""items"": {
|
||||
""type"": ""string""
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IntegerValidatesAgainstFloatFlags()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
[Test]
|
||||
public void IntegerValidatesAgainstFloatFlags()
|
||||
{
|
||||
JsonSchema schema = JsonSchema.Parse(@"{
|
||||
""type"": ""object"",
|
||||
""$schema"": ""http://json-schema.org/draft-03/schema"",
|
||||
""required"": false,
|
||||
@@ -574,11 +574,11 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
}
|
||||
}");
|
||||
|
||||
JObject json = JObject.Parse(@"{
|
||||
JObject json = JObject.Parse(@"{
|
||||
""NumberProperty"": 23
|
||||
}");
|
||||
|
||||
Assert.IsTrue(json.IsValid(schema));
|
||||
Assert.IsTrue(json.IsValid(schema));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user