From d88b16e5ab0d71003156206aaf38d9798d72a5e9 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Mon, 9 May 2022 12:35:30 -0500 Subject: [PATCH] fix: allow cast on types with underscores/numbers e.g. select=oid_array::_int4 --- CHANGELOG.md | 1 + src/PostgREST/Request/QueryParams.hs | 2 +- test/spec/Feature/Query/QuerySpec.hs | 7 +++++++ test/spec/fixtures/data.sql | 9 +++++++++ test/spec/fixtures/privileges.sql | 1 + test/spec/fixtures/schema.sql | 8 ++++---- 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 227678e5c..5cba0284c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #2254, Fix inferring a foreign key column as a primary key column on views - @steve-chavez - #2070, Restrict generated many-to-many relationships - @steve-chavez + Only adds many-to-many relationships when: a table has FKs to two other tables and these FK columns are part of the table's PK columns. + - #2278, Allow casting to types with underscores and numbers(e.g. `select=oid_array::_int4`) - @steve-chavez ### Changed diff --git a/src/PostgREST/Request/QueryParams.hs b/src/PostgREST/Request/QueryParams.hs index 23bcddeb5..20f238a57 100644 --- a/src/PostgREST/Request/QueryParams.hs +++ b/src/PostgREST/Request/QueryParams.hs @@ -394,7 +394,7 @@ pFieldSelect = lexeme $ do alias <- optionMaybe ( try(pFieldName <* aliasSeparator) ) fld <- pField - cast' <- optionMaybe (string "::" *> many letter) + cast' <- optionMaybe (string "::" *> many (letter <|> digit <|> oneOf "_")) return (fld, toS <$> cast', alias, Nothing, Nothing) ) <|> do diff --git a/test/spec/Feature/Query/QuerySpec.hs b/test/spec/Feature/Query/QuerySpec.hs index c3ecf3361..9649e328e 100644 --- a/test/spec/Feature/Query/QuerySpec.hs +++ b/test/spec/Feature/Query/QuerySpec.hs @@ -365,6 +365,13 @@ spec actualPgVersion = do , matchHeaders = [] } + it "can cast types with underscore and numbers" $ + get "/oid_test?select=id,oid_col::int,oid_array_col::_int4" + `shouldRespondWith` [json| + [{"id":1,"oid_col":12345,"oid_array_col":[1,2,3,4,5]}] + |] + { matchHeaders = [matchContentTypeJson] } + it "requesting parents and children" $ get "/projects?id=eq.1&select=id, name, clients(*), tasks(id, name)" `shouldRespondWith` [json|[{"id":1,"name":"Windows 7","clients":{"id":1,"name":"Microsoft"},"tasks":[{"id":1,"name":"Design w7"},{"id":2,"name":"Code w7"}]}]|] diff --git a/test/spec/fixtures/data.sql b/test/spec/fixtures/data.sql index 24bc78a4a..e68252e16 100644 --- a/test/spec/fixtures/data.sql +++ b/test/spec/fixtures/data.sql @@ -762,3 +762,12 @@ INSERT INTO test.limited_delete_items_cpk VALUES (1, 'item-1'), (2, 'item-2'), ( TRUNCATE TABLE test.limited_delete_items_no_pk CASCADE; INSERT INTO test.limited_delete_items_no_pk VALUES (1, 'item-1'), (2, 'item-2'), (3, 'item-3'); + +TRUNCATE TABLE test.xmltest CASCADE; +INSERT INTO test.xmltest VALUES +(1, 'foo'), +(2, 'bar'), +(3, ''); + +TRUNCATE TABLE test.oid_test CASCADE; +INSERT INTO oid_test(id, oid_col, oid_array_col) VALUES (1, '12345', '{1,2,3,4,5}'::oid[]); diff --git a/test/spec/fixtures/privileges.sql b/test/spec/fixtures/privileges.sql index 7ac0f7d5c..d50223069 100644 --- a/test/spec/fixtures/privileges.sql +++ b/test/spec/fixtures/privileges.sql @@ -179,6 +179,7 @@ GRANT ALL ON TABLE , limited_delete_items_cpk_view , limited_update_items_cpk_view , xmltest + , oid_test TO postgrest_test_anonymous; GRANT INSERT ON TABLE insertonly TO postgrest_test_anonymous; diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index 25013243a..cbc73b39f 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -2589,7 +2589,7 @@ CREATE TABLE test.xmltest ( xml pg_catalog.xml NOT NULL ); -INSERT INTO test.xmltest VALUES -(1, 'foo'), -(2, 'bar'), -(3, ''); +CREATE TABLE oid_test( + id int, + oid_col oid, + oid_array_col oid[]);