Skip to content

Commit

Permalink
Fix parsing Map and Array children of Unions (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
davydog187 authored Apr 3, 2022
1 parent 7204579 commit 8214503
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v2.0.1 - April 3rd, 2022

### Fixed
* Fixed bug where Array and Map children of Unions would fail to parse

## v2.0.0 - March 8th, 2022

### Changed
Expand Down
8 changes: 4 additions & 4 deletions lib/avro_ex/schema/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ defmodule AvroEx.Schema.Parser do
end

set_key =
if struct in [AvroEnum, Fixed, Record] do
{struct, parsed.name}
else
parsed.type
case parsed do
%{type: type} -> type
%{name: name} -> {struct, name}
%struct{} -> struct
end

if MapSet.member?(seen, set_key) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule AvroEx.Mixfile do
use Mix.Project

@url "http://github.com/beam-community/avro_ex"
@version "2.0.0"
@version "2.0.1"

def project do
[
Expand Down
29 changes: 29 additions & 0 deletions test/schema_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,35 @@ defmodule AvroEx.Schema.ParserTest do
}
end

test "it can have children that are arrays" do
assert %Schema{schema: schema, context: context} =
Parser.parse!([
"null",
%{"type" => "array", "items" => "int"}
])

assert schema == %Union{
possibilities: [
%Primitive{type: :null},
%Array{items: %Primitive{type: :int}}
]
}

assert context == %Context{}
end

test "it cannot have multiple array children" do
message =
"Union contains duplicated Array<items=string> in [%{\"items\" => \"int\", \"type\" => \"array\"}, %{\"items\" => \"string\", \"type\" => \"array\"}]"

assert_raise AvroEx.Schema.DecodeError, message, fn ->
Parser.parse!([
%{"type" => "array", "items" => "int"},
%{"type" => "array", "items" => "string"}
])
end
end

test "cannot have duplicated named types" do
message =
"Union contains duplicated Enum<name=directions> in [%{\"name\" => \"directions\", \"symbols\" => [\"east\", \"north\", \"south\", \"west\"], \"type\" => \"enum\"}, %{\"name\" => \"directions\", \"symbols\" => [\"blue\", \"red\", \"yellow\"], \"type\" => \"enum\"}]"
Expand Down

0 comments on commit 8214503

Please sign in to comment.