-
Notifications
You must be signed in to change notification settings - Fork 603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CORE-3182] Schema Registry json external references #24125
Open
BenPope
wants to merge
10
commits into
redpanda-data:dev
Choose a base branch
from
BenPope:feat/core-3182/schema-registry-json-external-references
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+926
−204
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e99858f
schema_registry/avro: move collect_schema to util.h
andijcr ba1307f
schema_registry/util: collect_schema overlad, collected_schema::get
andijcr 7d164b4
schema_registry/json: move to_json_pointer, conversion jsoncons->rapi…
andijcr 12b52b2
schema_registry/json: prepare document_context to store external schemas
andijcr d3486a8
schema_registry/json: add the support to resolve external references
andijcr 06c46bd
schema_registry/json: convert parse_json to coroutine
andijcr 91956cb
schema_registry/json: move parse_json to jsoncons-only
andijcr 511f12e
schema_registry/json: parse_json extract external schemas
andijcr 57ff37c
schema_registry/json collect_bundled_schemas_and_fix_refs improvement
andijcr 117abf9
schema_registry/json: remaning work
andijcr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,10 @@ json::Pointer to_json_pointer(std::string_view sv) { | |
return candidate; | ||
} | ||
|
||
json::Pointer to_json_pointer(const jsoncons::jsonpointer::json_pointer& jp) { | ||
return to_json_pointer(jp.to_string()); | ||
} | ||
|
||
// helper to convert a jsoncons::ojson to a rapidjson::Document | ||
json::Document to_json_document(const jsoncons::ojson& oj) { | ||
// serialize the input in a iobuf and parse it again | ||
|
@@ -506,11 +510,11 @@ try_validate_json_schema(const jsoncons::ojson& schema) { | |
} | ||
|
||
// forward declaration | ||
result<document_context::local_schemas_index_t> | ||
result<document_context_jsoncons::local_schemas_index_t> | ||
collect_bundled_schema_and_fix_refs( | ||
jsoncons::ojson& doc, json_schema_dialect dialect); | ||
|
||
ss::future<document_context> parse_json(iobuf buf) { | ||
ss::future<document_context_jsoncons> parse_jsoncons(iobuf buf) { | ||
// parse string in json document, check it's a valid json | ||
iobuf_istream is{buf.share(0, buf.size_bytes())}; | ||
|
||
|
@@ -568,17 +572,66 @@ ss::future<document_context> parse_json(iobuf buf) { | |
auto bundled_schemas_map | ||
= collect_bundled_schema_and_fix_refs(schema, dialect).value(); | ||
|
||
auto schemas_index = document_context::schemas_index_t{ | ||
auto schemas_index = document_context_jsoncons::schemas_index_t{ | ||
std::move_iterator(bundled_schemas_map.begin()), | ||
std::move_iterator(bundled_schemas_map.end())}; | ||
|
||
co_return document_context{ | ||
.doc = to_json_document(schema), | ||
co_return document_context_jsoncons{ | ||
.doc = std::move(schema), | ||
.dialect = dialect, | ||
.schemas_index = std::move(schemas_index), | ||
}; | ||
} | ||
|
||
// wrapper for parse_jsoncons that perform the conversion from jsoncons::ojson | ||
// to rapidjson::Document | ||
ss::future<document_context> parse_json(iobuf buf) { | ||
// we are parsing the root so we don't have a default_id | ||
auto doc_ctx = co_await parse_jsoncons(std::move(buf)); | ||
|
||
// convert external_ptr and local_ptr to rapidjson::Pointer | ||
constexpr static auto to_json_ctx_ptr = | ||
[](const document_context_jsoncons::schemas_index_t::mapped_type& v) { | ||
return ss::visit( | ||
v, | ||
[](const document_context_jsoncons::local_ptr& lp) | ||
-> document_context::schemas_index_t::mapped_type { | ||
return document_context::local_ptr{ | ||
.ptr = to_json_pointer(lp.ptr), .dialect = lp.dialect}; | ||
}, | ||
[](const document_context_jsoncons::external_ptr& ep) | ||
-> document_context::schemas_index_t::mapped_type { | ||
return document_context::external_ptr{ | ||
.external_schema_name = ep.external_schema_name, | ||
.ptr = to_json_pointer(ep.ptr)}; | ||
}); | ||
}; | ||
|
||
// convert index to rapidjson | ||
auto index_view = doc_ctx.schemas_index | ||
| std::views::transform([](auto& p) { | ||
return std::pair{ | ||
p.first, to_json_ctx_ptr(p.second)}; | ||
}) | ||
| std::views::common; | ||
// convert external_schemas to rapidjson | ||
auto external_view = doc_ctx.external_schemas | ||
| std::views::transform([](auto& p) { | ||
return std::pair{ | ||
p.first, | ||
document_context::external_document_ctx{ | ||
.doc = to_json_document(p.second.doc), | ||
.dialect = p.second.dialect}}; | ||
}) | ||
| std::views::common; | ||
Comment on lines
+794
to
+804
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: |
||
co_return document_context{ | ||
.doc = to_json_document(doc_ctx.doc), | ||
.dialect = doc_ctx.dialect, | ||
.schemas_index = {index_view.begin(), index_view.end()}, | ||
.external_schemas = {external_view.begin(), external_view.end()}, | ||
}; | ||
} | ||
|
||
/// is_superset section | ||
|
||
// a schema O is a superset of another schema N if every schema that is valid | ||
|
@@ -2249,7 +2302,7 @@ void sort(json::Value& val) { | |
} | ||
|
||
void collect_bundled_schemas_and_fix_refs( | ||
document_context::local_schemas_index_t& bundled_schemas, | ||
document_context_jsoncons::local_schemas_index_t& bundled_schemas, | ||
jsoncons::uri base_uri, | ||
jsoncons::jsonpointer::json_pointer this_obj_ptr, | ||
jsoncons::ojson& this_obj, | ||
|
@@ -2343,8 +2396,8 @@ void collect_bundled_schemas_and_fix_refs( | |
dialect = maybe_new_dialect.value(); | ||
bundled_schemas.insert_or_assign( | ||
to_json_id_uri(base_uri), | ||
document_context::local_ptr{ | ||
.ptr = json::Pointer{this_obj_ptr.to_string()}, | ||
document_context_jsoncons::local_ptr{ | ||
.ptr = this_obj_ptr, | ||
.dialect = dialect, | ||
}); | ||
} | ||
|
@@ -2379,7 +2432,7 @@ void collect_bundled_schemas_and_fix_refs( | |
} | ||
} | ||
|
||
result<document_context::local_schemas_index_t> | ||
result<document_context_jsoncons::local_schemas_index_t> | ||
collect_bundled_schema_and_fix_refs( | ||
jsoncons::ojson& doc, json_schema_dialect dialect) { | ||
// entry point to collect all bundled schemas | ||
|
@@ -2402,10 +2455,10 @@ collect_bundled_schema_and_fix_refs( | |
}(); | ||
|
||
// insert the root schema as a bundled schema | ||
auto bundled_schemas = document_context::local_schemas_index_t{ | ||
auto bundled_schemas = document_context_jsoncons::local_schemas_index_t{ | ||
{root_id, | ||
document_context::local_ptr{ | ||
.ptr = json::Pointer{}, | ||
document_context_jsoncons::local_ptr{ | ||
.ptr = {}, | ||
.dialect = dialect, | ||
}}, | ||
}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd name this something like
to_rapidjson
orto_rpjson_document
to signal in the name that this is converting between different library representations