Detect certain JSON reference cycles while parsing schemas

This commit is contained in:
Tristan Penman 2024-12-22 19:41:51 +11:00
parent ae0112b4be
commit b151d1e99a
2 changed files with 20 additions and 7 deletions

View File

@ -0,0 +1,12 @@
#pragma once
#include <map>
#include <string>
#include <valijson/subschema.hpp>
namespace valijson {
typedef std::map<std::string, const Subschema *> SchemaCache;
} // namespace valijson

View File

@ -1,10 +1,8 @@
#pragma once
#include <stdexcept>
#include <functional>
#include <iostream>
#include <vector>
#include <memory>
#include <functional>
#include <valijson/constraints/concrete_constraints.hpp>
#include <valijson/internal/adapter.hpp>
@ -14,6 +12,7 @@
#include <valijson/internal/uri.hpp>
#include <valijson/constraint_builder.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_cache.hpp>
#include <valijson/exceptions.hpp>
namespace valijson {
@ -109,8 +108,8 @@ public:
void populateSchema(
const AdapterType &node,
Schema &schema,
typename FunctionPtrs<AdapterType>::FetchDoc fetchDoc = nullptr ,
typename FunctionPtrs<AdapterType>::FreeDoc freeDoc = nullptr )
typename FunctionPtrs<AdapterType>::FetchDoc fetchDoc = nullptr,
typename FunctionPtrs<AdapterType>::FreeDoc freeDoc = nullptr)
{
if ((fetchDoc == nullptr ) ^ (freeDoc == nullptr)) {
throwRuntimeError("Remote document fetching can't be enabled without both fetch and free functions");
@ -148,8 +147,6 @@ private:
typedef std::map<std::string, const DocumentType*> Type;
};
typedef std::map<std::string, const Subschema *> SchemaCache;
/**
* @brief Free memory used by fetched documents
*
@ -478,6 +475,10 @@ private:
}
if (std::find(newCacheKeys.begin(), newCacheKeys.end(), queryKey) != newCacheKeys.end()) {
throwRuntimeError("found cycle while resolving JSON reference");
}
// JSON References in nested schema will be resolved relative to the
// current document
const AdapterType &referencedAdapter =