servant-openapi-hs: Generate an OpenAPI 3.1 specification for your servant API.

[ bsd3, library, openapi, program, servant, web ] [ Propose Tags ] [ Report a vulnerability ]

OpenAPI is a language-agnostic format for describing and documenting HTTP APIs in JSON or YAML. This library generates an OpenAPI 3.1 specification from a Servant API and can partially test whether an API conforms with its specification. . A generated specification can be used for many things, such as . * displaying interactive documentation in any OpenAPI 3.1 viewer; . * generating clients and servers in many languages using OpenAPI Generator; . * and many others across the OpenAPI tooling ecosystem. . This package is a fork of servant-openapi3 that targets OpenAPI 3.1 via openapi-hs.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 4.0.0
Change log CHANGELOG.md
Dependencies aeson (>=2.0.1.0 && <2.3), aeson-pretty (>=0.8.7 && <0.9), base (>=4.21 && <4.23), base-compat (>=0.10.5 && <0.15), bytestring (>=0.10.8.1 && <0.13), hspec (>=2.6.0 && <2.12), http-media (>=0.7.1.3 && <0.9), insert-ordered-containers (>=0.2.1.0 && <0.3), lens (>=4.17 && <5.4), openapi-hs (>=4.0 && <5), QuickCheck (>=2.9 && <2.17), servant (>=0.17 && <0.21), servant-openapi-hs, singleton-bool (>=0.1.4 && <0.2), text (>=1.2.3.0 && <3), unordered-containers (>=0.2.9.0 && <0.3) [details]
Tested with ghc ==9.12.4 || ==9.14.1
License BSD-3-Clause
Copyright (c) 2015-2020, Servant contributors
Author David Johnson, Nickolay Kudasov, Maxim Koltsov
Maintainer nadeem@gmail.com
Uploaded by shinzui at 2026-06-25T14:42:17Z
Category Web, Servant, OpenApi
Home page https://github.com/shinzui/servant-openapi-hs
Bug tracker https://github.com/shinzui/servant-openapi-hs/issues
Source repo head: git clone https://github.com/shinzui/servant-openapi-hs.git
Distributions
Executables gen-openapi
Downloads 0 total (0 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for servant-openapi-hs-4.0.0

[back to package description]

servant-openapi-hs

License BSD-3-Clause

Generate an OpenAPI 3.1 specification for your Servant API, and partially test that the API conforms to its specification.

Fork notice. servant-openapi-hs is a fork of biocad/servant-openapi3, which targets OpenAPI 3.0. This fork retargets it at OpenAPI 3.1 by building on openapi-hs (a fork of biocad/openapi3) instead of openapi3. The Haskell module namespace is unchanged (Servant.OpenApi.*), and because openapi-hs keeps the Data.OpenApi.* namespace, migrating is usually just a dependency-name swap: servant-openapi3servant-openapi-hs and openapi3openapi-hs. The fork keeps the upstream BSD-3-Clause license and copyright.


Motivation

OpenAPI is a language-agnostic format for describing and documenting HTTP APIs. This package derives an OpenAPI 3.1 specification directly from a Servant API type, so the description stays in sync with the server, and provides combinators to test that handlers conform to the generated schema.

A generated specification can then be used to

  • display interactive documentation in any OpenAPI 3.1 viewer;
  • generate clients and servers in many languages with OpenAPI Generator;
  • and many other things across the OpenAPI tooling ecosystem.

Installation

Pre-release. The first Hackage release is still in preparation. Until it is published, depend on this repository directly (see Building from source below); the instructions in this section describe the package once it is on Hackage.

Add servant-openapi-hs to your package's build-depends:

build-depends: servant-openapi-hs

Its OpenAPI 3.1 data model comes from openapi-hs, which is pulled in automatically as a transitive dependency.

Import the umbrella module:

import Servant.OpenApi

Requires GHC 9.12.4 or 9.14.1.

Building from source. Until the first Hackage release, depend on this repository directly by adding a source-repository-package stanza for servant-openapi-hs to your cabal.project:

source-repository-package
    type:     git
    location: https://github.com/shinzui/servant-openapi-hs.git
    tag:      <commit-sha>

Its openapi-hs dependency resolves from Hackage automatically.

Usage

Derive an OpenAPI 3.1 document from a Servant API type with toOpenApi:

import Data.Aeson (encode)
import Data.OpenApi (OpenApi)
import Data.Proxy (Proxy (..))
import Servant.OpenApi (toOpenApi)

spec :: OpenApi
spec = toOpenApi (Proxy :: Proxy MyApi)
-- encode spec  ==>  {"openapi":"3.1.0", ...}

A runnable example lives in app/GenOpenApi.hs, built as the gen-openapi executable, which prints a complete Todo-CRUD document:

cabal run gen-openapi > openapi.json

The full API surface is unchanged from upstream; see the Haddock documentation. Generated specifications can be explored interactively in any OpenAPI 3.1 viewer or editor.

Validation

Generated documents are checked at three levels:

  1. Round-trip — the test suite decodes each generated document back through openapi-hs's FromJSON OpenApi, which rejects any openapi version outside 3.1.0 … 3.1.1, then compares the result for semantic equality.

  2. Example-conformanceServant.OpenApi.Test.validateEveryToJSON checks that random values of each response type validate against the generated schema.

  3. Authoritative conformance — the gen-openapi output lints cleanly under vacuum:

    cabal run gen-openapi > openapi.json
    nix run nixpkgs#vacuum-go -- lint -d openapi.json
    

Contributing

Bug reports, fixes, documentation improvements, and other contributions are welcome. Please open an issue or pull request on the GitHub issue tracker.

License

servant-openapi-hs retains the original BSD-3-Clause license of the upstream servant-openapi3 project, including its copyright. See the LICENSE file for the full text; this fork's changes are released under the same terms.


Originally derived from work by the Servant contributors (David Johnson, Nickolay Kudasov, Maxim Koltsov, and others).