git.iko.soy/typeable/openapi3
on master help sign in
Add 'How to use this library' documentation for Data.Swagger
aa13d31Nickolay Kudasov
src/Data/Swagger.hs+139−13
@@ -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
10+ -- $howto
11+
12+ -- ** @'Monoid'@ instances
13+ -- $monoids
14+
15+ -- ** Lenses and prisms
16+ -- $lens
17+
18+ -- ** Schema specification
19+ -- $schema
20+
21+ -- * Re-exports
9 module Data.Swagger.ParamSchema,
22 module Data.Swagger.ParamSchema,
10 module Data.Swagger.Schema,
23 module Data.Swagger.Schema,
11
24
@@ -14,25 +27,25 @@ module Data.Swagger (
14 Host(..),
27 Host(..),
15 Scheme(..),
28 Scheme(..),
16
29
17− -- * Info types
30+ -- ** Info types
18 Info(..),
31 Info(..),
19 Contact(..),
32 Contact(..),
20 License(..),
33 License(..),
21
34
22− -- * Paths
35+ -- ** Paths
23 Paths(..),
36 Paths(..),
24 PathItem(..),
37 PathItem(..),
25
38
26− -- * Operations
39+ -- ** Operations
27 Tag(..),
40 Tag(..),
28 Operation(..),
41 Operation(..),
29
42
30− -- * Types and formats
43+ -- ** Types and formats
31 SwaggerType(..),
44 SwaggerType(..),
32 Format,
45 Format,
33 CollectionFormat(..),
46 CollectionFormat(..),
34
47
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(..),
44
57
45− -- * Schemas
58+ -- ** Schemas
46 ParamSchema(..),
59 ParamSchema(..),
47 Schema(..),
60 Schema(..),
48 SwaggerItems(..),
61 SwaggerItems(..),
49 Xml(..),
62 Xml(..),
50
63
51− -- * Responses
64+ -- ** Responses
52 Responses(..),
65 Responses(..),
53 Response(..),
66 Response(..),
54
67
55− -- * Security
68+ -- ** Security
56 SecurityScheme(..),
69 SecurityScheme(..),
57 SecuritySchemeType(..),
70 SecuritySchemeType(..),
58 SecurityRequirement(..),
71 SecurityRequirement(..),
59
72
60− -- ** API key
73+ -- *** API key
61 ApiKeyParams(..),
74 ApiKeyParams(..),
62 ApiKeyLocation(..),
75 ApiKeyLocation(..),
63
76
64− -- ** OAuth2
77+ -- *** OAuth2
65 OAuth2Params(..),
78 OAuth2Params(..),
66 OAuth2Flow(..),
79 OAuth2Flow(..),
67 AuthorizationURL,
80 AuthorizationURL,
68 TokenURL,
81 TokenURL,
69
82
70− -- * External documentation
83+ -- ** External documentation
71 ExternalDocs(..),
84 ExternalDocs(..),
72
85
73− -- * References
86+ -- ** References
74 Reference(..),
87 Reference(..),
75 Referenced(..),
88 Referenced(..),
76
89
77− -- * Miscellaneous
90+ -- ** Miscellaneous
78 MimeList(..),
91 MimeList(..),
79 URL(..),
92 URL(..),
80) where
93) where
@@ -84,3 +97,116 @@ import Data.Swagger.Schema
84
97
85import Data.Swagger.Internal
98import Data.Swagger.Internal
86
99
100+-- $setup
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
110+
111+-- $howto
112+--
113+-- This section explains how to use this library to work with Swagger specification.
114+
115+-- $monoids
116+--
117+-- Virtually all types representing Swagger specification have @'Monoid'@ instances.
118+-- The @'Monoid'@ type class provides two methods — @'mempty'@ and @'mappend'@.
119+--
120+-- In this library you can use @'mempty'@ for a default/empty value. For instance:
121+--
122+-- >>> encode (mempty :: Swagger)
123+-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"}}"
124+--
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.
127+--
128+-- You /should/ always override the default (empty) value for these properties,
129+-- although it is not strictly necessary:
130+--
131+-- >>> encode mempty { _infoTitle = "Todo API", _infoVersion = "1.0" }
132+-- "{\"version\":\"1.0\",\"title\":\"Todo API\"}"
133+--
134+-- You can merge two values using @'mappend'@ or its infix version @('<>')@:
135+--
136+-- >>> encode $ mempty { _infoTitle = "Todo API" } <> mempty { _infoVersion = "1.0" }
137+-- "{\"version\":\"1.0\",\"title\":\"Todo API\"}"
138+--
139+-- This can be useful for combining specifications of endpoints into a whole API specification:
140+--
141+-- @
142+-- \-\- /account subAPI specification
143+-- accountAPI :: Swagger
144+--
145+-- \-\- /task subAPI specification
146+-- taskAPI :: Swagger
147+--
148+-- \-\- while API specification is just a combination
149+-- \-\- of subAPIs' specifications
150+-- api :: Swagger
151+-- api = accountAPI <> taskAPI
152+-- @
153+
154+-- $lens
155+--
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:
159+--
160+-- >>> :{
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")))))]
166+-- :}
167+-- "{\"/user\":{\"get\":{\"responses\":{\"200\":{\"schema\":{\"$ref\":\"#/definitions/#/definitions/User\"},\"description\":\"\"}},\"produces\":[\"application/json\"]}}}"
168+--
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'@:
172+--
173+-- >>> :{
174+-- encode $ mempty
175+-- & schemaTitle ?~ "Email"
176+-- & schemaType .~ SwaggerString
177+-- & schemaFormat ?~ "email"
178+-- :}
179+-- "{\"format\":\"email\",\"title\":\"Email\",\"type\":\"string\"}"
180+
181+-- $schema
182+--
183+-- This library provides two classes for schema encoding.
184+-- Both these classes provide means to encode _types_ as Swagger _schemas_.
185+--
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).
189+--
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).
193+--
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!
196+--
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.
201+--
202+-- Here's an example showing @'ToJSON'@–@'ToSchema'@ correspondance:
203+--
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\"}}}"
211+--
212+