From bb2e416d5fd732db6d4f2d51feade65abd6d728e Mon Sep 17 00:00:00 2001 From: Nik Date: Fri, 16 Jun 2023 22:30:58 -0400 Subject: [PATCH] fix empty set as a single object property decoding (#147) --- CHANGELOG.md | 1 + lib/edgedb/protocol/codecs/object.ex | 13 ++++++++- test/edgedb/protocol/codecs/object_test.exs | 32 ++++++++++++++------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64248b60..6d2eb9ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - crash after updating `db_connection` to `2.5`. +- decoding a single propery for `EdgeDB.Object` that equals to an empty set. - not catching an `EdgeDB.Error` exception during parameters encoding, which caused throwing an exception for non-`!` functions. diff --git a/lib/edgedb/protocol/codecs/object.ex b/lib/edgedb/protocol/codecs/object.ex index a14b985d..ae52c7ec 100644 --- a/lib/edgedb/protocol/codecs/object.ex +++ b/lib/edgedb/protocol/codecs/object.ex @@ -295,9 +295,20 @@ defimpl EdgeDB.Protocol.Codec, for: EdgeDB.Protocol.Codecs.Object do element.name end + single_property? = + element.cardinality in [:at_most_one, :one] and + (link_property?(element) or not link?(element)) + + value = + if single_property? do + nil + else + @empty_set + end + field = %EdgeDB.Object.Field{ name: name, - value: @empty_set, + value: value, is_link: link?(element), is_link_property: link_property?(element), is_implicit: implicit?(element) diff --git a/test/edgedb/protocol/codecs/object_test.exs b/test/edgedb/protocol/codecs/object_test.exs index 1d6c193b..1396dc13 100644 --- a/test/edgedb/protocol/codecs/object_test.exs +++ b/test/edgedb/protocol/codecs/object_test.exs @@ -232,17 +232,27 @@ defmodule Tests.EdgeDB.Protocol.Codecs.ObjectTest do end) end - test "decoding object with property equals to empty set", %{client: client} do - rollback(client, fn client -> - object = - EdgeDB.query_required_single!(client, """ - select { - a := {} - } - limit 1 - """) + test "decoding object with a single property equals to empty set", %{client: client} do + object = + EdgeDB.query_required_single!(client, """ + select { + a := {} + } + limit 1 + """) - assert Enum.empty?(object[:a]) - end) + assert is_nil(object[:a]) + end + + test "decoding object with a multi property equals to empty set", %{client: client} do + object = + EdgeDB.query_required_single!(client, """ + select { + multi a := {} + } + limit 1 + """) + + assert Enum.empty?(object[:a]) end end