From 6cc5e49807696d6dadcd0befbe17c08c353688ba Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 18 May 2017 00:21:42 +1200 Subject: [PATCH 1/5] updated schema repository --- Json-Schema-Test-Suite | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Json-Schema-Test-Suite b/Json-Schema-Test-Suite index 5fb3d9f..67c7b4d 160000 --- a/Json-Schema-Test-Suite +++ b/Json-Schema-Test-Suite @@ -1 +1 @@ -Subproject commit 5fb3d9f1a1c4136f544fbd0029942ea559732f8e +Subproject commit 67c7b4dc84c783d204e0c3a9324a63f3f7d9444d From e15c264c21eb2f09677d1e613ff7779e28496c63 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Mon, 22 May 2017 11:10:28 +1200 Subject: [PATCH 2/5] progress against reference failures --- Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs | 4 ++-- Manatee.Json/Schema/JsonSchema.cs | 10 ++++++++-- Manatee.Json/Schema/JsonSchemaReference.cs | 7 ++++--- Manatee.Json/UriExtensions.cs | 7 ++++++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs b/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs index dc8f73b..8e0a36f 100644 --- a/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs +++ b/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs @@ -30,9 +30,9 @@ static JsonSchemaTestSuite() public void RunSuite() { // uncomment and paste the filename of a test suite to debug it. - //_fileNameForDebugging = ""; + //_fileNameForDebugging = "ref.json"; // uncomment and paste the description of a test to debug it. - //_testNameForDebugging = "ref within ref valid"; + //_testNameForDebugging = "ref valid, maxItems ignored"; try { diff --git a/Manatee.Json/Schema/JsonSchema.cs b/Manatee.Json/Schema/JsonSchema.cs index 88f7ec6..8cf4e66 100644 --- a/Manatee.Json/Schema/JsonSchema.cs +++ b/Manatee.Json/Schema/JsonSchema.cs @@ -545,6 +545,12 @@ public virtual void FromJson(JsonValue json, JsonSerializer serializer) { var obj = json.Object; Id = obj.TryGetString("id"); + Uri uri; + if (!Id.IsNullOrWhiteSpace() && Uri.TryCreate(Id, UriKind.Absolute, out uri)) + { + DocumentPath = uri; + JsonSchemaRegistry.Register(this); + } Schema = obj.TryGetString("$schema"); Title = obj.TryGetString("title"); Description = obj.TryGetString("description"); @@ -561,10 +567,10 @@ public virtual void FromJson(JsonValue json, JsonSerializer serializer) if (obj.ContainsKey("additionalItems")) { if (obj["additionalItems"].Type == JsonValueType.Boolean) - + AdditionalItems = obj["additionalItems"].Boolean ? AdditionalItems.True : AdditionalItems.False; else - AdditionalItems = new AdditionalItems() { Definition = JsonSchemaFactory.FromJson(obj["additionalItems"], DocumentPath)}; + AdditionalItems = new AdditionalItems {Definition = JsonSchemaFactory.FromJson(obj["additionalItems"], DocumentPath)}; } MaxItems = (uint?) obj.TryGetNumber("maxItems"); MinItems = (uint?) obj.TryGetNumber("minItems"); diff --git a/Manatee.Json/Schema/JsonSchemaReference.cs b/Manatee.Json/Schema/JsonSchemaReference.cs index ee5c818..d112893 100644 --- a/Manatee.Json/Schema/JsonSchemaReference.cs +++ b/Manatee.Json/Schema/JsonSchemaReference.cs @@ -111,9 +111,9 @@ public override int GetHashCode() private JsonValue Resolve(JsonValue root) { - var referenceParts = Reference.Split(new[] {'#'}, StringSplitOptions.None); + var referenceParts = Reference.Split(new[] { '#' }, StringSplitOptions.None); var address = referenceParts[0]; - var path = referenceParts.Length > 1 ? referenceParts[1] : string.Empty; + var fragment = referenceParts.Length > 1 ? referenceParts[1] : string.Empty; var jValue = root; if (!address.IsNullOrWhiteSpace()) { @@ -122,6 +122,7 @@ private JsonValue Resolve(JsonValue root) if (DocumentPath != null && !Uri.TryCreate(address, UriKind.Absolute, out absolute)) { DocumentPath = new Uri(DocumentPath.GetParentUri(), address); + address = DocumentPath.OriginalString; } Uri uri; @@ -137,7 +138,7 @@ private JsonValue Resolve(JsonValue root) if (jValue == null) return root; if (jValue == _rootJson) throw new ArgumentException("Cannot use a root reference as the base schema."); - Resolved = ResolveLocalReference(jValue, path, DocumentPath); + Resolved = ResolveLocalReference(jValue, fragment, DocumentPath); return jValue; } private static IJsonSchema ResolveLocalReference(JsonValue root, string path, Uri documentPath) diff --git a/Manatee.Json/UriExtensions.cs b/Manatee.Json/UriExtensions.cs index 0947c0a..7d12b52 100644 --- a/Manatee.Json/UriExtensions.cs +++ b/Manatee.Json/UriExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace Manatee.Json { @@ -19,7 +20,11 @@ internal static class UriExtensions public static Uri GetParentUri(this Uri uri) { if (uri == null) throw new ArgumentNullException(nameof(uri)); - if (uri.Segments.Length == 1) throw new InvalidOperationException("Cannot get parent of root"); + if (!uri.IsAbsoluteUri && uri.Segments.Length == 1) throw new InvalidOperationException("Cannot get parent of root"); + + var path = uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length); + + return new Uri(path); string url = uri.ToString(); int index = url.Length - 1; From a3f27c0099d83c09f85eeeaee47c9bc2fb581167 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Sat, 27 May 2017 15:46:12 +1200 Subject: [PATCH 3/5] Added test to show that schema fails on wrong type when the JSON is a string with the name of a primitive type. Fixed schema references to ignore other properties per specification. --- Manatee.Json.Tests/Schema/ArraySchemaTest.cs | 11 +++++++++++ Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs | 8 ++++---- Manatee.Json/Schema/JsonSchemaReference.cs | 3 +-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Manatee.Json.Tests/Schema/ArraySchemaTest.cs b/Manatee.Json.Tests/Schema/ArraySchemaTest.cs index 4c388e2..8c706ca 100644 --- a/Manatee.Json.Tests/Schema/ArraySchemaTest.cs +++ b/Manatee.Json.Tests/Schema/ArraySchemaTest.cs @@ -20,6 +20,17 @@ public void ValidateReturnsErrorOnNonArray() Assert.AreEqual(false, results.Valid); } [TestMethod] + public void ValidateReturnsErrorOnString() + { + var schema = new JsonSchema {Type = JsonSchemaTypeDefinition.Array}; + JsonValue json = "string"; + + var results = schema.Validate(json); + + Assert.AreNotEqual(0, results.Errors.Count()); + Assert.AreEqual(false, results.Valid); + } + [TestMethod] public void ValidateReturnsErrorOnTooFewItems() { var schema = new JsonSchema {Type = JsonSchemaTypeDefinition.Array, MinItems = 5}; diff --git a/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs b/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs index 8e0a36f..556f5bc 100644 --- a/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs +++ b/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs @@ -13,7 +13,7 @@ public class JsonSchemaTestSuite private const string TestFolder = @"..\..\..\Json-Schema-Test-Suite\tests\draft4\"; private const string RemotesFolder = @"..\..\..\Json-Schema-Test-Suite\remotes\"; private static readonly Uri RemotesUri = new Uri(System.IO.Path.GetFullPath(RemotesFolder)); - private static readonly JsonSerializer _serializer; + private static readonly JsonSerializer Serializer; private int _failures; private int _passes; #pragma warning disable 649 @@ -23,7 +23,7 @@ public class JsonSchemaTestSuite static JsonSchemaTestSuite() { - _serializer = new JsonSerializer(); + Serializer = new JsonSerializer(); } [TestMethod] @@ -32,7 +32,7 @@ public void RunSuite() // uncomment and paste the filename of a test suite to debug it. //_fileNameForDebugging = "ref.json"; // uncomment and paste the description of a test to debug it. - //_testNameForDebugging = "ref valid, maxItems ignored"; + _testNameForDebugging = "ref invalid"; try { @@ -65,7 +65,7 @@ private void _RunFile(string fileName) var contents = File.ReadAllText(fileName); var json = JsonValue.Parse(contents); - var testSets = _serializer.Deserialize>(json); + var testSets = Serializer.Deserialize>(json); foreach (var testSet in testSets) { diff --git a/Manatee.Json/Schema/JsonSchemaReference.cs b/Manatee.Json/Schema/JsonSchemaReference.cs index d112893..fefb13a 100644 --- a/Manatee.Json/Schema/JsonSchemaReference.cs +++ b/Manatee.Json/Schema/JsonSchemaReference.cs @@ -54,12 +54,11 @@ public JsonSchemaReference(string reference) public override SchemaValidationResults Validate(JsonValue json, JsonValue root = null) { var jValue = root ?? ToJson(null); - var results = base.Validate(json, jValue); if (Resolved == null || root == null) jValue = Resolve(jValue); var refResults = Resolved?.Validate(json, jValue) ?? new SchemaValidationResults(null, "Error finding referenced schema."); - return new SchemaValidationResults(new[] {results, refResults}); + return new SchemaValidationResults(new[] {refResults}); } /// /// Builds an object from a . From 137b8b86555528e069539726f0e775283da02d1f Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Mon, 29 May 2017 00:08:48 +1200 Subject: [PATCH 4/5] progress... --- Manatee.Json.Tests/DevTest.cs | 11 ++++-- .../Schema/TestSuite/JsonSchemaTestSuite.cs | 13 ++++++- Manatee.Json.sln.DotSettings | 2 +- Manatee.Json/Internal/GeneralExtensions.cs | 12 ++++++ Manatee.Json/Schema/JsonSchema.cs | 17 ++++----- Manatee.Json/Schema/JsonSchemaFactory.cs | 3 +- Manatee.Json/Schema/JsonSchemaReference.cs | 44 +++++++++++++--------- Manatee.Json/Schema/JsonSchemaRegistry.cs | 2 +- .../Validators/TypeSchemaPropertyValidator.cs | 2 +- 9 files changed, 70 insertions(+), 36 deletions(-) diff --git a/Manatee.Json.Tests/DevTest.cs b/Manatee.Json.Tests/DevTest.cs index 56474a5..11b3672 100644 --- a/Manatee.Json.Tests/DevTest.cs +++ b/Manatee.Json.Tests/DevTest.cs @@ -1,16 +1,19 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Manatee.Json.Tests { [TestClass] - [Ignore] + //[Ignore] public class DevTest { [TestMethod] public void Test1() { - var text = "{\"key\":4,int:\"no\"}"; - var json = JsonValue.Parse(text); + var text = "http://www.google.com/file/"; + var uri = new Uri(text); + + Console.WriteLine(uri); } } } \ No newline at end of file diff --git a/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs b/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs index 556f5bc..ec54f5b 100644 --- a/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs +++ b/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs @@ -16,6 +16,8 @@ public class JsonSchemaTestSuite private static readonly JsonSerializer Serializer; private int _failures; private int _passes; + private string _currentFile; + private string _currentTest; #pragma warning disable 649 private string _fileNameForDebugging; private string _testNameForDebugging; @@ -32,7 +34,7 @@ public void RunSuite() // uncomment and paste the filename of a test suite to debug it. //_fileNameForDebugging = "ref.json"; // uncomment and paste the description of a test to debug it. - _testNameForDebugging = "ref invalid"; + _testNameForDebugging = "number is valid"; try { @@ -47,6 +49,13 @@ public void RunSuite() Assert.AreEqual(0, _failures); } + catch + { + _failures++; + Console.WriteLine(); + Console.WriteLine($"Failed on '{_currentTest}' in {_currentFile}"); + throw; + } finally { Console.WriteLine(); @@ -57,6 +66,7 @@ public void RunSuite() private void _RunFile(string fileName) { + _currentFile = fileName; if (fileName == _fileNameForDebugging) { System.Diagnostics.Debugger.Break(); @@ -78,6 +88,7 @@ private void _RunTestSet(SchemaTestSet testSet) { foreach (var test in testSet.Tests) { + _currentTest = test.Description; if (test.Description == _testNameForDebugging) { System.Diagnostics.Debugger.Break(); diff --git a/Manatee.Json.sln.DotSettings b/Manatee.Json.sln.DotSettings index f70fddb..1237c76 100644 --- a/Manatee.Json.sln.DotSettings +++ b/Manatee.Json.sln.DotSettings @@ -19,7 +19,7 @@ 200 <Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> - <Policy><Descriptor Staticness="Static, Instance" AccessRightKinds="Private" Description="Private methods, properties, and events"><ElementKinds><Kind Name="METHOD" /><Kind Name="EVENT" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy> + <Policy><Descriptor Staticness="Static, Instance" AccessRightKinds="Private" Description="Private methods"><ElementKinds><Kind Name="METHOD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="AaBb" /></Policy> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /> True True diff --git a/Manatee.Json/Internal/GeneralExtensions.cs b/Manatee.Json/Internal/GeneralExtensions.cs index 0449427..f7fe727 100644 --- a/Manatee.Json/Internal/GeneralExtensions.cs +++ b/Manatee.Json/Internal/GeneralExtensions.cs @@ -256,5 +256,17 @@ public static bool IsNumber(this object value) value is long || value is ulong; } + public static Uri GetBase(this Uri uri) + { + Uri previous = uri; + do + { + uri = previous; + previous = uri.GetParentUri(); + } while (uri != previous); + + return uri; + } + } } \ No newline at end of file diff --git a/Manatee.Json/Schema/JsonSchema.cs b/Manatee.Json/Schema/JsonSchema.cs index 8cf4e66..986ef95 100644 --- a/Manatee.Json/Schema/JsonSchema.cs +++ b/Manatee.Json/Schema/JsonSchema.cs @@ -535,6 +535,7 @@ public virtual SchemaValidationResults Validate(JsonValue json, JsonValue root = var results = validators.Select(v => v.Validate(this, json, jValue)).ToList(); return new SchemaValidationResults(results); } + /// /// Builds an object from a . /// @@ -546,7 +547,9 @@ public virtual void FromJson(JsonValue json, JsonSerializer serializer) var obj = json.Object; Id = obj.TryGetString("id"); Uri uri; - if (!Id.IsNullOrWhiteSpace() && Uri.TryCreate(Id, UriKind.Absolute, out uri)) + var uriFolder = DocumentPath?.OriginalString.EndsWith("/") ?? true ? DocumentPath : DocumentPath?.GetParentUri(); + if (!Id.IsNullOrWhiteSpace() && + (Uri.TryCreate(Id, UriKind.Absolute, out uri) || Uri.TryCreate(uriFolder + Id, UriKind.Absolute, out uri))) { DocumentPath = uri; JsonSchemaRegistry.Register(this); @@ -567,16 +570,14 @@ public virtual void FromJson(JsonValue json, JsonSerializer serializer) if (obj.ContainsKey("additionalItems")) { if (obj["additionalItems"].Type == JsonValueType.Boolean) - AdditionalItems = obj["additionalItems"].Boolean ? AdditionalItems.True : AdditionalItems.False; else - AdditionalItems = new AdditionalItems {Definition = JsonSchemaFactory.FromJson(obj["additionalItems"], DocumentPath)}; + AdditionalItems = new AdditionalItems {Definition = JsonSchemaFactory.FromJson(obj["additionalItems"], DocumentPath) }; } MaxItems = (uint?) obj.TryGetNumber("maxItems"); MinItems = (uint?) obj.TryGetNumber("minItems"); if (obj.ContainsKey("items")) Items = JsonSchemaFactory.FromJson(obj["items"], DocumentPath); - UniqueItems = obj.TryGetBoolean("uniqueItems"); MaxProperties = (uint?) obj.TryGetNumber("maxProperties"); MinProperties = (uint?) obj.TryGetNumber("minProperties"); @@ -617,15 +618,14 @@ public virtual void FromJson(JsonValue json, JsonSerializer serializer) if (obj["additionalProperties"].Type == JsonValueType.Boolean) AdditionalProperties = obj["additionalProperties"].Boolean ? AdditionalProperties.True : AdditionalProperties.False; else - AdditionalProperties = new AdditionalProperties() { Definition = JsonSchemaFactory.FromJson(obj["additionalProperties"], DocumentPath)}; - + AdditionalProperties = new AdditionalProperties {Definition = JsonSchemaFactory.FromJson(obj["additionalProperties"], DocumentPath)}; } if (obj.ContainsKey("definitions")) { Definitions = new JsonSchemaTypeDefinitionCollection(); foreach (var defn in obj["definitions"].Object) { - var definition = new JsonSchemaTypeDefinition(defn.Key) { Definition = JsonSchemaFactory.FromJson(defn.Value, DocumentPath) }; + var definition = new JsonSchemaTypeDefinition(defn.Key) {Definition = JsonSchemaFactory.FromJson(defn.Value, DocumentPath) }; Definitions.Add(definition); } @@ -642,8 +642,7 @@ public virtual void FromJson(JsonValue json, JsonSerializer serializer) switch (v.Value.Type) { case JsonValueType.Object: - dependency = new SchemaDependency(v.Key, JsonSchemaFactory.FromJson(v.Value, DocumentPath)); - + dependency = new SchemaDependency(v.Key, JsonSchemaFactory.FromJson(v.Value, DocumentPath)); break; case JsonValueType.Array: dependency = new PropertyDependency(v.Key, v.Value.Array.Select(jv => jv.String)); diff --git a/Manatee.Json/Schema/JsonSchemaFactory.cs b/Manatee.Json/Schema/JsonSchemaFactory.cs index c1e198e..cba0f3d 100644 --- a/Manatee.Json/Schema/JsonSchemaFactory.cs +++ b/Manatee.Json/Schema/JsonSchemaFactory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Manatee.Json.Internal; namespace Manatee.Json.Schema { @@ -56,7 +57,7 @@ public static IJsonSchema FromJson(JsonValue json, Uri documentPath = null) schema = new JsonSchemaCollection(); break; default: - throw new ArgumentOutOfRangeException("json.Type", "JSON Schema must be objects."); + throw new ArgumentOutOfRangeException(nameof(json.Type), "JSON Schema must be objects."); } schema.DocumentPath = documentPath; schema.FromJson(json, null); diff --git a/Manatee.Json/Schema/JsonSchemaReference.cs b/Manatee.Json/Schema/JsonSchemaReference.cs index fefb13a..f27796f 100644 --- a/Manatee.Json/Schema/JsonSchemaReference.cs +++ b/Manatee.Json/Schema/JsonSchemaReference.cs @@ -30,7 +30,7 @@ public class JsonSchemaReference : JsonSchema /// Exposes the schema at the references location. /// /// - /// The method must first be called. + /// The method must first be called. /// public IJsonSchema Resolved { get; private set; } @@ -55,7 +55,7 @@ public override SchemaValidationResults Validate(JsonValue json, JsonValue root { var jValue = root ?? ToJson(null); if (Resolved == null || root == null) - jValue = Resolve(jValue); + jValue = _Resolve(jValue); var refResults = Resolved?.Validate(json, jValue) ?? new SchemaValidationResults(null, "Error finding referenced schema."); return new SchemaValidationResults(new[] {refResults}); @@ -108,7 +108,7 @@ public override int GetHashCode() return Reference?.GetHashCode() ?? 0; } - private JsonValue Resolve(JsonValue root) + private JsonValue _Resolve(JsonValue root) { var referenceParts = Reference.Split(new[] { '#' }, StringSplitOptions.None); var address = referenceParts[0]; @@ -116,41 +116,49 @@ private JsonValue Resolve(JsonValue root) var jValue = root; if (!address.IsNullOrWhiteSpace()) { - Uri absolute; - if (DocumentPath != null && !Uri.TryCreate(address, UriKind.Absolute, out absolute)) + if (!Uri.TryCreate(address, UriKind.Absolute, out absolute)) { - DocumentPath = new Uri(DocumentPath.GetParentUri(), address); - address = DocumentPath.OriginalString; + address = Id + address; } - - Uri uri; - var search = JsonPathWith.Search("id"); - var allIds = new Stack(search.Evaluate(root ?? new JsonObject()).Select(jv => jv.String)); - while (allIds.Any() && !Uri.TryCreate(address, UriKind.Absolute, out uri)) + if (DocumentPath != null && !Uri.TryCreate(address, UriKind.Absolute, out absolute)) { - address = allIds.Pop() + address; + var uriFolder = DocumentPath.OriginalString.EndsWith("/") ? DocumentPath : DocumentPath.GetParentUri(); + absolute = new Uri(uriFolder, address); + address = absolute.OriginalString; } - jValue = JsonSchemaRegistry.Get(DocumentPath?.ToString() ?? address).ToJson(null); + jValue = JsonSchemaRegistry.Get(address).ToJson(null); + } + else + { + address = DocumentPath?.OriginalString; } if (jValue == null) return root; if (jValue == _rootJson) throw new ArgumentException("Cannot use a root reference as the base schema."); - Resolved = ResolveLocalReference(jValue, fragment, DocumentPath); + Resolved = _ResolveLocalReference(jValue, fragment, address == null ? null : new Uri(address)); return jValue; } - private static IJsonSchema ResolveLocalReference(JsonValue root, string path, Uri documentPath) + private static IJsonSchema _ResolveLocalReference(JsonValue root, string path, Uri documentPath) { var properties = path.Split('/').Skip(1).ToList(); if (!properties.Any()) return JsonSchemaFactory.FromJson(root, documentPath); var value = root; foreach (var property in properties) { - var unescaped = Unescape(property); + var unescaped = _Unescape(property); if (value.Type == JsonValueType.Object) { if (!value.Object.ContainsKey(unescaped)) return null; + JsonValue id; + if (value.Object.TryGetValue("id", out id)) + { + Uri uri; + documentPath = Uri.TryCreate(id.String, UriKind.Absolute, out uri) + ? uri + : new Uri(documentPath, id.String); + } value = value.Object[unescaped]; } else if (value.Type == JsonValueType.Array) @@ -162,7 +170,7 @@ private static IJsonSchema ResolveLocalReference(JsonValue root, string path, Ur } return JsonSchemaFactory.FromJson(value, documentPath); } - private static string Unescape(string reference) + private static string _Unescape(string reference) { var unescaped = reference.Replace("~1", "/") .Replace("~0", "~"); diff --git a/Manatee.Json/Schema/JsonSchemaRegistry.cs b/Manatee.Json/Schema/JsonSchemaRegistry.cs index 8f6e0da..c8807dd 100644 --- a/Manatee.Json/Schema/JsonSchemaRegistry.cs +++ b/Manatee.Json/Schema/JsonSchemaRegistry.cs @@ -62,7 +62,7 @@ public static void Register(JsonSchema schema) if (schema.Id.IsNullOrWhiteSpace()) return; lock (_schemaLookup) { - _schemaLookup[schema.Id] = schema; + _schemaLookup[schema.DocumentPath.ToString()] = schema; } } diff --git a/Manatee.Json/Schema/Validators/TypeSchemaPropertyValidator.cs b/Manatee.Json/Schema/Validators/TypeSchemaPropertyValidator.cs index b933666..5e4386a 100644 --- a/Manatee.Json/Schema/Validators/TypeSchemaPropertyValidator.cs +++ b/Manatee.Json/Schema/Validators/TypeSchemaPropertyValidator.cs @@ -33,7 +33,7 @@ public SchemaValidationResults Validate(JsonSchema schema, JsonValue json, JsonV if (json.Number.IsInt() && Equals(schema.Type, JsonSchemaTypeDefinition.Integer)) break; return new SchemaValidationResults(string.Empty, $"Expected: {schema.Type.Name}; Actual: {json.Type}."); case JsonValueType.String: - if (JsonSchemaTypeDefinition.PrimitiveDefinitions.Any(d => d.Name == json.String)) break; + if (schema.Type.Name == json.String) break; if (Equals(schema.Type, JsonSchemaTypeDefinition.String)) break; return new SchemaValidationResults(string.Empty, $"Expected: {schema.Type.Name}; Actual: {json.Type}."); case JsonValueType.Boolean: From 84da229166fb890c946026bc9b33f31e07114b4c Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Mon, 29 May 2017 01:16:39 +1200 Subject: [PATCH 5/5] all tests pass! --- Manatee.Json.Tests/DevTest.cs | 2 +- Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs | 14 ++++++++++---- Manatee.Json/Schema/JsonSchemaReference.cs | 9 ++------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Manatee.Json.Tests/DevTest.cs b/Manatee.Json.Tests/DevTest.cs index 11b3672..644d8ea 100644 --- a/Manatee.Json.Tests/DevTest.cs +++ b/Manatee.Json.Tests/DevTest.cs @@ -4,7 +4,7 @@ namespace Manatee.Json.Tests { [TestClass] - //[Ignore] + [Ignore] public class DevTest { [TestMethod] diff --git a/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs b/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs index ec54f5b..c9593c2 100644 --- a/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs +++ b/Manatee.Json.Tests/Schema/TestSuite/JsonSchemaTestSuite.cs @@ -34,7 +34,9 @@ public void RunSuite() // uncomment and paste the filename of a test suite to debug it. //_fileNameForDebugging = "ref.json"; // uncomment and paste the description of a test to debug it. - _testNameForDebugging = "number is valid"; + _testNameForDebugging = "object is invalid"; + + var ranAllTests = false; try { @@ -47,13 +49,17 @@ public void RunSuite() _RunFile(fileName); } + ranAllTests = true; Assert.AreEqual(0, _failures); } catch { - _failures++; - Console.WriteLine(); - Console.WriteLine($"Failed on '{_currentTest}' in {_currentFile}"); + if (!ranAllTests) + { + _failures++; + Console.WriteLine(); + Console.WriteLine($"Failed on '{_currentTest}' in {_currentFile}"); + } throw; } finally diff --git a/Manatee.Json/Schema/JsonSchemaReference.cs b/Manatee.Json/Schema/JsonSchemaReference.cs index f27796f..4a6cb34 100644 --- a/Manatee.Json/Schema/JsonSchemaReference.cs +++ b/Manatee.Json/Schema/JsonSchemaReference.cs @@ -111,7 +111,7 @@ public override int GetHashCode() private JsonValue _Resolve(JsonValue root) { var referenceParts = Reference.Split(new[] { '#' }, StringSplitOptions.None); - var address = referenceParts[0]; + var address = referenceParts[0].IsNullOrWhiteSpace() ? DocumentPath?.OriginalString : referenceParts[0]; var fragment = referenceParts.Length > 1 ? referenceParts[1] : string.Empty; var jValue = root; if (!address.IsNullOrWhiteSpace()) @@ -127,17 +127,12 @@ private JsonValue _Resolve(JsonValue root) absolute = new Uri(uriFolder, address); address = absolute.OriginalString; } - jValue = JsonSchemaRegistry.Get(address).ToJson(null); } - else - { - address = DocumentPath?.OriginalString; - } if (jValue == null) return root; if (jValue == _rootJson) throw new ArgumentException("Cannot use a root reference as the base schema."); - Resolved = _ResolveLocalReference(jValue, fragment, address == null ? null : new Uri(address)); + Resolved = _ResolveLocalReference(jValue, fragment, address.IsNullOrWhiteSpace() ? null : new Uri(address)); return jValue; } private static IJsonSchema _ResolveLocalReference(JsonValue root, string path, Uri documentPath)