rj_schema 1.0.0 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/rj_schema/rapidjson/CMakeLists.txt +23 -1
- data/ext/rj_schema/rapidjson/appveyor.yml +49 -1
- data/ext/rj_schema/rapidjson/bin/types/alotofkeys.json +502 -0
- data/ext/rj_schema/rapidjson/bin/unittestschema/idandref.json +69 -0
- data/ext/rj_schema/rapidjson/doc/stream.md +7 -7
- data/ext/rj_schema/rapidjson/doc/stream.zh-cn.md +1 -1
- data/ext/rj_schema/rapidjson/doc/tutorial.md +15 -15
- data/ext/rj_schema/rapidjson/example/schemavalidator/schemavalidator.cpp +2 -0
- data/ext/rj_schema/rapidjson/example/traverseaspointer.cpp +39 -0
- data/ext/rj_schema/rapidjson/include/rapidjson/allocators.h +460 -52
- data/ext/rj_schema/rapidjson/include/rapidjson/document.h +350 -60
- data/ext/rj_schema/rapidjson/include/rapidjson/internal/strfunc.h +14 -0
- data/ext/rj_schema/rapidjson/include/rapidjson/pointer.h +68 -1
- data/ext/rj_schema/rapidjson/include/rapidjson/rapidjson.h +60 -11
- data/ext/rj_schema/rapidjson/include/rapidjson/schema.h +249 -102
- data/ext/rj_schema/rapidjson/include/rapidjson/uri.h +466 -0
- data/ext/rj_schema/rapidjson/test/perftest/perftest.h +5 -4
- data/ext/rj_schema/rapidjson/test/perftest/rapidjsontest.cpp +20 -2
- data/ext/rj_schema/rapidjson/test/unittest/CMakeLists.txt +2 -0
- data/ext/rj_schema/rapidjson/test/unittest/allocatorstest.cpp +193 -1
- data/ext/rj_schema/rapidjson/test/unittest/documenttest.cpp +2 -0
- data/ext/rj_schema/rapidjson/test/unittest/platformtest.cpp +40 -0
- data/ext/rj_schema/rapidjson/test/unittest/pointertest.cpp +62 -2
- data/ext/rj_schema/rapidjson/test/unittest/schematest.cpp +372 -7
- data/ext/rj_schema/rapidjson/test/unittest/uritest.cpp +718 -0
- data/ext/rj_schema/rapidjson/test/unittest/valuetest.cpp +12 -2
- data/ext/rj_schema/rj_schema.cpp +3 -10
- data/lib/rj_schema.rb +1 -1
- metadata +9 -3
@@ -13,6 +13,7 @@
|
|
13
13
|
// specific language governing permissions and limitations under the License.
|
14
14
|
|
15
15
|
#define RAPIDJSON_SCHEMA_VERBOSE 0
|
16
|
+
#define RAPIDJSON_HAS_STDSTRING 1
|
16
17
|
|
17
18
|
#include "unittest.h"
|
18
19
|
#include "rapidjson/schema.h"
|
@@ -1811,6 +1812,189 @@ TEST(SchemaValidator, EscapedPointer) {
|
|
1811
1812
|
"}}");
|
1812
1813
|
}
|
1813
1814
|
|
1815
|
+
TEST(SchemaValidator, SchemaPointer) {
|
1816
|
+
Document sd;
|
1817
|
+
sd.Parse(
|
1818
|
+
"{"
|
1819
|
+
" \"swagger\": \"2.0\","
|
1820
|
+
" \"paths\": {"
|
1821
|
+
" \"/some/path\": {"
|
1822
|
+
" \"post\": {"
|
1823
|
+
" \"parameters\": ["
|
1824
|
+
" {"
|
1825
|
+
" \"in\": \"body\","
|
1826
|
+
" \"name\": \"body\","
|
1827
|
+
" \"schema\": {"
|
1828
|
+
" \"properties\": {"
|
1829
|
+
" \"a\": {"
|
1830
|
+
" \"$ref\": \"#/definitions/Prop_a\""
|
1831
|
+
" },"
|
1832
|
+
" \"b\": {"
|
1833
|
+
" \"type\": \"integer\""
|
1834
|
+
" }"
|
1835
|
+
" },"
|
1836
|
+
" \"type\": \"object\""
|
1837
|
+
" }"
|
1838
|
+
" }"
|
1839
|
+
" ],"
|
1840
|
+
" \"responses\": {"
|
1841
|
+
" \"200\": {"
|
1842
|
+
" \"schema\": {"
|
1843
|
+
" \"$ref\": \"#/definitions/Resp_200\""
|
1844
|
+
" }"
|
1845
|
+
" }"
|
1846
|
+
" }"
|
1847
|
+
" }"
|
1848
|
+
" }"
|
1849
|
+
" },"
|
1850
|
+
" \"definitions\": {"
|
1851
|
+
" \"Prop_a\": {"
|
1852
|
+
" \"properties\": {"
|
1853
|
+
" \"c\": {"
|
1854
|
+
" \"enum\": ["
|
1855
|
+
" \"C1\","
|
1856
|
+
" \"C2\","
|
1857
|
+
" \"C3\""
|
1858
|
+
" ],"
|
1859
|
+
" \"type\": \"string\""
|
1860
|
+
" },"
|
1861
|
+
" \"d\": {"
|
1862
|
+
" \"$ref\": \"#/definitions/Prop_d\""
|
1863
|
+
" },"
|
1864
|
+
" \"s\": {"
|
1865
|
+
" \"type\": \"string\""
|
1866
|
+
" }"
|
1867
|
+
" },"
|
1868
|
+
" \"required\": [\"c\"],"
|
1869
|
+
" \"type\": \"object\""
|
1870
|
+
" },"
|
1871
|
+
" \"Prop_d\": {"
|
1872
|
+
" \"properties\": {"
|
1873
|
+
" \"a\": {"
|
1874
|
+
" \"$ref\": \"#/definitions/Prop_a\""
|
1875
|
+
" },"
|
1876
|
+
" \"c\": {"
|
1877
|
+
" \"$ref\": \"#/definitions/Prop_a/properties/c\""
|
1878
|
+
" }"
|
1879
|
+
" },"
|
1880
|
+
" \"type\": \"object\""
|
1881
|
+
" },"
|
1882
|
+
" \"Resp_200\": {"
|
1883
|
+
" \"properties\": {"
|
1884
|
+
" \"e\": {"
|
1885
|
+
" \"type\": \"string\""
|
1886
|
+
" },"
|
1887
|
+
" \"f\": {"
|
1888
|
+
" \"type\": \"boolean\""
|
1889
|
+
" },"
|
1890
|
+
" \"cyclic_source\": {"
|
1891
|
+
" \"$ref\": \"#/definitions/Resp_200/properties/cyclic_target\""
|
1892
|
+
" },"
|
1893
|
+
" \"cyclic_target\": {"
|
1894
|
+
" \"$ref\": \"#/definitions/Resp_200/properties/cyclic_source\""
|
1895
|
+
" }"
|
1896
|
+
" },"
|
1897
|
+
" \"type\": \"object\""
|
1898
|
+
" }"
|
1899
|
+
" }"
|
1900
|
+
"}");
|
1901
|
+
SchemaDocument s1(sd, NULL, 0, NULL, NULL, Pointer("#/paths/~1some~1path/post/parameters/0/schema"));
|
1902
|
+
VALIDATE(s1,
|
1903
|
+
"{"
|
1904
|
+
" \"a\": {"
|
1905
|
+
" \"c\": \"C1\","
|
1906
|
+
" \"d\": {"
|
1907
|
+
" \"a\": {"
|
1908
|
+
" \"c\": \"C2\""
|
1909
|
+
" },"
|
1910
|
+
" \"c\": \"C3\""
|
1911
|
+
" }"
|
1912
|
+
" },"
|
1913
|
+
" \"b\": 123"
|
1914
|
+
"}",
|
1915
|
+
true);
|
1916
|
+
INVALIDATE(s1,
|
1917
|
+
"{"
|
1918
|
+
" \"a\": {"
|
1919
|
+
" \"c\": \"C1\","
|
1920
|
+
" \"d\": {"
|
1921
|
+
" \"a\": {"
|
1922
|
+
" \"c\": \"C2\""
|
1923
|
+
" },"
|
1924
|
+
" \"c\": \"C3\""
|
1925
|
+
" }"
|
1926
|
+
" },"
|
1927
|
+
" \"b\": \"should be an int\""
|
1928
|
+
"}",
|
1929
|
+
"#/paths/~1some~1path/post/parameters/0/schema/properties/b", "type", "#/b",
|
1930
|
+
"{ \"type\": {"
|
1931
|
+
" \"errorCode\": 20,"
|
1932
|
+
" \"instanceRef\":\"#/b\","
|
1933
|
+
" \"schemaRef\":\"#/paths/~1some~1path/post/parameters/0/schema/properties/b\","
|
1934
|
+
" \"expected\": [\"integer\"], \"actual\":\"string\""
|
1935
|
+
"}}");
|
1936
|
+
INVALIDATE(s1,
|
1937
|
+
"{"
|
1938
|
+
" \"a\": {"
|
1939
|
+
" \"c\": \"C1\","
|
1940
|
+
" \"d\": {"
|
1941
|
+
" \"a\": {"
|
1942
|
+
" \"c\": \"should be within enum\""
|
1943
|
+
" },"
|
1944
|
+
" \"c\": \"C3\""
|
1945
|
+
" }"
|
1946
|
+
" },"
|
1947
|
+
" \"b\": 123"
|
1948
|
+
"}",
|
1949
|
+
"#/definitions/Prop_a/properties/c", "enum", "#/a/d/a/c",
|
1950
|
+
"{ \"enum\": {"
|
1951
|
+
" \"errorCode\": 19,"
|
1952
|
+
" \"instanceRef\":\"#/a/d/a/c\","
|
1953
|
+
" \"schemaRef\":\"#/definitions/Prop_a/properties/c\""
|
1954
|
+
"}}");
|
1955
|
+
INVALIDATE(s1,
|
1956
|
+
"{"
|
1957
|
+
" \"a\": {"
|
1958
|
+
" \"c\": \"C1\","
|
1959
|
+
" \"d\": {"
|
1960
|
+
" \"a\": {"
|
1961
|
+
" \"s\": \"required 'c' is missing\""
|
1962
|
+
" }"
|
1963
|
+
" }"
|
1964
|
+
" },"
|
1965
|
+
" \"b\": 123"
|
1966
|
+
"}",
|
1967
|
+
"#/definitions/Prop_a", "required", "#/a/d/a",
|
1968
|
+
"{ \"required\": {"
|
1969
|
+
" \"errorCode\": 15,"
|
1970
|
+
" \"missing\":[\"c\"],"
|
1971
|
+
" \"instanceRef\":\"#/a/d/a\","
|
1972
|
+
" \"schemaRef\":\"#/definitions/Prop_a\""
|
1973
|
+
"}}");
|
1974
|
+
SchemaDocument s2(sd, NULL, 0, NULL, NULL, Pointer("#/paths/~1some~1path/post/responses/200/schema"));
|
1975
|
+
VALIDATE(s2,
|
1976
|
+
"{ \"e\": \"some string\", \"f\": false }",
|
1977
|
+
true);
|
1978
|
+
INVALIDATE(s2,
|
1979
|
+
"{ \"e\": true, \"f\": false }",
|
1980
|
+
"#/definitions/Resp_200/properties/e", "type", "#/e",
|
1981
|
+
"{ \"type\": {"
|
1982
|
+
" \"errorCode\": 20,"
|
1983
|
+
" \"instanceRef\":\"#/e\","
|
1984
|
+
" \"schemaRef\":\"#/definitions/Resp_200/properties/e\","
|
1985
|
+
" \"expected\": [\"string\"], \"actual\":\"boolean\""
|
1986
|
+
"}}");
|
1987
|
+
INVALIDATE(s2,
|
1988
|
+
"{ \"e\": \"some string\", \"f\": 123 }",
|
1989
|
+
"#/definitions/Resp_200/properties/f", "type", "#/f",
|
1990
|
+
"{ \"type\": {"
|
1991
|
+
" \"errorCode\": 20,"
|
1992
|
+
" \"instanceRef\":\"#/f\","
|
1993
|
+
" \"schemaRef\":\"#/definitions/Resp_200/properties/f\","
|
1994
|
+
" \"expected\": [\"boolean\"], \"actual\":\"integer\""
|
1995
|
+
"}}");
|
1996
|
+
}
|
1997
|
+
|
1814
1998
|
template <typename Allocator>
|
1815
1999
|
static char* ReadFile(const char* filename, Allocator& allocator) {
|
1816
2000
|
const char *paths[] = {
|
@@ -1952,7 +2136,7 @@ public:
|
|
1952
2136
|
|
1953
2137
|
virtual const SchemaDocumentType* GetRemoteDocument(const char* uri, SizeType length) {
|
1954
2138
|
for (size_t i = 0; i < kCount; i++)
|
1955
|
-
if (typename SchemaDocumentType::
|
2139
|
+
if (typename SchemaDocumentType::SValue(uri, length) == sd_[i]->GetURI())
|
1956
2140
|
return sd_[i];
|
1957
2141
|
return 0;
|
1958
2142
|
}
|
@@ -2032,7 +2216,7 @@ TEST(SchemaValidator, TestSuite) {
|
|
2032
2216
|
ADD_FAILURE();
|
2033
2217
|
}
|
2034
2218
|
else {
|
2035
|
-
//printf("
|
2219
|
+
//printf("\njson test suite file %s parsed ok\n", filename);
|
2036
2220
|
GenericDocument<UTF8<>, MemoryPoolAllocator<>, MemoryPoolAllocator<> > d(&documentAllocator, 1024, &documentStackAllocator);
|
2037
2221
|
d.Parse(json);
|
2038
2222
|
if (d.HasParseError()) {
|
@@ -2042,12 +2226,14 @@ TEST(SchemaValidator, TestSuite) {
|
|
2042
2226
|
else {
|
2043
2227
|
for (Value::ConstValueIterator schemaItr = d.Begin(); schemaItr != d.End(); ++schemaItr) {
|
2044
2228
|
{
|
2229
|
+
const char* description1 = (*schemaItr)["description"].GetString();
|
2230
|
+
//printf("\ncompiling schema for json test %s \n", description1);
|
2045
2231
|
SchemaDocumentType schema((*schemaItr)["schema"], filenames[i], static_cast<SizeType>(strlen(filenames[i])), &provider, &schemaAllocator);
|
2046
2232
|
GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > validator(schema, &validatorAllocator);
|
2047
|
-
const char* description1 = (*schemaItr)["description"].GetString();
|
2048
2233
|
const Value& tests = (*schemaItr)["tests"];
|
2049
2234
|
for (Value::ConstValueIterator testItr = tests.Begin(); testItr != tests.End(); ++testItr) {
|
2050
2235
|
const char* description2 = (*testItr)["description"].GetString();
|
2236
|
+
//printf("running json test %s \n", description2);
|
2051
2237
|
if (!onlyRunDescription || strcmp(description2, onlyRunDescription) == 0) {
|
2052
2238
|
const Value& data = (*testItr)["data"];
|
2053
2239
|
bool expected = (*testItr)["valid"].GetBool();
|
@@ -2075,8 +2261,8 @@ TEST(SchemaValidator, TestSuite) {
|
|
2075
2261
|
jsonAllocator.Clear();
|
2076
2262
|
}
|
2077
2263
|
printf("%d / %d passed (%2d%%)\n", passCount, testCount, passCount * 100 / testCount);
|
2078
|
-
|
2079
|
-
|
2264
|
+
if (passCount != testCount)
|
2265
|
+
ADD_FAILURE();
|
2080
2266
|
}
|
2081
2267
|
|
2082
2268
|
TEST(SchemaValidatingReader, Simple) {
|
@@ -2114,7 +2300,7 @@ TEST(SchemaValidatingReader, Invalid) {
|
|
2114
2300
|
Document e;
|
2115
2301
|
e.Parse(
|
2116
2302
|
"{ \"maxLength\": {"
|
2117
|
-
"
|
2303
|
+
" \"errorCode\": 6,"
|
2118
2304
|
" \"instanceRef\": \"#\", \"schemaRef\": \"#\","
|
2119
2305
|
" \"expected\": 3, \"actual\": \"ABCD\""
|
2120
2306
|
"}}");
|
@@ -2244,6 +2430,185 @@ TEST(SchemaValidator, Ref_remote) {
|
|
2244
2430
|
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2245
2431
|
}
|
2246
2432
|
|
2433
|
+
// Merge with id where $ref is full URI
|
2434
|
+
TEST(SchemaValidator, Ref_remote_change_resolution_scope_uri) {
|
2435
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2436
|
+
RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
|
2437
|
+
Document sd;
|
2438
|
+
sd.Parse("{\"id\": \"http://ignore/blah#/ref\", \"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"http://localhost:1234/subSchemas.json#/integer\"}}}");
|
2439
|
+
SchemaDocumentType s(sd, 0, 0, &provider);
|
2440
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2441
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2442
|
+
INVALIDATE_(s, "{\"myInt\": null}", "/integer", "type", "/myInt",
|
2443
|
+
"{ \"type\": {"
|
2444
|
+
" \"errorCode\": 20,"
|
2445
|
+
" \"instanceRef\": \"#/myInt\","
|
2446
|
+
" \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
|
2447
|
+
" \"expected\": [\"integer\"], \"actual\": \"null\""
|
2448
|
+
"}}",
|
2449
|
+
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2450
|
+
}
|
2451
|
+
|
2452
|
+
// Merge with id where $ref is a relative path
|
2453
|
+
TEST(SchemaValidator, Ref_remote_change_resolution_scope_relative_path) {
|
2454
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2455
|
+
RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
|
2456
|
+
Document sd;
|
2457
|
+
sd.Parse("{\"id\": \"http://localhost:1234/\", \"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"subSchemas.json#/integer\"}}}");
|
2458
|
+
SchemaDocumentType s(sd, 0, 0, &provider);
|
2459
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2460
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2461
|
+
INVALIDATE_(s, "{\"myInt\": null}", "/integer", "type", "/myInt",
|
2462
|
+
"{ \"type\": {"
|
2463
|
+
" \"errorCode\": 20,"
|
2464
|
+
" \"instanceRef\": \"#/myInt\","
|
2465
|
+
" \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
|
2466
|
+
" \"expected\": [\"integer\"], \"actual\": \"null\""
|
2467
|
+
"}}",
|
2468
|
+
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2469
|
+
}
|
2470
|
+
|
2471
|
+
// Merge with id where $ref is an absolute path
|
2472
|
+
TEST(SchemaValidator, Ref_remote_change_resolution_scope_absolute_path) {
|
2473
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2474
|
+
RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
|
2475
|
+
Document sd;
|
2476
|
+
sd.Parse("{\"id\": \"http://localhost:1234/xxxx\", \"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/subSchemas.json#/integer\"}}}");
|
2477
|
+
SchemaDocumentType s(sd, 0, 0, &provider);
|
2478
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2479
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2480
|
+
INVALIDATE_(s, "{\"myInt\": null}", "/integer", "type", "/myInt",
|
2481
|
+
"{ \"type\": {"
|
2482
|
+
" \"errorCode\": 20,"
|
2483
|
+
" \"instanceRef\": \"#/myInt\","
|
2484
|
+
" \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
|
2485
|
+
" \"expected\": [\"integer\"], \"actual\": \"null\""
|
2486
|
+
"}}",
|
2487
|
+
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2488
|
+
}
|
2489
|
+
|
2490
|
+
// Merge with id where $ref is an absolute path, and the document has a base URI
|
2491
|
+
TEST(SchemaValidator, Ref_remote_change_resolution_scope_absolute_path_document) {
|
2492
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2493
|
+
RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
|
2494
|
+
Document sd;
|
2495
|
+
sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/subSchemas.json#/integer\"}}}");
|
2496
|
+
SchemaDocumentType s(sd, "http://localhost:1234/xxxx", 26, &provider);
|
2497
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2498
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2499
|
+
INVALIDATE_(s, "{\"myInt\": null}", "/integer", "type", "/myInt",
|
2500
|
+
"{ \"type\": {"
|
2501
|
+
" \"errorCode\": 20,"
|
2502
|
+
" \"instanceRef\": \"#/myInt\","
|
2503
|
+
" \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
|
2504
|
+
" \"expected\": [\"integer\"], \"actual\": \"null\""
|
2505
|
+
"}}",
|
2506
|
+
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2507
|
+
}
|
2508
|
+
|
2509
|
+
// $ref is a non-JSON pointer fragment and there a matching id
|
2510
|
+
TEST(SchemaValidator, Ref_internal_id_1) {
|
2511
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2512
|
+
Document sd;
|
2513
|
+
sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myStr\": {\"type\": \"string\", \"id\": \"#myStrId\"}, \"myInt2\": {\"type\": \"integer\", \"id\": \"#myId\"}}}");
|
2514
|
+
SchemaDocumentType s(sd);
|
2515
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2516
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2517
|
+
INVALIDATE_(s, "{\"myInt1\": null}", "/properties/myInt2", "type", "/myInt1",
|
2518
|
+
"{ \"type\": {"
|
2519
|
+
" \"errorCode\": 20,"
|
2520
|
+
" \"instanceRef\": \"#/myInt1\","
|
2521
|
+
" \"schemaRef\": \"#/properties/myInt2\","
|
2522
|
+
" \"expected\": [\"integer\"], \"actual\": \"null\""
|
2523
|
+
"}}",
|
2524
|
+
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2525
|
+
}
|
2526
|
+
|
2527
|
+
// $ref is a non-JSON pointer fragment and there are two matching ids so we take the first
|
2528
|
+
TEST(SchemaValidator, Ref_internal_id_2) {
|
2529
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2530
|
+
Document sd;
|
2531
|
+
sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myInt2\": {\"type\": \"integer\", \"id\": \"#myId\"}, \"myStr\": {\"type\": \"string\", \"id\": \"#myId\"}}}");
|
2532
|
+
SchemaDocumentType s(sd);
|
2533
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2534
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2535
|
+
INVALIDATE_(s, "{\"myInt1\": null}", "/properties/myInt2", "type", "/myInt1",
|
2536
|
+
"{ \"type\": {"
|
2537
|
+
" \"errorCode\": 20,"
|
2538
|
+
" \"instanceRef\": \"#/myInt1\","
|
2539
|
+
" \"schemaRef\": \"#/properties/myInt2\","
|
2540
|
+
" \"expected\": [\"integer\"], \"actual\": \"null\""
|
2541
|
+
"}}",
|
2542
|
+
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2543
|
+
}
|
2544
|
+
|
2545
|
+
// $ref is a non-JSON pointer fragment and there is a matching id within array
|
2546
|
+
TEST(SchemaValidator, Ref_internal_id_in_array) {
|
2547
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2548
|
+
Document sd;
|
2549
|
+
sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myInt2\": {\"anyOf\": [{\"type\": \"string\", \"id\": \"#myStrId\"}, {\"type\": \"integer\", \"id\": \"#myId\"}]}}}");
|
2550
|
+
SchemaDocumentType s(sd);
|
2551
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2552
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2553
|
+
INVALIDATE_(s, "{\"myInt1\": null}", "/properties/myInt2/anyOf/1", "type", "/myInt1",
|
2554
|
+
"{ \"type\": {"
|
2555
|
+
" \"errorCode\": 20,"
|
2556
|
+
" \"instanceRef\": \"#/myInt1\","
|
2557
|
+
" \"schemaRef\": \"#/properties/myInt2/anyOf/1\","
|
2558
|
+
" \"expected\": [\"integer\"], \"actual\": \"null\""
|
2559
|
+
"}}",
|
2560
|
+
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2561
|
+
}
|
2562
|
+
|
2563
|
+
// $ref is a non-JSON pointer fragment and there is a matching id, and the schema is embedded in the document
|
2564
|
+
TEST(SchemaValidator, Ref_internal_id_and_schema_pointer) {
|
2565
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2566
|
+
Document sd;
|
2567
|
+
sd.Parse("{ \"schema\": {\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myInt2\": {\"anyOf\": [{\"type\": \"integer\", \"id\": \"#myId\"}]}}}}");
|
2568
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2569
|
+
SchemaDocumentType s(sd, 0, 0, 0, 0, PointerType("/schema"));
|
2570
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2571
|
+
INVALIDATE_(s, "{\"myInt1\": null}", "/schema/properties/myInt2/anyOf/0", "type", "/myInt1",
|
2572
|
+
"{ \"type\": {"
|
2573
|
+
" \"errorCode\": 20,"
|
2574
|
+
" \"instanceRef\": \"#/myInt1\","
|
2575
|
+
" \"schemaRef\": \"#/schema/properties/myInt2/anyOf/0\","
|
2576
|
+
" \"expected\": [\"integer\"], \"actual\": \"null\""
|
2577
|
+
"}}",
|
2578
|
+
kValidateDefaultFlags, SchemaValidatorType, PointerType);
|
2579
|
+
}
|
2580
|
+
|
2581
|
+
// Test that $refs are correctly resolved when intermediate multiple ids are present
|
2582
|
+
// Includes $ref to a part of the document with a different in-scope id, which also contains $ref..
|
2583
|
+
TEST(SchemaValidator, Ref_internal_multiple_ids) {
|
2584
|
+
typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
|
2585
|
+
//RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
|
2586
|
+
CrtAllocator allocator;
|
2587
|
+
char* schema = ReadFile("unittestschema/idandref.json", allocator);
|
2588
|
+
Document sd;
|
2589
|
+
sd.Parse(schema);
|
2590
|
+
ASSERT_FALSE(sd.HasParseError());
|
2591
|
+
SchemaDocumentType s(sd, "http://xyz", 10/*, &provider*/);
|
2592
|
+
typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
|
2593
|
+
typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
|
2594
|
+
INVALIDATE_(s, "{\"PA1\": \"s\", \"PA2\": \"t\", \"PA3\": \"r\", \"PX1\": 1, \"PX2Y\": 2, \"PX3Z\": 3, \"PX4\": 4, \"PX5\": 5, \"PX6\": 6, \"PX7W\": 7, \"PX8N\": { \"NX\": 8}}", "#", "errors", "#",
|
2595
|
+
"{ \"type\": ["
|
2596
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PA1\", \"schemaRef\": \"http://xyz#/definitions/A\", \"expected\": [\"integer\"], \"actual\": \"string\"},"
|
2597
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PA2\", \"schemaRef\": \"http://xyz#/definitions/A\", \"expected\": [\"integer\"], \"actual\": \"string\"},"
|
2598
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PA3\", \"schemaRef\": \"http://xyz#/definitions/A\", \"expected\": [\"integer\"], \"actual\": \"string\"},"
|
2599
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PX1\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
|
2600
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PX2Y\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
|
2601
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PX3Z\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
|
2602
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PX4\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
|
2603
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PX5\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
|
2604
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PX6\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
|
2605
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PX7W\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
|
2606
|
+
" {\"errorCode\": 20, \"instanceRef\": \"#/PX8N/NX\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"}"
|
2607
|
+
"]}",
|
2608
|
+
kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidatorType, PointerType);
|
2609
|
+
CrtAllocator::Free(schema);
|
2610
|
+
}
|
2611
|
+
|
2247
2612
|
TEST(SchemaValidator, Ref_remote_issue1210) {
|
2248
2613
|
class SchemaDocumentProvider : public IRemoteSchemaDocumentProvider {
|
2249
2614
|
SchemaDocument** collection;
|
@@ -2260,7 +2625,7 @@ TEST(SchemaValidator, Ref_remote_issue1210) {
|
|
2260
2625
|
SchemaDocumentProvider(SchemaDocument** collection) : collection(collection) { }
|
2261
2626
|
virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) {
|
2262
2627
|
int i = 0;
|
2263
|
-
while (collection[i] && SchemaDocument::
|
2628
|
+
while (collection[i] && SchemaDocument::SValue(uri, length) != collection[i]->GetURI()) ++i;
|
2264
2629
|
return collection[i];
|
2265
2630
|
}
|
2266
2631
|
};
|