Skip to content

Commit

Permalink
fixup! [#114] Swagger API generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dcastro committed Aug 22, 2022
1 parent 421a615 commit b6bb197
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
16 changes: 8 additions & 8 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"FieldName": {
"type": "string",
"example": "password",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]*"
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]+"
},
"Field": {
"type": "object",
Expand Down Expand Up @@ -159,7 +159,7 @@
"EntryTag": {
"type": "string",
"example": "some-tag",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]*"
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]+"
},
"NewField": {
"type": "object",
Expand Down Expand Up @@ -509,7 +509,7 @@
"schema": {
"type": "string",
"example": "password",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]*"
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]+"
}
}
],
Expand Down Expand Up @@ -917,7 +917,7 @@
"schema": {
"type": "string",
"example": "password",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]*"
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]+"
}
}
]
Expand Down Expand Up @@ -1006,7 +1006,7 @@
"schema": {
"type": "string",
"example": "some-tag",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]*"
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]+"
}
}
]
Expand Down Expand Up @@ -1093,7 +1093,7 @@
"schema": {
"type": "string",
"example": "some-tag",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]*"
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]+"
}
}
]
Expand Down Expand Up @@ -1182,7 +1182,7 @@
"schema": {
"type": "string",
"example": "password",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]*"
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]+"
}
}
]
Expand Down Expand Up @@ -1271,7 +1271,7 @@
"schema": {
"type": "string",
"example": "password",
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]*"
"pattern": "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;]+"
}
}
]
Expand Down
1 change: 0 additions & 1 deletion lib/CLI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ deriving stock instance Show SomeCommand
-- Command results
----------------------------------------------------------------------------


data ViewResult
= VRDirectory Directory
| VREntry Entry
Expand Down
6 changes: 4 additions & 2 deletions lib/Coffer/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
-- | Module with orphan instances
module Coffer.Instances () where

import Control.Lens
import Data.Data (Proxy(..))
import Data.OpenApi
import Data.Text (Text)
import Servant.Client (BaseUrl)

instance ToSchema BaseUrl where
declareNamedSchema _ = pure $ NamedSchema Nothing $ mempty & type_ ?~ OpenApiString
declareNamedSchema _ = pure $ NamedSchema Nothing $
toSchema @Text Proxy
11 changes: 4 additions & 7 deletions lib/Coffer/Path.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import Data.Text (Text)
import Data.Text qualified as T
import Fmt (Buildable, build, pretty)
import GHC.Generics (Generic)
import Servant (FromHttpApiData(..), ToHttpApiData(..))
import Servant (FromHttpApiData(..), Proxy(..), ToHttpApiData(..))
import Text.Interpolation.Nyan

-- $setup
Expand All @@ -67,9 +67,8 @@ instance FromHttpApiData PathSegment where

instance ToSchema PathSegment where
declareNamedSchema _ = pure $ NamedSchema (Just "PathSegment") $
mempty
toSchema @Text Proxy
& Schema.pattern ?~ pathSegmentPattern
& type_ ?~ OpenApiString
& Schema.example ?~ "accounts"

pathSegmentPattern :: Pattern
Expand Down Expand Up @@ -98,9 +97,8 @@ newtype Path = Path { unPath :: [PathSegment] }

instance ToParamSchema Path where
toParamSchema _ =
mempty
toSchema @Text Proxy
& Schema.pattern ?~ [int|s|(/#{pathSegmentPattern})*|]
& type_ ?~ OpenApiString
& example ?~ String (toUrlPiece examplePath)

examplePath :: Path
Expand Down Expand Up @@ -160,9 +158,8 @@ instance A.ToJSON EntryPath where

instance ToParamSchema EntryPath where
toParamSchema _ =
mempty
toSchema @Text Proxy
& Schema.pattern ?~ [int|s|(/#{pathSegmentPattern})+|]
& type_ ?~ OpenApiString
& example ?~ toJSON exampleEntryPath

instance ToSchema EntryPath where
Expand Down
19 changes: 9 additions & 10 deletions lib/Entry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Data.Aeson qualified as A
import Data.Aeson.Casing
import Data.Aeson.TH
import Data.Bifunctor (Bifunctor(first))
import Data.Data (Proxy(..))
import Data.HashMap.Strict (HashMap)
import Data.HashMap.Strict qualified as HS
import Data.Hashable (Hashable)
Expand Down Expand Up @@ -82,12 +83,11 @@ instance ToSchema FieldName where

instance ToParamSchema FieldName where
toParamSchema _ =
mempty
toSchema @Text Proxy
& Schema.pattern ?~ fieldNamePattern
& type_ ?~ OpenApiString
& example ?~ toJSON (either (error . pretty) id $ newFieldName "password")
where
fieldNamePattern = "[" <> T.pack allowedCharSet <> "]*"
fieldNamePattern = "[" <> T.pack allowedCharSet <> "]+"

allowedCharSet :: [Char]
allowedCharSet = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ "-_;"
Expand Down Expand Up @@ -125,12 +125,11 @@ instance ToSchema EntryTag where

instance ToParamSchema EntryTag where
toParamSchema _ =
mempty
toSchema @Text Proxy
& Schema.pattern ?~ entryTagPattern
& type_ ?~ OpenApiString
& example ?~ toJSON exampleEntryTag
where
entryTagPattern = "[" <> T.pack allowedCharSet <> "]*"
entryTagPattern = "[" <> T.pack allowedCharSet <> "]+"

newtype BadEntryTag = BadEntryTag { unBadEntryTag :: Text }
deriving newtype Buildable
Expand All @@ -154,8 +153,8 @@ data FieldVisibility = Public | Private
deriving anyclass (Hashable)

instance ToSchema FieldVisibility where
declareNamedSchema _ = pure $ NamedSchema (Just "FieldVisibility") $ mempty
& type_ ?~ OpenApiString
declareNamedSchema _ = pure $ NamedSchema (Just "FieldVisibility") $
toSchema @Text Proxy
& enum_ ?~ ["public", "private"]

instance Buildable FieldVisibility where
Expand All @@ -180,8 +179,8 @@ newtype FieldContents = FieldContents { unFieldContents :: Text }
makeLensesFor [("unFieldContents", "fieldContents")] ''FieldContents

instance ToSchema FieldContents where
declareNamedSchema _ = pure $ NamedSchema Nothing $ mempty
& type_ ?~ OpenApiString
declareNamedSchema _ = pure $ NamedSchema Nothing $
toSchema @Text Proxy
& example ?~ toJSON (FieldContents "some-password")

-- | User can use ANSI control sequences in field contents.
Expand Down
3 changes: 3 additions & 0 deletions lib/Web/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import Entry
import Servant.API
import Web.Types (CopiedEntry, NewEntry)

-- Note: We can't yet add swagger docs to `QueryParams` and `QueryFlag`.
-- See: https://github.com/haskell-servant/servant/issues/1602

type API
= RequiredHeaderDesc "Coffer-Backend" SomeBackend "Details about the backend to connect to."
:> "api" :> "v1" :> "content" :>
Expand Down

0 comments on commit b6bb197

Please sign in to comment.