Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: ToSchema + deriving-aeson #607

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions primer-service/primer-service.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ library
build-depends:
, aeson >=2.0 && <=2.1
, base >=4.12 && <=4.17
, deriving-aeson >=0.2 && <=0.3
, exceptions >=0.10.4 && <=0.11
, http-media >=0.8 && <=0.9
, mtl >=2.2.2 && <=2.3
Expand Down
34 changes: 28 additions & 6 deletions primer-service/src/Primer/OpenAPI.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Primer.OpenAPI (
-- * Orphan instances
-- $orphanInstances
) where

import Data.OpenApi (ToSchema)
import Data.Text (Text)
import Data.Typeable (Typeable)
import Data.OpenApi (SchemaOptions, fromAesonOptions)
import Data.OpenApi.Internal.Schema
import Deriving.Aeson (AesonOptions (aesonOptions))
import Foreword
import Primer.API (Def, Module, NodeBody, NodeFlavor, Prog, Tree)
import Primer.Core (GlobalName, ID (..), LVarName, ModuleName)
import Primer.Database (Session, SessionName)
import Primer.JSON (CustomJSON (..), PrimerJSON)
import Primer.Name (Name)

-- $orphanInstances
Expand All @@ -20,8 +23,24 @@ import Primer.Name (Name)
-- build primer with ghcjs, because openapi3 transitively depends on network,
-- which ghcjs currently cannot build.

instance ToSchema SessionName
instance ToSchema Session
-- TODO: this compiles, but is it correct?

-- Suitable for deriving via, when ToJSON is via the custom json
-- instance (AesonOptions os, ToSchema a, Typeable os, Typeable ks) => ToSchema (CustomJSON (os :: ks) a) where
-- declareNamedSchema = genericDeclareNamedSchemaNewtype opts (declareNamedSchema @a)
instance
(Typeable a, Generic a, GToSchema (Rep a), Typeable os, Typeable ks, AesonOptions os) =>
ToSchema (CustomJSON (os :: ks) a)
where
declareNamedSchema _ = genericDeclareNamedSchema opts (Proxy @a)
where
opts :: SchemaOptions
opts = fromAesonOptions (aesonOptions @os)

-- Technically should GND this, but cannot because of exports...
-- instance ToSchema SessionName
deriving via Text instance ToSchema SessionName -- see comments on instance ToSchema Name
deriving via PrimerJSON Session instance ToSchema Session

-- We need to GND the ID instance to match its To/FromJSON instances
deriving newtype instance ToSchema ID
Expand All @@ -34,11 +53,14 @@ deriving via Text instance (ToSchema Name)
-- For GlobalName and LVarName, we must derive ToSchema via Name,
-- as that is how the To/FromJSON instances are derived
deriving via Name instance Typeable k => ToSchema (GlobalName k)
--instance Typeable k => ToSchema (GlobalName k) -- actually, just plain ToJSON here..., but is invalid openapi!
deriving via Name instance (ToSchema LVarName)

-- These are just plain ToJSON
instance ToSchema Tree
instance ToSchema NodeBody
instance ToSchema NodeFlavor
instance ToSchema Def
instance ToSchema ModuleName
deriving via NonEmpty Name instance ToSchema ModuleName
instance ToSchema Module
instance ToSchema Prog
2 changes: 1 addition & 1 deletion primer/primer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ library
Primer.Eval
Primer.EvalFull
Primer.Examples
Primer.JSON
Primer.Module
Primer.Name
Primer.Name.Fresh
Expand All @@ -45,7 +46,6 @@ library
hs-source-dirs: src
other-modules:
Control.Monad.NestedError
Primer.JSON

default-language: GHC2021
default-extensions:
Expand Down