diff --git a/src/connection/mc_connection_man.erl b/src/connection/mc_connection_man.erl index b25fe76d..74750733 100644 --- a/src/connection/mc_connection_man.erl +++ b/src/connection/mc_connection_man.erl @@ -140,8 +140,10 @@ normalize_value({K, V}) when is_atom(K); is_binary(K) -> normalize_value(V) when is_map(V) -> %% Recursively normalize nested maps normalize_map_values(V); +normalize_value([]) -> + []; normalize_value(V) when is_list(V) -> - %% Check if it's a proplist or just a list + %% Convert proplists to maps; regular lists keep BSON array semantics. case is_proplist(V) of true -> maps:from_list([{ensure_binary(K), normalize_value(Val)} || {K, Val} <- V]); false -> V %% Regular list, leave as-is diff --git a/test/tuple_normalization_SUITE.erl b/test/tuple_normalization_SUITE.erl index f210c013..37c37711 100644 --- a/test/tuple_normalization_SUITE.erl +++ b/test/tuple_normalization_SUITE.erl @@ -16,6 +16,7 @@ test_mixed_nested_structures/1, test_already_normalized_map/1, test_atom_keys_to_binary/1, + test_empty_list_unchanged/1, test_regular_list_unchanged/1, test_deep_nesting/1 ]). @@ -30,6 +31,7 @@ all() -> test_mixed_nested_structures, test_already_normalized_map, test_atom_keys_to_binary, + test_empty_list_unchanged, test_regular_list_unchanged, test_deep_nesting ]. @@ -146,6 +148,20 @@ test_atom_keys_to_binary(_Config) -> Result = normalize_map_values(Input), ?assertEqual(Expected, Result). +test_empty_list_unchanged(_Config) -> + %% Empty lists should remain arrays, not be coerced into empty maps + Input = #{ + <<"findAndModify">> => <<"devices">>, + <<"update">> => #{ + <<"$set">> => #{ + <<"test_3">> => [] + } + } + }, + Expected = Input, + Result = normalize_map_values(Input), + ?assertEqual(Expected, Result). + test_regular_list_unchanged(_Config) -> %% Regular lists (not proplists) should remain unchanged Input = #{ @@ -172,4 +188,4 @@ test_deep_nesting(_Config) -> } }, Result = normalize_map_values(Input), - ?assertEqual(Expected, Result). \ No newline at end of file + ?assertEqual(Expected, Result).