reference_handler.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/parser_context.h>
  8. #include <jvalidate/detail/pointer.h>
  9. #include <jvalidate/detail/reference.h>
  10. #include <jvalidate/detail/reference_cache.h>
  11. #include <jvalidate/document_cache.h>
  12. #include <jvalidate/enum.h>
  13. #include <jvalidate/forward.h>
  14. #include <jvalidate/uri.h>
  15. namespace jvalidate {
  16. template <Adapter A> class ReferenceManager {
  17. public:
  18. using Keywords = std::unordered_map<std::string_view, std::set<schema::Wraps>>;
  19. private:
  20. DocumentCache<A> & external_;
  21. detail::ReferenceCache references_;
  22. std::map<detail::RootReference, A> roots_;
  23. std::multimap<URI, detail::Anchor> dynamic_anchors_;
  24. public:
  25. ReferenceManager(DocumentCache<A> & external, A const & root, schema::Version version,
  26. Keywords const & keywords)
  27. : external_(external), roots_{{{}, root}} {
  28. prime_impl(root, {}, version, keywords);
  29. }
  30. bool has_dynamic_anchor(detail::RootReference const & root) const {
  31. for (auto [it, end] = dynamic_anchors_.equal_range(root.uri()); it != end; ++it) {
  32. if (it->second == root.anchor()) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. std::optional<A> root(detail::RootReference const & root) const {
  39. if (auto it = roots_.find(root); it != roots_.end()) {
  40. return it->second;
  41. }
  42. return std::nullopt;
  43. }
  44. std::optional<A> load(detail::Reference const & ref, detail::ParserContext<A> const & context) {
  45. if (std::optional<A> in_cache = root(ref.root())) {
  46. return ref.pointer().walk(*in_cache);
  47. }
  48. std::optional<A> external = external_.try_load(ref.uri());
  49. if (not external) {
  50. return std::nullopt;
  51. }
  52. // TODO(samjaffe): Change Versions if needed...
  53. prime(*external, ref.uri(), context.version, context.factory.keywords(context.version));
  54. // May have a sub-id that we map to
  55. if (std::optional<A> in_cache = root(ref.root())) {
  56. return ref.pointer().walk(*in_cache);
  57. }
  58. // Will get called if the external schema does not declare a root document id?
  59. return ref.pointer().walk(*external);
  60. }
  61. detail::Reference canonicalize(detail::Reference const & ref,
  62. detail::Reference const & parent) const {
  63. // Relative URI, not in the HEREDOC (or we set an $id)
  64. if (ref.uri().empty() and ref.anchor().empty()) {
  65. return detail::Reference(references_.relative_to_nearest_anchor(parent).root(),
  66. ref.pointer());
  67. }
  68. URI 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. detail::Reference rval(uri, ref.anchor(), ref.pointer());
  89. // Will now need to go make an external fetch...
  90. // TODO(samjaffe): Make that process internal?
  91. return rval;
  92. }
  93. void prime(Adapter auto const & json, URI const & where, schema::Version version,
  94. Keywords const & keywords) {
  95. references_.emplace(where);
  96. prime_impl(json, detail::Reference(where), version, keywords);
  97. }
  98. private:
  99. void prime_impl(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_impl(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_impl(elem, where / key / prop, version, keywords);
  120. }
  121. } else if (vit->second.contains(schema::Wraps::Schema)) {
  122. prime_impl(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. dynamic_anchors_.emplace(root.uri(), anchor);
  160. }
  161. if (schema.contains("$dynamicAnchor") && version > schema::Version::Draft2019_09) {
  162. detail::Anchor anchor(schema["$dynamicAnchor"].as_string());
  163. root = detail::RootReference(root.uri(), anchor);
  164. roots_.emplace(root, json);
  165. where = references_.emplace(where, root);
  166. dynamic_anchors_.emplace(root.uri(), anchor);
  167. }
  168. }
  169. };
  170. }