Skip to content

Commit

Permalink
[#114] Fix swagger structural error
Browse files Browse the repository at this point in the history
Problem: list of tuples - [(EntryPath, EntryPath)] - used in
the WebAPI for `copy` and `rename` causes errors in Swagger docs.
See #115 (comment)

Solution: introduce type `PairObject` just to fix this error.
  • Loading branch information
sancho20021 committed Jul 25, 2022
1 parent 93c62d6 commit fa353ce
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
39 changes: 17 additions & 22 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
},
"components": {
"schemas": {
"PairObject_EntryPath": {
"type": "object",
"required": [
"first",
"second"
],
"properties": {
"second": {
"$ref": "#/components/schemas/EntryPath"
},
"first": {
"$ref": "#/components/schemas/EntryPath"
}
}
},
"EntryPath": {
"type": "string",
"pattern": "(/[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_]*)+"
Expand Down Expand Up @@ -353,17 +368,7 @@
"application/json;charset=utf-8": {
"schema": {
"items": {
"items": [
{
"$ref": "#/components/schemas/EntryPath"
},
{
"$ref": "#/components/schemas/EntryPath"
}
],
"type": "array",
"maxItems": 2,
"minItems": 2
"$ref": "#/components/schemas/PairObject_EntryPath"
},
"type": "array"
}
Expand Down Expand Up @@ -425,17 +430,7 @@
"application/json;charset=utf-8": {
"schema": {
"items": {
"items": [
{
"$ref": "#/components/schemas/EntryPath"
},
{
"$ref": "#/components/schemas/EntryPath"
}
],
"type": "array",
"maxItems": 2,
"minItems": 2
"$ref": "#/components/schemas/PairObject_EntryPath"
},
"type": "array"
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Web/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Data.Text (Text)
import Entry
import GHC.Generics (Generic)
import Servant.API
import Web.Types (NewEntry)
import Web.Types (NewEntry, PairObject)

type API
= Header' [Required, Strict] "token" VaultToken
Expand Down Expand Up @@ -54,14 +54,14 @@ type API
:> RequiredParam "old-path" (QualifiedPath Path)
:> RequiredParam "new-path" (QualifiedPath Path)
:> QueryFlag "force"
:> Post '[JSON] [(EntryPath, EntryPath)]
:> Post '[JSON] [PairObject EntryPath]

:<|> "copy"
:> QueryFlag "dry-run"
:> RequiredParam "old-path" (QualifiedPath Path)
:> RequiredParam "new-path" (QualifiedPath Path)
:> QueryFlag "force"
:> Post '[JSON] [(EntryPath, EntryPath)]
:> Post '[JSON] [PairObject EntryPath]

:<|> "delete"
:> QueryFlag "dry-run"
Expand Down
14 changes: 7 additions & 7 deletions lib/Web/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Fmt (Builder, pretty, unlinesF)
import GHC.Generics (Generic)
import Servant.API
import Servant.Server
import Web.Types (NewEntry(NewEntry), NewField(NewField))
import Web.Types (NewEntry(NewEntry), NewField(NewField), mkPair, PairObject)

data CofferServerError = CofferServerError
{ cseError :: Text
Expand Down Expand Up @@ -71,9 +71,9 @@ handleSetFieldResult = \case
where
pretty = resultToText buildSetFieldResult

handleCopyOrRenameResult :: Bool -> CopyResult -> Handler [(EntryPath, EntryPath)]
handleCopyOrRenameResult :: Bool -> CopyResult -> Handler [PairObject EntryPath]
handleCopyOrRenameResult rename = \case
CPRSuccess _ paths -> pure (paths <&> bimap qpPath qpPath)
CPRSuccess _ paths -> pure (paths <&> (mkPair . bimap qpPath qpPath))
res@CPRPathNotFound{} ->
throwCofferServerError err404 500 (prettySingleMessage res)
res@CPRMissingEntryName{} ->
Expand All @@ -97,10 +97,10 @@ handleCopyOrRenameResult rename = \case
CEDestinationIsDirectory{} -> 504
CEEntryAlreadyExists{} -> 505

handleCopyResult :: CopyResult -> Handler [(EntryPath, EntryPath)]
handleCopyResult :: CopyResult -> Handler [PairObject EntryPath]
handleCopyResult = handleCopyOrRenameResult False

handleRenameResult :: RenameResult -> Handler [(EntryPath, EntryPath)]
handleRenameResult :: RenameResult -> Handler [PairObject EntryPath]
handleRenameResult = handleCopyOrRenameResult True

runBackendIO' :: Sem '[BackendEffect, Error CofferError, Embed IO, Final IO] a -> IO (Either CofferError a)
Expand Down Expand Up @@ -286,7 +286,7 @@ rename
-> QualifiedPath Path
-> QualifiedPath Path
-> Bool
-> Handler [(EntryPath, EntryPath)]
-> Handler [PairObject EntryPath]
rename run token roDryRun roQOldPath roQNewPath roForce =
run token (CmdRename RenameOptions
{ roDryRun
Expand All @@ -302,7 +302,7 @@ copy'
-> QualifiedPath Path
-> QualifiedPath Path
-> Bool
-> Handler [(EntryPath, EntryPath)]
-> Handler [PairObject EntryPath]
copy' run token cpoDryRun cpoQOldPath cpoQNewPath cpoForce =
run token (CmdCopy CopyOptions
{ cpoDryRun
Expand Down
16 changes: 16 additions & 0 deletions lib/Web/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module Web.Types where

import Data.Aeson qualified as A
import Data.Aeson.Casing (aesonPrefix, camelCase)
import Data.Aeson.TH (deriveJSON)
import Data.HashMap.Strict (HashMap)
Expand All @@ -30,3 +31,18 @@ deriveJSON (aesonPrefix camelCase) ''NewEntry

instance ToSchema NewEntry where
declareNamedSchema = genericDeclareNamedSchema $ fromAesonOptions (aesonPrefix camelCase)

-- | Datatype that serves as a workaround for this issue:
-- https://github.com/biocad/openapi3/issues/31
data PairObject a = PairObject
{ poFirst :: a
, poSecond :: a
}
deriving stock (Show, Eq, Generic)
deriving anyclass (A.ToJSON, A.FromJSON)

mkPair :: (a, a) -> PairObject a
mkPair (a, b) = PairObject a b

instance ToSchema a => ToSchema (PairObject a) where
declareNamedSchema = genericDeclareNamedSchema $ fromAesonOptions (aesonPrefix camelCase)

0 comments on commit fa353ce

Please sign in to comment.