@@ -6,6 +6,20 @@
6-- and Swagger-Codegen to generate clients in various languages.
6-- and Swagger-Codegen to generate clients in various languages.
7-- Additional utilities can also take advantage of the resulting files, such as testing tools.
7-- Additional utilities can also take advantage of the resulting files, such as testing tools.
8module Data.Swagger (
8module Data.Swagger (
9+ -- * How to use this library
12+ -- ** @'Monoid'@ instances
15+ -- ** Lenses and prisms
18+ -- ** Schema specification
22+ module Data.Swagger.Lens,
9 module Data.Swagger.ParamSchema,
23 module Data.Swagger.ParamSchema,
10 module Data.Swagger.Schema,
24 module Data.Swagger.Schema,
@@ -14,25 +28,25 @@ module Data.Swagger (
15 Scheme(..),
29 Scheme(..),
17− -- * Info types
31+ -- ** Info types
19 Contact(..),
33 Contact(..),
20 License(..),
34 License(..),
22− -- * Paths
36+ -- ** Paths
23 Paths(..),
37 Paths(..),
24 PathItem(..),
38 PathItem(..),
26− -- * Operations
40+ -- ** Operations
28 Operation(..),
42 Operation(..),
30− -- * Types and formats
44+ -- ** Types and formats
31 SwaggerType(..),
45 SwaggerType(..),
33 CollectionFormat(..),
47 CollectionFormat(..),
35− -- * Parameters
49+ -- ** Parameters
36 Param(..),
50 Param(..),
37 ParamAnySchema(..),
51 ParamAnySchema(..),
38 ParamOtherSchema(..),
52 ParamOtherSchema(..),
@@ -42,45 +56,159 @@ module Data.Swagger (
42 Header(..),
56 Header(..),
43 Example(..),
57 Example(..),
45− -- * Schemas
59+ -- ** Schemas
46 ParamSchema(..),
60 ParamSchema(..),
47 Schema(..),
61 Schema(..),
48 SwaggerItems(..),
62 SwaggerItems(..),
51− -- * Responses
65+ -- ** Responses
52 Responses(..),
66 Responses(..),
53 Response(..),
67 Response(..),
55− -- * Security
69+ -- ** Security
56 SecurityScheme(..),
70 SecurityScheme(..),
57 SecuritySchemeType(..),
71 SecuritySchemeType(..),
58 SecurityRequirement(..),
72 SecurityRequirement(..),
60− -- ** API key
74+ -- *** API key
61 ApiKeyParams(..),
75 ApiKeyParams(..),
62 ApiKeyLocation(..),
76 ApiKeyLocation(..),
64− -- ** OAuth2
78+ -- *** OAuth2
65 OAuth2Params(..),
79 OAuth2Params(..),
66 OAuth2Flow(..),
80 OAuth2Flow(..),
67 AuthorizationURL,
81 AuthorizationURL,
70− -- * External documentation
84+ -- ** External documentation
71 ExternalDocs(..),
85 ExternalDocs(..),
73− -- * References
87+ -- ** References
74 Reference(..),
88 Reference(..),
75 Referenced(..),
89 Referenced(..),
77− -- * Miscellaneous
91+ -- ** Miscellaneous
78 MimeList(..),
92 MimeList(..),
96+import Data.Swagger.Lens
82import Data.Swagger.ParamSchema
97import Data.Swagger.ParamSchema
83import Data.Swagger.Schema
98import Data.Swagger.Schema
85import Data.Swagger.Internal
100import Data.Swagger.Internal
103+-- >>> import Control.Lens
104+-- >>> import Data.Aeson
105+-- >>> import Data.Monoid
106+-- >>> import Data.Proxy
107+-- >>> import GHC.Generics
108+-- >>> :set -XDeriveGeneric
109+-- >>> :set -XOverloadedStrings
110+-- >>> :set -XOverloadedLists
111+-- >>> :set -fno-warn-missing-methods
115+-- This section explains how to use this library to work with Swagger specification.
119+-- Virtually all types representing Swagger specification have @'Monoid'@ instances.
120+-- The @'Monoid'@ type class provides two methods — @'mempty'@ and @'mappend'@.
122+-- In this library you can use @'mempty'@ for a default/empty value. For instance:
124+-- >>> encode (mempty :: Swagger)
125+-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"}}"
127+-- As you can see some spec properties (e.g. @"version"@) are there even when the spec is empty.
128+-- That is because these properties are actually required ones.
130+-- You /should/ always override the default (empty) value for these properties,
131+-- although it is not strictly necessary:
133+-- >>> encode mempty { _infoTitle = "Todo API", _infoVersion = "1.0" }
134+-- "{\"version\":\"1.0\",\"title\":\"Todo API\"}"
136+-- You can merge two values using @'mappend'@ or its infix version @('<>')@:
138+-- >>> encode $ mempty { _infoTitle = "Todo API" } <> mempty { _infoVersion = "1.0" }
139+-- "{\"version\":\"1.0\",\"title\":\"Todo API\"}"
141+-- This can be useful for combining specifications of endpoints into a whole API specification:
144+-- \-\- /account subAPI specification
145+-- accountAPI :: Swagger
147+-- \-\- /task subAPI specification
148+-- taskAPI :: Swagger
150+-- \-\- while API specification is just a combination
151+-- \-\- of subAPIs' specifications
153+-- api = accountAPI <> taskAPI
158+-- Since @'Swagger'@ has a fairly complex structure, lenses and prisms are used
159+-- to modify this structure. In combination with @'Monoid'@ instances, lenses
160+-- also make it fairly simple to construct/modify any part of the specification:
163+-- encode $ mempty & pathsMap .~
164+-- [ ("/user", mempty & pathItemGet ?~ (mempty
165+-- & operationProduces ?~ MimeList ["application/json"]
166+-- & operationResponses .~ (mempty
167+-- & responsesResponses . at 200 ?~ Inline (mempty & responseSchema ?~ Ref (Reference "#/definitions/User")))))]
169+-- "{\"/user\":{\"get\":{\"responses\":{\"200\":{\"schema\":{\"$ref\":\"#/definitions/#/definitions/User\"},\"description\":\"\"}},\"produces\":[\"application/json\"]}}}"
171+-- Since @'ParamSchema'@ is basically the /base schema specification/, a special
172+-- @'HasParamSchema'@ class has been introduced to generalize @'ParamSchema'@ lenses
173+-- and allow them to be used by any type that has a @'ParamSchema'@:
177+-- & schemaTitle ?~ "Email"
178+-- & schemaType .~ SwaggerString
179+-- & schemaFormat ?~ "email"
181+-- "{\"format\":\"email\",\"title\":\"Email\",\"type\":\"string\"}"
185+-- This library provides two classes for schema encoding.
186+-- Both these classes provide means to encode _types_ as Swagger _schemas_.
188+-- @'ToParamSchema'@ is intended to be used for primitive API endpoint parameters,
189+-- such as query parameters, headers and URL path pieces.
190+-- Its corresponding value-encoding class is @'ToHttpApiData'@ (from @http-api-data@ package).
192+-- @'ToSchema'@ is used for request and response bodies and mostly differ from
193+-- primitive parameters by allowing objects/mappings in addition to primitive types and arrays.
194+-- Its corresponding value-encoding class is @'ToJSON'@ (from @aeson@ package).
196+-- While lenses and prisms make it easy to define schemas, it might be that you don't need to:
197+-- @'ToSchema'@ and @'ToParamSchema'@ classes both have default @'Generic'@-based implementations!
199+-- @'ToSchema'@ default implementation is also aligned with @'ToJSON'@ default implementation with
200+-- the only difference being for sum encoding. @'ToJSON'@ defaults sum encoding to @'defaultTaggedObject'@,
201+-- while @'ToSchema'@ defaults to something which corresponds to @'ObjectWithSingleField'@. This is due to
202+-- @'defaultTaggedObject'@ behavior being hard to specify in Swagger.
204+-- Here's an example showing @'ToJSON'@–@'ToSchema'@ correspondance:
206+-- >>> data Person = Person { name :: String, age :: Integer } deriving Generic
207+-- >>> instance ToJSON Person
208+-- >>> instance ToSchema Person
209+-- >>> encode (Person "David" 28)
210+-- "{\"age\":28,\"name\":\"David\"}"
211+-- >>> encode $ toSchema (Proxy :: Proxy Person)
212+-- "{\"required\":[\"name\",\"age\"],\"type\":\"object\",\"properties\":{\"age\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}}}"