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 modify this structure. In combination with @'Monoid'@ instances, lenses
163-- also make it fairly simple to construct/modify any part of the specification:
166-- encode $ (mempty :: Swagger) & paths .~
167-- [ ("/user", mempty & get ?~ (mempty
168-- & produces ?~ MimeList ["application/json"]
169-- & responses .~ (mempty
170-- & responses . at 200 ?~ Inline (mempty & schema ?~ Ref (Reference "#/definitions/User")))))]
172-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"200\":{\"schema\":{\"$ref\":\"#/definitions/#/definitions/User\"},\"description\":\"\"}},\"produces\":[\"application/json\"]}}}}"
174-- In the snippet above we declare API paths with a single path @/user@ providing method @GET@
175-- which produces @application/json@ output and should respond with code @200@ and body specified
176-- by schema @User@ (which should be defined in @definitions@ property of swagger specification).
178-- Since @'ParamSchema'@ is basically the /base schema specification/, a special
179-- @'HasParamSchema'@ class has been introduced to generalize @'ParamSchema'@ lenses
180-- and allow them to be used by any type that has a @'ParamSchema'@:
183-- encode $ (mempty :: Schema)
185-- & type_ .~ SwaggerString
186-- & format ?~ "email"
188-- "{\"format\":\"email\",\"title\":\"Email\",\"type\":\"string\"}"
192-- @'ParamSchema'@ and @'Schema'@ are the two core types for data model specification.
194-- @'ParamSchema' t@ specifies all the common properties, available for every data schema.
195-- The @t@ parameter imposes some restrictions on @type@ and @items@ properties (see @'SwaggerType'@ and @'SwaggerItems'@).
197-- @'Schema'@ is used for request and response bodies and allows specifying objects
198-- with properties in addition to what @'ParamSchema'@ provides.
200-- In most cases you will have a Haskell data type for which you would like to
201-- define a corresponding schema. To facilitate thise use case
202-- this library provides two classes for schema encoding.
203-- Both these classes provide means to encode /types/ as Swagger /schemas/.
205-- @'ToParamSchema'@ is intended to be used for primitive API endpoint parameters,
206-- such as query parameters, headers and URL path pieces.
207-- Its corresponding value-encoding class is @'ToHttpApiData'@ (from @http-api-data@ package).
209-- @'ToSchema'@ is used for request and response bodies and mostly differ from
210-- primitive parameters by allowing objects/mappings in addition to primitive types and arrays.
211-- Its corresponding value-encoding class is @'ToJSON'@ (from @aeson@ package).
213-- While lenses and prisms make it easy to define schemas, it might be that you don't need to:
214-- @'ToSchema'@ and @'ToParamSchema'@ classes both have default @'Generic'@-based implementations!
216-- @'ToSchema'@ default implementation is also aligned with @'ToJSON'@ default implementation with
217-- the only difference being for sum encoding. @'ToJSON'@ defaults sum encoding to @'defaultTaggedObject'@,
218-- while @'ToSchema'@ defaults to something which corresponds to @'ObjectWithSingleField'@. This is due to
219-- @'defaultTaggedObject'@ behavior being hard to specify in Swagger.
221-- Here's an example showing @'ToJSON'@–@'ToSchema'@ correspondance:
223-- >>> data Person = Person { name :: String, age :: Integer } deriving Generic
224-- >>> instance ToJSON Person
225-- >>> instance ToSchema Person
226-- >>> encode (Person "David" 28)
227-- "{\"age\":28,\"name\":\"David\"}"
228-- >>> encode $ toSchema (Proxy :: Proxy Person)
229-- "{\"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