From 308986ce180315fc21a8dbb3d0025a275a529174 Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Tue, 29 Sep 2026 05:51:10 +0800 Subject: [PATCH] fix(json): return parse error for list type missing the element key ListTypeFromJson read the nested element type with the raw const json[kElement] accessor, which is undefined behavior (an assertion in debug nlohmann builds) when a list type object is missing the required 'element' key. Use GetJsonValue(json, kElement) like the sibling MapTypeFromJson does, so malformed schema or table-metadata JSON yields JsonParseError instead of asserting. --- src/iceberg/json_serde.cc | 4 +++- src/iceberg/test/json_serde_test.cc | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/iceberg/json_serde.cc b/src/iceberg/json_serde.cc index f322996ec7..126be22d87 100644 --- a/src/iceberg/json_serde.cc +++ b/src/iceberg/json_serde.cc @@ -494,7 +494,9 @@ Result> StructTypeFromJson(const nlohmann::json& json) { } Result> ListTypeFromJson(const nlohmann::json& json) { - ICEBERG_ASSIGN_OR_RAISE(auto element_type, TypeFromJson(json[kElement])); + ICEBERG_ASSIGN_OR_RAISE( + auto element_type, + GetJsonValue(json, kElement).and_then(TypeFromJson)); ICEBERG_ASSIGN_OR_RAISE(auto element_id, GetJsonValue(json, kElementId)); ICEBERG_ASSIGN_OR_RAISE(auto element_required, GetJsonValue(json, kElementRequired)); diff --git a/src/iceberg/test/json_serde_test.cc b/src/iceberg/test/json_serde_test.cc index 22b0a08dec..97707b06e5 100644 --- a/src/iceberg/test/json_serde_test.cc +++ b/src/iceberg/test/json_serde_test.cc @@ -189,6 +189,16 @@ TEST(JsonInternalTest, SchemaFieldRejectsNonUtcTimestamptzDefault) { EXPECT_TRUE(FieldFromJson(utc).has_value()); } +TEST(JsonInternalTest, ListTypeFromJsonMissingElement) { + nlohmann::json invalid_json = + R"({"type":"list","element-id":1,"element-required":true})"_json; + // missing "element" + + auto result = TypeFromJson(invalid_json); + EXPECT_THAT(result, IsError(ErrorKind::kJsonParseError)); + EXPECT_THAT(result, HasErrorMessage("Missing 'element'")); +} + TEST(JsonInternalTest, SortField) { auto identity_transform = Transform::Identity();