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

Encode nil as null #92

Merged
merged 3 commits into from
Jan 31, 2020
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
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
based on `record_type` and `map_type` options.
* 2.8.1
- Support 'object' as custom type properties
* 2.8.2
- Encode atom `nil` as "null" for Elixir consumers
3 changes: 3 additions & 0 deletions src/avro_primitive.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ string_type() -> type(?AVRO_STRING, []).
-spec cast(avro_type(), term()) -> {ok, avro_value()} | {error, term()}.
cast(Type, null) when ?IS_NULL_TYPE(Type) ->
{ok, ?AVRO_VALUE(Type, null)};
% For Elixir compatibility
cast(Type, nil) when ?IS_NULL_TYPE(Type) ->
davydog187 marked this conversation as resolved.
Show resolved Hide resolved
{ok, ?AVRO_VALUE(Type, null)};
cast(Type, Value) when ?IS_BOOLEAN_TYPE(Type) andalso
is_boolean(Value) ->
{ok, ?AVRO_VALUE(Type, Value)};
Expand Down
2 changes: 1 addition & 1 deletion src/erlavro.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{application, erlavro,
[
{description, "Apache Avro support for Erlang/Elixir"},
{vsn, "2.8.1"},
{vsn, "2.8.2"},
{registered, []},
{applications, [
kernel,
Expand Down
29 changes: 29 additions & 0 deletions test/avro_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,35 @@ default_values_test() ->
TestFun([{encoding, avro_json}]),
ok.

nil_values_test() ->
File = filename:join(priv_dir(), "test.avsc"),
{ok, JSON} = file:read_file(File),
Type = avro:decode_schema(JSON),
Lkup = avro:make_lkup_fun(Type),
%% Encode input has no 'children' field, default value should be used
Input = [{"f1", [{"label", "x"}]}, {"f2", nil}, {"f3", nil}],
Expect = [{<<"f1">>, [ {<<"label">>, <<"x">>}
, {<<"children">>,
[ [ {<<"label">>, <<"default-label">>}
, {<<"children">>, []}
]
]}
]},
{<<"f2">>, null},
{<<"f3">>, null}],
TestFun =
fun(Opts) ->
RootType = "org.apache.avro.test",
Encoder = avro:make_encoder(Lkup, Opts),
Decoder = avro:make_decoder(Lkup, Opts),
Encoded = Encoder(RootType, Input),
Decoded = Decoder(RootType, Encoded),
?assertEqual(Expect, Decoded)
end,
TestFun([{encoding, avro_binary}]),
TestFun([{encoding, avro_json}]),
ok.

default_values_with_map_type_test() ->
File = filename:join(priv_dir(), "test.avsc"),
{ok, JSON} = file:read_file(File),
Expand Down