From bdb3139b40c12662b7238b52fc12fdff95becd3a Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 20 Apr 2017 08:16:40 +1200 Subject: [PATCH 1/4] added replication test --- Manatee.Json.Tests/ClientTest.cs | 14 +++++++++++--- Manatee.Json.Tests/Files/issue55.json | 12 ++++++++++++ Manatee.Json.Tests/Manatee.Json.Tests.csproj | 3 +++ 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 Manatee.Json.Tests/Files/issue55.json diff --git a/Manatee.Json.Tests/ClientTest.cs b/Manatee.Json.Tests/ClientTest.cs index 39095ff..7565a0b 100644 --- a/Manatee.Json.Tests/ClientTest.cs +++ b/Manatee.Json.Tests/ClientTest.cs @@ -319,7 +319,6 @@ public void Issue49_RequiredAndAllOfInSingleSchema() public void Issue50_MulitpleSchemaInSubFoldersShouldReferenceRelatively() { string path = System.IO.Path.Combine(TestContext.TestDeploymentDir, @"Files\Issue50A.json"); - var schema = JsonSchemaRegistry.Get(path); var json = new JsonObject { @@ -339,11 +338,20 @@ public void Issue50_MulitpleSchemaInSubFoldersShouldReferenceRelatively() } } }; - - var results = schema.Validate(json); + Assert.IsTrue(results.Valid); + } + + [TestMethod] + [DeploymentItem(@"Files\Issue55.json")] + public void Issue55_EmptyJsonNotValidatedBySchema() + { + var path = System.IO.Path.Combine(TestContext.TestDeploymentDir, "issue55.json"); + var schema = JsonSchemaRegistry.Get(path); + var results = schema.Validate("{}"); Assert.IsTrue(results.Valid); + Assert.AreEqual(0, results.Errors.Count()); } } } diff --git a/Manatee.Json.Tests/Files/issue55.json b/Manatee.Json.Tests/Files/issue55.json new file mode 100644 index 0000000..9aadb4e --- /dev/null +++ b/Manatee.Json.Tests/Files/issue55.json @@ -0,0 +1,12 @@ +{ + "$schema" : "http://json-schema.org/draft-04/schema", + "title" : "JSON Schema for Universally Unique Identifiers (UUIDs).", + "type" : "object", + "properties": { + "uuid":{ + "description": "A universally unique identifier (UUID).", + "type":"string", + "pattern": "^([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})|[a-f0-9]{32}$" + } + } +} \ No newline at end of file diff --git a/Manatee.Json.Tests/Manatee.Json.Tests.csproj b/Manatee.Json.Tests/Manatee.Json.Tests.csproj index 6c0219b..a4c2eed 100644 --- a/Manatee.Json.Tests/Manatee.Json.Tests.csproj +++ b/Manatee.Json.Tests/Manatee.Json.Tests.csproj @@ -112,6 +112,9 @@ Always + + Always + Always From 3e244f52353f6df8e81190ef7bc918e348861c89 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Thu, 20 Apr 2017 11:29:22 +1200 Subject: [PATCH 2/4] made test pass --- Manatee.Json.Tests/ClientTest.cs | 32 ++++++++++++---------- .../Validators/TypeSchemaPropertyValidator.cs | 10 +++---- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Manatee.Json.Tests/ClientTest.cs b/Manatee.Json.Tests/ClientTest.cs index 7565a0b..e64dcfe 100644 --- a/Manatee.Json.Tests/ClientTest.cs +++ b/Manatee.Json.Tests/ClientTest.cs @@ -321,23 +321,21 @@ public void Issue50_MulitpleSchemaInSubFoldersShouldReferenceRelatively() string path = System.IO.Path.Combine(TestContext.TestDeploymentDir, @"Files\Issue50A.json"); var schema = JsonSchemaRegistry.Get(path); var json = new JsonObject - { - ["text"] = "something", - ["refa"] = new JsonObject { - ["text"] = "something else", - ["refb"] = new JsonObject() - { - ["refd"] = new JsonObject() + ["text"] = "something", + ["refa"] = new JsonObject { - ["refe"] = new JsonObject() { - ["test"] = "test" - }, - ["text"] = "test" + ["text"] = "something else", + ["refb"] = new JsonObject + { + ["refd"] = new JsonObject + { + ["refe"] = new JsonObject{["test"] = "test"}, + ["text"] = "test" + } + } } - } - } - }; + }; var results = schema.Validate(json); Assert.IsTrue(results.Valid); } @@ -350,8 +348,12 @@ public void Issue55_EmptyJsonNotValidatedBySchema() var schema = JsonSchemaRegistry.Get(path); var results = schema.Validate("{}"); + Assert.IsFalse(results.Valid); + Assert.AreEqual(1, results.Errors.Count()); + + results = schema.Validate(new JsonObject()); + Assert.IsTrue(results.Valid); - Assert.AreEqual(0, results.Errors.Count()); } } } diff --git a/Manatee.Json/Schema/Validators/TypeSchemaPropertyValidator.cs b/Manatee.Json/Schema/Validators/TypeSchemaPropertyValidator.cs index 3b3c63d..b933666 100644 --- a/Manatee.Json/Schema/Validators/TypeSchemaPropertyValidator.cs +++ b/Manatee.Json/Schema/Validators/TypeSchemaPropertyValidator.cs @@ -35,19 +35,19 @@ public SchemaValidationResults Validate(JsonSchema schema, JsonValue json, JsonV case JsonValueType.String: if (JsonSchemaTypeDefinition.PrimitiveDefinitions.Any(d => d.Name == json.String)) break; if (Equals(schema.Type, JsonSchemaTypeDefinition.String)) break; - return new SchemaValidationResults(string.Empty, $"Expected: String; Actual: {json.Type}."); + return new SchemaValidationResults(string.Empty, $"Expected: {schema.Type.Name}; Actual: {json.Type}."); case JsonValueType.Boolean: if (Equals(schema.Type, JsonSchemaTypeDefinition.Boolean)) break; - return new SchemaValidationResults(string.Empty, $"Expected: Boolean; Actual: {json.Type}."); + return new SchemaValidationResults(string.Empty, $"Expected: {schema.Type.Name}; Actual: {json.Type}."); case JsonValueType.Object: if (Equals(schema.Type, JsonSchemaTypeDefinition.Object)) break; - return new SchemaValidationResults(string.Empty, $"Expected: Object; Actual: {json.Type}."); + return new SchemaValidationResults(string.Empty, $"Expected: {schema.Type.Name}; Actual: {json.Type}."); case JsonValueType.Array: if (Equals(schema.Type, JsonSchemaTypeDefinition.Array)) break; - return new SchemaValidationResults(string.Empty, $"Expected: Array; Actual: {json.Type}."); + return new SchemaValidationResults(string.Empty, $"Expected: {schema.Type.Name}; Actual: {json.Type}."); case JsonValueType.Null: if (Equals(schema.Type, JsonSchemaTypeDefinition.Null)) break; - return new SchemaValidationResults(string.Empty, $"Expected: Null; Actual: {json.Type}."); + return new SchemaValidationResults(string.Empty, $"Expected: {schema.Type.Name}; Actual: {json.Type}."); } return new SchemaValidationResults(); } From 7d6e537e335593ed19fbff72871cd50194469d02 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Wed, 26 Apr 2017 22:55:00 +1200 Subject: [PATCH 3/4] Further unified .Net-null and JSON-null. Resolves #56. --- Manatee.Json.Tests/ClientTest.cs | 27 +++++++++++++++------------ Manatee.Json/JsonValue.cs | 31 +++++++++++++++++++++---------- Manatee.Json/Properties/AssemblyInfo.cs | 2 +- Manatee.Json/project.nuspec | 5 ++++- Wiki | 2 +- 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/Manatee.Json.Tests/ClientTest.cs b/Manatee.Json.Tests/ClientTest.cs index e64dcfe..38f0fc0 100644 --- a/Manatee.Json.Tests/ClientTest.cs +++ b/Manatee.Json.Tests/ClientTest.cs @@ -341,19 +341,22 @@ public void Issue50_MulitpleSchemaInSubFoldersShouldReferenceRelatively() } [TestMethod] - [DeploymentItem(@"Files\Issue55.json")] - public void Issue55_EmptyJsonNotValidatedBySchema() + public void Issue56_InconsistentNullAssignment() { - var path = System.IO.Path.Combine(TestContext.TestDeploymentDir, "issue55.json"); - var schema = JsonSchemaRegistry.Get(path); - var results = schema.Validate("{}"); - - Assert.IsFalse(results.Valid); - Assert.AreEqual(1, results.Errors.Count()); - - results = schema.Validate(new JsonObject()); - - Assert.IsTrue(results.Valid); + JsonValue json1 = null; // this is actually null + string myVar = null; + JsonValue json2 = myVar; // this is JsonValue.Null + + Assert.IsNull(json1); + Assert.IsTrue(Equals(null, json1)); + + Assert.IsNotNull(json2); + Assert.IsTrue(null == json2); + // R# isn't considering my == overload + // ReSharper disable once HeuristicUnreachableCode + Assert.IsTrue(json2.Equals(null)); + // This may seem inconsistent, but check out the notes in the issue. + Assert.IsFalse(Equals(null, json2)); } } } diff --git a/Manatee.Json/JsonValue.cs b/Manatee.Json/JsonValue.cs index afa960e..af48c43 100644 --- a/Manatee.Json/JsonValue.cs +++ b/Manatee.Json/JsonValue.cs @@ -14,7 +14,7 @@ namespace Manatee.Json /// A value can consist of a string, a numerical value, a boolean (true or false), a null /// placeholder, a JSON array of values, or a nested JSON object. /// - public class JsonValue + public class JsonValue : IEquatable { private bool _boolValue; private string _stringValue; @@ -256,21 +256,32 @@ public override string ToString() /// The to compare with the current . 2 public override bool Equals(object obj) { - var json = obj.AsJsonValue(); - if (json == null) return false; - if (json.Type != Type) return false; + if (obj == null) return Type == JsonValueType.Null; + + return Equals(obj.AsJsonValue()); + } + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// true if the current object is equal to the parameter; otherwise, false. + /// An object to compare with this object. + public bool Equals(JsonValue other) + { + // using a == here would result in recursion and death by stack overflow + if (ReferenceEquals(other, null)) return Type == JsonValueType.Null; + if (other.Type != Type) return false; switch (Type) { case JsonValueType.Number: - return Number.Equals(json.Number); + return Number.Equals(other.Number); case JsonValueType.String: - return String.Equals(json.String); + return String.Equals(other.String); case JsonValueType.Boolean: - return Boolean.Equals(json.Boolean); + return Boolean.Equals(other.Boolean); case JsonValueType.Object: - return Object.Equals(json.Object); + return Object.Equals(other.Object); case JsonValueType.Array: - return Array.Equals(json.Array); + return Array.Equals(other.Array); case JsonValueType.Null: return true; } @@ -453,7 +464,7 @@ public static JsonValue Parse(StreamReader stream) /// public static bool operator ==(JsonValue a, JsonValue b) { - return ReferenceEquals(a,b) || ((a != null) && (a.Equals(b))); + return ReferenceEquals(a, b) || (a != null ? a.Equals(b) : b.Equals(a)); } /// /// diff --git a/Manatee.Json/Properties/AssemblyInfo.cs b/Manatee.Json/Properties/AssemblyInfo.cs index 555ee76..04e5af4 100644 --- a/Manatee.Json/Properties/AssemblyInfo.cs +++ b/Manatee.Json/Properties/AssemblyInfo.cs @@ -33,6 +33,6 @@ // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyFileVersion("6.0.0")] +[assembly: AssemblyFileVersion("6.0.1")] [assembly: InternalsVisibleTo("Manatee.Json.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100bbe54aad91e473ca58d56672e66770435869a2388ac5a077457bf23b0872f5d656414709391f78751fd762ec25132d42ad60e319b821e37357964ac194b31a52e1d9772c507aed15527ff41564fdf85c7a90b09b7f8c8c321daf05404e7ccdaee0b5808db8aaa0ac2e8e3065509b5f1c75c4d1de5ceed995ae83fd36d10e8be0")] diff --git a/Manatee.Json/project.nuspec b/Manatee.Json/project.nuspec index ceea14e..674f670 100644 --- a/Manatee.Json/project.nuspec +++ b/Manatee.Json/project.nuspec @@ -2,7 +2,7 @@ Manatee.Json - 6.0.0 + 6.0.1 A fully object-oriented approach to JSON manipulation, validation, and serialization that focuses on modeling the JSON structure rather than mere string parsing. @@ -12,6 +12,9 @@ https://github.com/gregsdennis/Manatee.Json/blob/master/LICENSE.txt https://bytebucket.org/gregsdennis/manatee.json/wiki/Resources/Manatee-Json-Logo.png + v6.0.1 + Updated equality logic to further unify .Net-null and JSON-null. Please read Equality Comparisons section of Usage wiki page. + v6.0.0 Added DocumentPath to IJsonSchema. BUG FIX: Schema now properly resolve referenced schema relative to document path instead of application's current directory. (#50) diff --git a/Wiki b/Wiki index 3537c0e..b06e173 160000 --- a/Wiki +++ b/Wiki @@ -1 +1 @@ -Subproject commit 3537c0e7c780be8cbab689bd0972d3a7db272d54 +Subproject commit b06e173b927c852984a1a16b94b6e263cd3cf2f1 From d243d6b34b94f0a65c807195f85aeffb30fef9a5 Mon Sep 17 00:00:00 2001 From: Greg Dennis Date: Wed, 26 Apr 2017 23:00:47 +1200 Subject: [PATCH 4/4] Updated release notes for publish. --- Manatee.Json/project.nuspec | 1 + 1 file changed, 1 insertion(+) diff --git a/Manatee.Json/project.nuspec b/Manatee.Json/project.nuspec index 674f670..66058a0 100644 --- a/Manatee.Json/project.nuspec +++ b/Manatee.Json/project.nuspec @@ -14,6 +14,7 @@ v6.0.1 Updated equality logic to further unify .Net-null and JSON-null. Please read Equality Comparisons section of Usage wiki page. + BUG FIX: Vague error messaging when validating schema. (#55) v6.0.0 Added DocumentPath to IJsonSchema.