-
Notifications
You must be signed in to change notification settings - Fork 580
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-6836] schema_registry/json: Support Internal References #22815
Closed
BenPope
wants to merge
4
commits into
redpanda-data:dev
from
BenPope:schema_registry/json/internal_references
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aed3fee
schema_registry/json: Encapsulate refs in the impl
BenPope d43f488
schema_registry/json: Introduce as_string_view
BenPope 87393e6
schema_registry/json: Introduce a context
BenPope 955f062
schema_registry/json: Support internal references
BenPope 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
#include "json/chunked_input_stream.h" | ||
#include "json/document.h" | ||
#include "json/ostreamwrapper.h" | ||
#include "json/pointer.h" | ||
#include "json/schema.h" | ||
#include "json/stringbuffer.h" | ||
#include "json/writer.h" | ||
|
@@ -182,6 +183,26 @@ class schema_context { | |
explicit schema_context(json_schema_definition::impl const& schema) | ||
: _schema{schema} {} | ||
|
||
json::Document::ValueType const& resolve(std::string_view ref) const { | ||
// Internal reference | ||
if (ref.starts_with("#")) { | ||
json::Pointer ptr{ | ||
ref.data() + 1, | ||
ref.length() - 1, | ||
}; | ||
if (auto* p = ptr.Get(_schema.doc); p) { | ||
return *p; | ||
} | ||
throw as_exception(error_info{ | ||
error_code::schema_invalid, | ||
fmt::format("Reference not found: '{}'", ref)}); | ||
} | ||
|
||
throw as_exception(error_info{ | ||
error_code::schema_invalid, | ||
fmt::format("External references not supported: '{}'", ref)}); | ||
} | ||
|
||
private: | ||
json_schema_definition::impl const& _schema; | ||
}; | ||
|
@@ -499,10 +520,13 @@ json_type_list normalized_type(json::Value const& v) { | |
return ret; | ||
} | ||
|
||
// helper to convert a boolean to a schema | ||
// helper to convert a boolean to a schema, and traverse references | ||
json::Value::ConstObject | ||
get_schema(schema_context const&, json::Value const& v) { | ||
get_schema(schema_context const& ctx, json::Value const& v) { | ||
if (v.IsObject()) { | ||
if (auto it = v.FindMember("$ref"); it != v.MemberEnd()) { | ||
return ctx.resolve(as_string_view(it->value)).GetObject(); | ||
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. this should be the ok for draft4/7 ($ref overwrites the schema), but reading the spec seems like for 2019/20 the result should be merged on top of the schema.
Then again, I suspect our reference implementation does not implement the merge behavior. |
||
} | ||
return v.GetObject(); | ||
} | ||
|
||
|
@@ -1444,10 +1468,7 @@ bool is_superset( | |
} | ||
|
||
for (auto not_yet_handled_keyword : { | ||
"definitions", | ||
"dependencies", | ||
// draft 6 unhandled keywords: | ||
"$ref", | ||
// draft 2019-09 unhandled keywords: | ||
"dependentRequired", | ||
"dependentSchemas", | ||
|
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
Oops, something went wrong.
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.
By internal references, do you mean non-cross-schema-version references in general or references starting with
#
specifically? Looking at https://json-schema.org/understanding-json-schema/structuring#dollarref, it's possible to have "internal" references (ie. a reference to a schema within the same schema document) that don't start with#
. Do we want to support those kinds of references as well (eg."$ref": "/schemas/address"
)?