reference_handler.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/on_block_exit.h>
  8. #include <jvalidate/detail/out.h>
  9. #include <jvalidate/detail/parser_context.h>
  10. #include <jvalidate/detail/pointer.h>
  11. #include <jvalidate/detail/reference.h>
  12. #include <jvalidate/detail/reference_cache.h>
  13. #include <jvalidate/document_cache.h>
  14. #include <jvalidate/enum.h>
  15. #include <jvalidate/forward.h>
  16. #include <jvalidate/uri.h>
  17. namespace jvalidate {
  18. template <Adapter A> class ReferenceManager {
  19. public:
  20. using Keywords = std::unordered_map<std::string_view, std::set<schema::Wraps>>;
  21. private:
  22. DocumentCache<A> & external_;
  23. detail::ReferenceCache references_;
  24. std::map<detail::RootReference, A> roots_;
  25. std::map<detail::Anchor, detail::Reference> active_dynamic_anchors_;
  26. std::map<URI, std::map<detail::Anchor, detail::Reference>> dynamic_anchors_;
  27. public:
  28. ReferenceManager(DocumentCache<A> & external, A const & root, schema::Version version,
  29. Keywords const & keywords)
  30. : external_(external), roots_{{{}, root}} {
  31. prime(root, {}, version, keywords);
  32. }
  33. detail::OnBlockExit suppress(detail::Anchor const & anchor) {
  34. if (auto it = active_dynamic_anchors_.find(anchor); it != active_dynamic_anchors_.end()) {
  35. detail::Reference where = it->second;
  36. active_dynamic_anchors_.erase(it);
  37. return [this, anchor, where]() { active_dynamic_anchors_[anchor] = where; };
  38. }
  39. return nullptr;
  40. }
  41. auto scoped_activate(detail::Reference ref) {
  42. URI const uri = references_.relative_to_nearest_anchor(ref).uri();
  43. activate_dynamic(uri);
  44. return [this, uri]() { deactivate_dynamic(uri); };
  45. }
  46. auto load(detail::Reference const & ref, detail::ParserContext<A> const & context)
  47. -> std::pair<std::optional<A>, detail::OnBlockExit> {
  48. auto scoped_schema = [this, &ref](A const & schema) {
  49. activate_dynamic(ref.uri());
  50. return std::make_pair(ref.pointer().walk(schema),
  51. [this, uri = ref.uri()] { deactivate_dynamic(uri); });
  52. };
  53. if (auto it = roots_.find(ref.root()); it != roots_.end()) {
  54. return scoped_schema(it->second);
  55. }
  56. std::optional<A> external = external_.try_load(ref.uri());
  57. if (not external) {
  58. return std::make_pair(std::nullopt, nullptr);
  59. }
  60. // TODO(samjaffe): Change Versions if needed...
  61. references_.emplace(ref.uri());
  62. prime(*external, ref, context.version, context.factory.keywords(context.version));
  63. // May have a sub-id that we map to
  64. if (auto it = roots_.find(ref.root()); it != roots_.end()) {
  65. return scoped_schema(it->second);
  66. }
  67. // Will get called if the external schema does not declare a root document id?
  68. return scoped_schema(*external);
  69. }
  70. detail::Reference canonicalize(detail::Reference const & ref, detail::Reference const & parent,
  71. detail::inout<bool> dynamic_reference) const {
  72. if (auto it = active_dynamic_anchors_.find(ref.anchor()); it != active_dynamic_anchors_.end()) {
  73. if (dynamic_reference) {
  74. return it->second;
  75. }
  76. dynamic_reference = ref.uri().empty() && ref.pointer().empty();
  77. }
  78. if (active_dynamic_anchors_.empty()) {
  79. dynamic_reference = true;
  80. }
  81. // Relative URI, not in the HEREDOC (or we set an $id)
  82. if (ref.uri().empty() and ref.anchor().empty()) {
  83. return detail::Reference(references_.relative_to_nearest_anchor(parent).root(),
  84. ref.pointer());
  85. }
  86. URI uri = [this, &ref, &parent]() {
  87. if (ref.uri().empty() && parent.uri().empty()) {
  88. return references_.actual_parent_uri(parent);
  89. }
  90. URI uri = ref.uri().empty() ? parent.uri() : ref.uri();
  91. if (not uri.is_rootless()) {
  92. return uri;
  93. }
  94. URI base = references_.actual_parent_uri(parent);
  95. EXPECT_M(base.resource().rfind('/') != std::string::npos,
  96. "Unable to deduce root for relative uri " << uri << " (" << base << ")");
  97. if (not uri.is_relative()) {
  98. return base.root() / uri;
  99. }
  100. if (auto br = base.resource(), ur = uri.resource();
  101. br.ends_with(ur) && br[br.size() - ur.size() - 1] == '/') {
  102. return base;
  103. }
  104. return base.parent() / uri;
  105. }();
  106. detail::Reference rval(uri, ref.anchor(), ref.pointer());
  107. // Will now need to go make an external fetch...
  108. // TODO(samjaffe): Make that process internal?
  109. return rval;
  110. }
  111. private:
  112. void activate_dynamic(URI const & uri) {
  113. active_dynamic_anchors_.insert(dynamic_anchors_[uri].begin(), dynamic_anchors_[uri].end());
  114. }
  115. void deactivate_dynamic(URI const & uri) {
  116. std::map<detail::Anchor, detail::Reference> const & candidates = dynamic_anchors_[uri];
  117. for (auto it = active_dynamic_anchors_.begin(); it != active_dynamic_anchors_.end();) {
  118. if (auto cit = candidates.find(it->first); it->second == cit->second) {
  119. it = active_dynamic_anchors_.erase(it);
  120. } else {
  121. ++it;
  122. }
  123. }
  124. }
  125. void prime(Adapter auto const & json, detail::Reference where, schema::Version version,
  126. Keywords const & keywords) {
  127. if (json.type() != adapter::Type::Object) {
  128. return;
  129. }
  130. canonicalize(where, version, json);
  131. for (auto const & [key, value] : json.as_object()) {
  132. auto vit = keywords.find(key);
  133. if (vit == keywords.end()) {
  134. continue;
  135. }
  136. if (vit->second.contains(schema::Wraps::Array) && value.type() == adapter::Type::Array) {
  137. size_t index = 0;
  138. for (auto const & elem : value.as_array()) {
  139. prime(elem, where / key / index, version, keywords);
  140. ++index;
  141. }
  142. } else if (vit->second.contains(schema::Wraps::Object) &&
  143. value.type() == adapter::Type::Object) {
  144. for (auto const & [prop, elem] : value.as_object()) {
  145. prime(elem, where / key / prop, version, keywords);
  146. }
  147. } else if (vit->second.contains(schema::Wraps::Schema)) {
  148. prime(value, where / key, version, keywords);
  149. }
  150. }
  151. }
  152. void canonicalize(detail::Reference & where, schema::Version version, A const & json) {
  153. std::string const id = version <= schema::Version::Draft04 ? "id" : "$id";
  154. auto const schema = json.as_object();
  155. detail::RootReference root = where.root();
  156. if (schema.contains(id)) {
  157. root = detail::RootReference(schema[id].as_string());
  158. if (root.uri().empty()) {
  159. root = detail::RootReference(where.uri(), root.anchor());
  160. } else if (not root.uri().is_rootless() || where.uri().empty()) {
  161. // By definition - rootless URIs cannot be relative
  162. } else if (root.uri().is_relative()) {
  163. root = detail::RootReference(where.uri().parent() / root.uri(), root.anchor());
  164. } else {
  165. root = detail::RootReference(where.uri().root() / root.uri(), root.anchor());
  166. }
  167. roots_.emplace(root, json);
  168. where = references_.emplace(where, root);
  169. }
  170. // $anchor and its related keywords were introduced in Draft 2019-09
  171. if (version < schema::Version::Draft2019_09) {
  172. return;
  173. }
  174. if (schema.contains("$anchor")) {
  175. root = detail::RootReference(root.uri(), detail::Anchor(schema["$anchor"].as_string()));
  176. roots_.emplace(root, json);
  177. where = references_.emplace(where, root);
  178. }
  179. if (version == schema::Version::Draft2019_09 && schema.contains("$recursiveAnchor") &&
  180. schema["$recursiveAnchor"].as_boolean()) {
  181. detail::Anchor anchor;
  182. root = detail::RootReference(root.uri(), anchor);
  183. roots_.emplace(root, json);
  184. where = references_.emplace(where, root);
  185. if (detail::Reference & dynamic = dynamic_anchors_[root.uri()][anchor];
  186. dynamic == detail::Reference() || where < dynamic) {
  187. dynamic = where;
  188. }
  189. }
  190. if (schema.contains("$dynamicAnchor") && version > schema::Version::Draft2019_09) {
  191. detail::Anchor anchor(schema["$dynamicAnchor"].as_string());
  192. root = detail::RootReference(root.uri(), anchor);
  193. roots_.emplace(root, json);
  194. where = references_.emplace(where, root);
  195. if (detail::Reference & dynamic = dynamic_anchors_[root.uri()][anchor];
  196. dynamic == detail::Reference() || where < dynamic) {
  197. dynamic = where;
  198. }
  199. }
  200. }
  201. };
  202. }