From 379b337444c5993a5bebf941f22d0fca236db8cf Mon Sep 17 00:00:00 2001 From: Yuri Khan Date: Sat, 16 Sep 2017 16:32:37 +0700 Subject: [PATCH 1/2] Add failing test for the case when a remote schema is violated (#1064) --- test/unittest/schematest.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 9b99ba8..6c395d1 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -124,14 +124,20 @@ TEST(SchemaValidator, Hasher) { #define INVALIDATE(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer) \ {\ - SchemaValidator validator(schema);\ + INVALIDATE_(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer, SchemaValidator, Pointer) \ +} + +#define INVALIDATE_(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer,\ + SchemaValidatorType, PointerType) \ +{\ + SchemaValidatorType validator(schema);\ Document d;\ /*printf("\n%s\n", json);*/\ d.Parse(json);\ EXPECT_FALSE(d.HasParseError());\ EXPECT_FALSE(d.Accept(validator));\ EXPECT_FALSE(validator.IsValid());\ - if (validator.GetInvalidSchemaPointer() != Pointer(invalidSchemaPointer)) {\ + if (validator.GetInvalidSchemaPointer() != PointerType(invalidSchemaPointer)) {\ StringBuffer sb;\ validator.GetInvalidSchemaPointer().Stringify(sb);\ printf("GetInvalidSchemaPointer() Expected: %s Actual: %s\n", invalidSchemaPointer, sb.GetString());\ @@ -142,7 +148,7 @@ TEST(SchemaValidator, Hasher) { printf("GetInvalidSchemaKeyword() Expected: %s Actual %s\n", invalidSchemaKeyword, validator.GetInvalidSchemaKeyword());\ ADD_FAILURE();\ }\ - if (validator.GetInvalidDocumentPointer() != Pointer(invalidDocumentPointer)) {\ + if (validator.GetInvalidDocumentPointer() != PointerType(invalidDocumentPointer)) {\ StringBuffer sb;\ validator.GetInvalidDocumentPointer().Stringify(sb);\ printf("GetInvalidDocumentPointer() Expected: %s Actual: %s\n", invalidDocumentPointer, sb.GetString());\ @@ -1348,6 +1354,17 @@ TEST(SchemaValidator, Issue1017_allOfHandler) { EXPECT_STREQ("{\"cyanArray2\":[],\"blackArray\":[]}", sb.GetString()); } +TEST(SchemaValidator, Ref_remote) { + typedef GenericSchemaDocument > SchemaDocumentType; + RemoteSchemaDocumentProvider provider; + Document sd; + sd.Parse("{\"$ref\": \"http://localhost:1234/subSchemas.json#/integer\"}"); + SchemaDocumentType s(sd, &provider); + typedef GenericSchemaValidator >, MemoryPoolAllocator<> > SchemaValidatorType; + typedef GenericPointer > PointerType; + INVALIDATE_(s, "null", "/integer", "type", "", SchemaValidatorType, PointerType); +} + #ifdef __clang__ RAPIDJSON_DIAG_POP #endif From 2bfd0cc6c7f1f2ad2c2b142ec64906ba6c8a551c Mon Sep 17 00:00:00 2001 From: Yuri Khan Date: Sat, 23 Sep 2017 20:42:39 +0700 Subject: [PATCH 2/2] internal::Schema: Keep pointer for future use --- include/rapidjson/schema.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index abcf1a1..e51b369 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -349,6 +349,7 @@ public: Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) : allocator_(allocator), + pointer_(p), typeless_(schemaDocument->GetTypeless()), enum_(), enumCount_(), @@ -596,6 +597,10 @@ public: #endif } + const PointerType& GetPointer() const { + return pointer_; + } + bool BeginValue(Context& context) const { if (context.inArray) { if (uniqueItems_) @@ -1215,6 +1220,7 @@ private: }; AllocatorType* allocator_; + PointerType pointer_; const SchemaType* typeless_; uint64_t* enum_; SizeType enumCount_; @@ -1650,7 +1656,7 @@ public: //! Gets the JSON pointer pointed to the invalid schema. PointerType GetInvalidSchemaPointer() const { - return schemaStack_.Empty() ? PointerType() : schemaDocument_->GetPointer(&CurrentSchema()); + return schemaStack_.Empty() ? PointerType() : CurrentSchema().GetPointer(); } //! Gets the keyword of invalid schema.