-JSON Schema fixes

-Fixed static values being included with fields serialization
-Examples! (work in progress)
This commit is contained in:
James Newton-King
2013-01-23 21:39:54 +13:00
parent 9187d8a6b3
commit 68eacc480b
87 changed files with 3519 additions and 53 deletions
@@ -565,5 +565,85 @@ namespace Newtonsoft.Json.Tests.Schema
Assert.AreEqual(JsonSchemaType.Object, schema.Properties["slash"].Type);
Assert.AreEqual(JsonSchemaType.Array, schema.Properties["percent"].Type);
}
[Test]
public void References_Array()
{
string json = @"{
""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
""properties"": {
""array"": {""$ref"": ""#/array/0""},
""arrayprop"": {""$ref"": ""#/array/1/prop""}
}
}";
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);
}
[Test]
public void References_IndexTooBig()
{
// JsonException : Could not resolve schema reference '#/array/10'.
string json = @"{
""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
""properties"": {
""array"": {""$ref"": ""#/array/0""},
""arrayprop"": {""$ref"": ""#/array/10""}
}
}";
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 = @"{
""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
""properties"": {
""array"": {""$ref"": ""#/array/0""},
""arrayprop"": {""$ref"": ""#/array/-1""}
}
}";
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 = @"{
""array"": [{""type"": ""integer""},{""prop"":{""type"": ""object""}}],
""properties"": {
""array"": {""$ref"": ""#/array/0""},
""arrayprop"": {""$ref"": ""#/array/one""}
}
}";
ExceptionAssert.Throws<JsonException>(
"Could not resolve schema reference '#/array/one'.",
() =>
{
JsonSchemaBuilder builder = new JsonSchemaBuilder(new JsonSchemaResolver());
builder.Read(new JsonTextReader(new StringReader(json)));
});
}
}
}
@@ -54,8 +54,6 @@ namespace Newtonsoft.Json.Tests.Schema
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string baseTestPath = Path.Combine(baseDirectory, "Schema", "Specs");
Console.WriteLine("Loading JSON Schema tests from " + baseTestPath);
string[] testFiles = Directory.GetFiles(baseTestPath, "*.json", SearchOption.AllDirectories);
// read through each of the *.json test files and extract the test details
@@ -23,6 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
@@ -119,6 +120,89 @@ namespace Newtonsoft.Json.Tests.Schema
}", writtenJson);
}
[Test]
public void Extends_Multiple()
{
string json = @"{
""type"":""object"",
""extends"":{""type"":""string""},
""additionalProperties"":{""type"":""string""}
}";
JsonSchema s = JsonSchema.Parse(json);
StringWriter writer = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
jsonWriter.Formatting = Formatting.Indented;
string newJson = s.ToString();
Assert.AreEqual(@"{
""type"": ""object"",
""additionalProperties"": {
""type"": ""string""
},
""extends"": {
""type"": ""string""
}
}", newJson);
json = @"{
""type"":""object"",
""extends"":[{""type"":""string""}],
""additionalProperties"":{""type"":""string""}
}";
s = JsonSchema.Parse(json);
writer = new StringWriter();
jsonWriter = new JsonTextWriter(writer);
jsonWriter.Formatting = Formatting.Indented;
newJson = s.ToString();
Assert.AreEqual(@"{
""type"": ""object"",
""additionalProperties"": {
""type"": ""string""
},
""extends"": {
""type"": ""string""
}
}", newJson);
json = @"{
""type"":""object"",
""extends"":[{""type"":""string""},{""type"":""object""}],
""additionalProperties"":{""type"":""string""}
}";
s = JsonSchema.Parse(json);
writer = new StringWriter();
jsonWriter = new JsonTextWriter(writer);
jsonWriter.Formatting = Formatting.Indented;
newJson = s.ToString();
Assert.AreEqual(@"{
""type"": ""object"",
""additionalProperties"": {
""type"": ""string""
},
""extends"": [
{
""type"": ""string""
},
{
""type"": ""object""
}
]
}", newJson);
}
[Test]
public void WriteTo_AdditionalProperties()
{