HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleopenapi3-3.2.4Haskell2010

Data.OpenApi.Optics

Lenses and prisms for the optics library.

Example5 expressions
import Data.Aesonimport Optics.Core:set -XOverloadedLabelsimport qualified Data.ByteString.Lazy.Char8 as BSLimport qualified Data.HashMap.Strict.InsOrd as IOHM

Example from the Data.OpenApi module using optics:

Example1 expression
:{BSL.putStrLn $ encodePretty $ (mempty :: OpenApi)  & #components % #schemas .~ IOHM.fromList [ ("User", mempty & #type ?~ OpenApiString) ]  & #paths .~    IOHM.fromList [ ("/user", mempty & #get ?~ (mempty        & at 200 ?~ ("OK" & #_Inline % #content % at "application/json" ?~ (mempty & #schema ?~ Ref (Reference "User")))        & at 404 ?~ "User info not found")) ]:}{    "components": {        "schemas": {            "User": {                "type": "string"            }        }    },    "info": {        "title": "",        "version": ""    },    "openapi": "3.0.0",    "paths": {        "/user": {            "get": {                "responses": {                    "200": {                        "content": {                            "application/json": {                                "schema": {                                    "$ref": "#/components/schemas/User"                                }                            }                        },                        "description": "OK"                    },                    "404": {                        "description": "User info not found"                    }                }            }        }    }}

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
BSL.putStrLn $ encodePretty $ (mempty :: Response) & #description .~ "No content"{    "description": "No content"}:{BSL.putStrLn $ encodePretty $ (mempty :: Schema)  & #type        ?~ OpenApiBoolean  & #description ?~ "To be or not to be":}{    "description": "To be or not to be",    "type": "boolean"}

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

Example1 expression
:{BSL.putStrLn $ encodePretty $ (mempty :: Operation)  & at 404 ?~ "Not found":}{    "responses": {        "404": {            "description": "Not found"        }    }}
    • Packageopenapi3-3.2.4
    • Exports0
    • LanguageHaskell2010
    • LicenceBSD-3-Clause
    • SourceOptics.hs

    Orphan instances

    194 instances