diff --git a/CHANGELOG.md b/CHANGELOG.md index 77cc7a640..c678872a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed + - #1871, Fix OpenAPI missing default values for String types and identify Array types as "array" instead of "string" - @laurenceisla + ## [8.0.0] - 2021-07-25 ### Added diff --git a/src/PostgREST/OpenAPI.hs b/src/PostgREST/OpenAPI.hs index 8530128ac..ac81306fe 100644 --- a/src/PostgREST/OpenAPI.hs +++ b/src/PostgREST/OpenAPI.hs @@ -65,8 +65,19 @@ toSwaggerType "bigint" = SwaggerInteger toSwaggerType "numeric" = SwaggerNumber toSwaggerType "real" = SwaggerNumber toSwaggerType "double precision" = SwaggerNumber +toSwaggerType "ARRAY" = SwaggerArray toSwaggerType _ = SwaggerString +parseDefault :: Text -> Text -> Text +parseDefault colType colDefault = + case toSwaggerType colType of + SwaggerString -> wrapInQuotations $ case T.stripSuffix ("::" <> colType) colDefault of + Just def -> T.dropAround (=='\'') def + Nothing -> colDefault + _ -> colDefault + where + wrapInQuotations text = "\"" <> text <> "\"" + makeTableDef :: [Relationship] -> [PrimaryKey] -> (Table, [Column], [Text]) -> (Text, Schema) makeTableDef rels pks (t, cs, _) = let tn = tableName t in @@ -107,7 +118,7 @@ makeProperty rels pks c = (colName c, Inline s) colDescription c s = (mempty :: Schema) - & default_ .~ (JSON.decode . toS =<< colDefault c) + & default_ .~ (JSON.decode . toS . parseDefault (colType c) =<< colDefault c) & description .~ d & enum_ .~ e & format ?~ colType c diff --git a/test/Feature/OpenApiSpec.hs b/test/Feature/OpenApiSpec.hs index 190ecb303..4779b6428 100644 --- a/test/Feature/OpenApiSpec.hs +++ b/test/Feature/OpenApiSpec.hs @@ -425,6 +425,62 @@ spec actualPgVersion = describe "OpenAPI" $ do } |] + describe "Detects default values" $ do + + it "text" $ do + r <- simpleBody <$> get "/" + + let defaultValue = r ^? key "definitions" . key "openapi_defaults" . key "properties" . key "text" . key "default" + + liftIO $ + + defaultValue `shouldBe` Just "default" + + it "boolean" $ do + r <- simpleBody <$> get "/" + + let types = r ^? key "definitions" . key "openapi_defaults" . key "properties" . key "boolean" . key "default" + + liftIO $ + + types `shouldBe` Just (Bool False) + + it "integer" $ do + r <- simpleBody <$> get "/" + + let types = r ^? key "definitions" . key "openapi_defaults" . key "properties" . key "integer" . key "default" + + liftIO $ + + types `shouldBe` Just (Number 42) + + it "numeric" $ do + r <- simpleBody <$> get "/" + + let types = r ^? key "definitions" . key "openapi_defaults" . key "properties" . key "numeric" . key "default" + + liftIO $ + + types `shouldBe` Just (Number 42.2) + + it "date" $ do + r <- simpleBody <$> get "/" + + let types = r ^? key "definitions" . key "openapi_defaults" . key "properties" . key "date" . key "default" + + liftIO $ + + types `shouldBe` Just "1900-01-01" + + it "time" $ do + r <- simpleBody <$> get "/" + + let types = r ^? key "definitions" . key "openapi_defaults" . key "properties" . key "time" . key "default" + + liftIO $ + + types `shouldBe` Just "13:00:00" + describe "RPC" $ do it "includes function summary/description and body schema for arguments" $ do diff --git a/test/fixtures/privileges.sql b/test/fixtures/privileges.sql index 48301366f..187106dcf 100644 --- a/test/fixtures/privileges.sql +++ b/test/fixtures/privileges.sql @@ -115,6 +115,7 @@ GRANT ALL ON TABLE , pgrst_reserved_chars , authors_w_entities , openapi_types + , openapi_defaults , getallprojects_view , get_projects_above_view , web_content diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index abc470fe9..58b664cf5 100644 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -1751,6 +1751,15 @@ CREATE TABLE test.openapi_types( "a_double_precision" double precision ); +CREATE TABLE test.openapi_defaults( + "text" text default 'default', + "boolean" boolean default false, + "integer" integer default 42, + "numeric" numeric default 42.2, + "date" date default '1900-01-01'::date, + "time" time default '13:00:00'::time without time zone +); + create function add_them(a integer, b integer) returns integer as $$ select a + b;