reference_handler.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #include <functional>
  3. #include <map>
  4. #include <set>
  5. #include <unordered_map>
  6. #include <jvalidate/detail/anchor.h>
  7. #include <jvalidate/detail/parser_context.h>
  8. #include <jvalidate/detail/pointer.h>
  9. #include <jvalidate/detail/reference.h>
  10. #include <jvalidate/detail/reference_cache.h>
  11. #include <jvalidate/document_cache.h>
  12. #include <jvalidate/enum.h>
  13. #include <jvalidate/forward.h>
  14. #include <jvalidate/uri.h>
  15. namespace jvalidate {
  16. template <Adapter A> class ReferenceManager {
  17. public:
  18. using Keywords = std::unordered_map<std::string_view, std::set<schema::Wraps>>;
  19. private:
  20. DocumentCache<A> & external_;
  21. detail::ReferenceCache references_;
  22. std::map<detail::RootReference, A> roots_;
  23. std::multimap<URI, detail::Anchor> dynamic_anchors_;
  24. public:
  25. ReferenceManager(DocumentCache<A> & external, A const & root, schema::Version version,
  26. Keywords const & keywords)
  27. : external_(external), roots_{{{}, root}} {
  28. prime(root, {}, version, keywords);
  29. }
  30. std::optional<A> load(detail::Reference const & ref, detail::ParserContext<A> const & context) {
  31. if (auto it = roots_.find(ref.root()); it != roots_.end()) {
  32. return ref.pointer().walk(it->second);
  33. }
  34. std::optional<A> external = external_.try_load(ref.uri());
  35. if (not external) {
  36. return std::nullopt;
  37. }
  38. // TODO(samjaffe): Change Versions if needed...
  39. references_.emplace(ref.uri());
  40. prime(*external, ref, context.version, context.factory.keywords(context.version));
  41. // May have a sub-id that we map to
  42. if (auto it = roots_.find(ref.root()); it != roots_.end()) {
  43. return ref.pointer().walk(it->second);
  44. }
  45. // Will get called if the external schema does not declare a root document id?
  46. return ref.pointer().walk(*external);
  47. }
  48. detail::Reference canonicalize(detail::Reference const & ref,
  49. detail::Reference const & parent) const {
  50. // Relative URI, not in the HEREDOC (or we set an $id)
  51. if (ref.uri().empty() and ref.anchor().empty()) {
  52. return detail::Reference(references_.relative_to_nearest_anchor(parent).root(),
  53. ref.pointer());
  54. }
  55. URI uri = [this, &ref, &parent]() {
  56. if (ref.uri().empty() && parent.uri().empty()) {
  57. return references_.actual_parent_uri(parent);
  58. }
  59. URI uri = ref.uri().empty() ? parent.uri() : ref.uri();
  60. if (not uri.is_rootless()) {
  61. return uri;
  62. }
  63. URI base = references_.actual_parent_uri(parent);
  64. EXPECT_M(base.resource().rfind('/') != std::string::npos,
  65. "Unable to deduce root for relative uri " << uri << " (" << base << ")");
  66. if (not uri.is_relative()) {
  67. return base.root() / uri;
  68. }
  69. if (auto br = base.resource(), ur = uri.resource();
  70. br.ends_with(ur) && br[br.size() - ur.size() - 1] == '/') {
  71. return base;
  72. }
  73. return base.parent() / uri;
  74. }();
  75. detail::Reference rval(uri, ref.anchor(), ref.pointer());
  76. // Will now need to go make an external fetch...
  77. // TODO(samjaffe): Make that process internal?
  78. return rval;
  79. }
  80. private:
  81. void prime(Adapter auto const & json, detail::Reference where, schema::Version version,
  82. Keywords const & keywords) {
  83. if (json.type() != adapter::Type::Object) {
  84. return;
  85. }
  86. canonicalize(where, version, json);
  87. for (auto const & [key, value] : json.as_object()) {
  88. auto vit = keywords.find(key);
  89. if (vit == keywords.end()) {
  90. continue;
  91. }
  92. if (vit->second.contains(schema::Wraps::Array) && value.type() == adapter::Type::Array) {
  93. size_t index = 0;
  94. for (auto const & elem : value.as_array()) {
  95. prime(elem, where / key / index, version, keywords);
  96. ++index;
  97. }
  98. } else if (vit->second.contains(schema::Wraps::Object) &&
  99. value.type() == adapter::Type::Object) {
  100. for (auto const & [prop, elem] : value.as_object()) {
  101. prime(elem, where / key / prop, version, keywords);
  102. }
  103. } else if (vit->second.contains(schema::Wraps::Schema)) {
  104. prime(value, where / key, version, keywords);
  105. }
  106. }
  107. }
  108. void canonicalize(detail::Reference & where, schema::Version version, A const & json) {
  109. std::string const id = version <= schema::Version::Draft04 ? "id" : "$id";
  110. auto const schema = json.as_object();
  111. detail::RootReference root = where.root();
  112. if (schema.contains(id)) {
  113. root = detail::RootReference(schema[id].as_string());
  114. if (root.uri().empty()) {
  115. root = detail::RootReference(where.uri(), root.anchor());
  116. } else if (not root.uri().is_rootless() || where.uri().empty()) {
  117. // By definition - rootless URIs cannot be relative
  118. } else if (root.uri().is_relative()) {
  119. root = detail::RootReference(where.uri().parent() / root.uri(), root.anchor());
  120. } else {
  121. root = detail::RootReference(where.uri().root() / root.uri(), root.anchor());
  122. }
  123. roots_.emplace(root, json);
  124. where = references_.emplace(where, root);
  125. }
  126. // $anchor and its related keywords were introduced in Draft 2019-09
  127. if (version < schema::Version::Draft2019_09) {
  128. return;
  129. }
  130. if (schema.contains("$anchor")) {
  131. root = detail::RootReference(root.uri(), detail::Anchor(schema["$anchor"].as_string()));
  132. roots_.emplace(root, json);
  133. where = references_.emplace(where, root);
  134. }
  135. if (version == schema::Version::Draft2019_09 && schema.contains("$recursiveAnchor") &&
  136. schema["$recursiveAnchor"].as_boolean()) {
  137. detail::Anchor anchor;
  138. root = detail::RootReference(root.uri(), anchor);
  139. roots_.emplace(root, json);
  140. where = references_.emplace(where, root);
  141. dynamic_anchors_.emplace(root.uri(), anchor);
  142. }
  143. if (schema.contains("$dynamicAnchor") && version > schema::Version::Draft2019_09) {
  144. detail::Anchor anchor(schema["$dynamicAnchor"].as_string());
  145. root = detail::RootReference(root.uri(), anchor);
  146. roots_.emplace(root, json);
  147. where = references_.emplace(where, root);
  148. dynamic_anchors_.emplace(root.uri(), anchor);
  149. }
  150. }
  151. };
  152. }