| Copyright | (c) 2026 The Polytropos Authors |
|---|---|
| License | EUPL-1.2 OR Apache-2.0 |
| Maintainer | nadia.yvette.chambers@ik.me |
| Stability | experimental |
| Portability | non-portable (GHC extensions) |
| Safe Haskell | None |
| Language | GHC2021 |
Polytropos.OpenApi
Description
Generates OpenAPI 3.0 schema specifications directly from polytropos
open records and variants.
Architectural Overview & Rationale
Schema Shape
- A record declares an anonymous object schema whose
propertiescarry one entry per field (reified from the row symbols) and whoserequiredlist contains every field: a row-typed record is total, so all fields are required. - A variant declares an anonymous
oneOfschema with one entry per constructor, each a single-member tagged object{"ctor": fields}— the same wire shape Polytropos.Aeson uses, so REST APIs documented by these schemas round-trip seamlessly.
Eliminating Generic Schema Blow-up
Standard ToSchema deriving walks a GHC.Generics tree, which causes severe
slowdowns on large schema models (50+ fields). polytropos-openapi3 synthesizes
the schema by directly traversing the row type—one Declare step per field—with
zero generic overhead.
When to Use This Module
- When documenting REST APIs, Swagger / OpenAPI endpoints that produce or consume
polytroposrecords and variants.
Worked Example
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_GHC -fplugin=Polytropos.Plugin #-}
import Data.OpenApi (toSchema, Schema)
import Data.Proxy (Proxy (..))
import Polytropos
import Polytropos.OpenApi
type User = Record '["id" ':= Int, "name" ':= String]
-- A conforming record value:
userVal :: User
userVal = REC { id = 1, name = "Alice" }
-- Generated OpenAPI schema with properties and required list in row order:
userSchema :: Schema
userSchema = toSchema (Proxy @User)Synopsis
- class AllToSchema (r :: Row) where
- fieldSchemas :: Proxy r -> Declare (Definitions Schema) [(Text, Referenced Schema)]
- class AllCtorsToSchema (s :: SumRow) where
- ctorSchemas :: Proxy s -> Declare (Definitions Schema) [Schema]
Row Schema Walkers
class AllToSchema (r :: Row) where Source #
One property entry per field of the row, in row order.
Methods
fieldSchemas :: Proxy r -> Declare (Definitions Schema) [(Text, Referenced Schema)] Source #
Instances
| AllToSchema ('[] :: [Pair Symbol Type]) Source # | |
Defined in Polytropos.OpenApi Methods fieldSchemas :: Proxy ('[] :: [Pair Symbol Type]) -> Declare (Definitions Schema) [(Text, Referenced Schema)] Source # | |
| (KnownSymbol n, ToSchema a, AllToSchema r) => AllToSchema ((n ':= a) ': r) Source # | |
Defined in Polytropos.OpenApi Methods fieldSchemas :: Proxy ((n ':= a) ': r) -> Declare (Definitions Schema) [(Text, Referenced Schema)] Source # | |
class AllCtorsToSchema (s :: SumRow) where Source #
One tagged object schema per constructor of the sum row.
Methods
ctorSchemas :: Proxy s -> Declare (Definitions Schema) [Schema] Source #
Instances
| AllCtorsToSchema ('[] :: [Pair Symbol Row]) Source # | |
Defined in Polytropos.OpenApi Methods ctorSchemas :: Proxy ('[] :: [Pair Symbol Row]) -> Declare (Definitions Schema) [Schema] Source # | |
| (KnownSymbol c, AllToSchema fs, AllCtorsToSchema s) => AllCtorsToSchema ((c ':= fs) ': s) Source # | |
Defined in Polytropos.OpenApi Methods ctorSchemas :: Proxy ((c ':= fs) ': s) -> Declare (Definitions Schema) [Schema] Source # | |
Orphan instances
| (Typeable r, AllToSchema r) => ToSchema (Record r) Source # | |
Methods declareNamedSchema :: Proxy (Record r) -> Declare (Definitions Schema) NamedSchema # | |
| (Typeable s, AllCtorsToSchema s) => ToSchema (Variant s) Source # | |
Methods declareNamedSchema :: Proxy (Variant s) -> Declare (Definitions Schema) NamedSchema # | |