fix: Detect default values for String types in OpenAPI output (#1928)

This commit is contained in:
laurenceisla
2021-08-27 21:59:54 -05:00
committed by GitHub
parent ed98ac9d7e
commit ed5072f4b1
5 changed files with 80 additions and 1 deletions
+2
View File
@@ -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
+12 -1
View File
@@ -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
+56
View File
@@ -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
+1
View File
@@ -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
+9
View File
@@ -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;