Commit c2371584 authored by Yuri Khan's avatar Yuri Khan

Keep schema URI in GenericSchemaDocument and internal::Schema

parent 2bfd0cc6
...@@ -349,6 +349,7 @@ public: ...@@ -349,6 +349,7 @@ public:
Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) : Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) :
allocator_(allocator), allocator_(allocator),
uri_(schemaDocument->GetURI(), *allocator),
pointer_(p), pointer_(p),
typeless_(schemaDocument->GetTypeless()), typeless_(schemaDocument->GetTypeless()),
enum_(), enum_(),
...@@ -597,6 +598,10 @@ public: ...@@ -597,6 +598,10 @@ public:
#endif #endif
} }
const SValue& GetURI() const {
return uri_;
}
const PointerType& GetPointer() const { const PointerType& GetPointer() const {
return pointer_; return pointer_;
} }
...@@ -1220,6 +1225,7 @@ private: ...@@ -1220,6 +1225,7 @@ private:
}; };
AllocatorType* allocator_; AllocatorType* allocator_;
SValue uri_;
PointerType pointer_; PointerType pointer_;
const SchemaType* typeless_; const SchemaType* typeless_;
uint64_t* enum_; uint64_t* enum_;
...@@ -1330,6 +1336,7 @@ public: ...@@ -1330,6 +1336,7 @@ public:
typedef typename EncodingType::Ch Ch; typedef typename EncodingType::Ch Ch;
typedef internal::Schema<GenericSchemaDocument> SchemaType; typedef internal::Schema<GenericSchemaDocument> SchemaType;
typedef GenericPointer<ValueType, Allocator> PointerType; typedef GenericPointer<ValueType, Allocator> PointerType;
typedef GenericValue<EncodingType, Allocator> URIType;
friend class internal::Schema<GenericSchemaDocument>; friend class internal::Schema<GenericSchemaDocument>;
template <typename, typename, typename> template <typename, typename, typename>
friend class GenericSchemaValidator; friend class GenericSchemaValidator;
...@@ -1339,10 +1346,13 @@ public: ...@@ -1339,10 +1346,13 @@ public:
Compile a JSON document into schema document. Compile a JSON document into schema document.
\param document A JSON document as source. \param document A JSON document as source.
\param uri The base URI of this schema document for purposes of violation reporting.
\param uriLength Length of \c name, in code points.
\param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null. \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null.
\param allocator An optional allocator instance for allocating memory. Can be null. \param allocator An optional allocator instance for allocating memory. Can be null.
*/ */
explicit GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : explicit GenericSchemaDocument(const ValueType& document, const Ch* uri = 0, SizeType uriLength = 0,
IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) :
remoteProvider_(remoteProvider), remoteProvider_(remoteProvider),
allocator_(allocator), allocator_(allocator),
ownAllocator_(), ownAllocator_(),
...@@ -1354,8 +1364,11 @@ public: ...@@ -1354,8 +1364,11 @@ public:
if (!allocator_) if (!allocator_)
ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
Ch noUri[1] = {0};
uri_.SetString(uri ? uri : noUri, uriLength, *allocator_);
typeless_ = static_cast<SchemaType*>(allocator_->Malloc(sizeof(SchemaType))); typeless_ = static_cast<SchemaType*>(allocator_->Malloc(sizeof(SchemaType)));
new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0); new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), allocator_);
// Generate root schema, it will call CreateSchema() to create sub-schemas, // Generate root schema, it will call CreateSchema() to create sub-schemas,
// And call AddRefSchema() if there are $ref. // And call AddRefSchema() if there are $ref.
...@@ -1393,7 +1406,8 @@ public: ...@@ -1393,7 +1406,8 @@ public:
root_(rhs.root_), root_(rhs.root_),
typeless_(rhs.typeless_), typeless_(rhs.typeless_),
schemaMap_(std::move(rhs.schemaMap_)), schemaMap_(std::move(rhs.schemaMap_)),
schemaRef_(std::move(rhs.schemaRef_)) schemaRef_(std::move(rhs.schemaRef_)),
uri_(std::move(rhs.uri_))
{ {
rhs.remoteProvider_ = 0; rhs.remoteProvider_ = 0;
rhs.allocator_ = 0; rhs.allocator_ = 0;
...@@ -1415,6 +1429,8 @@ public: ...@@ -1415,6 +1429,8 @@ public:
RAPIDJSON_DELETE(ownAllocator_); RAPIDJSON_DELETE(ownAllocator_);
} }
const URIType& GetURI() const { return uri_; }
//! Get the root schema. //! Get the root schema.
const SchemaType& GetRoot() const { return *root_; } const SchemaType& GetRoot() const { return *root_; }
...@@ -1545,6 +1561,7 @@ private: ...@@ -1545,6 +1561,7 @@ private:
SchemaType* typeless_; SchemaType* typeless_;
internal::Stack<Allocator> schemaMap_; // Stores created Pointer -> Schemas internal::Stack<Allocator> schemaMap_; // Stores created Pointer -> Schemas
internal::Stack<Allocator> schemaRef_; // Stores Pointer from $ref and schema which holds the $ref internal::Stack<Allocator> schemaRef_; // Stores Pointer from $ref and schema which holds the $ref
URIType uri_;
}; };
//! GenericSchemaDocument using Value type. //! GenericSchemaDocument using Value type.
......
...@@ -1071,6 +1071,12 @@ public: ...@@ -1071,6 +1071,12 @@ public:
"jsonschema/remotes/folder/folderInteger.json", "jsonschema/remotes/folder/folderInteger.json",
"draft-04/schema" "draft-04/schema"
}; };
const char* uris[kCount] = {
"http://localhost:1234/integer.json",
"http://localhost:1234/subSchemas.json",
"http://localhost:1234/folder/folderInteger.json",
"http://json-schema.org/draft-04/schema"
};
for (size_t i = 0; i < kCount; i++) { for (size_t i = 0; i < kCount; i++) {
sd_[i] = 0; sd_[i] = 0;
...@@ -1087,7 +1093,7 @@ public: ...@@ -1087,7 +1093,7 @@ public:
MemoryPoolAllocator<> stackAllocator(stackBuffer, sizeof(stackBuffer)); MemoryPoolAllocator<> stackAllocator(stackBuffer, sizeof(stackBuffer));
DocumentType d(&documentAllocator_, 1024, &stackAllocator); DocumentType d(&documentAllocator_, 1024, &stackAllocator);
d.Parse(json); d.Parse(json);
sd_[i] = new SchemaDocumentType(d, 0, &schemaAllocator_); sd_[i] = new SchemaDocumentType(d, uris[i], static_cast<SizeType>(strlen(uris[i])), 0, &schemaAllocator_);
MemoryPoolAllocator<>::Free(json); MemoryPoolAllocator<>::Free(json);
} }
}; };
...@@ -1099,15 +1105,8 @@ public: ...@@ -1099,15 +1105,8 @@ public:
} }
virtual const SchemaDocumentType* GetRemoteDocument(const char* uri, SizeType length) { virtual const SchemaDocumentType* GetRemoteDocument(const char* uri, SizeType length) {
const char* uris[kCount] = {
"http://localhost:1234/integer.json",
"http://localhost:1234/subSchemas.json",
"http://localhost:1234/folder/folderInteger.json",
"http://json-schema.org/draft-04/schema"
};
for (size_t i = 0; i < kCount; i++) for (size_t i = 0; i < kCount; i++)
if (strncmp(uri, uris[i], length) == 0 && strlen(uris[i]) == length) if (typename SchemaDocumentType::URIType(uri, length) == sd_[i]->GetURI())
return sd_[i]; return sd_[i];
return 0; return 0;
} }
...@@ -1196,7 +1195,7 @@ TEST(SchemaValidator, TestSuite) { ...@@ -1196,7 +1195,7 @@ TEST(SchemaValidator, TestSuite) {
else { else {
for (Value::ConstValueIterator schemaItr = d.Begin(); schemaItr != d.End(); ++schemaItr) { for (Value::ConstValueIterator schemaItr = d.Begin(); schemaItr != d.End(); ++schemaItr) {
{ {
SchemaDocumentType schema((*schemaItr)["schema"], &provider, &schemaAllocator); SchemaDocumentType schema((*schemaItr)["schema"], filenames[i], static_cast<SizeType>(strlen(filenames[i])), &provider, &schemaAllocator);
GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > validator(schema, &validatorAllocator); GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > validator(schema, &validatorAllocator);
const char* description1 = (*schemaItr)["description"].GetString(); const char* description1 = (*schemaItr)["description"].GetString();
const Value& tests = (*schemaItr)["tests"]; const Value& tests = (*schemaItr)["tests"];
...@@ -1359,7 +1358,7 @@ TEST(SchemaValidator, Ref_remote) { ...@@ -1359,7 +1358,7 @@ TEST(SchemaValidator, Ref_remote) {
RemoteSchemaDocumentProvider<SchemaDocumentType> provider; RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
Document sd; Document sd;
sd.Parse("{\"$ref\": \"http://localhost:1234/subSchemas.json#/integer\"}"); sd.Parse("{\"$ref\": \"http://localhost:1234/subSchemas.json#/integer\"}");
SchemaDocumentType s(sd, &provider); SchemaDocumentType s(sd, 0, 0, &provider);
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType; typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType; typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
INVALIDATE_(s, "null", "/integer", "type", "", SchemaValidatorType, PointerType); INVALIDATE_(s, "null", "/integer", "type", "", SchemaValidatorType, PointerType);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment