reference_manager.h 7.9 KB

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