Skip to content

Commit 701d39c

Browse files
fix(rest): use "ref" field name for AssertRefSnapshotId requirement (apache#702)
The `AssertRefSnapshotId` table requirement was serialized and deserialized with the JSON field name `ref-name`. Per the Iceberg REST OpenAPI spec (`rest-catalog-open-api.yaml`), this requirement's field is named `ref`. Only the `SetSnapshotRef` / `RemoveSnapshotRef` *updates* use `ref-name`
1 parent 50aee7e commit 701d39c

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/iceberg/json_serde.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ constexpr std::string_view kSortOrderId = "sort-order-id";
207207
constexpr std::string_view kSnapshot = "snapshot";
208208
constexpr std::string_view kSnapshotIds = "snapshot-ids";
209209
constexpr std::string_view kRefName = "ref-name";
210+
constexpr std::string_view kRef = "ref";
210211
constexpr std::string_view kUpdates = "updates";
211212
constexpr std::string_view kRemovals = "removals";
212213

@@ -1505,7 +1506,8 @@ nlohmann::json ToJson(const TableRequirement& requirement) {
15051506
const auto& r =
15061507
internal::checked_cast<const table::AssertRefSnapshotID&>(requirement);
15071508
json[kType] = kRequirementAssertRefSnapshotID;
1508-
json[kRefName] = r.ref_name();
1509+
// REST spec names this field "ref", not "ref-name".
1510+
json[kRef] = r.ref_name();
15091511
if (r.snapshot_id().has_value()) {
15101512
json[kSnapshotId] = r.snapshot_id().value();
15111513
} else {
@@ -1702,7 +1704,7 @@ Result<std::unique_ptr<TableRequirement>> TableRequirementFromJson(
17021704
return std::make_unique<table::AssertUUID>(std::move(uuid));
17031705
}
17041706
if (type == kRequirementAssertRefSnapshotID) {
1705-
ICEBERG_ASSIGN_OR_RAISE(auto ref_name, GetJsonValue<std::string>(json, kRefName));
1707+
ICEBERG_ASSIGN_OR_RAISE(auto ref_name, GetJsonValue<std::string>(json, kRef));
17061708
ICEBERG_ASSIGN_OR_RAISE(auto snapshot_id_opt,
17071709
GetJsonValueOptional<int64_t>(json, kSnapshotId));
17081710
return std::make_unique<table::AssertRefSnapshotID>(std::move(ref_name),

src/iceberg/test/json_serde_test.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ TEST(TableRequirementJsonTest, TableRequirementAssertUUID) {
681681
TEST(TableRequirementJsonTest, TableRequirementAssertRefSnapshotID) {
682682
table::AssertRefSnapshotID req("main", 123456789);
683683
nlohmann::json expected =
684-
R"({"type":"assert-ref-snapshot-id","ref-name":"main","snapshot-id":123456789})"_json;
684+
R"({"type":"assert-ref-snapshot-id","ref":"main","snapshot-id":123456789})"_json;
685685

686686
EXPECT_EQ(ToJson(req), expected);
687687
auto parsed = TableRequirementFromJson(expected);
@@ -693,7 +693,7 @@ TEST(TableRequirementJsonTest, TableRequirementAssertRefSnapshotID) {
693693
TEST(TableRequirementJsonTest, TableRequirementAssertRefSnapshotIDWithNull) {
694694
table::AssertRefSnapshotID req("main", std::nullopt);
695695
nlohmann::json expected =
696-
R"({"type":"assert-ref-snapshot-id","ref-name":"main","snapshot-id":null})"_json;
696+
R"({"type":"assert-ref-snapshot-id","ref":"main","snapshot-id":null})"_json;
697697

698698
EXPECT_EQ(ToJson(req), expected);
699699
auto parsed = TableRequirementFromJson(expected);
@@ -702,6 +702,14 @@ TEST(TableRequirementJsonTest, TableRequirementAssertRefSnapshotIDWithNull) {
702702
req);
703703
}
704704

705+
TEST(TableRequirementJsonTest, TableRequirementAssertRefSnapshotIDRejectsRefName) {
706+
nlohmann::json legacy =
707+
R"({"type":"assert-ref-snapshot-id","ref-name":"main","snapshot-id":123456789})"_json;
708+
auto result = TableRequirementFromJson(legacy);
709+
EXPECT_THAT(result, IsError(ErrorKind::kJsonParseError));
710+
EXPECT_THAT(result, HasErrorMessage("Missing 'ref'"));
711+
}
712+
705713
TEST(TableRequirementJsonTest, TableRequirementAssertLastAssignedFieldId) {
706714
table::AssertLastAssignedFieldId req(100);
707715
nlohmann::json expected =

0 commit comments

Comments
 (0)