2-- Swagger™ is a project used to describe and document RESTful APIs.
4-- The Swagger specification defines a set of files required to describe such an API.
5-- These files can then be used by the Swagger-UI project to display the API
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.
9 -- * How to use this library
12 -- ** @'Monoid'@ instances
15 -- ** Lenses and prisms
18 -- ** Schema specification
22 module Data.Swagger.Lens,
23 module Data.Swagger.ParamSchema,
24 module Data.Swagger.Schema,
26 -- * Swagger specification
44 -- ** Types and formats
74 SecuritySchemeType(..),
75 SecurityRequirement(..),
87 -- ** External documentation
99import Data.Swagger.Lens
100import Data.Swagger.ParamSchema
101import Data.Swagger.Schema
103import Data.Swagger.Internal
106-- >>> import Control.Lens
107-- >>> import Data.Aeson
108-- >>> import Data.Monoid
109-- >>> import Data.Proxy
110-- >>> import GHC.Generics
111-- >>> :set -XDeriveGeneric
112-- >>> :set -XOverloadedStrings
113-- >>> :set -XOverloadedLists
114-- >>> :set -fno-warn-missing-methods
118-- This section explains how to use this library to work with Swagger specification.
122-- Virtually all types representing Swagger specification have @'Monoid'@ instances.
123-- The @'Monoid'@ type class provides two methods — @'mempty'@ and @'mappend'@.
125-- In this library you can use @'mempty'@ for a default/empty value. For instance:
127-- >>> encode (mempty :: Swagger)
128-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"}}"
130-- As you can see some spec properties (e.g. @"version"@) are there even when the spec is empty.
131-- That is because these properties are actually required ones.
133-- You /should/ always override the default (empty) value for these properties,
134-- although it is not strictly necessary:
136-- >>> encode mempty { _infoTitle = "Todo API", _infoVersion = "1.0" }
137-- "{\"version\":\"1.0\",\"title\":\"Todo API\"}"
139-- You can merge two values using @'mappend'@ or its infix version @('<>')@:
141-- >>> encode $ mempty { _infoTitle = "Todo API" } <> mempty { _infoVersion = "1.0" }
142-- "{\"version\":\"1.0\",\"title\":\"Todo API\"}"
144-- This can be useful for combining specifications of endpoints into a whole API specification:
147-- \-\- /account subAPI specification
148-- accountAPI :: Swagger
150-- \-\- /task subAPI specification
153-- \-\- while API specification is just a combination
154-- \-\- of subAPIs' specifications
156-- api = accountAPI <> taskAPI
161-- Since @'Swagger'@ has a fairly complex structure, lenses and prisms are used
162-- to work comfortly with it. In combination with @'Monoid'@ instances, lenses
163-- make it fairly simple to construct/modify any part of the specification:
166-- encode $ (mempty :: Swagger)
167-- & definitions .~ [ ("User", mempty & type_ .~ SwaggerString) ]
169-- [ ("/user", mempty & get ?~ (mempty
170-- & produces ?~ MimeList ["application/json"]
171-- & at 200 ?~ ("OK" & _Inline.schema ?~ Ref (Reference "User"))
172-- & at 404 ?~ "User info not found")) ]
174-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"definitions\":{\"User\":{\"type\":\"string\"}},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"404\":{\"description\":\"User info not found\"},\"200\":{\"schema\":{\"$ref\":\"#/definitions/User\"},\"description\":\"OK\"}},\"produces\":[\"application/json\"]}}}}"
176-- In the snippet above we declare API with a single path @/user@ providing method @GET@
177-- which produces @application/json@ output and should respond with code @200@ and body specified
178-- by schema @User@ which is defined in @'definitions'@ property of swagger specification.
180-- For convenience, @swagger2@ uses /classy field lenses/. It means that
181-- field accessor names can be overloaded for different types. One such
182-- common field is @'description'@. Many components of Swagger specification
183-- can have descriptions, and you can use the same name for them:
185-- >>> encode $ (mempty :: Response) & description .~ "No content"
186-- "{\"description\":\"No content\"}"
188-- encode $ (mempty :: Schema)
189-- & type_ .~ SwaggerBoolean
190-- & description ?~ "To be or not to be"
192-- "{\"type\":\"boolean\",\"description\":\"To be or not to be\"}"
194-- @'ParamSchema'@ is basically the /base schema specification/ and many types contain it (see @'HasParamSchema'@).
195-- So for convenience, all @'ParamSchema'@ fields are transitively made fields of the type that has it.
196-- For example, you can use @'type_'@ to access @'SwaggerType'@ of @'Header'@ schema without having to use @'paramSchema'@:
198-- >>> encode $ (mempty :: Header) & type_ .~ SwaggerNumber
199-- "{\"type\":\"number\"}"
201-- Additionally, to simplify working with @'Response'@, both @'Operation'@ and @'Responses'@
202-- have direct access to it via @'at' code@. Example:
205-- encode $ (mempty :: Operation)
206-- & at 404 ?~ "Not found"
208-- "{\"responses\":{\"404\":{\"description\":\"Not found\"}}}"
210-- You might've noticed that @'type_'@ has an extra underscore in its name
211-- compared to, say, @'description'@ field accessor.
212-- This is because @type@ is a keyword in Haskell.
213-- A few other field accessors are modified in this way:
215-- - @'in_'@, @'type_'@, @'default_'@ (as keywords);
216-- - @'maximum_'@ and @'minimum_'@ (as conflicting with @Prelude@);
217-- - @'enum_'@ (as conflicting with @Control.Lens@).
221-- @'ParamSchema'@ and @'Schema'@ are the two core types for data model specification.
223-- @'ParamSchema' t@ specifies all the common properties, available for every data schema.
224-- The @t@ parameter imposes some restrictions on @type@ and @items@ properties (see @'SwaggerType'@ and @'SwaggerItems'@).
226-- @'Schema'@ is used for request and response bodies and allows specifying objects
227-- with properties in addition to what @'ParamSchema'@ provides.
229-- In most cases you will have a Haskell data type for which you would like to
230-- define a corresponding schema. To facilitate this use case
231-- @swagger2@ provides two classes for schema encoding.
232-- Both these classes provide means to encode /types/ as Swagger /schemas/.
234-- @'ToParamSchema'@ is intended to be used for primitive API endpoint parameters,
235-- such as query parameters, headers and URL path pieces.
236-- Its corresponding value-encoding class is @'ToHttpApiData'@ (from @http-api-data@ package).
238-- @'ToSchema'@ is used for request and response bodies and mostly differ from
239-- primitive parameters by allowing objects/mappings in addition to primitive types and arrays.
240-- Its corresponding value-encoding class is @'ToJSON'@ (from @aeson@ package).
242-- While lenses and prisms make it easy to define schemas, it might be that you don't need to:
243-- @'ToSchema'@ and @'ToParamSchema'@ classes both have default @'Generic'@-based implementations!
245-- @'ToSchema'@ default implementation is also aligned with @'ToJSON'@ default implementation with
246-- the only difference being for sum encoding. @'ToJSON'@ defaults sum encoding to @'defaultTaggedObject'@,
247-- while @'ToSchema'@ defaults to something which corresponds to @'ObjectWithSingleField'@. This is due to
248-- @'defaultTaggedObject'@ behavior being hard to specify in Swagger.
250-- Here's an example showing @'ToJSON'@–@'ToSchema'@ correspondance:
252-- >>> data Person = Person { name :: String, age :: Integer } deriving Generic
253-- >>> instance ToJSON Person
254-- >>> instance ToSchema Person
255-- >>> encode (Person "David" 28)
256-- "{\"age\":28,\"name\":\"David\"}"
257-- >>> encode $ toSchema (Proxy :: Proxy Person)
258-- "{\"required\":[\"name\",\"age\"],\"type\":\"object\",\"properties\":{\"age\":{\"type\":\"integer\"},\"name\":{\"type\":\"string\"}}}"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list