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