1
1
{-# LANGUAGE CPP #-}
2
2
{-# LANGUAGE ConstraintKinds #-}
3
3
{-# LANGUAGE DataKinds #-}
4
+ {-# LANGUAGE GeneralizedNewtypeDeriving #-} -- TODO: can we get rid of this?
4
5
{-# LANGUAGE FlexibleContexts #-}
5
6
{-# LANGUAGE FlexibleInstances #-}
6
7
{-# LANGUAGE OverloadedStrings #-}
7
8
{-# LANGUAGE PolyKinds #-}
8
9
{-# LANGUAGE RankNTypes #-}
9
10
{-# LANGUAGE ScopedTypeVariables #-}
11
+ {-# LANGUAGE StandaloneDeriving #-} -- TODO: can we get away with terminating support for ghcs that don't have this?
10
12
{-# LANGUAGE TypeOperators #-}
11
13
#if __GLASGOW_HASKELL__ >= 806
12
14
{-# LANGUAGE UndecidableInstances #-}
@@ -16,6 +18,10 @@ module Servant.OpenApi.Internal where
16
18
import Prelude ()
17
19
import Prelude.Compat
18
20
21
+ -- TODO: turn on lower version bound once servant is released.
22
+ -- #if MIN_VERSION_servant(0,19,0)
23
+ import Control.Applicative ((<|>) )
24
+ -- #endif
19
25
import Control.Lens
20
26
import Data.Aeson
21
27
import Data.Foldable (toList )
@@ -183,6 +189,56 @@ instance OpenApiMethod 'OPTIONS where openApiMethod _ = options
183
189
instance OpenApiMethod 'HEAD where openApiMethod _ = head_
184
190
instance OpenApiMethod 'PATCH where openApiMethod _ = patch
185
191
192
+ -- TODO: turn on lower version bound once servant is released.
193
+ -- #if MIN_VERSION_servant(0,19,0)
194
+ instance HasOpenApi (UVerb method cs '[] ) where
195
+ toOpenApi _ = mempty
196
+
197
+ -- | @since <TODO>
198
+ instance
199
+ {-# OVERLAPPABLE #-}
200
+ ( ToSchema a ,
201
+ HasStatus a ,
202
+ AllAccept cs ,
203
+ OpenApiMethod method ,
204
+ HasOpenApi (UVerb method cs as )
205
+ ) =>
206
+ HasOpenApi (UVerb method cs (a ': as ))
207
+ where
208
+ toOpenApi _ =
209
+ toOpenApi (Proxy :: Proxy (Verb method (StatusOf a ) cs a ))
210
+ `combineOpenApi` toOpenApi (Proxy :: Proxy (UVerb method cs as ))
211
+ where
212
+ -- workaround for https://github.com/GetShopTV/swagger2/issues/218
213
+ -- We'd like to juse use (<>) but the instances are wrong
214
+ combinePathItem :: PathItem -> PathItem -> PathItem
215
+ combinePathItem s t = s
216
+ { _pathItemGet = _pathItemGet s <> _pathItemGet t
217
+ , _pathItemPut = _pathItemPut s <> _pathItemPut t
218
+ , _pathItemPost = _pathItemPost s <> _pathItemPost t
219
+ , _pathItemDelete = _pathItemDelete s <> _pathItemDelete t
220
+ , _pathItemOptions = _pathItemOptions s <> _pathItemOptions t
221
+ , _pathItemHead = _pathItemHead s <> _pathItemHead t
222
+ , _pathItemPatch = _pathItemPatch s <> _pathItemPatch t
223
+ , _pathItemTrace = _pathItemTrace s <> _pathItemTrace t
224
+ , _pathItemServers = _pathItemServers s <> _pathItemServers t
225
+ , _pathItemParameters = _pathItemParameters s <> _pathItemParameters t
226
+ }
227
+
228
+ combineOpenApi :: OpenApi -> OpenApi -> OpenApi
229
+ combineOpenApi s t = OpenApi
230
+ { _openApiInfo = _openApiInfo s <> _openApiInfo t
231
+ , _openApiServers = _openApiServers s <> _openApiServers t
232
+ , _openApiPaths = InsOrdHashMap. unionWith combinePathItem (_openApiPaths s) (_openApiPaths t)
233
+ , _openApiComponents = _openApiComponents s <> _openApiComponents t
234
+ , _openApiSecurity = _openApiSecurity s <> _openApiSecurity t
235
+ , _openApiTags = _openApiTags s <> _openApiTags t
236
+ , _openApiExternalDocs = _openApiExternalDocs s <|> _openApiExternalDocs t
237
+ }
238
+
239
+ deriving instance ToSchema a => ToSchema (WithStatus s a )
240
+ -- #endif
241
+
186
242
instance {-# OVERLAPPABLE #-} (ToSchema a , AllAccept cs , KnownNat status , OpenApiMethod method ) => HasOpenApi (Verb method status cs a ) where
187
243
toOpenApi _ = toOpenApi (Proxy :: Proxy (Verb method status cs (Headers '[] a )))
188
244
0 commit comments