polytropos-openapi3
Copyright(c) 2026 The Polytropos Authors
LicenseEUPL-1.2 OR Apache-2.0
Maintainernadia.yvette.chambers@ik.me
Stabilityexperimental
Portabilitynon-portable (GHC extensions)
Safe HaskellNone
LanguageGHC2021

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 properties carry one entry per field (reified from the row symbols) and whose required list contains every field: a row-typed record is total, so all fields are required.
  • A variant declares an anonymous oneOf schema 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 polytropos records 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

Row Schema Walkers

class AllToSchema (r :: Row) where Source #

One property entry per field of the row, in row order.

Instances

Instances details
AllToSchema ('[] :: [Pair Symbol Type]) Source # 
Instance details

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 # 
Instance details

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.

Instances

Instances details
AllCtorsToSchema ('[] :: [Pair Symbol Row]) Source # 
Instance details

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 # 
Instance details

Defined in Polytropos.OpenApi

Methods

ctorSchemas :: Proxy ((c ':= fs) ': s) -> Declare (Definitions Schema) [Schema] Source #

Orphan instances