reference_manager.h 7.8 KB

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