Skip to content

Commit 03f5de9

Browse files
authored
Merge pull request Tencent#1065 from yurikhan/invalidSchemaPointer
Fix reported violated schema pointer when that schema is remote
2 parents 9338148 + 2bfd0cc commit 03f5de9

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

include/rapidjson/schema.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ class Schema {
349349

350350
Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) :
351351
allocator_(allocator),
352+
pointer_(p),
352353
typeless_(schemaDocument->GetTypeless()),
353354
enum_(),
354355
enumCount_(),
@@ -597,6 +598,10 @@ class Schema {
597598
#endif
598599
}
599600

601+
const PointerType& GetPointer() const {
602+
return pointer_;
603+
}
604+
600605
bool BeginValue(Context& context) const {
601606
if (context.inArray) {
602607
if (uniqueItems_)
@@ -1216,6 +1221,7 @@ class Schema {
12161221
};
12171222

12181223
AllocatorType* allocator_;
1224+
PointerType pointer_;
12191225
const SchemaType* typeless_;
12201226
uint64_t* enum_;
12211227
SizeType enumCount_;
@@ -1651,7 +1657,7 @@ class GenericSchemaValidator :
16511657

16521658
//! Gets the JSON pointer pointed to the invalid schema.
16531659
PointerType GetInvalidSchemaPointer() const {
1654-
return schemaStack_.Empty() ? PointerType() : schemaDocument_->GetPointer(&CurrentSchema());
1660+
return schemaStack_.Empty() ? PointerType() : CurrentSchema().GetPointer();
16551661
}
16561662

16571663
//! Gets the keyword of invalid schema.

test/unittest/schematest.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,20 @@ TEST(SchemaValidator, Hasher) {
124124

125125
#define INVALIDATE(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer) \
126126
{\
127-
SchemaValidator validator(schema);\
127+
INVALIDATE_(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer, SchemaValidator, Pointer) \
128+
}
129+
130+
#define INVALIDATE_(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer,\
131+
SchemaValidatorType, PointerType) \
132+
{\
133+
SchemaValidatorType validator(schema);\
128134
Document d;\
129135
/*printf("\n%s\n", json);*/\
130136
d.Parse(json);\
131137
EXPECT_FALSE(d.HasParseError());\
132138
EXPECT_FALSE(d.Accept(validator));\
133139
EXPECT_FALSE(validator.IsValid());\
134-
if (validator.GetInvalidSchemaPointer() != Pointer(invalidSchemaPointer)) {\
140+
if (validator.GetInvalidSchemaPointer() != PointerType(invalidSchemaPointer)) {\
135141
StringBuffer sb;\
136142
validator.GetInvalidSchemaPointer().Stringify(sb);\
137143
printf("GetInvalidSchemaPointer() Expected: %s Actual: %s\n", invalidSchemaPointer, sb.GetString());\
@@ -142,7 +148,7 @@ TEST(SchemaValidator, Hasher) {
142148
printf("GetInvalidSchemaKeyword() Expected: %s Actual %s\n", invalidSchemaKeyword, validator.GetInvalidSchemaKeyword());\
143149
ADD_FAILURE();\
144150
}\
145-
if (validator.GetInvalidDocumentPointer() != Pointer(invalidDocumentPointer)) {\
151+
if (validator.GetInvalidDocumentPointer() != PointerType(invalidDocumentPointer)) {\
146152
StringBuffer sb;\
147153
validator.GetInvalidDocumentPointer().Stringify(sb);\
148154
printf("GetInvalidDocumentPointer() Expected: %s Actual: %s\n", invalidDocumentPointer, sb.GetString());\
@@ -1348,6 +1354,17 @@ TEST(SchemaValidator, Issue1017_allOfHandler) {
13481354
EXPECT_STREQ("{\"cyanArray2\":[],\"blackArray\":[]}", sb.GetString());
13491355
}
13501356

1357+
TEST(SchemaValidator, Ref_remote) {
1358+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
1359+
RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
1360+
Document sd;
1361+
sd.Parse("{\"$ref\": \"http://localhost:1234/subSchemas.json#/integer\"}");
1362+
SchemaDocumentType s(sd, &provider);
1363+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
1364+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
1365+
INVALIDATE_(s, "null", "/integer", "type", "", SchemaValidatorType, PointerType);
1366+
}
1367+
13511368
#ifdef __clang__
13521369
RAPIDJSON_DIAG_POP
13531370
#endif

0 commit comments

Comments
 (0)