fix: Detect default values for String types in OpenAPI output (#1928)
This commit is contained in:
@@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
### Fixed
|
### 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
|
## [8.0.0] - 2021-07-25
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -65,8 +65,19 @@ toSwaggerType "bigint" = SwaggerInteger
|
|||||||
toSwaggerType "numeric" = SwaggerNumber
|
toSwaggerType "numeric" = SwaggerNumber
|
||||||
toSwaggerType "real" = SwaggerNumber
|
toSwaggerType "real" = SwaggerNumber
|
||||||
toSwaggerType "double precision" = SwaggerNumber
|
toSwaggerType "double precision" = SwaggerNumber
|
||||||
|
toSwaggerType "ARRAY" = SwaggerArray
|
||||||
toSwaggerType _ = SwaggerString
|
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 :: [Relationship] -> [PrimaryKey] -> (Table, [Column], [Text]) -> (Text, Schema)
|
||||||
makeTableDef rels pks (t, cs, _) =
|
makeTableDef rels pks (t, cs, _) =
|
||||||
let tn = tableName t in
|
let tn = tableName t in
|
||||||
@@ -107,7 +118,7 @@ makeProperty rels pks c = (colName c, Inline s)
|
|||||||
colDescription c
|
colDescription c
|
||||||
s =
|
s =
|
||||||
(mempty :: Schema)
|
(mempty :: Schema)
|
||||||
& default_ .~ (JSON.decode . toS =<< colDefault c)
|
& default_ .~ (JSON.decode . toS . parseDefault (colType c) =<< colDefault c)
|
||||||
& description .~ d
|
& description .~ d
|
||||||
& enum_ .~ e
|
& enum_ .~ e
|
||||||
& format ?~ colType c
|
& format ?~ colType c
|
||||||
|
|||||||
@@ -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
|
describe "RPC" $ do
|
||||||
|
|
||||||
it "includes function summary/description and body schema for arguments" $ do
|
it "includes function summary/description and body schema for arguments" $ do
|
||||||
|
|||||||
Vendored
+1
@@ -115,6 +115,7 @@ GRANT ALL ON TABLE
|
|||||||
, pgrst_reserved_chars
|
, pgrst_reserved_chars
|
||||||
, authors_w_entities
|
, authors_w_entities
|
||||||
, openapi_types
|
, openapi_types
|
||||||
|
, openapi_defaults
|
||||||
, getallprojects_view
|
, getallprojects_view
|
||||||
, get_projects_above_view
|
, get_projects_above_view
|
||||||
, web_content
|
, web_content
|
||||||
|
|||||||
Vendored
+9
@@ -1751,6 +1751,15 @@ CREATE TABLE test.openapi_types(
|
|||||||
"a_double_precision" double precision
|
"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)
|
create function add_them(a integer, b integer)
|
||||||
returns integer as $$
|
returns integer as $$
|
||||||
select a + b;
|
select a + b;
|
||||||
|
|||||||
Reference in New Issue
Block a user