git.iko.soy/typeable/openapi3
on master help sign in
Update and improve documentation
3f5e9e3Nickolay Kudasov
src/Data/Swagger.hs+47−18
@@ -159,32 +159,61 @@ import Data.Swagger.Internal
159-- $lens
159-- $lens
160--
160--
161-- Since @'Swagger'@ has a fairly complex structure, lenses and prisms are used
161-- Since @'Swagger'@ has a fairly complex structure, lenses and prisms are used
162−-- to modify this structure. In combination with @'Monoid'@ instances, lenses
162+-- to work comfortly with it. In combination with @'Monoid'@ instances, lenses
163−-- also make it fairly simple to construct/modify any part of the specification:
163+-- make it fairly simple to construct/modify any part of the specification:
164--
164--
165-- >>> :{
165-- >>> :{
166−-- encode $ (mempty :: Swagger) & paths .~
166+-- encode $ (mempty :: Swagger)
167+-- & definitions .~ [ ("User", mempty & type_ .~ SwaggerString) ]
168+-- & paths .~
167−-- [ ("/user", mempty & get ?~ (mempty
169+-- [ ("/user", mempty & get ?~ (mempty
168−-- & produces ?~ MimeList ["application/json"]
170+-- & produces ?~ MimeList ["application/json"]
169−-- & at 200 ?~ Inline (mempty & schema ?~ Ref (Reference "#/definitions/User")))) ]
171+-- & at 200 ?~ Inline (mempty & schema ?~ Ref (Reference "User")))) ]
170-- :}
172-- :}
171−-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"200\":{\"schema\":{\"$ref\":\"#/definitions/#/definitions/User\"},\"description\":\"\"}},\"produces\":[\"application/json\"]}}}}"
173+-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"definitions\":{\"User\":{\"type\":\"string\"}},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"200\":{\"schema\":{\"$ref\":\"#/definitions/User\"},\"description\":\"\"}},\"produces\":[\"application/json\"]}}}}"
172--
174--
173−-- In the snippet above we declare API paths with a single path @/user@ providing method @GET@
175+-- In the snippet above we declare API with a single path @/user@ providing method @GET@
174-- which produces @application/json@ output and should respond with code @200@ and body specified
176-- which produces @application/json@ output and should respond with code @200@ and body specified
175−-- by schema @User@ (which should be defined in @definitions@ property of swagger specification).
177+-- by schema @User@ which is defined in @'definitions'@ property of swagger specification.
176--
178--
177−-- Since @'ParamSchema'@ is basically the /base schema specification/, a special
179+-- For convenience, @swagger2@ uses /classy field lenses/. It means that
178−-- @'HasParamSchema'@ class has been introduced to generalize @'ParamSchema'@ lenses
180+-- field accessor names can be overloaded for different types. One such
179−-- and allow them to be used by any type that has a @'ParamSchema'@:
181+-- common field is @'description'@. Many components of Swagger specification
182+-- can have descriptions, and you can use the same name for them:
180--
183--
184+-- >>> encode $ (mempty :: Response) & description .~ "No content"
185+-- "{\"description\":\"No content\"}"
181-- >>> :{
186-- >>> :{
182-- encode $ (mempty :: Schema)
187-- encode $ (mempty :: Schema)
183−-- & title ?~ "Email"
188+-- & type_ .~ SwaggerBoolean
184−-- & type_ .~ SwaggerString
189+-- & description ?~ "To be or not to be"
185−-- & format ?~ "email"
186-- :}
190-- :}
187−-- "{\"format\":\"email\",\"title\":\"Email\",\"type\":\"string\"}"
191+-- "{\"type\":\"boolean\",\"description\":\"To be or not to be\"}"
192+--
193+-- @'ParamSchema'@ is basically the /base schema specification/ and many types contain it (see @'HasParamSchema'@).
194+-- So for convenience, all @'ParamSchema'@ fields are transitively made fields of the type that has it.
195+-- For example, you can use @'type_'@ to access @'SwaggerType'@ of @'Header'@ schema without having to use @'paramSchema'@:
196+--
197+-- >>> encode $ (mempty :: Header) & type_ .~ SwaggerNumber
198+-- "{\"type\":\"number\"}"
199+--
200+-- Additionally, to simplify working with @'Response'@, both @'Operation'@ and @'Responses'@
201+-- have direct access to it via @'at' code@. Example:
202+--
203+-- >>> :{
204+-- encode $ (mempty :: Operation)
205+-- & at 404 ?~ Inline (mempty & description .~ "Not found")
206+-- :}
207+-- "{\"responses\":{\"404\":{\"description\":\"Not found\"}}}"
208+--
209+-- You might've noticed that @'type_'@ has an extra underscore in its name
210+-- compared to, say, @'description'@ field accessor.
211+-- This is because @type@ is a keyword in Haskell.
212+-- A few other field accessors are modified in this way:
213+--
214+-- - @'in_'@, @'type_'@, @'default_'@ (as keywords);
215+-- - @'maximum_'@ and @'minimum_'@ (as conflicting with @Prelude@);
216+-- - @'enum_'@ (as conflicting with @Control.Lens@).
188
217
189-- $schema
218-- $schema
190--
219--
@@ -197,8 +226,8 @@ import Data.Swagger.Internal
197-- with properties in addition to what @'ParamSchema'@ provides.
226-- with properties in addition to what @'ParamSchema'@ provides.
198--
227--
199-- In most cases you will have a Haskell data type for which you would like to
228-- In most cases you will have a Haskell data type for which you would like to
200−-- define a corresponding schema. To facilitate thise use case
229+-- define a corresponding schema. To facilitate this use case
201−-- this library provides two classes for schema encoding.
230+-- @swagger2@ provides two classes for schema encoding.
202-- Both these classes provide means to encode /types/ as Swagger /schemas/.
231-- Both these classes provide means to encode /types/ as Swagger /schemas/.
203--
232--
204-- @'ToParamSchema'@ is intended to be used for primitive API endpoint parameters,
233-- @'ToParamSchema'@ is intended to be used for primitive API endpoint parameters,
src/Data/Swagger/Lens.hs+15−36
@@ -16,37 +16,35 @@ import Data.Swagger.Internal
16import Data.Swagger.Internal.Utils
16import Data.Swagger.Internal.Utils
17import Data.Text (Text)
17import Data.Text (Text)
18
18
19−-- =======================================================================
20−-- * TH derived lenses
19+-- * Classy lenses
21
20
22−-- ** 'Swagger' lenses
23makeFields ''Swagger
21makeFields ''Swagger
24−-- ** 'Host' lenses
25makeFields ''Host
22makeFields ''Host
26−-- ** 'Info' lenses
27makeFields ''Info
23makeFields ''Info
28−-- ** 'Contact' lenses
29makeFields ''Contact
24makeFields ''Contact
30−-- ** 'License' lenses
31makeFields ''License
25makeFields ''License
32−-- ** 'PathItem' lenses
33makeFields ''PathItem
26makeFields ''PathItem
34−-- ** 'Tag' lenses
35makeFields ''Tag
27makeFields ''Tag
36−-- ** 'Operation' lenses
37makeFields ''Operation
28makeFields ''Operation
38−-- ** 'Param' lenses
39makeFields ''Param
29makeFields ''Param
40−-- ** 'ParamAnySchema' prisms
41−makePrisms ''ParamAnySchema
42−-- ** 'ParamOtherSchema' lenses
43makeLensesWith swaggerFieldRules ''ParamOtherSchema
30makeLensesWith swaggerFieldRules ''ParamOtherSchema
44−-- ** 'Header' lenses
45makeFields ''Header
31makeFields ''Header
46−-- ** 'Schema' lenses
47makeFields ''Schema
32makeFields ''Schema
48−-- ** 'NamedSchema' lenses
49makeFields ''NamedSchema
33makeFields ''NamedSchema
34+makeLensesWith swaggerFieldRules ''ParamSchema
35+makeFields ''Xml
36+makeLensesWith swaggerFieldRules ''Responses
37+makeFields ''Response
38+makeLensesWith swaggerFieldRules ''SecurityScheme
39+makeFields ''ApiKeyParams
40+makeFields ''OAuth2Params
41+makeFields ''ExternalDocs
42+
43+-- * Prisms
44+-- ** 'ParamAnySchema' prisms
45+makePrisms ''ParamAnySchema
46+-- ** 'SecuritySchemeType' prisms
47+makePrisms ''SecuritySchemeType
50
48
51-- ** 'SwaggerItems' prisms
49-- ** 'SwaggerItems' prisms
52
50
@@ -67,25 +65,6 @@ _SwaggerItemsObject
67_SwaggerItemsPrimitive :: forall t p f. (Profunctor p, Bifunctor p, Functor f) => Optic' p f (SwaggerItems t) (Maybe (CollectionFormat t), ParamSchema t)
65_SwaggerItemsPrimitive :: forall t p f. (Profunctor p, Bifunctor p, Functor f) => Optic' p f (SwaggerItems t) (Maybe (CollectionFormat t), ParamSchema t)
68_SwaggerItemsPrimitive = unto (\(c, p) -> SwaggerItemsPrimitive c p)
66_SwaggerItemsPrimitive = unto (\(c, p) -> SwaggerItemsPrimitive c p)
69
67
70−-- ** 'ParamSchema' lenses
71−makeLensesWith swaggerFieldRules ''ParamSchema
72−-- ** 'Xml' lenses
73−makeFields ''Xml
74−-- ** 'Responses' lenses
75−makeLensesWith swaggerFieldRules ''Responses
76−-- ** 'Response' lenses
77−makeFields ''Response
78−-- ** 'SecurityScheme' lenses
79−makeLensesWith swaggerFieldRules ''SecurityScheme
80−-- ** 'SecuritySchemeType' prisms
81−makePrisms ''SecuritySchemeType
82−-- ** 'ApiKeyParams' lenses
83−makeFields ''ApiKeyParams
84−-- ** 'OAuth2Params' lenses
85−makeFields ''OAuth2Params
86−-- ** 'ExternalDocs' lenses
87−makeFields ''ExternalDocs
88−
89-- =============================================================
68-- =============================================================
90-- More helpful instances for easier access to schema properties
69-- More helpful instances for easier access to schema properties
91
70