@@ -30,6 +30,9 @@ import qualified Data.Text as Text
30
30
import GHC.TypeLits
31
31
import Network.HTTP.Media (MediaType )
32
32
import Servant.API
33
+ import Servant.API.Description (FoldDescription , reflectDescription )
34
+ import Servant.API.Modifiers (FoldRequired )
35
+ import Data.Singletons.Bool
33
36
34
37
import Servant.Swagger.Internal.TypeLevel.API
35
38
@@ -71,10 +74,8 @@ class HasSwagger api where
71
74
instance HasSwagger Raw where
72
75
toSwagger _ = mempty & paths . at " /" ?~ mempty
73
76
74
- #if MIN_VERSION_servant(0,11,0)
75
77
instance HasSwagger EmptyAPI where
76
78
toSwagger _ = mempty
77
- #endif
78
79
79
80
-- | All operations of sub API.
80
81
-- This is similar to @'operationsOf'@ but ensures that operations
@@ -214,49 +215,53 @@ instance (KnownSymbol sym, HasSwagger sub) => HasSwagger (sym :> sub) where
214
215
where
215
216
piece = symbolVal (Proxy :: Proxy sym )
216
217
217
- instance (KnownSymbol sym , ToParamSchema a , HasSwagger sub ) => HasSwagger (Capture sym a :> sub ) where
218
+ instance (KnownSymbol sym , ToParamSchema a , HasSwagger sub , KnownSymbol ( FoldDescription mods )) => HasSwagger (Capture' mods sym a :> sub ) where
218
219
toSwagger _ = toSwagger (Proxy :: Proxy sub )
219
220
& addParam param
220
221
& prependPath capture
221
222
& addDefaultResponse404 tname
222
223
where
223
224
pname = symbolVal (Proxy :: Proxy sym )
224
225
tname = Text. pack pname
226
+ transDesc " " = Nothing
227
+ transDesc desc = Just (Text. pack desc)
225
228
capture = " {" <> pname <> " }"
226
229
param = mempty
227
230
& name .~ tname
231
+ & description .~ transDesc (reflectDescription (Proxy :: Proxy mods ))
228
232
& required ?~ True
229
233
& schema .~ ParamOther (mempty
230
234
& in_ .~ ParamPath
231
235
& paramSchema .~ toParamSchema (Proxy :: Proxy a ))
232
236
233
- #if MIN_VERSION_servant(0,8,1)
234
237
-- | Swagger Spec doesn't have a notion of CaptureAll, this instance is the best effort.
235
238
instance (KnownSymbol sym , ToParamSchema a , HasSwagger sub ) => HasSwagger (CaptureAll sym a :> sub ) where
236
239
toSwagger _ = toSwagger (Proxy :: Proxy (Capture sym a :> sub ))
237
- #endif
238
240
239
- #if MIN_VERSION_servant(0,12,0)
240
241
instance (KnownSymbol desc , HasSwagger api ) => HasSwagger (Description desc :> api ) where
241
242
toSwagger _ = toSwagger (Proxy :: Proxy api )
242
243
& allOperations. description %~ (Just (Text. pack (symbolVal (Proxy :: Proxy desc ))) <> )
243
244
244
245
instance (KnownSymbol desc , HasSwagger api ) => HasSwagger (Summary desc :> api ) where
245
246
toSwagger _ = toSwagger (Proxy :: Proxy api )
246
247
& allOperations. summary %~ (Just (Text. pack (symbolVal (Proxy :: Proxy desc ))) <> )
247
- #endif
248
248
249
- instance (KnownSymbol sym , ToParamSchema a , HasSwagger sub ) => HasSwagger (QueryParam sym a :> sub ) where
249
+ instance (KnownSymbol sym , ToParamSchema a , HasSwagger sub , SBoolI ( FoldRequired mods ), KnownSymbol ( FoldDescription mods )) => HasSwagger (QueryParam' mods sym a :> sub ) where
250
250
toSwagger _ = toSwagger (Proxy :: Proxy sub )
251
251
& addParam param
252
252
& addDefaultResponse400 tname
253
253
where
254
254
tname = Text. pack (symbolVal (Proxy :: Proxy sym ))
255
+ transDesc " " = Nothing
256
+ transDesc desc = Just (Text. pack desc)
255
257
param = mempty
256
258
& name .~ tname
257
- & schema .~ ParamOther (mempty
258
- & in_ .~ ParamQuery
259
- & paramSchema .~ toParamSchema (Proxy :: Proxy a ))
259
+ & description .~ transDesc (reflectDescription (Proxy :: Proxy mods ))
260
+ & required ?~ reflectBool (Proxy :: Proxy (FoldRequired mods ))
261
+ & schema .~ ParamOther sch
262
+ sch = mempty
263
+ & in_ .~ ParamQuery
264
+ & paramSchema .~ toParamSchema (Proxy :: Proxy a )
260
265
261
266
instance (KnownSymbol sym , ToParamSchema a , HasSwagger sub ) => HasSwagger (QueryParams sym a :> sub ) where
262
267
toSwagger _ = toSwagger (Proxy :: Proxy sub )
@@ -266,11 +271,13 @@ instance (KnownSymbol sym, ToParamSchema a, HasSwagger sub) => HasSwagger (Query
266
271
tname = Text. pack (symbolVal (Proxy :: Proxy sym ))
267
272
param = mempty
268
273
& name .~ tname
269
- & schema .~ ParamOther (mempty
270
- & in_ .~ ParamQuery
271
- & paramSchema .~ (mempty
272
- & type_ .~ SwaggerArray
273
- & items ?~ SwaggerItemsPrimitive (Just CollectionMulti ) (toParamSchema (Proxy :: Proxy a ))))
274
+ & schema .~ ParamOther sch
275
+ sch = mempty
276
+ & in_ .~ ParamQuery
277
+ & paramSchema .~ pschema
278
+ pschema = mempty
279
+ & type_ .~ SwaggerArray
280
+ & items ?~ SwaggerItemsPrimitive (Just CollectionMulti ) (toParamSchema (Proxy :: Proxy a ))
274
281
275
282
instance (KnownSymbol sym , HasSwagger sub ) => HasSwagger (QueryFlag sym :> sub ) where
276
283
toSwagger _ = toSwagger (Proxy :: Proxy sub )
@@ -286,29 +293,36 @@ instance (KnownSymbol sym, HasSwagger sub) => HasSwagger (QueryFlag sym :> sub)
286
293
& paramSchema .~ (toParamSchema (Proxy :: Proxy Bool )
287
294
& default_ ?~ toJSON False ))
288
295
289
- instance (KnownSymbol sym , ToParamSchema a , HasSwagger sub ) => HasSwagger (Header sym a :> sub ) where
296
+ instance (KnownSymbol sym , ToParamSchema a , HasSwagger sub , SBoolI ( FoldRequired mods ), KnownSymbol ( FoldDescription mods )) => HasSwagger (Header' mods sym a :> sub ) where
290
297
toSwagger _ = toSwagger (Proxy :: Proxy sub )
291
298
& addParam param
292
299
& addDefaultResponse400 tname
293
300
where
294
301
tname = Text. pack (symbolVal (Proxy :: Proxy sym ))
302
+ transDesc " " = Nothing
303
+ transDesc desc = Just (Text. pack desc)
295
304
param = mempty
296
305
& name .~ tname
306
+ & description .~ transDesc (reflectDescription (Proxy :: Proxy mods ))
307
+ & required ?~ reflectBool (Proxy :: Proxy (FoldRequired mods ))
297
308
& schema .~ ParamOther (mempty
298
309
& in_ .~ ParamHeader
299
310
& paramSchema .~ toParamSchema (Proxy :: Proxy a ))
300
311
301
- instance (ToSchema a , AllAccept cs , HasSwagger sub ) => HasSwagger (ReqBody cs a :> sub ) where
312
+ instance (ToSchema a , AllAccept cs , HasSwagger sub , KnownSymbol ( FoldDescription mods )) => HasSwagger (ReqBody' mods cs a :> sub ) where
302
313
toSwagger _ = toSwagger (Proxy :: Proxy sub )
303
314
& addParam param
304
315
& addConsumes (allContentType (Proxy :: Proxy cs ))
305
316
& addDefaultResponse400 tname
306
317
& definitions %~ (<> defs)
307
318
where
308
319
tname = " body"
320
+ transDesc " " = Nothing
321
+ transDesc desc = Just (Text. pack desc)
309
322
(defs, ref) = runDeclare (declareSchemaRef (Proxy :: Proxy a )) mempty
310
323
param = mempty
311
324
& name .~ tname
325
+ & description .~ transDesc (reflectDescription (Proxy :: Proxy mods ))
312
326
& required ?~ True
313
327
& schema .~ ParamBody ref
314
328
0 commit comments