fix: json/jsonb columns should not have type "string" in OpenAPI spec (#2203)

* Switch to no type for json/jsonb
This commit is contained in:
Ezequiel Alvarez
2022-03-26 15:16:35 +01:00
committed by GitHub
parent a3c1d9977f
commit 799daa7556
5 changed files with 58 additions and 25 deletions
+1
View File
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed ### Fixed
- #2165, Fix json/jsonb columns should not have type in OpenAPI spec
- #2020, Execute deferred constraint triggers when using `Prefer: tx=rollback` - @wolfgangwalther - #2020, Execute deferred constraint triggers when using `Prefer: tx=rollback` - @wolfgangwalther
- #2058, Return 204 No Content without Content-Type for PUT - @wolfgangwalther - #2058, Return 204 No Content without Content-Type for PUT - @wolfgangwalther
- #2077, Fix `is` not working with upper or mixed case values like `NULL, TrUe, FaLsE` - @steve-chavez - #2077, Fix `is` not working with upper or mixed case values like `NULL, TrUe, FaLsE` - @steve-chavez
+18 -16
View File
@@ -55,24 +55,26 @@ encode conf dbStructure tables procs schemaDescription =
makeMimeList :: [ContentType] -> MimeList makeMimeList :: [ContentType] -> MimeList
makeMimeList cs = MimeList $ fmap (fromString . BS.unpack . toMime) cs makeMimeList cs = MimeList $ fmap (fromString . BS.unpack . toMime) cs
toSwaggerType :: Text -> SwaggerType t toSwaggerType :: Text -> Maybe (SwaggerType t)
toSwaggerType "character varying" = SwaggerString toSwaggerType "character varying" = Just SwaggerString
toSwaggerType "character" = SwaggerString toSwaggerType "character" = Just SwaggerString
toSwaggerType "text" = SwaggerString toSwaggerType "text" = Just SwaggerString
toSwaggerType "boolean" = SwaggerBoolean toSwaggerType "boolean" = Just SwaggerBoolean
toSwaggerType "smallint" = SwaggerInteger toSwaggerType "smallint" = Just SwaggerInteger
toSwaggerType "integer" = SwaggerInteger toSwaggerType "integer" = Just SwaggerInteger
toSwaggerType "bigint" = SwaggerInteger toSwaggerType "bigint" = Just SwaggerInteger
toSwaggerType "numeric" = SwaggerNumber toSwaggerType "numeric" = Just SwaggerNumber
toSwaggerType "real" = SwaggerNumber toSwaggerType "real" = Just SwaggerNumber
toSwaggerType "double precision" = SwaggerNumber toSwaggerType "double precision" = Just SwaggerNumber
toSwaggerType "ARRAY" = SwaggerArray toSwaggerType "ARRAY" = Just SwaggerArray
toSwaggerType _ = SwaggerString toSwaggerType "json" = Nothing
toSwaggerType "jsonb" = Nothing
toSwaggerType _ = Just SwaggerString
parseDefault :: Text -> Text -> Text parseDefault :: Text -> Text -> Text
parseDefault colType colDefault = parseDefault colType colDefault =
case toSwaggerType colType of case toSwaggerType colType of
SwaggerString -> wrapInQuotations $ case T.stripSuffix ("::" <> colType) colDefault of Just SwaggerString -> wrapInQuotations $ case T.stripSuffix ("::" <> colType) colDefault of
Just def -> T.dropAround (=='\'') def Just def -> T.dropAround (=='\'') def
Nothing -> colDefault Nothing -> colDefault
_ -> colDefault _ -> colDefault
@@ -124,7 +126,7 @@ makeProperty rels pks c = (colName c, Inline s)
& enum_ .~ e & enum_ .~ e
& format ?~ colType c & format ?~ colType c
& maxLength .~ (fromIntegral <$> colMaxLen c) & maxLength .~ (fromIntegral <$> colMaxLen c)
& type_ ?~ toSwaggerType (colType c) & type_ .~ toSwaggerType (colType c)
makeProcSchema :: ProcDescription -> Schema makeProcSchema :: ProcDescription -> Schema
makeProcSchema pd = makeProcSchema pd =
@@ -138,7 +140,7 @@ makeProcProperty :: ProcParam -> (Text, Referenced Schema)
makeProcProperty (ProcParam n t _ _) = (n, Inline s) makeProcProperty (ProcParam n t _ _) = (n, Inline s)
where where
s = (mempty :: Schema) s = (mempty :: Schema)
& type_ ?~ toSwaggerType t & type_ .~ toSwaggerType t
& format ?~ t & format ?~ t
makePreferParam :: [Text] -> Param makePreferParam :: [Text] -> Param
+6 -4
View File
@@ -883,17 +883,19 @@ def test_admin_live_dependent_on_main_app(defaultenv):
response = postgrest.admin.get("/live") response = postgrest.admin.get("/live")
assert response.status_code == 503 assert response.status_code == 503
@pytest.mark.parametrize("specialhostvalue", FIXTURES["specialhostvalues"]) @pytest.mark.parametrize("specialhostvalue", FIXTURES["specialhostvalues"])
def test_admin_works_with_host_special_values(specialhostvalue, defaultenv): def test_admin_works_with_host_special_values(specialhostvalue, defaultenv):
"Should get a success from the admin live and ready endpoints when using special host values for the main app" "Should get a success from the admin live and ready endpoints when using special host values for the main app"
with run(env=defaultenv, port=freeport(), host=specialhostvalue) as postgrest: with run(env=defaultenv, port=freeport(), host=specialhostvalue) as postgrest:
response = postgrest.admin.get("/live") response = postgrest.admin.get("/live")
assert response.status_code == 200 assert response.status_code == 200
response = postgrest.admin.get("/ready")
assert response.status_code == 200
response = postgrest.admin.get("/ready")
assert response.status_code == 200
@pytest.mark.parametrize( @pytest.mark.parametrize(
"level, has_output", "level, has_output",
+30 -4
View File
@@ -430,6 +430,34 @@ spec actualPgVersion = describe "OpenAPI" $ do
} }
|] |]
it "json to any" $ do
r <- simpleBody <$> get "/"
let types = r ^? key "definitions" . key "openapi_types" . key "properties" . key "a_json"
liftIO $
types `shouldBe` Just
[aesonQQ|
{
"format": "json"
}
|]
it "jsonb to any" $ do
r <- simpleBody <$> get "/"
let types = r ^? key "definitions" . key "openapi_types" . key "properties" . key "a_jsonb"
liftIO $
types `shouldBe` Just
[aesonQQ|
{
"format": "jsonb"
}
|]
describe "Detects default values" $ do describe "Detects default values" $ do
it "text" $ do it "text" $ do
@@ -548,12 +576,10 @@ spec actualPgVersion = describe "OpenAPI" $ do
"type": "integer" "type": "integer"
}, },
"json": { "json": {
"format": "json", "format": "json"
"type": "string"
}, },
"jsonb": { "jsonb": {
"format": "jsonb", "format": "jsonb"
"type": "string"
} }
}, },
"type": "object", "type": "object",
+3 -1
View File
@@ -1807,7 +1807,9 @@ CREATE TABLE test.openapi_types(
"a_bigint" bigint, "a_bigint" bigint,
"a_numeric" numeric, "a_numeric" numeric,
"a_real" real, "a_real" real,
"a_double_precision" double precision "a_double_precision" double precision,
"a_json" json,
"a_jsonb" jsonb
); );
CREATE TABLE test.openapi_defaults( CREATE TABLE test.openapi_defaults(