diff --git a/CHANGELOG.md b/CHANGELOG.md index 375cfc8d8..d6145d779 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed + - #2165, Fix json/jsonb columns should not have type in OpenAPI spec - #2020, Execute deferred constraint triggers when using `Prefer: tx=rollback` - @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 diff --git a/src/PostgREST/OpenAPI.hs b/src/PostgREST/OpenAPI.hs index cfdaae5c2..50e932466 100644 --- a/src/PostgREST/OpenAPI.hs +++ b/src/PostgREST/OpenAPI.hs @@ -55,24 +55,26 @@ encode conf dbStructure tables procs schemaDescription = makeMimeList :: [ContentType] -> MimeList makeMimeList cs = MimeList $ fmap (fromString . BS.unpack . toMime) cs -toSwaggerType :: Text -> SwaggerType t -toSwaggerType "character varying" = SwaggerString -toSwaggerType "character" = SwaggerString -toSwaggerType "text" = SwaggerString -toSwaggerType "boolean" = SwaggerBoolean -toSwaggerType "smallint" = SwaggerInteger -toSwaggerType "integer" = SwaggerInteger -toSwaggerType "bigint" = SwaggerInteger -toSwaggerType "numeric" = SwaggerNumber -toSwaggerType "real" = SwaggerNumber -toSwaggerType "double precision" = SwaggerNumber -toSwaggerType "ARRAY" = SwaggerArray -toSwaggerType _ = SwaggerString +toSwaggerType :: Text -> Maybe (SwaggerType t) +toSwaggerType "character varying" = Just SwaggerString +toSwaggerType "character" = Just SwaggerString +toSwaggerType "text" = Just SwaggerString +toSwaggerType "boolean" = Just SwaggerBoolean +toSwaggerType "smallint" = Just SwaggerInteger +toSwaggerType "integer" = Just SwaggerInteger +toSwaggerType "bigint" = Just SwaggerInteger +toSwaggerType "numeric" = Just SwaggerNumber +toSwaggerType "real" = Just SwaggerNumber +toSwaggerType "double precision" = Just SwaggerNumber +toSwaggerType "ARRAY" = Just SwaggerArray +toSwaggerType "json" = Nothing +toSwaggerType "jsonb" = Nothing +toSwaggerType _ = Just SwaggerString parseDefault :: Text -> Text -> Text parseDefault colType colDefault = 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 Nothing -> colDefault _ -> colDefault @@ -124,7 +126,7 @@ makeProperty rels pks c = (colName c, Inline s) & enum_ .~ e & format ?~ colType c & maxLength .~ (fromIntegral <$> colMaxLen c) - & type_ ?~ toSwaggerType (colType c) + & type_ .~ toSwaggerType (colType c) makeProcSchema :: ProcDescription -> Schema makeProcSchema pd = @@ -138,7 +140,7 @@ makeProcProperty :: ProcParam -> (Text, Referenced Schema) makeProcProperty (ProcParam n t _ _) = (n, Inline s) where s = (mempty :: Schema) - & type_ ?~ toSwaggerType t + & type_ .~ toSwaggerType t & format ?~ t makePreferParam :: [Text] -> Param diff --git a/test/spec/Feature/OpenApiSpec.hs b/test/spec/Feature/OpenApiSpec.hs index e594bb04e..7e888a0ca 100644 --- a/test/spec/Feature/OpenApiSpec.hs +++ b/test/spec/Feature/OpenApiSpec.hs @@ -425,6 +425,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 it "text" $ do @@ -543,12 +571,10 @@ spec actualPgVersion = describe "OpenAPI" $ do "type": "integer" }, "json": { - "format": "json", - "type": "string" + "format": "json" }, "jsonb": { - "format": "jsonb", - "type": "string" + "format": "jsonb" } }, "type": "object", diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index 88a6d3b18..43f0ed851 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -1807,7 +1807,9 @@ CREATE TABLE test.openapi_types( "a_bigint" bigint, "a_numeric" numeric, "a_real" real, - "a_double_precision" double precision + "a_double_precision" double precision, + "a_json" json, + "a_jsonb" jsonb ); CREATE TABLE test.openapi_defaults(