Skip to content

Commit 3df804c

Browse files
author
Steve Hanson
committed
fix coverage, unit test allocators and equality
1 parent 18ab3b1 commit 3df804c

File tree

5 files changed

+627
-592
lines changed

5 files changed

+627
-592
lines changed

include/rapidjson/pointer.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,9 @@ class GenericPointer {
530530
// For use with JSON pointers into JSON schema documents.
531531
/*!
532532
\param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root.
533+
\param rootUri Root URI
533534
\param unresolvedTokenIndex If the pointer cannot resolve a token in the pointer, this parameter can obtain the index of unresolved token.
535+
\param allocator Allocator for Uris
534536
\return Uri if it can be resolved. Otherwise null.
535537
536538
\note
@@ -541,10 +543,10 @@ class GenericPointer {
541543
542544
Use unresolvedTokenIndex to retrieve the token index.
543545
*/
544-
UriType GetUri(ValueType& root, const UriType& rootUri, size_t* unresolvedTokenIndex = 0) const {
546+
UriType GetUri(ValueType& root, const UriType& rootUri, size_t* unresolvedTokenIndex = 0, Allocator* allocator = 0) const {
545547
static const Ch kIdString[] = { 'i', 'd', '\0' };
546548
static const ValueType kIdValue(kIdString, 2);
547-
UriType base = rootUri;
549+
UriType base = UriType(rootUri, allocator);
548550
RAPIDJSON_ASSERT(IsValid());
549551
ValueType* v = &root;
550552
for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
@@ -554,7 +556,7 @@ class GenericPointer {
554556
// See if we have an id, and if so resolve with the current base
555557
typename ValueType::MemberIterator m = v->FindMember(kIdValue);
556558
if (m != v->MemberEnd() && (m->value).IsString()) {
557-
UriType here = UriType(m->value, allocator_).Resolve(base, allocator_);
559+
UriType here = UriType(m->value, allocator).Resolve(base, allocator);
558560
base = here;
559561
}
560562
m = v->FindMember(GenericValue<EncodingType>(GenericStringRef<Ch>(t->name, t->length)));
@@ -575,13 +577,13 @@ class GenericPointer {
575577
// Error: unresolved token
576578
if (unresolvedTokenIndex)
577579
*unresolvedTokenIndex = static_cast<size_t>(t - tokens_);
578-
return UriType(allocator_);
580+
return UriType(allocator);
579581
}
580582
return base;
581583
}
582584

583-
UriType GetUri(const ValueType& root, const UriType& rootUri, size_t* unresolvedTokenIndex = 0) const {
584-
return GetUri(const_cast<ValueType&>(root), rootUri, unresolvedTokenIndex);
585+
UriType GetUri(const ValueType& root, const UriType& rootUri, size_t* unresolvedTokenIndex = 0, Allocator* allocator = 0) const {
586+
return GetUri(const_cast<ValueType&>(root), rootUri, unresolvedTokenIndex, allocator);
585587
}
586588

587589

include/rapidjson/schema.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ class GenericSchemaDocument {
17411741
// Changed by PR #1393
17421742
void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document, const UriType& id) {
17431743
if (v.GetType() == kObjectType) {
1744-
UriType newid = CreateSchema(schema, pointer, v, document, id);
1744+
UriType newid = UriType(CreateSchema(schema, pointer, v, document, id), allocator_);
17451745

17461746
for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
17471747
CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document, newid);
@@ -1790,7 +1790,7 @@ class GenericSchemaDocument {
17901790
SizeType len = itr->value.GetStringLength();
17911791
if (len > 0) {
17921792
// First resolve $ref against the in-scope id
1793-
UriType scopeId = id;
1793+
UriType scopeId = UriType(id, allocator_);
17941794
UriType ref = UriType(itr->value, allocator_).Resolve(scopeId, allocator_);
17951795
// See if the resolved $ref minus the fragment matches a resolved id in this document
17961796
// Search from the root. Returns the subschema in the document and its absolute JSON pointer.
@@ -1838,7 +1838,8 @@ class GenericSchemaDocument {
18381838
if (pointer.IsValid() && !IsCyclicRef(pointer)) {
18391839
// Call CreateSchema recursively, but first compute the in-scope id for the $ref target as we have jumped there
18401840
// TODO: cache pointer <-> id mapping
1841-
scopeId = pointer.GetUri(document, docId_);
1841+
size_t unresolvedTokenIndex;
1842+
scopeId = pointer.GetUri(document, docId_, &unresolvedTokenIndex, allocator_);
18421843
CreateSchema(schema, pointer, *pv, document, scopeId);
18431844
return true;
18441845
}
@@ -1855,7 +1856,8 @@ class GenericSchemaDocument {
18551856
//pointer.StringifyUriFragment(sb);
18561857
// Call CreateSchema recursively, but first compute the in-scope id for the $ref target as we have jumped there
18571858
// TODO: cache pointer <-> id mapping
1858-
scopeId = pointer.GetUri(document, docId_);
1859+
size_t unresolvedTokenIndex;
1860+
scopeId = pointer.GetUri(document, docId_, &unresolvedTokenIndex, allocator_);
18591861
CreateSchema(schema, pointer, *pv, document, scopeId);
18601862
return true;
18611863
}
@@ -1879,8 +1881,8 @@ class GenericSchemaDocument {
18791881
ValueType* FindId(const ValueType& doc, const UriType& finduri, PointerType& resptr, const UriType& baseuri, bool full, const PointerType& here = PointerType()) const {
18801882
SizeType i = 0;
18811883
ValueType* resval = 0;
1882-
UriType tempuri = finduri;
1883-
UriType localuri = baseuri;
1884+
UriType tempuri = UriType(finduri, allocator_);
1885+
UriType localuri = UriType(baseuri, allocator_);
18841886
if (doc.GetType() == kObjectType) {
18851887
// Establish the base URI of this object
18861888
typename ValueType::ConstMemberIterator m = doc.FindMember(SchemaType::GetIdString());

include/rapidjson/uri.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ RAPIDJSON_NAMESPACE_BEGIN
2929
///////////////////////////////////////////////////////////////////////////////
3030
// GenericUri
3131

32-
template <typename ValueType, typename Allocator=CrtAllocator>
33-
class GenericUri {
34-
public:
32+
template <typename ValueType, typename Allocator=CrtAllocator>
33+
class GenericUri {
34+
public:
3535
typedef typename ValueType::Ch Ch;
3636
#if RAPIDJSON_HAS_STDSTRING
3737
typedef std::basic_string<Ch> String;
@@ -462,10 +462,10 @@ RAPIDJSON_NAMESPACE_BEGIN
462462

463463
Allocator* allocator_; //!< The current allocator. It is either user-supplied or equal to ownAllocator_.
464464
Allocator* ownAllocator_; //!< Allocator owned by this Uri.
465-
};
465+
};
466466

467467
//! GenericUri for Value (UTF-8, default allocator).
468-
typedef GenericUri<Value> Uri;
468+
typedef GenericUri<Value> Uri;
469469

470470
RAPIDJSON_NAMESPACE_END
471471

test/unittest/pointertest.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ static const char kJsonIds[] = "{\n"
659659

660660

661661
TEST(Pointer, GetUri) {
662+
CrtAllocator allocator;
662663
Document d;
663664
d.Parse(kJsonIds);
664665
Pointer::UriType doc("http://doc");
@@ -677,15 +678,16 @@ TEST(Pointer, GetUri) {
677678
EXPECT_TRUE(Pointer("/jbo").GetUri(d, doc) == root);
678679
EXPECT_TRUE(Pointer("/jbo/child").GetUri(d, doc) == root); // id not string
679680

680-
EXPECT_TRUE(Pointer("/abc").GetUri(d, doc) == empty); // Out of boundary
681681
size_t unresolvedTokenIndex;
682-
EXPECT_TRUE(Pointer("/foo/3").GetUri(d, doc, &unresolvedTokenIndex) == empty); // Out of boundary
682+
EXPECT_TRUE(Pointer("/abc").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // Out of boundary
683+
EXPECT_EQ(0u, unresolvedTokenIndex);
684+
EXPECT_TRUE(Pointer("/foo/3").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // Out of boundary
683685
EXPECT_EQ(1u, unresolvedTokenIndex);
684-
EXPECT_TRUE(Pointer("/foo/a").GetUri(d, doc, &unresolvedTokenIndex) == empty); // "/foo" is an array, cannot query by "a"
686+
EXPECT_TRUE(Pointer("/foo/a").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // "/foo" is an array, cannot query by "a"
685687
EXPECT_EQ(1u, unresolvedTokenIndex);
686-
EXPECT_TRUE(Pointer("/foo/0/0").GetUri(d, doc, &unresolvedTokenIndex) == empty); // "/foo/0" is an string, cannot further query
688+
EXPECT_TRUE(Pointer("/foo/0/0").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // "/foo/0" is an string, cannot further query
687689
EXPECT_EQ(2u, unresolvedTokenIndex);
688-
EXPECT_TRUE(Pointer("/foo/0/a").GetUri(d, doc, &unresolvedTokenIndex) == empty); // "/foo/0" is an string, cannot further query
690+
EXPECT_TRUE(Pointer("/foo/0/a").GetUri(d, doc, &unresolvedTokenIndex, &allocator) == empty); // "/foo/0" is an string, cannot further query
689691
EXPECT_EQ(2u, unresolvedTokenIndex);
690692

691693
Pointer::Token tokens[] = { { "foo ...", 3, kPointerInvalidIndex } };

0 commit comments

Comments
 (0)