Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalise over the Swagger datatype. #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion servant-swagger-ui-core/servant-swagger-ui-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ library
ghc-options: -Wall
build-depends:
base >=4.7 && <4.15
, aeson >=0.8.0.2 && <1.6
, blaze-markup >=0.7.0.2 && <0.9
, bytestring >=0.10.4.0 && <0.11
, http-media >=0.7.1.3 && <0.9
Expand Down
18 changes: 9 additions & 9 deletions servant-swagger-ui-core/src/Servant/Swagger/UI/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
-- :\<|> "cat" :> Capture ":name" CatName :> Get '[JSON] Cat
--
-- -- | API type with bells and whistles, i.e. schema file and swagger-ui.
-- type API = 'SwaggerSchemaUI' "swagger-ui" "swagger.json"
-- type API = 'SwaggerSchemaUI' "swagger-ui" "swagger.json" Swagger
-- :\<|> BasicAPI
--
-- -- | Servant server for an API
Expand All @@ -46,7 +46,6 @@ module Servant.Swagger.UI.Core (
Handler,
) where

import Data.Aeson (ToJSON (..), Value)
import Data.ByteString (ByteString)
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import Network.Wai.Application.Static (embeddedSettings, staticApp)
Expand All @@ -58,7 +57,7 @@ import qualified Data.Text as T

-- | Swagger schema + ui api.
--
-- @SwaggerSchemaUI "swagger-ui" "swagger.json"@ will result into following hierarchy:
-- @SwaggerSchemaUI "swagger-ui" "swagger.json" Swagger@ will result into following hierarchy:
--
-- @
-- \/swagger.json
Expand All @@ -67,10 +66,11 @@ import qualified Data.Text as T
-- \/swagger-ui\/...
-- @
--
-- This type does not actually force served type to be @Swagger@ from @swagger2@ package,
-- it could be arbitrary @aeson@ 'Value'.
type SwaggerSchemaUI (dir :: Symbol) (schema :: Symbol) =
SwaggerSchemaUI' dir (schema :> Get '[JSON] Value)
-- The third type parameter specifies which Haskell datatype contains the Swagger
-- description. Typical instantiations are @Swagger@ from the @swagger2@ package,
-- and @OpenApi@ from the @openapi3@ package.
type SwaggerSchemaUI (dir :: Symbol) (schema :: Symbol) (a :: *) =
SwaggerSchemaUI' dir (schema :> Get '[JSON] a)

-- | Use 'SwaggerSchemaUI'' when you need even more control over
-- where @swagger.json@ is served (e.g. subdirectory).
Expand Down Expand Up @@ -103,11 +103,11 @@ instance (KnownSymbol dir, HasLink api, Link ~ MkLink api Link, IsElem api api)
proxyApi = Proxy :: Proxy api

swaggerSchemaUIServerImpl
:: (Monad m, ServerT api m ~ m Value, ToJSON a)
:: (Monad m, ServerT api m ~ m a)
=> T.Text -> [(FilePath, ByteString)]
-> a -> ServerT (SwaggerSchemaUI' dir api) m
swaggerSchemaUIServerImpl indexTemplate files swagger
= swaggerSchemaUIServerImpl' indexTemplate files $ return $ toJSON swagger
= swaggerSchemaUIServerImpl' indexTemplate files $ return $ swagger

-- | Use a custom server to serve the Swagger spec source.
swaggerSchemaUIServerImpl'
Expand Down
8 changes: 4 additions & 4 deletions servant-swagger-ui-example/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Prelude ()
import Prelude.Compat

import Control.Lens hiding ((.=))
import Data.Aeson (FromJSON, ToJSON, Value)
import Data.Aeson (FromJSON, ToJSON)
import Data.Maybe (fromMaybe)
import Data.String (IsString (..))
import Data.Text (Text)
Expand Down Expand Up @@ -136,13 +136,13 @@ type BasicAPI = Get '[PlainText, JSON] Text

type API =
-- this serves both: swagger.json and swagger-ui
SwaggerSchemaUI "swagger-ui" "swagger.json"
SwaggerSchemaUI "swagger-ui" "swagger.json" Swagger
:<|> BasicAPI

-- To test nested case
type API' = API
:<|> "nested" :> API
:<|> SwaggerSchemaUI' "foo-ui" ("foo" :> "swagger.json" :> Get '[JSON] Value)
:<|> SwaggerSchemaUI' "foo-ui" ("foo" :> "swagger.json" :> Get '[JSON] Swagger)

-- Implementation

Expand Down Expand Up @@ -174,7 +174,7 @@ server' uiFlavour = server Normal
-- Unfortunately we have to specify the basePath manually atm.

schemaUiServer
:: (Server api ~ Handler Value)
:: (Server api ~ Handler Swagger)
=> Swagger -> Server (SwaggerSchemaUI' dir api)
schemaUiServer = case uiFlavour of
Original -> swaggerSchemaUIServer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ library
build-depends: servant-swagger-ui-core >=0.3.5 && <0.4
build-depends:
base >=4.7 && <4.15
, aeson >=0.8.0.2 && <1.6
, bytestring >=0.10.4.0 && <0.11
, file-embed-lzma >=0 && <0.1
, servant >=0.14 && <0.19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ module Servant.Swagger.UI.JensOleG (

import Servant.Swagger.UI.Core

import Data.Aeson (ToJSON, Value)
import Data.ByteString (ByteString)
import Data.Text (Text)
import FileEmbedLzma
Expand All @@ -67,7 +66,7 @@ import Servant
--
-- See <https://github.com/jensoleg/swagger-ui>
jensolegSwaggerSchemaUIServer
:: (Server api ~ Handler Value, ToJSON a)
:: (Server api ~ Handler a)
=> a -> Server (SwaggerSchemaUI' dir api)
jensolegSwaggerSchemaUIServer =
swaggerSchemaUIServerImpl jensolegIndexTemplate jensolegFiles
Expand Down
1 change: 0 additions & 1 deletion servant-swagger-ui-redoc/servant-swagger-ui-redoc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ library
build-depends: servant-swagger-ui-core >=0.3.5 && <0.4
build-depends:
base >=4.7 && <4.15
, aeson >=0.8.0.2 && <1.6
, bytestring >=0.10.4.0 && <0.11
, file-embed-lzma >=0 && <0.1
, servant >=0.14 && <0.19
Expand Down
5 changes: 2 additions & 3 deletions servant-swagger-ui-redoc/src/Servant/Swagger/UI/ReDoc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ module Servant.Swagger.UI.ReDoc (

import Servant.Swagger.UI.Core

import Data.Aeson (ToJSON, Value)
import Data.ByteString (ByteString)
import Data.Text (Text)
import FileEmbedLzma
Expand All @@ -67,7 +66,7 @@ import Servant
--
-- See <https://github.com/Rebilly/ReDoc/tree/v1.x>
redocSchemaUIServer
:: (Server api ~ Handler Value, ToJSON a)
:: (Server api ~ Handler a)
=> a -> Server (SwaggerSchemaUI' dir api)
redocSchemaUIServer =
swaggerSchemaUIServerImpl redocIndexTemplate redocFiles
Expand All @@ -80,7 +79,7 @@ redocSchemaUIServer =
-- redocSchemaUIServerT :: Swagger -> ServerT (SwaggerSchemaUI schema dir) m
-- @
redocSchemaUIServerT
:: (Monad m, ServerT api m ~ m Value, ToJSON a)
:: (Monad m, ServerT api m ~ m a)
=> a -> ServerT (SwaggerSchemaUI' dir api) m
redocSchemaUIServerT =
swaggerSchemaUIServerImpl redocIndexTemplate redocFiles
Expand Down
1 change: 0 additions & 1 deletion servant-swagger-ui/servant-swagger-ui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ library
build-depends: servant-swagger-ui-core >=0.3.5 && <0.4
build-depends:
base >=4.7 && <4.15
, aeson >=0.8.0.2 && <1.6
, bytestring >=0.10.4.0 && <0.11
, file-embed-lzma >=0 && <0.1
, servant >=0.14 && <0.19
Expand Down
6 changes: 3 additions & 3 deletions servant-swagger-ui/src/Servant/Swagger/UI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module Servant.Swagger.UI (

import Servant.Swagger.UI.Core

import Data.Aeson (ToJSON, Value)
-- import Data.Aeson (ToJSON, Value)
import Data.ByteString (ByteString)
import Data.Text (Text)
import FileEmbedLzma
Expand All @@ -75,7 +75,7 @@ import Servant
-- swaggerSchemaUIServer :: OpenApi -> Server (SwaggerSchemaUI schema dir)
-- @
swaggerSchemaUIServer
:: (Server api ~ Handler Value, ToJSON a)
:: (Server api ~ Handler a)
=> a -> Server (SwaggerSchemaUI' dir api)
swaggerSchemaUIServer =
swaggerSchemaUIServerImpl swaggerUiIndexTemplate swaggerUiFiles
Expand All @@ -89,7 +89,7 @@ swaggerSchemaUIServer =
-- swaggerSchemaUIServerT :: OpenApi -> ServerT (SwaggerSchemaUI schema dir) m
-- @
swaggerSchemaUIServerT
:: (Monad m, ServerT api m ~ m Value, ToJSON a)
:: (Monad m, ServerT api m ~ m a)
=> a -> ServerT (SwaggerSchemaUI' dir api) m
swaggerSchemaUIServerT =
swaggerSchemaUIServerImpl swaggerUiIndexTemplate swaggerUiFiles
Expand Down