diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 05e9ae6a9b..e8e71e01e2 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -27,6 +27,7 @@ import Network.Wai.Handler.Warp (defaultSettings, setHost, setPort, import System.Posix.Types (FileMode) import qualified Data.HashMap.Strict as HM +import qualified Data.Text.Encoding as T import qualified Hasql.Transaction.Sessions as SQL import qualified Network.Wai as Wai import qualified Network.Wai.Handler.Warp as Warp @@ -53,7 +54,7 @@ import PostgREST.Error (Error) import PostgREST.Query (DbHandler) import PostgREST.SchemaCache (SchemaCache (..)) import PostgREST.SchemaCache.Routine (Routine (..)) -import PostgREST.Version (prettyVersion) +import PostgREST.Version (docsVersion, prettyVersion) import Protolude hiding (Handler) @@ -199,7 +200,7 @@ handleRequest AuthResult{..} conf appState authenticated prepared pgVer apiReq@A (ActionInspect headersOnly, TargetDefaultSpec tSchema) -> do oaiResult <- runQuery roleIsoLvl Plan.inspectPlanTxMode $ Query.openApiQuery sCache pgVer conf tSchema - return $ Response.openApiResponse headersOnly oaiResult conf sCache iSchema iNegotiatedByProfile + return $ Response.openApiResponse (T.decodeUtf8 prettyVersion, docsVersion) headersOnly oaiResult conf sCache iSchema iNegotiatedByProfile (ActionInfo, TargetIdent identifier) -> return $ Response.infoIdentResponse identifier sCache diff --git a/src/PostgREST/Response.hs b/src/PostgREST/Response.hs index 6d2b96f1c8..39e084ec7e 100644 --- a/src/PostgREST/Response.hs +++ b/src/PostgREST/Response.hs @@ -223,11 +223,11 @@ invokeResponse invMethod proc ctxApiRequest@ApiRequest{..} resultSet = case resu RSPlan plan -> Wai.responseLBS HTTP.status200 (contentTypeHeaders ctxApiRequest) $ LBS.fromStrict plan -openApiResponse :: Bool -> Maybe (TablesMap, RoutineMap, Maybe Text) -> AppConfig -> SchemaCache -> Schema -> Bool -> Wai.Response -openApiResponse headersOnly body conf sCache schema negotiatedByProfile = +openApiResponse :: (Text, Text) -> Bool -> Maybe (TablesMap, RoutineMap, Maybe Text) -> AppConfig -> SchemaCache -> Schema -> Bool -> Wai.Response +openApiResponse versions headersOnly body conf sCache schema negotiatedByProfile = Wai.responseLBS HTTP.status200 (MediaType.toContentType MTOpenAPI : maybeToList (profileHeader schema negotiatedByProfile)) - (maybe mempty (\(x, y, z) -> if headersOnly then mempty else OpenAPI.encode conf sCache x y z) body) + (maybe mempty (\(x, y, z) -> if headersOnly then mempty else OpenAPI.encode versions conf sCache x y z) body) -- | Response with headers and status overridden from GUCs. gucResponse diff --git a/src/PostgREST/Response/OpenAPI.hs b/src/PostgREST/Response/OpenAPI.hs index b1391247d2..8e957e0d21 100644 --- a/src/PostgREST/Response/OpenAPI.hs +++ b/src/PostgREST/Response/OpenAPI.hs @@ -12,7 +12,6 @@ import qualified Data.ByteString.Lazy as LBS import qualified Data.HashMap.Strict as HM import qualified Data.HashSet.InsOrd as Set import qualified Data.Text as T -import qualified Data.Text.Encoding as T import Control.Arrow ((&&&)) import Data.HashMap.Strict.InsOrd (InsOrdHashMap, fromList) @@ -36,16 +35,16 @@ import PostgREST.SchemaCache.Routine (Routine (..), import PostgREST.SchemaCache.Table (Column (..), Table (..), TablesMap, tableColumnsList) -import PostgREST.Version (docsVersion, prettyVersion) import PostgREST.MediaType import Protolude hiding (Proxy, get) -encode :: AppConfig -> SchemaCache -> TablesMap -> HM.HashMap k [Routine] -> Maybe Text -> LBS.ByteString -encode conf sCache tables procs schemaDescription = +encode :: (Text, Text) -> AppConfig -> SchemaCache -> TablesMap -> HM.HashMap k [Routine] -> Maybe Text -> LBS.ByteString +encode versions conf sCache tables procs schemaDescription = JSON.encode $ postgrestSpec + versions (dbRelationships sCache) (concat $ HM.elems procs) (snd <$> HM.toList tables) @@ -392,12 +391,12 @@ escapeHostName "*6" = "0.0.0.0" escapeHostName "!6" = "0.0.0.0" escapeHostName h = h -postgrestSpec :: RelationshipsMap -> [Routine] -> [Table] -> (Text, Text, Integer, Text) -> Maybe Text -> Bool -> Swagger -postgrestSpec rels pds ti (s, h, p, b) sd allowSecurityDef = (mempty :: Swagger) +postgrestSpec :: (Text, Text) -> RelationshipsMap -> [Routine] -> [Table] -> (Text, Text, Integer, Text) -> Maybe Text -> Bool -> Swagger +postgrestSpec (prettyVersion, docsVersion) rels pds ti (s, h, p, b) sd allowSecurityDef = (mempty :: Swagger) & basePath ?~ T.unpack b & schemes ?~ [s'] & info .~ ((mempty :: Info) - & version .~ T.decodeUtf8 prettyVersion + & version .~ prettyVersion & title .~ fromMaybe "PostgREST API" dTitle & description ?~ fromMaybe "This is a dynamic API generated by PostgREST" dDesc) & externalDocs ?~ ((mempty :: ExternalDocs)