Include proc parama and their types in openapi

This commit is contained in:
Joe Nelson
2016-09-08 23:00:52 -07:00
parent f49c6aa0f3
commit b9777dec35
3 changed files with 33 additions and 6 deletions
+12 -3
View File
@@ -16,7 +16,7 @@ import Control.Applicative
import Data.List (elemIndex) import Data.List (elemIndex)
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
import Data.Monoid import Data.Monoid
import Data.Text (split) import Data.Text (split, strip)
import qualified Hasql.Session as H import qualified Hasql.Session as H
import PostgREST.Types import PostgREST.Types
import Text.InterpolatedString.Perl6 (q) import Text.InterpolatedString.Perl6 (q)
@@ -99,12 +99,21 @@ accessibleProcs :: H.Query Schema [(Text, ProcDescription)]
accessibleProcs = accessibleProcs =
H.statement sql (HE.value HE.text) H.statement sql (HE.value HE.text)
(map addName <$> HD.rowsList (ProcDescription <$> HD.value HD.text (map addName <$> HD.rowsList (ProcDescription <$> HD.value HD.text
<*> HD.value HD.text <*> (parseArgs <$> HD.value HD.text)
<*> HD.value HD.text)) True <*> HD.value HD.text)) True
where where
addName :: ProcDescription -> (Text, ProcDescription) addName :: ProcDescription -> (Text, ProcDescription)
addName pd = (pdName pd, pd) addName pd = (pdName pd, pd)
parseArgs :: Text -> [(PgArgName, PgArgType)]
parseArgs = mapMaybe list2pair
. map (split (==' ') . strip)
. split (==',')
list2pair :: [a] -> Maybe (a,a)
list2pair (x:y:_) = Just (x,y)
list2pair _ = Nothing
sql = [q| sql = [q|
SELECT p.proname as "proc_name", SELECT p.proname as "proc_name",
pg_get_function_arguments(p.oid) as "args", pg_get_function_arguments(p.oid) as "args",
+17 -2
View File
@@ -24,7 +24,8 @@ import PostgREST.ApiRequest (ContentType(..), toHeader)
import PostgREST.Config (prettyVersion) import PostgREST.Config (prettyVersion)
import PostgREST.QueryBuilder (operators) import PostgREST.QueryBuilder (operators)
import PostgREST.Types (Table(..), Column(..), import PostgREST.Types (Table(..), Column(..),
Proxy(..), ProcDescription(..)) Proxy(..), ProcDescription(..),
PgArgName, PgArgType)
makeMimeList :: [ContentType] -> MimeList makeMimeList :: [ContentType] -> MimeList
makeMimeList cs = MimeList $ map (fromString . toS . toHeader) cs makeMimeList cs = MimeList $ map (fromString . toS . toHeader) cs
@@ -173,6 +174,20 @@ makePostParams tn =
& schema .~ ParamBody (Ref (Reference tn)) & schema .~ ParamBody (Ref (Reference tn))
] ]
makeProcParams :: ProcDescription -> [Param]
makeProcParams pd =
map (makeProcParam $ pdName pd) (pdArgs pd)
makeProcParam :: Text -> (PgArgName, PgArgType) -> Param
makeProcParam refName (n, t) =
(mempty :: Param)
& name .~ n
& required ?~ True
-- & schema .~ ParamBody ((Ref (Reference refName))
& schema .~ ParamOther ((mempty :: ParamOtherSchema)
-- & in_ .~ ParamQuery
& type_ .~ toSwaggerType t)
makeDeleteParams :: [Param] makeDeleteParams :: [Param]
makeDeleteParams = makeDeleteParams =
[ makePreferParam ["return=representation", "return=minimal", "return=none"] ] [ makePreferParam ["return=representation", "return=minimal", "return=none"] ]
@@ -208,7 +223,7 @@ makeProcPathItem :: ProcDescription -> (FilePath, PathItem)
makeProcPathItem pd = ("/rpc/" ++ toS (pdName pd), pe) makeProcPathItem pd = ("/rpc/" ++ toS (pdName pd), pe)
where where
postOp = (mempty :: Operation) postOp = (mempty :: Operation)
& parameters .~ [] & parameters .~ map Inline (makeProcParams pd)
& tags .~ Set.fromList ["/rpc/" <> pdName pd] & tags .~ Set.fromList ["/rpc/" <> pdName pd]
& produces ?~ makeMimeList [CTApplicationJSON] & produces ?~ makeMimeList [CTApplicationJSON]
& at 200 ?~ "OK" & at 200 ?~ "OK"
+4 -1
View File
@@ -16,9 +16,12 @@ data DbStructure = DbStructure {
, dbProcs :: [(Text,ProcDescription)] , dbProcs :: [(Text,ProcDescription)]
} deriving (Show, Eq) } deriving (Show, Eq)
type PgArgName = Text
type PgArgType = Text
data ProcDescription = ProcDescription { data ProcDescription = ProcDescription {
pdName :: Text pdName :: Text
, pdArgs :: Text , pdArgs :: [(PgArgName, PgArgType)]
, pdReturnType :: Text , pdReturnType :: Text
} deriving (Show, Eq) } deriving (Show, Eq)