From 9b397878dc69928d3c2814fbb8fce345816874e8 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Tue, 30 Nov 2021 17:52:27 +0100 Subject: [PATCH] refactor: Make asJsonSingleF "safe", by accessing only the first element of a json_agg result --- src/PostgREST/Query/SqlFragment.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PostgREST/Query/SqlFragment.hs b/src/PostgREST/Query/SqlFragment.hs index e35a8c2dc..fb1941212 100644 --- a/src/PostgREST/Query/SqlFragment.hs +++ b/src/PostgREST/Query/SqlFragment.hs @@ -172,10 +172,10 @@ asJsonF returnsScalar | returnsScalar = "coalesce(json_agg(_postgrest_t.pgrst_scalar), '[]')::character varying" | otherwise = "coalesce(json_agg(_postgrest_t), '[]')::character varying" -asJsonSingleF :: Bool -> SqlFragment --TODO! unsafe when the query actually returns multiple rows, used only on inserting and returning single element +asJsonSingleF :: Bool -> SqlFragment asJsonSingleF returnsScalar - | returnsScalar = "coalesce(string_agg(to_json(_postgrest_t.pgrst_scalar)::text, ','), 'null')::character varying" - | otherwise = "coalesce(string_agg(to_json(_postgrest_t)::text, ','), '')::character varying" + | returnsScalar = "coalesce((json_agg(_postgrest_t.pgrst_scalar)->0)::text, 'null')" + | otherwise = "coalesce((json_agg(_postgrest_t)->0)::text, 'null')" asBinaryF :: FieldName -> SqlFragment asBinaryF fieldName = "coalesce(string_agg(_postgrest_t." <> pgFmtIdent fieldName <> ", ''), '')"