|
11 | 11 | #if __GLASGOW_HASKELL__ >= 806
|
12 | 12 | {-# LANGUAGE UndecidableInstances #-}
|
13 | 13 | #endif
|
| 14 | +{-# OPTIONS_GHC -Wno-orphans #-} |
14 | 15 | module Servant.OpenApi.Internal where
|
15 | 16 |
|
16 | 17 | import Prelude ()
|
17 | 18 | import Prelude.Compat
|
18 | 19 |
|
| 20 | +#if MIN_VERSION_servant(0,18,1) |
| 21 | +import Control.Applicative ((<|>)) |
| 22 | +#endif |
19 | 23 | import Control.Lens
|
20 | 24 | import Data.Aeson
|
21 | 25 | import Data.Foldable (toList)
|
@@ -183,6 +187,57 @@ instance OpenApiMethod 'OPTIONS where openApiMethod _ = options
|
183 | 187 | instance OpenApiMethod 'HEAD where openApiMethod _ = head_
|
184 | 188 | instance OpenApiMethod 'PATCH where openApiMethod _ = patch
|
185 | 189 |
|
| 190 | +#if MIN_VERSION_servant(0,18,1) |
| 191 | +instance HasOpenApi (UVerb method cs '[]) where |
| 192 | + toOpenApi _ = mempty |
| 193 | + |
| 194 | +-- | @since <2.0.1.0> |
| 195 | +instance |
| 196 | + {-# OVERLAPPABLE #-} |
| 197 | + ( ToSchema a, |
| 198 | + HasStatus a, |
| 199 | + AllAccept cs, |
| 200 | + OpenApiMethod method, |
| 201 | + HasOpenApi (UVerb method cs as) |
| 202 | + ) => |
| 203 | + HasOpenApi (UVerb method cs (a ': as)) |
| 204 | + where |
| 205 | + toOpenApi _ = |
| 206 | + toOpenApi (Proxy :: Proxy (Verb method (StatusOf a) cs a)) |
| 207 | + `combineSwagger` toOpenApi (Proxy :: Proxy (UVerb method cs as)) |
| 208 | + where |
| 209 | + -- workaround for https://github.com/GetShopTV/swagger2/issues/218 |
| 210 | + combinePathItem :: PathItem -> PathItem -> PathItem |
| 211 | + combinePathItem s t = PathItem |
| 212 | + { _pathItemGet = _pathItemGet s <> _pathItemGet t |
| 213 | + , _pathItemPut = _pathItemPut s <> _pathItemPut t |
| 214 | + , _pathItemPost = _pathItemPost s <> _pathItemPost t |
| 215 | + , _pathItemDelete = _pathItemDelete s <> _pathItemDelete t |
| 216 | + , _pathItemOptions = _pathItemOptions s <> _pathItemOptions t |
| 217 | + , _pathItemHead = _pathItemHead s <> _pathItemHead t |
| 218 | + , _pathItemPatch = _pathItemPatch s <> _pathItemPatch t |
| 219 | + , _pathItemTrace = _pathItemTrace s <> _pathItemTrace t |
| 220 | + , _pathItemParameters = _pathItemParameters s <> _pathItemParameters t |
| 221 | + , _pathItemSummary = _pathItemSummary s <|> _pathItemSummary t |
| 222 | + , _pathItemDescription = _pathItemDescription s <|> _pathItemDescription t |
| 223 | + , _pathItemServers = _pathItemServers s <> _pathItemServers t |
| 224 | + } |
| 225 | + |
| 226 | + combineSwagger :: OpenApi -> OpenApi -> OpenApi |
| 227 | + combineSwagger s t = OpenApi |
| 228 | + { _openApiInfo = _openApiInfo s <> _openApiInfo t |
| 229 | + , _openApiServers = _openApiServers s <> _openApiServers t |
| 230 | + , _openApiPaths = InsOrdHashMap.unionWith combinePathItem (_openApiPaths s) (_openApiPaths t) |
| 231 | + , _openApiComponents = _openApiComponents s <> _openApiComponents t |
| 232 | + , _openApiSecurity = _openApiSecurity s <> _openApiSecurity t |
| 233 | + , _openApiTags = _openApiTags s <> _openApiTags t |
| 234 | + , _openApiExternalDocs = _openApiExternalDocs s <|> _openApiExternalDocs t |
| 235 | + } |
| 236 | + |
| 237 | +instance ToSchema a => ToSchema (WithStatus s a) where |
| 238 | + declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a) |
| 239 | +#endif |
| 240 | + |
186 | 241 | instance {-# OVERLAPPABLE #-} (ToSchema a, AllAccept cs, KnownNat status, OpenApiMethod method) => HasOpenApi (Verb method status cs a) where
|
187 | 242 | toOpenApi _ = toOpenApi (Proxy :: Proxy (Verb method status cs (Headers '[] a)))
|
188 | 243 |
|
|
0 commit comments