reference_handler.h 7.3 KB

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