reference_manager.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/dynamic_reference_context.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::detail {
  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. ReferenceCache references_;
  25. std::map<RootReference, A> roots_;
  26. DynamicReferenceContext active_dynamic_anchors_;
  27. std::map<URI, std::map<Anchor, 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(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(Reference const & ref, 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. Reference canonicalize(Reference const & ref, Reference const & parent,
  58. 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. URI const dyn_uri = ref.uri().empty() ? ref.uri() : uri;
  80. if (std::optional dynref = dynamic(dyn_uri, ref, dynamic_reference)) {
  81. return *dynref;
  82. }
  83. dynamic_reference = dynamic_reference || active_dynamic_anchors_.empty();
  84. // Relative URI, not in the HEREDOC (or we set an $id)
  85. if (ref.uri().empty() and ref.anchor().empty()) {
  86. return Reference(references_.relative_to_nearest_anchor(parent).root(), ref.pointer());
  87. }
  88. return Reference(uri, ref.anchor(), ref.pointer());
  89. }
  90. private:
  91. std::optional<Reference> dynamic(URI const & uri, Reference const & ref,
  92. inout<bool> dynamic_reference) {
  93. bool const anchor_is_dynamic = active_dynamic_anchors_.contains(ref.anchor());
  94. if (not dynamic_reference) {
  95. // A normal $ref to an $anchor that matches a $dynamicAnchor breaks the
  96. // dynamic recursion pattern. This requires that we are not looking for a
  97. // subschema of the anchor AND that we are not targetting an anchor in a
  98. // different root document.
  99. dynamic_reference = (anchor_is_dynamic && ref.uri().empty() && ref.pointer().empty());
  100. return std::nullopt;
  101. }
  102. OnBlockExit scope;
  103. if (not ref.uri().empty() && anchor_is_dynamic) {
  104. // Register the scope of this (potential) $dynamicAnchor BEFORE we attempt
  105. // to enter the reference, in case we end up pointing to an otherwise
  106. // suppressed $dynamicAnchor in a higher scope.
  107. scope = dynamic_scope(Reference(uri));
  108. }
  109. return active_dynamic_anchors_.lookup(uri, ref.anchor());
  110. }
  111. void prime(Adapter auto const & json, Reference where, schema::Version version,
  112. Keywords const & keywords) {
  113. if (json.type() != adapter::Type::Object) {
  114. return;
  115. }
  116. canonicalize(where, version, json);
  117. for (auto const & [key, value] : json.as_object()) {
  118. auto vit = keywords.find(key);
  119. if (vit == keywords.end()) {
  120. continue;
  121. }
  122. if (vit->second.contains(schema::Wraps::Array) && value.type() == adapter::Type::Array) {
  123. size_t index = 0;
  124. for (auto const & elem : value.as_array()) {
  125. prime(elem, where / key / index, version, keywords);
  126. ++index;
  127. }
  128. } else if (vit->second.contains(schema::Wraps::Object) &&
  129. value.type() == adapter::Type::Object) {
  130. for (auto const & [prop, elem] : value.as_object()) {
  131. prime(elem, where / key / prop, version, keywords);
  132. }
  133. } else if (vit->second.contains(schema::Wraps::Schema)) {
  134. prime(value, where / key, version, keywords);
  135. }
  136. }
  137. }
  138. void canonicalize(Reference & where, schema::Version version, A const & json) {
  139. std::string const id = version <= schema::Version::Draft04 ? "id" : "$id";
  140. auto const schema = json.as_object();
  141. RootReference root = where.root();
  142. if (schema.contains(id)) {
  143. root = RootReference(schema[id].as_string());
  144. if (root.uri().empty()) {
  145. root = RootReference(where.uri(), root.anchor());
  146. } else if (not root.uri().is_rootless() || where.uri().empty()) {
  147. // By definition - rootless URIs cannot be relative
  148. } else if (root.uri().is_relative()) {
  149. root = RootReference(where.uri().parent() / root.uri(), root.anchor());
  150. } else {
  151. root = RootReference(where.uri().root() / root.uri(), root.anchor());
  152. }
  153. roots_.emplace(root, json);
  154. where = references_.emplace(where, root);
  155. }
  156. // $anchor and its related keywords were introduced in Draft 2019-09
  157. if (version < schema::Version::Draft2019_09) {
  158. return;
  159. }
  160. if (schema.contains("$anchor")) {
  161. root = RootReference(root.uri(), Anchor(schema["$anchor"].as_string()));
  162. roots_.emplace(root, json);
  163. where = references_.emplace(where, root);
  164. }
  165. if (version == schema::Version::Draft2019_09 && schema.contains("$recursiveAnchor") &&
  166. schema["$recursiveAnchor"].as_boolean()) {
  167. Anchor anchor;
  168. root = RootReference(root.uri(), anchor);
  169. roots_.emplace(root, json);
  170. where = references_.emplace(where, root);
  171. if (Reference & dynamic = dynamic_anchors_[root.uri()][anchor];
  172. dynamic == Reference() || where < dynamic) {
  173. dynamic = where;
  174. }
  175. }
  176. if (schema.contains("$dynamicAnchor") && version > schema::Version::Draft2019_09) {
  177. Anchor anchor(schema["$dynamicAnchor"].as_string());
  178. root = RootReference(root.uri(), anchor);
  179. roots_.emplace(root, json);
  180. where = references_.emplace(where, root);
  181. if (Reference & dynamic = dynamic_anchors_[root.uri()][anchor];
  182. dynamic == Reference() || where < dynamic) {
  183. dynamic = where;
  184. }
  185. }
  186. }
  187. };
  188. }