HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleswagger2-2.8.9Haskell2010

Data.Swagger.Optics

Lenses and prisms for the optics library.

Example4 expressions
import Data.Aesonimport qualified Data.HashMap.Strict.InsOrd as IOHMimport Optics.Core:set -XOverloadedLabels

Example from the Data.Swagger module using optics:

Example1 expression
:{encode $ (mempty :: Swagger)  & #definitions .~ IOHM.fromList [ ("User", mempty & #type ?~ SwaggerString) ]  & #paths .~ IOHM.fromList    [ ("/user", mempty & #get ?~ (mempty        & #produces ?~ MimeList ["application/json"]        & at 200 ?~ ("OK" & #_Inline % #schema ?~ Ref (Reference "User"))        & at 404 ?~ "User info not found")) ]:}"{\"swagger\":\"2.0\",\"info\":{\"title\":\"\",\"version\":\"\"},\"paths\":{\"/user\":{\"get\":{\"produces\":[\"application/json\"],\"responses\":{\"200\":{\"description\":\"OK\",\"schema\":{\"$ref\":\"#/definitions/User\"}},\"404\":{\"description\":\"User info not found\"}}}}},\"definitions\":{\"User\":{\"type\":\"string\"}}}"

For convenience optics are defined as labels. It means that field accessor names can be overloaded for different types. One such common field is #description. Many components of a Swagger specification can have descriptions, and you can use the same name for them:

Example2 expressions
encode $ (mempty :: Response) & #description .~ "No content""{\"description\":\"No content\"}":{encode $ (mempty :: Schema)  & #type        ?~ SwaggerBoolean  & #description ?~ "To be or not to be":}"{\"description\":\"To be or not to be\",\"type\":\"boolean\"}"

ParamSchema is basically the base schema specification and many types contain it. So for convenience, all ParamSchema fields are transitively made fields of the type that has it. For example, you can use #type to access SwaggerType of Header schema without having to use #paramSchema:

Example1 expression
encode $ (mempty :: Header) & #type ?~ SwaggerNumber"{\"type\":\"number\"}"

Additionally, to simplify working with Response, both Operation and Responses have direct access to it via Optics.Core.At.at. Example:

Example1 expression
:{encode $ (mempty :: Operation)  & at 404 ?~ "Not found":}"{\"responses\":{\"404\":{\"description\":\"Not found\"}}}"
    • Packageswagger2-2.8.9
    • Exports0
    • LanguageHaskell2010
    • LicenceBSD-3-Clause
    • SourceOptics.hs

    Orphan instances

    186 instances