reference_handler.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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<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(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) const {
  59. if (std::optional dynamic = active_dynamic_anchors_.lookup(ref.anchor())) {
  60. if (dynamic_reference) {
  61. return *dynamic;
  62. }
  63. dynamic_reference = ref.uri().empty() && ref.pointer().empty();
  64. }
  65. if (active_dynamic_anchors_.empty()) {
  66. dynamic_reference = true;
  67. }
  68. // Relative URI, not in the HEREDOC (or we set an $id)
  69. if (ref.uri().empty() and ref.anchor().empty()) {
  70. return detail::Reference(references_.relative_to_nearest_anchor(parent).root(),
  71. ref.pointer());
  72. }
  73. URI uri = [this, &ref, &parent]() {
  74. if (ref.uri().empty() && parent.uri().empty()) {
  75. return references_.actual_parent_uri(parent);
  76. }
  77. URI uri = ref.uri().empty() ? parent.uri() : ref.uri();
  78. if (not uri.is_rootless()) {
  79. return uri;
  80. }
  81. URI base = references_.actual_parent_uri(parent);
  82. EXPECT_M(base.resource().rfind('/') != std::string::npos,
  83. "Unable to deduce root for relative uri " << uri << " (" << base << ")");
  84. if (not uri.is_relative()) {
  85. return base.root() / uri;
  86. }
  87. if (auto br = base.resource(), ur = uri.resource();
  88. br.ends_with(ur) && br[br.size() - ur.size() - 1] == '/') {
  89. return base;
  90. }
  91. return base.parent() / uri;
  92. }();
  93. detail::Reference rval(uri, ref.anchor(), ref.pointer());
  94. // Will now need to go make an external fetch...
  95. // TODO(samjaffe): Make that process internal?
  96. return rval;
  97. }
  98. private:
  99. void prime(Adapter auto const & json, detail::Reference where, schema::Version version,
  100. Keywords const & keywords) {
  101. if (json.type() != adapter::Type::Object) {
  102. return;
  103. }
  104. canonicalize(where, version, json);
  105. for (auto const & [key, value] : json.as_object()) {
  106. auto vit = keywords.find(key);
  107. if (vit == keywords.end()) {
  108. continue;
  109. }
  110. if (vit->second.contains(schema::Wraps::Array) && value.type() == adapter::Type::Array) {
  111. size_t index = 0;
  112. for (auto const & elem : value.as_array()) {
  113. prime(elem, where / key / index, version, keywords);
  114. ++index;
  115. }
  116. } else if (vit->second.contains(schema::Wraps::Object) &&
  117. value.type() == adapter::Type::Object) {
  118. for (auto const & [prop, elem] : value.as_object()) {
  119. prime(elem, where / key / prop, version, keywords);
  120. }
  121. } else if (vit->second.contains(schema::Wraps::Schema)) {
  122. prime(value, where / key, version, keywords);
  123. }
  124. }
  125. }
  126. void canonicalize(detail::Reference & where, schema::Version version, A const & json) {
  127. std::string const id = version <= schema::Version::Draft04 ? "id" : "$id";
  128. auto const schema = json.as_object();
  129. detail::RootReference root = where.root();
  130. if (schema.contains(id)) {
  131. root = detail::RootReference(schema[id].as_string());
  132. if (root.uri().empty()) {
  133. root = detail::RootReference(where.uri(), root.anchor());
  134. } else if (not root.uri().is_rootless() || where.uri().empty()) {
  135. // By definition - rootless URIs cannot be relative
  136. } else if (root.uri().is_relative()) {
  137. root = detail::RootReference(where.uri().parent() / root.uri(), root.anchor());
  138. } else {
  139. root = detail::RootReference(where.uri().root() / root.uri(), root.anchor());
  140. }
  141. roots_.emplace(root, json);
  142. where = references_.emplace(where, root);
  143. }
  144. // $anchor and its related keywords were introduced in Draft 2019-09
  145. if (version < schema::Version::Draft2019_09) {
  146. return;
  147. }
  148. if (schema.contains("$anchor")) {
  149. root = detail::RootReference(root.uri(), detail::Anchor(schema["$anchor"].as_string()));
  150. roots_.emplace(root, json);
  151. where = references_.emplace(where, root);
  152. }
  153. if (version == schema::Version::Draft2019_09 && schema.contains("$recursiveAnchor") &&
  154. schema["$recursiveAnchor"].as_boolean()) {
  155. detail::Anchor anchor;
  156. root = detail::RootReference(root.uri(), anchor);
  157. roots_.emplace(root, json);
  158. where = references_.emplace(where, root);
  159. if (detail::Reference & dynamic = dynamic_anchors_[root.uri()][anchor];
  160. dynamic == detail::Reference() || where < dynamic) {
  161. dynamic = where;
  162. }
  163. }
  164. if (schema.contains("$dynamicAnchor") && version > schema::Version::Draft2019_09) {
  165. detail::Anchor anchor(schema["$dynamicAnchor"].as_string());
  166. root = detail::RootReference(root.uri(), anchor);
  167. roots_.emplace(root, json);
  168. where = references_.emplace(where, root);
  169. if (detail::Reference & dynamic = dynamic_anchors_[root.uri()][anchor];
  170. dynamic == detail::Reference() || where < dynamic) {
  171. dynamic = where;
  172. }
  173. }
  174. }
  175. };
  176. }