Skip to content

Commit

Permalink
schema_registry/json: switch jsoncons to order_preserving_policy
Browse files Browse the repository at this point in the history
the switch to jsoncons::json in parse_json means that the function always performed
key sorting, making the "normalize" flag redundant

jsoncons can work in insertion-order mode, to do so we switch to the
type alias jsoncons::ojson.

this is done to preserve the original order of the input
see
tests/rptest/tests/schema_registry_test.py::SchemaRegistryAutoAuthTest.test_normalize
for an example where this can be observed externally from the schema
registry api
  • Loading branch information
andijcr committed Oct 2, 2024
1 parent 9d993f1 commit bce34ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/v/pandaproxy/schema_registry/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,25 +298,25 @@ struct context {
};

template<json_schema_dialect Dialect>
const jsoncons::jsonschema::json_schema<jsoncons::json>& get_metaschema() {
const jsoncons::jsonschema::json_schema<jsoncons::ojson>& get_metaschema() {
static const auto meteschema_doc = [] {
auto metaschema = [] {
switch (Dialect) {
case json_schema_dialect::draft4:
return jsoncons::jsonschema::draft4::schema_draft4<
jsoncons::json>::get_schema();
jsoncons::ojson>::get_schema();
case json_schema_dialect::draft6:
return jsoncons::jsonschema::draft6::schema_draft6<
jsoncons::json>::get_schema();
jsoncons::ojson>::get_schema();
case json_schema_dialect::draft7:
return jsoncons::jsonschema::draft7::schema_draft7<
jsoncons::json>::get_schema();
jsoncons::ojson>::get_schema();
case json_schema_dialect::draft201909:
return jsoncons::jsonschema::draft201909::schema_draft201909<
jsoncons::json>::get_schema();
jsoncons::ojson>::get_schema();
case json_schema_dialect::draft202012:
return jsoncons::jsonschema::draft202012::schema_draft202012<
jsoncons::json>::get_schema();
jsoncons::ojson>::get_schema();
}
}();

Expand All @@ -329,7 +329,7 @@ const jsoncons::jsonschema::json_schema<jsoncons::json>& get_metaschema() {
}

result<json_schema_dialect> validate_json_schema(
json_schema_dialect dialect, const jsoncons::json& schema) {
json_schema_dialect dialect, const jsoncons::ojson& schema) {
// validation pre-step: get metaschema for json draft
const auto& metaschema_doc = [=]() -> const auto& {
using enum json_schema_dialect;
Expand Down Expand Up @@ -367,7 +367,7 @@ result<json_schema_dialect> validate_json_schema(
}

result<json_schema_dialect>
try_validate_json_schema(const jsoncons::json& schema) {
try_validate_json_schema(const jsoncons::ojson& schema) {
using enum json_schema_dialect;

// no explicit $schema: try to validate from newest to oldest draft
Expand All @@ -392,13 +392,13 @@ try_validate_json_schema(const jsoncons::json& schema) {

// forward declaration
id_to_schema_pointer collect_bundled_schema_and_fix_refs(
jsoncons::json& doc, json_schema_dialect dialect);
jsoncons::ojson& doc, json_schema_dialect dialect);

result<document_context> parse_json(iobuf buf) {
// parse string in json document, check it's a valid json
iobuf_istream is{buf.share(0, buf.size_bytes())};

auto decoder = jsoncons::json_decoder<jsoncons::json>{};
auto decoder = jsoncons::json_decoder<jsoncons::ojson>{};
auto reader = jsoncons::basic_json_reader(is.istream(), decoder);
auto ec = std::error_code{};
reader.read(ec);
Expand Down Expand Up @@ -2193,7 +2193,7 @@ void collect_bundled_schemas_and_fix_refs(
id_to_schema_pointer& bundled_schemas,
jsoncons::uri base_uri,
jsoncons::jsonpointer::json_pointer this_obj_ptr,
jsoncons::json& this_obj,
jsoncons::ojson& this_obj,
json_schema_dialect dialect) {
// scan the json schema object for bundled schema.
// A bundled schema is defined as a schema with `"$id" : a_base_uri`.
Expand Down Expand Up @@ -2309,7 +2309,7 @@ void collect_bundled_schemas_and_fix_refs(
}

id_to_schema_pointer collect_bundled_schema_and_fix_refs(
jsoncons::json& doc, json_schema_dialect dialect) {
jsoncons::ojson& doc, json_schema_dialect dialect) {
// entry point to collect all bundled schemas
auto bundled_schemas = id_to_schema_pointer{};
if (doc.is_bool()) {
Expand Down
2 changes: 1 addition & 1 deletion src/v/pandaproxy/schema_registry/test/test_json_schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static const auto error_test_cases = std::to_array({
})",
pps::error_info{
pps::error_code::schema_invalid,
R"(Invalid json schema: '{"$schema":"http://json-schema.org/draft-06/schema#","exclusiveMinimum":false,"minimum":0,"type":"number"}'. Error: '/exclusiveMinimum: Expected number, found boolean')"}},
R"(Invalid json schema: '{"$schema":"http://json-schema.org/draft-06/schema#","type":"number","minimum":0,"exclusiveMinimum":false}'. Error: '/exclusiveMinimum: Expected number, found boolean')"}},
});
SEASTAR_THREAD_TEST_CASE(test_make_invalid_json_schema) {
for (const auto& data : error_test_cases) {
Expand Down

0 comments on commit bce34ad

Please sign in to comment.