git.iko.soy/typeable/openapi3
on 365b5ee help sign in
1310 lines 50 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DeriveDataTypeable #-}
4{-# LANGUAGE DeriveFunctor #-}
5{-# LANGUAGE DeriveGeneric #-}
6{-# LANGUAGE DeriveTraversable #-}
7{-# LANGUAGE FlexibleContexts #-}
8{-# LANGUAGE FlexibleInstances #-}
9{-# LANGUAGE GADTs #-}
10{-# LANGUAGE GeneralizedNewtypeDeriving #-}
11{-# LANGUAGE OverloadedStrings #-}
12{-# LANGUAGE RecordWildCards #-}
13{-# LANGUAGE ScopedTypeVariables #-}
14{-# LANGUAGE StandaloneDeriving #-}
15{-# LANGUAGE TemplateHaskell #-}
16{-# LANGUAGE TypeFamilies #-}
17{-# LANGUAGE UndecidableInstances #-}
18#if __GLASGOW_HASKELL__ <710
19{-# LANGUAGE PolyKinds #-}
20#endif
21#include "overlapping-compat.h"
22module Data.Swagger.Internal where
24import Prelude ()
25import Prelude.Compat
27import Control.Lens ((&), (.~), (?~))
28import Control.Applicative
29import Data.Aeson
30import qualified Data.Aeson.Types as JSON
31import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
32import qualified Data.HashMap.Strict as HashMap
33import Data.Map (Map)
34import qualified Data.Map as Map
35import Data.Monoid
36import Data.Scientific (Scientific)
37import Data.Set (Set)
38import Data.String (IsString(..))
39import Data.Text (Text)
40import qualified Data.Text as Text
41import GHC.Generics (Generic)
42import Network (HostName, PortNumber)
43import Network.HTTP.Media (MediaType)
44import Text.Read (readMaybe)
46import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
47import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
49import Generics.SOP.TH (deriveGeneric)
50import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
51 ,sopSwaggerGenericToJSONWithOpts
52 ,sopSwaggerGenericParseJSON
53 ,HasSwaggerAesonOptions(..)
54 ,AesonDefaultValue(..)
55 ,mkSwaggerAesonOptions
56 ,saoAdditionalPairs
57 ,saoSubObject)
58import Data.Swagger.Internal.Utils
60#if MIN_VERSION_aeson(0,10,0)
61import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
62#define DEFINE_TOENCODING toEncoding = sopSwaggerGenericToEncoding
63#else
64#define DEFINE_TOENCODING
65#endif
67-- | A list of definitions that can be used in references.
68type Definitions = InsOrdHashMap Text
70-- | This is the root document object for the API specification.
71data Swagger = Swagger
72 { -- | Provides metadata about the API.
73 -- The metadata can be used by the clients if needed.
74 _swaggerInfo :: Info
76 -- | The host (name or ip) serving the API. It MAY include a port.
77 -- If the host is not included, the host serving the documentation is to be used (including the port).
78 , _swaggerHost :: Maybe Host
80 -- | The base path on which the API is served, which is relative to the host.
81 -- If it is not included, the API is served directly under the host.
82 -- The value MUST start with a leading slash (/).
83 , _swaggerBasePath :: Maybe FilePath
85 -- | The transfer protocol of the API.
86 -- If the schemes is not included, the default scheme to be used is the one used to access the Swagger definition itself.
87 , _swaggerSchemes :: Maybe [Scheme]
89 -- | A list of MIME types the APIs can consume.
90 -- This is global to all APIs but can be overridden on specific API calls.
91 , _swaggerConsumes :: MimeList
93 -- | A list of MIME types the APIs can produce.
94 -- This is global to all APIs but can be overridden on specific API calls.
95 , _swaggerProduces :: MimeList
97 -- | The available paths and operations for the API.
98 -- Holds the relative paths to the individual endpoints.
99 -- The path is appended to the @'basePath'@ in order to construct the full URL.
100 , _swaggerPaths :: InsOrdHashMap FilePath PathItem
102 -- | An object to hold data types produced and consumed by operations.
103 , _swaggerDefinitions :: Definitions Schema
105 -- | An object to hold parameters that can be used across operations.
106 -- This property does not define global parameters for all operations.
107 , _swaggerParameters :: Definitions Param
109 -- | An object to hold responses that can be used across operations.
110 -- This property does not define global responses for all operations.
111 , _swaggerResponses :: Definitions Response
113 -- | Security scheme definitions that can be used across the specification.
114 , _swaggerSecurityDefinitions :: Definitions SecurityScheme
116 -- | A declaration of which security schemes are applied for the API as a whole.
117 -- The list of values describes alternative security schemes that can be used
118 -- (that is, there is a logical OR between the security requirements).
119 -- Individual operations can override this definition.
120 , _swaggerSecurity :: [SecurityRequirement]
122 -- | A list of tags used by the specification with additional metadata.
123 -- The order of the tags can be used to reflect on their order by the parsing tools.
124 -- Not all tags that are used by the Operation Object must be declared.
125 -- The tags that are not declared may be organized randomly or based on the tools' logic.
126 -- Each tag name in the list MUST be unique.
127 , _swaggerTags :: Set Tag
129 -- | Additional external documentation.
130 , _swaggerExternalDocs :: Maybe ExternalDocs
131 } deriving (Eq, Show, Generic, Data, Typeable)
133-- | The object provides metadata about the API.
134-- The metadata can be used by the clients if needed,
135-- and can be presented in the Swagger-UI for convenience.
136data Info = Info
137 { -- | The title of the application.
138 _infoTitle :: Text
140 -- | A short description of the application.
141 -- GFM syntax can be used for rich text representation.
142 , _infoDescription :: Maybe Text
144 -- | The Terms of Service for the API.
145 , _infoTermsOfService :: Maybe Text
147 -- | The contact information for the exposed API.
148 , _infoContact :: Maybe Contact
150 -- | The license information for the exposed API.
151 , _infoLicense :: Maybe License
153 -- | Provides the version of the application API
154 -- (not to be confused with the specification version).
155 , _infoVersion :: Text
156 } deriving (Eq, Show, Generic, Data, Typeable)
158-- | Contact information for the exposed API.
159data Contact = Contact
160 { -- | The identifying name of the contact person/organization.
161 _contactName :: Maybe Text
163 -- | The URL pointing to the contact information.
164 , _contactUrl :: Maybe URL
166 -- | The email address of the contact person/organization.
167 , _contactEmail :: Maybe Text
168 } deriving (Eq, Show, Generic, Data, Typeable)
170-- | License information for the exposed API.
171data License = License
172 { -- | The license name used for the API.
173 _licenseName :: Text
175 -- | A URL to the license used for the API.
176 , _licenseUrl :: Maybe URL
177 } deriving (Eq, Show, Generic, Data, Typeable)
179instance IsString License where
180 fromString s = License (fromString s) Nothing
182-- | The host (name or ip) serving the API. It MAY include a port.
183data Host = Host
184 { _hostName :: HostName -- ^ Host name.
185 , _hostPort :: Maybe PortNumber -- ^ Optional port.
186 } deriving (Eq, Show, Generic, Typeable)
188instance IsString Host where
189 fromString s = Host s Nothing
191hostConstr :: Constr
192hostConstr = mkConstr hostDataType "Host" [] Prefix
194hostDataType :: DataType
195hostDataType = mkDataType "Data.Swagger.Host" [hostConstr]
197instance Data Host where
198 gunfold k z c = case constrIndex c of
199 1 -> k (k (z (\name mport -> Host name (fromInteger <$> mport))))
200 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type Host."
201 toConstr (Host _ _) = hostConstr
202 dataTypeOf _ = hostDataType
204-- | The transfer protocol of the API.
205data Scheme
206 = Http
207 | Https
208 | Ws
209 | Wss
210 deriving (Eq, Show, Generic, Data, Typeable)
212-- | Describes the operations available on a single path.
213-- A @'PathItem'@ may be empty, due to ACL constraints.
214-- The path itself is still exposed to the documentation viewer
215-- but they will not know which operations and parameters are available.
216data PathItem = PathItem
217 { -- | A definition of a GET operation on this path.
218 _pathItemGet :: Maybe Operation
220 -- | A definition of a PUT operation on this path.
221 , _pathItemPut :: Maybe Operation
223 -- | A definition of a POST operation on this path.
224 , _pathItemPost :: Maybe Operation
226 -- | A definition of a DELETE operation on this path.
227 , _pathItemDelete :: Maybe Operation
229 -- | A definition of a OPTIONS operation on this path.
230 , _pathItemOptions :: Maybe Operation
232 -- | A definition of a HEAD operation on this path.
233 , _pathItemHead :: Maybe Operation
235 -- | A definition of a PATCH operation on this path.
236 , _pathItemPatch :: Maybe Operation
238 -- | A list of parameters that are applicable for all the operations described under this path.
239 -- These parameters can be overridden at the operation level, but cannot be removed there.
240 -- The list MUST NOT include duplicated parameters.
241 -- A unique parameter is defined by a combination of a name and location.
242 , _pathItemParameters :: [Referenced Param]
243 } deriving (Eq, Show, Generic, Data, Typeable)
245-- | Describes a single API operation on a path.
246data Operation = Operation
247 { -- | A list of tags for API documentation control.
248 -- Tags can be used for logical grouping of operations by resources or any other qualifier.
249 _operationTags :: Set TagName
251 -- | A short summary of what the operation does.
252 -- For maximum readability in the swagger-ui, this field SHOULD be less than 120 characters.
253 , _operationSummary :: Maybe Text
255 -- | A verbose explanation of the operation behavior.
256 -- GFM syntax can be used for rich text representation.
257 , _operationDescription :: Maybe Text
259 -- | Additional external documentation for this operation.
260 , _operationExternalDocs :: Maybe ExternalDocs
262 -- | Unique string used to identify the operation.
263 -- The id MUST be unique among all operations described in the API.
264 -- Tools and libraries MAY use the it to uniquely identify an operation,
265 -- therefore, it is recommended to follow common programming naming conventions.
266 , _operationOperationId :: Maybe Text
268 -- | A list of MIME types the operation can consume.
269 -- This overrides the @'consumes'@.
270 -- @Just []@ MAY be used to clear the global definition.
271 , _operationConsumes :: Maybe MimeList
273 -- | A list of MIME types the operation can produce.
274 -- This overrides the @'produces'@.
275 -- @Just []@ MAY be used to clear the global definition.
276 , _operationProduces :: Maybe MimeList
278 -- | A list of parameters that are applicable for this operation.
279 -- If a parameter is already defined at the @'PathItem'@,
280 -- the new definition will override it, but can never remove it.
281 -- The list MUST NOT include duplicated parameters.
282 -- A unique parameter is defined by a combination of a name and location.
283 , _operationParameters :: [Referenced Param]
285 -- | The list of possible responses as they are returned from executing this operation.
286 , _operationResponses :: Responses
288 -- | The transfer protocol for the operation.
289 -- The value overrides @'schemes'@.
290 , _operationSchemes :: Maybe [Scheme]
292 -- | Declares this operation to be deprecated.
293 -- Usage of the declared operation should be refrained.
294 -- Default value is @False@.
295 , _operationDeprecated :: Maybe Bool
297 -- | A declaration of which security schemes are applied for this operation.
298 -- The list of values describes alternative security schemes that can be used
299 -- (that is, there is a logical OR between the security requirements).
300 -- This definition overrides any declared top-level security.
301 -- To remove a top-level security declaration, @Just []@ can be used.
302 , _operationSecurity :: [SecurityRequirement]
303 } deriving (Eq, Show, Generic, Data, Typeable)
305newtype MimeList = MimeList { getMimeList :: [MediaType] }
306 deriving (Eq, Show, Monoid, Typeable)
308mimeListConstr :: Constr
309mimeListConstr = mkConstr mimeListDataType "MimeList" ["getMimeList"] Prefix
311mimeListDataType :: DataType
312mimeListDataType = mkDataType "Data.Swagger.MimeList" [mimeListConstr]
314instance Data MimeList where
315 gunfold k z c = case constrIndex c of
316 1 -> k (z (\xs -> MimeList (map fromString xs)))
317 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type MimeList."
318 toConstr (MimeList _) = mimeListConstr
319 dataTypeOf _ = mimeListDataType
321-- | Describes a single operation parameter.
322-- A unique parameter is defined by a combination of a name and location.
323data Param = Param
324 { -- | The name of the parameter.
325 -- Parameter names are case sensitive.
326 _paramName :: Text
328 -- | A brief description of the parameter.
329 -- This could contain examples of use.
330 -- GFM syntax can be used for rich text representation.
331 , _paramDescription :: Maybe Text
333 -- | Determines whether this parameter is mandatory.
334 -- If the parameter is in "path", this property is required and its value MUST be true.
335 -- Otherwise, the property MAY be included and its default value is @False@.
336 , _paramRequired :: Maybe Bool
338 -- | Parameter schema.
339 , _paramSchema :: ParamAnySchema
340 } deriving (Eq, Show, Generic, Data, Typeable)
342data ParamAnySchema
343 = ParamBody (Referenced Schema)
344 | ParamOther ParamOtherSchema
345 deriving (Eq, Show, Generic, Data, Typeable)
347data ParamOtherSchema = ParamOtherSchema
348 { -- | The location of the parameter.
349 _paramOtherSchemaIn :: ParamLocation
351 -- | Sets the ability to pass empty-valued parameters.
352 -- This is valid only for either @'ParamQuery'@ or @'ParamFormData'@
353 -- and allows you to send a parameter with a name only or an empty value.
354 -- Default value is @False@.
355 , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
357 , _paramOtherSchemaParamSchema :: ParamSchema 'SwaggerKindParamOtherSchema
358 } deriving (Eq, Show, Generic, Typeable, Data)
360-- | Items for @'SwaggerArray'@ schemas.
361--
362-- @'SwaggerItemsPrimitive'@ should be used only for query params, headers and path pieces.
363-- The @'CollectionFormat' t@ parameter specifies how elements of an array should be displayed.
364-- Note that @fmt@ in @'SwaggerItemsPrimitive' fmt schema@ specifies format for elements of type @schema@.
365-- This is different from the original Swagger's <http://swagger.io/specification/#itemsObject Items Object>.
366--
367-- @'SwaggerItemsObject'@ should be used to specify homogenous array @'Schema'@s.
368--
369-- @'SwaggerItemsArray'@ should be used to specify tuple @'Schema'@s.
370data SwaggerItems t where
371 SwaggerItemsPrimitive :: Maybe (CollectionFormat k) -> ParamSchema k-> SwaggerItems k
372 SwaggerItemsObject :: Referenced Schema -> SwaggerItems 'SwaggerKindSchema
373 SwaggerItemsArray :: [Referenced Schema] -> SwaggerItems 'SwaggerKindSchema
374 deriving (Typeable)
376deriving instance Eq (SwaggerItems t)
377deriving instance Show (SwaggerItems t)
378--deriving instance Typeable (SwaggerItems t)
380swaggerItemsPrimitiveConstr :: Constr
381swaggerItemsPrimitiveConstr = mkConstr swaggerItemsDataType "SwaggerItemsPrimitive" [] Prefix
383swaggerItemsObjectConstr :: Constr
384swaggerItemsObjectConstr = mkConstr swaggerItemsDataType "SwaggerItemsObject" [] Prefix
386swaggerItemsArrayConstr :: Constr
387swaggerItemsArrayConstr = mkConstr swaggerItemsDataType "SwaggerItemsArray" [] Prefix
389swaggerItemsDataType :: DataType
390swaggerItemsDataType = mkDataType "Data.Swagger.SwaggerItems" [swaggerItemsPrimitiveConstr]
392-- Note: unfortunately we have to write these Data instances by hand,
393-- to get better contexts / avoid duplicate name when using standalone deriving
395instance Data t => Data (SwaggerItems ('SwaggerKindNormal t)) where
396 -- TODO: define gfoldl
397 gunfold k z c = case constrIndex c of
398 1 -> k (k (z SwaggerItemsPrimitive))
399 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems t)."
400 toConstr _ = swaggerItemsPrimitiveConstr
401 dataTypeOf _ = swaggerItemsDataType
403-- SwaggerItems SwaggerKindParamOtherSchema can be constructed using SwaggerItemsPrimitive only
404instance Data (SwaggerItems 'SwaggerKindParamOtherSchema) where
405 -- TODO: define gfoldl
406 gunfold k z c = case constrIndex c of
407 1 -> k (k (z SwaggerItemsPrimitive))
408 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindParamOtherSchema)."
409 toConstr _ = swaggerItemsPrimitiveConstr
410 dataTypeOf _ = swaggerItemsDataType
412instance Data (SwaggerItems 'SwaggerKindSchema) where
413 gfoldl _ _ (SwaggerItemsPrimitive _ _) = error $ " Data.Data.gfoldl: Constructor SwaggerItemsPrimitive used to construct SwaggerItems SwaggerKindSchema"
414 gfoldl k z (SwaggerItemsObject ref) = z SwaggerItemsObject `k` ref
415 gfoldl k z (SwaggerItemsArray ref) = z SwaggerItemsArray `k` ref
417 gunfold k z c = case constrIndex c of
418 2 -> k (z SwaggerItemsObject)
419 3 -> k (z SwaggerItemsArray)
420 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type (SwaggerItems SwaggerKindSchema)."
422 toConstr (SwaggerItemsPrimitive _ _) = error "Not supported"
423 toConstr (SwaggerItemsObject _) = swaggerItemsObjectConstr
424 toConstr (SwaggerItemsArray _) = swaggerItemsArrayConstr
426 dataTypeOf _ = swaggerItemsDataType
428-- | Type used as a kind to avoid overlapping instances.
429data SwaggerKind t
430 = SwaggerKindNormal t
431 | SwaggerKindParamOtherSchema
432 | SwaggerKindSchema
433 deriving (Typeable)
435deriving instance Typeable 'SwaggerKindNormal
436deriving instance Typeable 'SwaggerKindParamOtherSchema
437deriving instance Typeable 'SwaggerKindSchema
439type family SwaggerKindType (k :: SwaggerKind *) :: *
440type instance SwaggerKindType ('SwaggerKindNormal t) = t
441type instance SwaggerKindType 'SwaggerKindSchema = Schema
442type instance SwaggerKindType 'SwaggerKindParamOtherSchema = ParamOtherSchema
444data SwaggerType t where
445 SwaggerString :: SwaggerType t
446 SwaggerNumber :: SwaggerType t
447 SwaggerInteger :: SwaggerType t
448 SwaggerBoolean :: SwaggerType t
449 SwaggerArray :: SwaggerType t
450 SwaggerFile :: SwaggerType 'SwaggerKindParamOtherSchema
451 SwaggerNull :: SwaggerType 'SwaggerKindSchema
452 SwaggerObject :: SwaggerType 'SwaggerKindSchema
453 deriving (Typeable)
455deriving instance Eq (SwaggerType t)
456deriving instance Show (SwaggerType t)
458swaggerTypeConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
459swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
461swaggerTypeDataType :: {- Data (SwaggerType t) => -} SwaggerType t -> DataType
462swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
464swaggerCommonTypes :: [SwaggerType k]
465swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
467swaggerParamTypes :: [SwaggerType 'SwaggerKindParamOtherSchema]
468swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
470swaggerSchemaTypes :: [SwaggerType 'SwaggerKindSchema]
471swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
473swaggerTypeConstrs :: [Constr]
474swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType 'SwaggerKindSchema])
475 ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
477instance Typeable t => Data (SwaggerType ('SwaggerKindNormal t)) where
478 gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
479 toConstr = swaggerTypeConstr
480 dataTypeOf = swaggerTypeDataType
482instance Data (SwaggerType 'SwaggerKindParamOtherSchema) where
483 gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
484 toConstr = swaggerTypeConstr
485 dataTypeOf = swaggerTypeDataType
487instance Data (SwaggerType 'SwaggerKindSchema) where
488 gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
489 toConstr = swaggerTypeConstr
490 dataTypeOf = swaggerTypeDataType
492data ParamLocation
493 = -- | Parameters that are appended to the URL.
494 -- For example, in @/items?id=###@, the query parameter is @id@.
495 ParamQuery
496 -- | Custom headers that are expected as part of the request.
497 | ParamHeader
498 -- | Used together with Path Templating, where the parameter value is actually part of the operation's URL.
499 -- This does not include the host or base path of the API.
500 -- For example, in @/items/{itemId}@, the path parameter is @itemId@.
501 | ParamPath
502 -- | Used to describe the payload of an HTTP request when either @application/x-www-form-urlencoded@
503 -- or @multipart/form-data@ are used as the content type of the request
504 -- (in Swagger's definition, the @consumes@ property of an operation).
505 -- This is the only parameter type that can be used to send files, thus supporting the @'ParamFile'@ type.
506 -- Since form parameters are sent in the payload, they cannot be declared together with a body parameter for the same operation.
507 -- Form parameters have a different format based on the content-type used
508 -- (for further details, consult <http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4>).
509 | ParamFormData
510 deriving (Eq, Show, Generic, Data, Typeable)
512type Format = Text
514-- | Determines the format of the array.
515data CollectionFormat t where
516 -- Comma separated values: @foo,bar@.
517 CollectionCSV :: CollectionFormat t
518 -- Space separated values: @foo bar@.
519 CollectionSSV :: CollectionFormat t
520 -- Tab separated values: @foo\\tbar@.
521 CollectionTSV :: CollectionFormat t
522 -- Pipe separated values: @foo|bar@.
523 CollectionPipes :: CollectionFormat t
524 -- Corresponds to multiple parameter instances
525 -- instead of multiple values for a single instance @foo=bar&foo=baz@.
526 -- This is valid only for parameters in @'ParamQuery'@ or @'ParamFormData'@.
527 CollectionMulti :: CollectionFormat 'SwaggerKindParamOtherSchema
528 deriving (Typeable)
530deriving instance Eq (CollectionFormat t)
531deriving instance Show (CollectionFormat t)
533collectionFormatConstr :: CollectionFormat t -> Constr
534collectionFormatConstr cf = mkConstr collectionFormatDataType (show cf) [] Prefix
536collectionFormatDataType :: DataType
537collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
538 map collectionFormatConstr collectionCommonFormats
540collectionCommonFormats :: [CollectionFormat t]
541collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
543instance Data t => Data (CollectionFormat ('SwaggerKindNormal t)) where
544 gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
545 toConstr = collectionFormatConstr
546 dataTypeOf _ = collectionFormatDataType
548deriving instance Data (CollectionFormat 'SwaggerKindParamOtherSchema)
550type ParamName = Text
552data Schema = Schema
553 { _schemaTitle :: Maybe Text
554 , _schemaDescription :: Maybe Text
555 , _schemaRequired :: [ParamName]
557 , _schemaAllOf :: Maybe [Schema]
558 , _schemaProperties :: InsOrdHashMap Text (Referenced Schema)
559 , _schemaAdditionalProperties :: Maybe (Referenced Schema)
561 , _schemaDiscriminator :: Maybe Text
562 , _schemaReadOnly :: Maybe Bool
563 , _schemaXml :: Maybe Xml
564 , _schemaExternalDocs :: Maybe ExternalDocs
565 , _schemaExample :: Maybe Value
567 , _schemaMaxProperties :: Maybe Integer
568 , _schemaMinProperties :: Maybe Integer
570 , _schemaParamSchema :: ParamSchema 'SwaggerKindSchema
571 } deriving (Eq, Show, Generic, Data, Typeable)
573-- | A @'Schema'@ with an optional name.
574-- This name can be used in references.
575data NamedSchema = NamedSchema
576 { _namedSchemaName :: Maybe Text
577 , _namedSchemaSchema :: Schema
578 } deriving (Eq, Show, Generic, Data, Typeable)
580-- | Regex pattern for @string@ type.
581type Pattern = Text
583data ParamSchema (t :: SwaggerKind *) = ParamSchema
584 { -- | Declares the value of the parameter that the server will use if none is provided,
585 -- for example a @"count"@ to control the number of results per page might default to @100@
586 -- if not supplied by the client in the request.
587 -- (Note: "default" has no meaning for required parameters.)
588 -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
589 _paramSchemaDefault :: Maybe Value
591 , _paramSchemaType :: SwaggerType t
592 , _paramSchemaFormat :: Maybe Format
593 , _paramSchemaItems :: Maybe (SwaggerItems t)
594 , _paramSchemaMaximum :: Maybe Scientific
595 , _paramSchemaExclusiveMaximum :: Maybe Bool
596 , _paramSchemaMinimum :: Maybe Scientific
597 , _paramSchemaExclusiveMinimum :: Maybe Bool
598 , _paramSchemaMaxLength :: Maybe Integer
599 , _paramSchemaMinLength :: Maybe Integer
600 , _paramSchemaPattern :: Maybe Pattern
601 , _paramSchemaMaxItems :: Maybe Integer
602 , _paramSchemaMinItems :: Maybe Integer
603 , _paramSchemaUniqueItems :: Maybe Bool
604 , _paramSchemaEnum :: Maybe [Value]
605 , _paramSchemaMultipleOf :: Maybe Scientific
606 } deriving (Eq, Show, Generic, Typeable)
608deriving instance (Typeable k, Data (SwaggerType k), Data (SwaggerItems k)) => Data (ParamSchema k)
610data Xml = Xml
611 { -- | Replaces the name of the element/attribute used for the described schema property.
612 -- When defined within the @'SwaggerItems'@ (items), it will affect the name of the individual XML elements within the list.
613 -- When defined alongside type being array (outside the items),
614 -- it will affect the wrapping element and only if wrapped is true.
615 -- If wrapped is false, it will be ignored.
616 _xmlName :: Maybe Text
618 -- | The URL of the namespace definition.
619 -- Value SHOULD be in the form of a URL.
620 , _xmlNamespace :: Maybe Text
622 -- | The prefix to be used for the name.
623 , _xmlPrefix :: Maybe Text
625 -- | Declares whether the property definition translates to an attribute instead of an element.
626 -- Default value is @False@.
627 , _xmlAttribute :: Maybe Bool
629 -- | MAY be used only for an array definition.
630 -- Signifies whether the array is wrapped
631 -- (for example, @\<books\>\<book/\>\<book/\>\</books\>@)
632 -- or unwrapped (@\<book/\>\<book/\>@).
633 -- Default value is @False@.
634 -- The definition takes effect only when defined alongside type being array (outside the items).
635 , _xmlWrapped :: Maybe Bool
636 } deriving (Eq, Show, Generic, Data, Typeable)
638-- | A container for the expected responses of an operation.
639-- The container maps a HTTP response code to the expected response.
640-- It is not expected from the documentation to necessarily cover all possible HTTP response codes,
641-- since they may not be known in advance.
642-- However, it is expected from the documentation to cover a successful operation response and any known errors.
643data Responses = Responses
644 { -- | The documentation of responses other than the ones declared for specific HTTP response codes.
645 -- It can be used to cover undeclared responses.
646 _responsesDefault :: Maybe (Referenced Response)
648 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
649 -- Describes the expected response for those HTTP status codes.
650 , _responsesResponses :: InsOrdHashMap HttpStatusCode (Referenced Response)
651 } deriving (Eq, Show, Generic, Data, Typeable)
653type HttpStatusCode = Int
655-- | Describes a single response from an API Operation.
656data Response = Response
657 { -- | A short description of the response.
658 -- GFM syntax can be used for rich text representation.
659 _responseDescription :: Text
661 -- | A definition of the response structure.
662 -- It can be a primitive, an array or an object.
663 -- If this field does not exist, it means no content is returned as part of the response.
664 -- As an extension to the Schema Object, its root type value may also be "file".
665 -- This SHOULD be accompanied by a relevant produces mime-type.
666 , _responseSchema :: Maybe (Referenced Schema)
668 -- | A list of headers that are sent with the response.
669 , _responseHeaders :: InsOrdHashMap HeaderName Header
671 -- | An example of the response message.
672 , _responseExamples :: Maybe Example
673 } deriving (Eq, Show, Generic, Data, Typeable)
675instance IsString Response where
676 fromString s = Response (fromString s) Nothing mempty Nothing
678type HeaderName = Text
680data Header = Header
681 { -- | A short description of the header.
682 _headerDescription :: Maybe Text
684 , _headerParamSchema :: ParamSchema ('SwaggerKindNormal Header)
685 } deriving (Eq, Show, Generic, Data, Typeable)
687data Example = Example { getExample :: Map MediaType Value }
688 deriving (Eq, Show, Generic, Typeable)
690exampleConstr :: Constr
691exampleConstr = mkConstr exampleDataType "Example" ["getExample"] Prefix
693exampleDataType :: DataType
694exampleDataType = mkDataType "Data.Swagger.Example" [exampleConstr]
696instance Data Example where
697 gunfold k z c = case constrIndex c of
698 1 -> k (z (\m -> Example (Map.mapKeys fromString m)))
699 _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type Example."
700 toConstr (Example _) = exampleConstr
701 dataTypeOf _ = exampleDataType
703-- | The location of the API key.
704data ApiKeyLocation
705 = ApiKeyQuery
706 | ApiKeyHeader
707 deriving (Eq, Show, Generic, Data, Typeable)
709data ApiKeyParams = ApiKeyParams
710 { -- | The name of the header or query parameter to be used.
711 _apiKeyName :: Text
713 -- | The location of the API key.
714 , _apiKeyIn :: ApiKeyLocation
715 } deriving (Eq, Show, Generic, Data, Typeable)
717-- | The authorization URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
718type AuthorizationURL = Text
720-- | The token URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
721type TokenURL = Text
723data OAuth2Flow
724 = OAuth2Implicit AuthorizationURL
725 | OAuth2Password TokenURL
726 | OAuth2Application TokenURL
727 | OAuth2AccessCode AuthorizationURL TokenURL
728 deriving (Eq, Show, Generic, Data, Typeable)
730data OAuth2Params = OAuth2Params
731 { -- | The flow used by the OAuth2 security scheme.
732 _oauth2Flow :: OAuth2Flow
734 -- | The available scopes for the OAuth2 security scheme.
735 , _oauth2Scopes :: InsOrdHashMap Text Text
736 } deriving (Eq, Show, Generic, Data, Typeable)
738data SecuritySchemeType
739 = SecuritySchemeBasic
740 | SecuritySchemeApiKey ApiKeyParams
741 | SecuritySchemeOAuth2 OAuth2Params
742 deriving (Eq, Show, Generic, Data, Typeable)
744data SecurityScheme = SecurityScheme
745 { -- | The type of the security scheme.
746 _securitySchemeType :: SecuritySchemeType
748 -- | A short description for security scheme.
749 , _securitySchemeDescription :: Maybe Text
750 } deriving (Eq, Show, Generic, Data, Typeable)
752-- | Lists the required security schemes to execute this operation.
753-- The object can have multiple security schemes declared in it which are all required
754-- (that is, there is a logical AND between the schemes).
755newtype SecurityRequirement = SecurityRequirement
756 { getSecurityRequirement :: InsOrdHashMap Text [Text]
757 } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON, Data, Typeable)
759-- | Tag name.
760type TagName = Text
762-- | Allows adding meta data to a single tag that is used by @Operation@.
763-- It is not mandatory to have a @Tag@ per tag used there.
764data Tag = Tag
765 { -- | The name of the tag.
766 _tagName :: TagName
768 -- | A short description for the tag.
769 -- GFM syntax can be used for rich text representation.
770 , _tagDescription :: Maybe Text
772 -- | Additional external documentation for this tag.
773 , _tagExternalDocs :: Maybe ExternalDocs
774 } deriving (Eq, Ord, Show, Generic, Data, Typeable)
776instance IsString Tag where
777 fromString s = Tag (fromString s) Nothing Nothing
779-- | Allows referencing an external resource for extended documentation.
780data ExternalDocs = ExternalDocs
781 { -- | A short description of the target documentation.
782 -- GFM syntax can be used for rich text representation.
783 _externalDocsDescription :: Maybe Text
785 -- | The URL for the target documentation.
786 , _externalDocsUrl :: URL
787 } deriving (Eq, Ord, Show, Generic, Data, Typeable)
789-- | A simple object to allow referencing other definitions in the specification.
790-- It can be used to reference parameters and responses that are defined at the top level for reuse.
791newtype Reference = Reference { getReference :: Text }
792 deriving (Eq, Show, Data, Typeable)
794data Referenced a
795 = Ref Reference
796 | Inline a
797 deriving (Eq, Show, Functor, Data, Typeable)
799instance IsString a => IsString (Referenced a) where
800 fromString = Inline . fromString
802newtype URL = URL { getUrl :: Text } deriving (Eq, Ord, Show, ToJSON, FromJSON, Data, Typeable)
804-- =======================================================================
805-- Monoid instances
806-- =======================================================================
808instance Monoid Swagger where
809 mempty = genericMempty
810 mappend = genericMappend
812instance Monoid Info where
813 mempty = genericMempty
814 mappend = genericMappend
816instance Monoid Contact where
817 mempty = genericMempty
818 mappend = genericMappend
820instance Monoid PathItem where
821 mempty = genericMempty
822 mappend = genericMappend
824instance Monoid Schema where
825 mempty = genericMempty
826 mappend = genericMappend
828instance Monoid (ParamSchema t) where
829 mempty = genericMempty
830 mappend = genericMappend
832instance Monoid Param where
833 mempty = genericMempty
834 mappend = genericMappend
836instance Monoid ParamOtherSchema where
837 mempty = genericMempty
838 mappend = genericMappend
840instance Monoid Header where
841 mempty = genericMempty
842 mappend = genericMappend
844instance Monoid Responses where
845 mempty = genericMempty
846 mappend = genericMappend
848instance Monoid Response where
849 mempty = genericMempty
850 mappend = genericMappend
852instance Monoid ExternalDocs where
853 mempty = genericMempty
854 mappend = genericMappend
856instance Monoid Operation where
857 mempty = genericMempty
858 mappend = genericMappend
860instance Monoid Example where
861 mempty = genericMempty
862 mappend = genericMappend
864-- =======================================================================
865-- SwaggerMonoid helper instances
866-- =======================================================================
868instance SwaggerMonoid Info
869instance SwaggerMonoid PathItem
870instance SwaggerMonoid Schema
871instance SwaggerMonoid (ParamSchema t)
872instance SwaggerMonoid Param
873instance SwaggerMonoid ParamOtherSchema
874instance SwaggerMonoid Responses
875instance SwaggerMonoid Response
876instance SwaggerMonoid ExternalDocs
877instance SwaggerMonoid Operation
879instance SwaggerMonoid MimeList
880deriving instance SwaggerMonoid URL
882instance SwaggerMonoid (SwaggerType t) where
883 swaggerMempty = SwaggerString
884 swaggerMappend _ y = y
886instance SwaggerMonoid ParamLocation where
887 swaggerMempty = ParamQuery
888 swaggerMappend _ y = y
890instance OVERLAPPING_ SwaggerMonoid (InsOrdHashMap FilePath PathItem) where
891 swaggerMempty = InsOrdHashMap.empty
892 swaggerMappend = InsOrdHashMap.unionWith mappend
894instance Monoid a => SwaggerMonoid (Referenced a) where
895 swaggerMempty = Inline mempty
896 swaggerMappend (Inline x) (Inline y) = Inline (x <> y)
897 swaggerMappend _ y = y
899instance SwaggerMonoid ParamAnySchema where
900 swaggerMempty = ParamOther swaggerMempty
901 swaggerMappend (ParamBody x) (ParamBody y) = ParamBody (swaggerMappend x y)
902 swaggerMappend (ParamOther x) (ParamOther y) = ParamOther (swaggerMappend x y)
903 swaggerMappend _ y = y
905-- =======================================================================
906-- Simple Generic-based ToJSON instances
907-- =======================================================================
909instance ToJSON ParamLocation where
910 toJSON = genericToJSON (jsonPrefix "Param")
912instance ToJSON Info where
913 toJSON = genericToJSON (jsonPrefix "Info")
915instance ToJSON Contact where
916 toJSON = genericToJSON (jsonPrefix "Contact")
918instance ToJSON License where
919 toJSON = genericToJSON (jsonPrefix "License")
921instance ToJSON ApiKeyLocation where
922 toJSON = genericToJSON (jsonPrefix "ApiKey")
924instance ToJSON ApiKeyParams where
925 toJSON = genericToJSON (jsonPrefix "apiKey")
927instance ToJSON Scheme where
928 toJSON = genericToJSON (jsonPrefix "")
930instance ToJSON Tag where
931 toJSON = genericToJSON (jsonPrefix "Tag")
933instance ToJSON ExternalDocs where
934 toJSON = genericToJSON (jsonPrefix "ExternalDocs")
936instance ToJSON Xml where
937 toJSON = genericToJSON (jsonPrefix "Xml")
939-- =======================================================================
940-- Simple Generic-based FromJSON instances
941-- =======================================================================
943instance FromJSON ParamLocation where
944 parseJSON = genericParseJSON (jsonPrefix "Param")
946instance FromJSON Info where
947 parseJSON = genericParseJSON (jsonPrefix "Info")
949instance FromJSON Contact where
950 parseJSON = genericParseJSON (jsonPrefix "Contact")
952instance FromJSON License where
953 parseJSON = genericParseJSON (jsonPrefix "License")
955instance FromJSON ApiKeyLocation where
956 parseJSON = genericParseJSON (jsonPrefix "ApiKey")
958instance FromJSON ApiKeyParams where
959 parseJSON = genericParseJSON (jsonPrefix "apiKey")
961instance FromJSON Scheme where
962 parseJSON = genericParseJSON (jsonPrefix "")
964instance FromJSON Tag where
965 parseJSON = genericParseJSON (jsonPrefix "Tag")
967instance FromJSON ExternalDocs where
968 parseJSON = genericParseJSON (jsonPrefix "ExternalDocs")
970-- =======================================================================
971-- Manual ToJSON instances
972-- =======================================================================
974instance ToJSON OAuth2Flow where
975 toJSON (OAuth2Implicit authUrl) = object
976 [ "flow" .= ("implicit" :: Text)
977 , "authorizationUrl" .= authUrl ]
978 toJSON (OAuth2Password tokenUrl) = object
979 [ "flow" .= ("password" :: Text)
980 , "tokenUrl" .= tokenUrl ]
981 toJSON (OAuth2Application tokenUrl) = object
982 [ "flow" .= ("application" :: Text)
983 , "tokenUrl" .= tokenUrl ]
984 toJSON (OAuth2AccessCode authUrl tokenUrl) = object
985 [ "flow" .= ("accessCode" :: Text)
986 , "authorizationUrl" .= authUrl
987 , "tokenUrl" .= tokenUrl ]
989instance ToJSON OAuth2Params where
990 toJSON = sopSwaggerGenericToJSON
991 DEFINE_TOENCODING
993instance ToJSON SecuritySchemeType where
994 toJSON SecuritySchemeBasic
995 = object [ "type" .= ("basic" :: Text) ]
996 toJSON (SecuritySchemeApiKey params)
997 = toJSON params
998 <+> object [ "type" .= ("apiKey" :: Text) ]
999 toJSON (SecuritySchemeOAuth2 params)
1000 = toJSON params
1001 <+> object [ "type" .= ("oauth2" :: Text) ]
1003instance ToJSON Swagger where
1004 toJSON = sopSwaggerGenericToJSON
1005 DEFINE_TOENCODING
1007instance ToJSON SecurityScheme where
1008 toJSON = sopSwaggerGenericToJSON
1009 DEFINE_TOENCODING
1011instance ToJSON Schema where
1012 toJSON = sopSwaggerGenericToJSON
1013 DEFINE_TOENCODING
1015instance ToJSON Header where
1016 toJSON = sopSwaggerGenericToJSON
1017 DEFINE_TOENCODING
1019instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
1020 toJSON (SwaggerItemsPrimitive fmt schema) = object
1021 [ "collectionFormat" .= fmt
1022 , "items" .= schema ]
1023 toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1024 toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
1026instance ToJSON Host where
1027 toJSON (Host host mport) = toJSON $
1028 case mport of
1029 Nothing -> host
1030 Just port -> host ++ ":" ++ show port
1032instance ToJSON MimeList where
1033 toJSON (MimeList xs) = toJSON (map show xs)
1035instance ToJSON Param where
1036 toJSON = sopSwaggerGenericToJSON
1037 DEFINE_TOENCODING
1039instance ToJSON ParamAnySchema where
1040 toJSON (ParamBody s) = object [ "in" .= ("body" :: Text), "schema" .= s ]
1041 toJSON (ParamOther s) = toJSON s
1043instance ToJSON ParamOtherSchema where
1044 toJSON = sopSwaggerGenericToJSON
1045 DEFINE_TOENCODING
1047instance ToJSON Responses where
1048 toJSON = sopSwaggerGenericToJSON
1049 DEFINE_TOENCODING
1051instance ToJSON Response where
1052 toJSON = sopSwaggerGenericToJSON
1053 DEFINE_TOENCODING
1055instance ToJSON Operation where
1056 toJSON = sopSwaggerGenericToJSON
1057 DEFINE_TOENCODING
1059instance ToJSON PathItem where
1060 toJSON = sopSwaggerGenericToJSON
1061 DEFINE_TOENCODING
1063instance ToJSON Example where
1064 toJSON = toJSON . Map.mapKeys show . getExample
1066instance ToJSON Reference where
1067 toJSON (Reference ref) = object [ "$ref" .= ref ]
1069referencedToJSON :: ToJSON a => Text -> Referenced a -> Value
1070referencedToJSON prefix (Ref (Reference ref)) = object [ "$ref" .= (prefix <> ref) ]
1071referencedToJSON _ (Inline x) = toJSON x
1073instance ToJSON (Referenced Schema) where toJSON = referencedToJSON "#/definitions/"
1074instance ToJSON (Referenced Param) where toJSON = referencedToJSON "#/parameters/"
1075instance ToJSON (Referenced Response) where toJSON = referencedToJSON "#/responses/"
1077instance ToJSON (SwaggerType t) where
1078 toJSON SwaggerArray = "array"
1079 toJSON SwaggerString = "string"
1080 toJSON SwaggerInteger = "integer"
1081 toJSON SwaggerNumber = "number"
1082 toJSON SwaggerBoolean = "boolean"
1083 toJSON SwaggerFile = "file"
1084 toJSON SwaggerNull = "null"
1085 toJSON SwaggerObject = "object"
1087instance ToJSON (CollectionFormat t) where
1088 toJSON CollectionCSV = "csv"
1089 toJSON CollectionSSV = "ssv"
1090 toJSON CollectionTSV = "tsv"
1091 toJSON CollectionPipes = "pipes"
1092 toJSON CollectionMulti = "multi"
1094instance ToJSON (ParamSchema k) where
1095 -- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
1096 toJSON = sopSwaggerGenericToJSONWithOpts $
1097 mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1099-- =======================================================================
1100-- Manual FromJSON instances
1101-- =======================================================================
1103instance FromJSON OAuth2Flow where
1104 parseJSON (Object o) = do
1105 (flow :: Text) <- o .: "flow"
1106 case flow of
1107 "implicit" -> OAuth2Implicit <$> o .: "authorizationUrl"
1108 "password" -> OAuth2Password <$> o .: "tokenUrl"
1109 "application" -> OAuth2Application <$> o .: "tokenUrl"
1110 "accessCode" -> OAuth2AccessCode
1111 <$> o .: "authorizationUrl"
1112 <*> o .: "tokenUrl"
1113 _ -> empty
1114 parseJSON _ = empty
1116instance FromJSON OAuth2Params where
1117 parseJSON = sopSwaggerGenericParseJSON
1119instance FromJSON SecuritySchemeType where
1120 parseJSON js@(Object o) = do
1121 (t :: Text) <- o .: "type"
1122 case t of
1123 "basic" -> pure SecuritySchemeBasic
1124 "apiKey" -> SecuritySchemeApiKey <$> parseJSON js
1125 "oauth2" -> SecuritySchemeOAuth2 <$> parseJSON js
1126 _ -> empty
1127 parseJSON _ = empty
1129instance FromJSON Swagger where
1130 parseJSON = sopSwaggerGenericParseJSON
1132instance FromJSON SecurityScheme where
1133 parseJSON = sopSwaggerGenericParseJSON
1135instance FromJSON Schema where
1136 parseJSON = sopSwaggerGenericParseJSON
1138instance FromJSON Header where
1139 parseJSON = sopSwaggerGenericParseJSON
1141instance (FromJSON (CollectionFormat ('SwaggerKindNormal t)), FromJSON (ParamSchema ('SwaggerKindNormal t))) => FromJSON (SwaggerItems ('SwaggerKindNormal t)) where
1142 parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1143 <$> o .:? "collectionFormat"
1144 <*> (o .: "items" >>= parseJSON)
1146instance FromJSON (SwaggerItems 'SwaggerKindParamOtherSchema) where
1147 parseJSON = withObject "SwaggerItemsPrimitive" $ \o -> SwaggerItemsPrimitive
1148 <$> o .:? "collectionFormat"
1149 <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
1151instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
1152 parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1153 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1154 parseJSON _ = empty
1156instance FromJSON Host where
1157 parseJSON (String s) = case map Text.unpack $ Text.split (== ':') s of
1158 [host] -> return $ Host host Nothing
1159 [host, port] -> case readMaybe port of
1160 Nothing -> fail $ "Invalid port `" ++ port ++ "'"
1161 Just p -> return $ Host host (Just (fromInteger p))
1162 _ -> fail $ "Invalid host `" ++ Text.unpack s ++ "'"
1163 parseJSON _ = empty
1165instance FromJSON MimeList where
1166 parseJSON js = (MimeList . map fromString) <$> parseJSON js
1168instance FromJSON Param where
1169 parseJSON = sopSwaggerGenericParseJSON
1171instance FromJSON ParamAnySchema where
1172 parseJSON js@(Object o) = do
1173 (i :: Text) <- o .: "in"
1174 case i of
1175 "body" -> do
1176 schema <- o .: "schema"
1177 ParamBody <$> parseJSON schema
1178 _ -> ParamOther <$> parseJSON js
1179 parseJSON _ = empty
1181instance FromJSON ParamOtherSchema where
1182 parseJSON = sopSwaggerGenericParseJSON
1184instance FromJSON Responses where
1185 parseJSON (Object o) = Responses
1186 <$> o .:? "default"
1187 <*> (parseJSON (Object (HashMap.delete "default" o)))
1188 parseJSON _ = empty
1190instance FromJSON Example where
1191 parseJSON js = do
1192 m <- parseJSON js
1193 pure $ Example (Map.mapKeys fromString m)
1195instance FromJSON Response where
1196 parseJSON = sopSwaggerGenericParseJSON
1198instance FromJSON Operation where
1199 parseJSON = sopSwaggerGenericParseJSON
1201instance FromJSON PathItem where
1202 parseJSON = sopSwaggerGenericParseJSON
1204instance FromJSON Reference where
1205 parseJSON (Object o) = Reference <$> o .: "$ref"
1206 parseJSON _ = empty
1208referencedParseJSON :: FromJSON a => Text -> Value -> JSON.Parser (Referenced a)
1209referencedParseJSON prefix js@(Object o) = do
1210 ms <- o .:? "$ref"
1211 case ms of
1212 Nothing -> Inline <$> parseJSON js
1213 Just s -> Ref <$> parseRef s
1214 where
1215 parseRef s = do
1216 case Text.stripPrefix prefix s of
1217 Nothing -> fail $ "expected $ref of the form \"" <> Text.unpack prefix <> "*\", but got " <> show s
1218 Just suffix -> pure (Reference suffix)
1219referencedParseJSON _ _ = fail "referenceParseJSON: not an object"
1221instance FromJSON (Referenced Schema) where parseJSON = referencedParseJSON "#/definitions/"
1222instance FromJSON (Referenced Param) where parseJSON = referencedParseJSON "#/parameters/"
1223instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#/responses/"
1225instance FromJSON Xml where
1226 parseJSON = genericParseJSON (jsonPrefix "xml")
1228instance FromJSON (SwaggerType 'SwaggerKindSchema) where
1229 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerNull, SwaggerObject]
1231instance FromJSON (SwaggerType 'SwaggerKindParamOtherSchema) where
1232 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray, SwaggerFile]
1234instance FromJSON (SwaggerType ('SwaggerKindNormal t)) where
1235 parseJSON = parseOneOf [SwaggerString, SwaggerInteger, SwaggerNumber, SwaggerBoolean, SwaggerArray]
1237instance FromJSON (CollectionFormat ('SwaggerKindNormal t)) where
1238 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1240-- NOTE: There aren't collections of 'Schema'
1241--instance FromJSON (CollectionFormat (SwaggerKindSchema)) where
1242-- parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes]
1244instance FromJSON (CollectionFormat 'SwaggerKindParamOtherSchema) where
1245 parseJSON = parseOneOf [CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes, CollectionMulti]
1247instance (FromJSON (SwaggerType ('SwaggerKindNormal t)), FromJSON (SwaggerItems ('SwaggerKindNormal t))) => FromJSON (ParamSchema ('SwaggerKindNormal t)) where
1248 parseJSON = sopSwaggerGenericParseJSON
1249instance FromJSON (ParamSchema 'SwaggerKindParamOtherSchema) where
1250 parseJSON = sopSwaggerGenericParseJSON
1251instance FromJSON (ParamSchema 'SwaggerKindSchema) where
1252 parseJSON = sopSwaggerGenericParseJSON
1254-------------------------------------------------------------------------------
1255-- TH splices
1256-------------------------------------------------------------------------------
1258deriveGeneric ''Header
1259deriveGeneric ''OAuth2Params
1260deriveGeneric ''Operation
1261deriveGeneric ''Param
1262deriveGeneric ''ParamOtherSchema
1263deriveGeneric ''PathItem
1264deriveGeneric ''Response
1265deriveGeneric ''Responses
1266deriveGeneric ''SecurityScheme
1267deriveGeneric ''Schema
1268deriveGeneric ''ParamSchema
1269deriveGeneric ''Swagger
1271instance HasSwaggerAesonOptions Header where
1272 swaggerAesonOptions _ = mkSwaggerAesonOptions "header" & saoSubObject ?~ "paramSchema"
1273instance HasSwaggerAesonOptions OAuth2Params where
1274 swaggerAesonOptions _ = mkSwaggerAesonOptions "oauth2" & saoSubObject ?~ "flow"
1275instance HasSwaggerAesonOptions Operation where
1276 swaggerAesonOptions _ = mkSwaggerAesonOptions "operation"
1277instance HasSwaggerAesonOptions Param where
1278 swaggerAesonOptions _ = mkSwaggerAesonOptions "param" & saoSubObject ?~ "schema"
1279instance HasSwaggerAesonOptions ParamOtherSchema where
1280 swaggerAesonOptions _ = mkSwaggerAesonOptions "paramOtherSchema" & saoSubObject ?~ "paramSchema"
1281instance HasSwaggerAesonOptions PathItem where
1282 swaggerAesonOptions _ = mkSwaggerAesonOptions "pathItem"
1283instance HasSwaggerAesonOptions Response where
1284 swaggerAesonOptions _ = mkSwaggerAesonOptions "response"
1285instance HasSwaggerAesonOptions Responses where
1286 swaggerAesonOptions _ = mkSwaggerAesonOptions "responses" & saoSubObject ?~ "responses"
1287instance HasSwaggerAesonOptions SecurityScheme where
1288 swaggerAesonOptions _ = mkSwaggerAesonOptions "securityScheme" & saoSubObject ?~ "type"
1289instance HasSwaggerAesonOptions Schema where
1290 swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject ?~ "paramSchema"
1291instance HasSwaggerAesonOptions Swagger where
1292 swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("swagger", "2.0")]
1294instance HasSwaggerAesonOptions (ParamSchema ('SwaggerKindNormal t)) where
1295 swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1296instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindParamOtherSchema) where
1297 swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1298-- NOTE: Schema doesn't have 'items' sub object!
1299instance HasSwaggerAesonOptions (ParamSchema 'SwaggerKindSchema) where
1300 swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
1302instance AesonDefaultValue (ParamSchema s)
1303instance AesonDefaultValue OAuth2Flow
1304instance AesonDefaultValue Responses
1305instance AesonDefaultValue ParamAnySchema
1306instance AesonDefaultValue SecuritySchemeType
1307instance AesonDefaultValue (SwaggerType a)
1308instance AesonDefaultValue MimeList where defaultValue = Just mempty
1309instance AesonDefaultValue Info
1310instance AesonDefaultValue ParamLocation