Skip to content
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

Backport #4129: schema_registry: Support default nil type in union for Avro #4133

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/v/pandaproxy/schema_registry/avro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ bool check_compatible(avro::Node& reader, avro::Node& writer) {
// if the reader's record schema has a field with no default
// value, and writer's schema does not have a field with the
// same name, an error is signalled.
return false;

// For union, the default must correspond to the first type.
// The default may be null.
const auto& r_leaf = reader.leafAt(int(r_idx));
if (
r_leaf->type() != avro::Type::AVRO_UNION
|| r_leaf->leafAt(0)->type() != avro::Type::AVRO_NULL) {
return false;
}
}
}
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/v/pandaproxy/schema_registry/test/compatibility_avro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ BOOST_AUTO_TEST_CASE(test_avro_basic_backwards_compat) {
// "adding a field with default is a backward compatible change
BOOST_CHECK(check_compatible(schema2, schema1));

// "adding a union field with default is a backward compatible change
BOOST_CHECK(check_compatible(schema2_union_null_first, schema1));

// "adding a field w/o default is NOT a backward compatible change
BOOST_CHECK(!check_compatible(schema3, schema1));

Expand Down
4 changes: 4 additions & 0 deletions src/v/pandaproxy/schema_registry/test/compatibility_avro.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ const auto schema2
= pps::make_avro_schema_definition(
R"({"type":"record","name":"myrecord","fields":[{"type":"string","name":"f1"},{"type":"string","name":"f2","default":"foo"}]})")
.value();
const auto schema2_union_null_first
= pps::make_avro_schema_definition(
R"({"type":"record","name":"myrecord","fields":[{"type":"string","name":"f1"},{"type":["null","int"],"name":"f2_enum","default":null}]})")
.value();
const auto schema3
= pps::make_avro_schema_definition(
R"({"type":"record","name":"myrecord","fields":[{"type":"string","name":"f1"},{"type":"string","name":"f2"}]})")
Expand Down