Skip to content

Commit c237158

Browse files
committed
Keep schema URI in GenericSchemaDocument and internal::Schema
1 parent 2bfd0cc commit c237158

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

include/rapidjson/schema.h

Lines changed: 20 additions & 3 deletions
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+
uri_(schemaDocument->GetURI(), *allocator),
352353
pointer_(p),
353354
typeless_(schemaDocument->GetTypeless()),
354355
enum_(),
@@ -597,6 +598,10 @@ class Schema {
597598
#endif
598599
}
599600

601+
const SValue& GetURI() const {
602+
return uri_;
603+
}
604+
600605
const PointerType& GetPointer() const {
601606
return pointer_;
602607
}
@@ -1220,6 +1225,7 @@ class Schema {
12201225
};
12211226

12221227
AllocatorType* allocator_;
1228+
SValue uri_;
12231229
PointerType pointer_;
12241230
const SchemaType* typeless_;
12251231
uint64_t* enum_;
@@ -1330,6 +1336,7 @@ class GenericSchemaDocument {
13301336
typedef typename EncodingType::Ch Ch;
13311337
typedef internal::Schema<GenericSchemaDocument> SchemaType;
13321338
typedef GenericPointer<ValueType, Allocator> PointerType;
1339+
typedef GenericValue<EncodingType, Allocator> URIType;
13331340
friend class internal::Schema<GenericSchemaDocument>;
13341341
template <typename, typename, typename>
13351342
friend class GenericSchemaValidator;
@@ -1339,10 +1346,13 @@ class GenericSchemaDocument {
13391346
Compile a JSON document into schema document.
13401347
13411348
\param document A JSON document as source.
1349+
\param uri The base URI of this schema document for purposes of violation reporting.
1350+
\param uriLength Length of \c name, in code points.
13421351
\param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null.
13431352
\param allocator An optional allocator instance for allocating memory. Can be null.
13441353
*/
1345-
explicit GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) :
1354+
explicit GenericSchemaDocument(const ValueType& document, const Ch* uri = 0, SizeType uriLength = 0,
1355+
IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) :
13461356
remoteProvider_(remoteProvider),
13471357
allocator_(allocator),
13481358
ownAllocator_(),
@@ -1354,8 +1364,11 @@ class GenericSchemaDocument {
13541364
if (!allocator_)
13551365
ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
13561366

1367+
Ch noUri[1] = {0};
1368+
uri_.SetString(uri ? uri : noUri, uriLength, *allocator_);
1369+
13571370
typeless_ = static_cast<SchemaType*>(allocator_->Malloc(sizeof(SchemaType)));
1358-
new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0);
1371+
new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), allocator_);
13591372

13601373
// Generate root schema, it will call CreateSchema() to create sub-schemas,
13611374
// And call AddRefSchema() if there are $ref.
@@ -1393,7 +1406,8 @@ class GenericSchemaDocument {
13931406
root_(rhs.root_),
13941407
typeless_(rhs.typeless_),
13951408
schemaMap_(std::move(rhs.schemaMap_)),
1396-
schemaRef_(std::move(rhs.schemaRef_))
1409+
schemaRef_(std::move(rhs.schemaRef_)),
1410+
uri_(std::move(rhs.uri_))
13971411
{
13981412
rhs.remoteProvider_ = 0;
13991413
rhs.allocator_ = 0;
@@ -1415,6 +1429,8 @@ class GenericSchemaDocument {
14151429
RAPIDJSON_DELETE(ownAllocator_);
14161430
}
14171431

1432+
const URIType& GetURI() const { return uri_; }
1433+
14181434
//! Get the root schema.
14191435
const SchemaType& GetRoot() const { return *root_; }
14201436

@@ -1545,6 +1561,7 @@ class GenericSchemaDocument {
15451561
SchemaType* typeless_;
15461562
internal::Stack<Allocator> schemaMap_; // Stores created Pointer -> Schemas
15471563
internal::Stack<Allocator> schemaRef_; // Stores Pointer from $ref and schema which holds the $ref
1564+
URIType uri_;
15481565
};
15491566

15501567
//! GenericSchemaDocument using Value type.

test/unittest/schematest.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,12 @@ class RemoteSchemaDocumentProvider : public IGenericRemoteSchemaDocumentProvider
10711071
"jsonschema/remotes/folder/folderInteger.json",
10721072
"draft-04/schema"
10731073
};
1074+
const char* uris[kCount] = {
1075+
"http://localhost:1234/integer.json",
1076+
"http://localhost:1234/subSchemas.json",
1077+
"http://localhost:1234/folder/folderInteger.json",
1078+
"http://json-schema.org/draft-04/schema"
1079+
};
10741080

10751081
for (size_t i = 0; i < kCount; i++) {
10761082
sd_[i] = 0;
@@ -1087,7 +1093,7 @@ class RemoteSchemaDocumentProvider : public IGenericRemoteSchemaDocumentProvider
10871093
MemoryPoolAllocator<> stackAllocator(stackBuffer, sizeof(stackBuffer));
10881094
DocumentType d(&documentAllocator_, 1024, &stackAllocator);
10891095
d.Parse(json);
1090-
sd_[i] = new SchemaDocumentType(d, 0, &schemaAllocator_);
1096+
sd_[i] = new SchemaDocumentType(d, uris[i], static_cast<SizeType>(strlen(uris[i])), 0, &schemaAllocator_);
10911097
MemoryPoolAllocator<>::Free(json);
10921098
}
10931099
};
@@ -1099,15 +1105,8 @@ class RemoteSchemaDocumentProvider : public IGenericRemoteSchemaDocumentProvider
10991105
}
11001106

11011107
virtual const SchemaDocumentType* GetRemoteDocument(const char* uri, SizeType length) {
1102-
const char* uris[kCount] = {
1103-
"http://localhost:1234/integer.json",
1104-
"http://localhost:1234/subSchemas.json",
1105-
"http://localhost:1234/folder/folderInteger.json",
1106-
"http://json-schema.org/draft-04/schema"
1107-
};
1108-
11091108
for (size_t i = 0; i < kCount; i++)
1110-
if (strncmp(uri, uris[i], length) == 0 && strlen(uris[i]) == length)
1109+
if (typename SchemaDocumentType::URIType(uri, length) == sd_[i]->GetURI())
11111110
return sd_[i];
11121111
return 0;
11131112
}
@@ -1196,7 +1195,7 @@ TEST(SchemaValidator, TestSuite) {
11961195
else {
11971196
for (Value::ConstValueIterator schemaItr = d.Begin(); schemaItr != d.End(); ++schemaItr) {
11981197
{
1199-
SchemaDocumentType schema((*schemaItr)["schema"], &provider, &schemaAllocator);
1198+
SchemaDocumentType schema((*schemaItr)["schema"], filenames[i], static_cast<SizeType>(strlen(filenames[i])), &provider, &schemaAllocator);
12001199
GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > validator(schema, &validatorAllocator);
12011200
const char* description1 = (*schemaItr)["description"].GetString();
12021201
const Value& tests = (*schemaItr)["tests"];
@@ -1359,7 +1358,7 @@ TEST(SchemaValidator, Ref_remote) {
13591358
RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
13601359
Document sd;
13611360
sd.Parse("{\"$ref\": \"http://localhost:1234/subSchemas.json#/integer\"}");
1362-
SchemaDocumentType s(sd, &provider);
1361+
SchemaDocumentType s(sd, 0, 0, &provider);
13631362
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
13641363
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
13651364
INVALIDATE_(s, "null", "/integer", "type", "", SchemaValidatorType, PointerType);

0 commit comments

Comments
 (0)