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

Fixes bug in routes #31

Merged
merged 1 commit into from
Oct 19, 2023
Merged
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
13 changes: 8 additions & 5 deletions mig/src/Mig/Core/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,18 @@ toNormalApi api = ApiNormal $ fmap (fmap toInputMediaMap . toOutputMediaMap) (to
toInputMediaMap = InputMediaMap . toMediaMapBy getInputType

toOutputMediaMap :: Api (Route.Route m) -> OutputMediaMap (Api (Route.Route m))
toOutputMediaMap = OutputMediaMap . toMediaMapBy (\routeInfo -> routeInfo.output.media)
toOutputMediaMap = OutputMediaMap . toMediaMapBy (\routeInfo -> Just routeInfo.output.media)

toMediaMapBy :: (RouteInfo -> MediaType) -> Api (Route.Route m) -> MediaMap (Api (Route.Route m))
toMediaMapBy :: (RouteInfo -> Maybe MediaType) -> Api (Route.Route m) -> MediaMap (Api (Route.Route m))
toMediaMapBy getMedia a =
MediaMap (toMediaApi <$> medias) a
MediaMap (filterAnyCases $ toMediaApi <$> medias) a
where
medias = Set.toList $ foldMap (\route -> Set.singleton (getMedia route.info)) a
medias = Set.toList $ foldMap (\route -> maybe Set.empty Set.singleton (getMedia route.info)) a

toMediaApi media = (media, filterApi (\route -> getMedia route.info == media) a)
toMediaApi media = (media, filterApi (\route -> getMedia route.info == Just media) a)

-- filter out any cases as they are covered by second argument of MediaMap value
filterAnyCases = filter (("*/*" /= ) . fst)

-- | Read sub-api by HTTP method, accept-type and content-type
fromNormalApi :: Method -> ByteString -> ByteString -> ApiNormal a -> Maybe (Api a)
Expand Down
5 changes: 2 additions & 3 deletions mig/src/Mig/Core/Types/Info.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ module Mig.Core.Types.Info (

import Data.List.Extra (firstJust)
import Data.Map.Strict qualified as Map
import Data.Maybe
import Data.OpenApi
import Data.OpenApi.Declare (runDeclare)
import Data.Proxy
Expand Down Expand Up @@ -104,8 +103,8 @@ data RouteInput
deriving (Show, Eq)

-- | Get input media-type
getInputType :: RouteInfo -> MediaType
getInputType route = fromMaybe "*/*" $ firstJust (fromInput . (.content)) route.inputs
getInputType :: RouteInfo -> Maybe MediaType
getInputType route = firstJust (fromInput . (.content)) route.inputs
where
fromInput = \case
ReqBodyInput ty _ -> Just ty
Expand Down
Loading