Add test case for circular references in schemas

This commit is contained in:
Tristan Penman 2024-12-24 11:17:34 +11:00
parent 0a15cf4fe9
commit db04461018
2 changed files with 29 additions and 1 deletions

View File

@ -0,0 +1,8 @@
{
"description": "Circular reference when parsing properties keyword",
"properties": {
"foo": {"$ref": "#/properties/bar"},
"bar": {"$ref": "#/properties/baz"},
"baz": {"$ref": "#/properties/foo"}
}
}

View File

@ -3,11 +3,18 @@
#include <gtest/gtest.h>
#include <valijson/internal/json_pointer.hpp>
#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/utils/rapidjson_utils.hpp>
#define TEST_DATA_DIR "../tests/data"
using valijson::adapters::RapidJsonAdapter;
using valijson::internal::json_pointer::resolveJsonPointer;
using valijson::utils::loadDocument;
using valijson::Schema;
using valijson::SchemaParser;
typedef rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> RapidJsonCrtAllocator;
@ -307,3 +314,16 @@ TEST_F(TestJsonPointer, JsonPointerTestCases)
}
}
}
TEST_F(TestJsonPointer, CircularReferences)
{
// Load schema document
rapidjson::Document schemaDocument;
ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/schemas/circular_reference.schema.json", schemaDocument) );
RapidJsonAdapter schemaAdapter(schemaDocument);
// Attempt to parse schema
Schema schema;
SchemaParser parser;
EXPECT_THROW(parser.populateSchema(schemaAdapter, schema), std::runtime_error);
}