Skip to content

Commit

Permalink
Merge pull request #299 from thalesmg/fix-null-map-key
Browse files Browse the repository at this point in the history
fix: quote `"null"` map keys
  • Loading branch information
thalesmg authored Aug 8, 2024
2 parents c7cff4b + f2b6ae7 commit 9711619
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/hocon_pp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ maybe_quote_key(K0) ->
K0
end.

is_quote_key("null") ->
%% `null' is a primitive value in hocon, so if a `"null"' string is used as a map key,
%% it must be quoted to avoid confusion with the `null' primitive.
true;
is_quote_key(K) ->
case io_lib:printable_latin1_list(K) of
true ->
Expand Down
18 changes: 18 additions & 0 deletions test/hocon_pp_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,21 @@ crlf_multiline_test_() ->
?_assertEqual({ok, Value}, hocon:binary(Expected)),
?_assertEqual({ok, Value}, hocon:binary(Variant))
].

%% Checks that if the string `"null"' is used as a map key, it's correctly serialized and
%% then deserialized.
null_map_key_test_() ->
Roundtrip = fun(NVal) ->
Value = #{<<"root">> => #{NVal => 1}},
IOList = hocon_pp:do(Value, #{}),
?assertEqual({ok, Value}, hocon:binary(IOList))
end,
[
?_test(Roundtrip(Value))
|| Value <- [
<<"null">>,
<<"Null">>,
<<"NULL">>,
<<"nUlL">>
]
].

0 comments on commit 9711619

Please sign in to comment.