Skip to content

Commit 4bc2cb6

Browse files
committed
feat: ToSchema instance for CustomJSON
1 parent 78eb693 commit 4bc2cb6

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

primer-service/primer-service.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ library
3434
build-depends:
3535
, aeson >=2.0 && <=2.1
3636
, base >=4.12 && <=4.17
37+
, deriving-aeson >=0.2 && <=0.3
3738
, exceptions >=0.10.4 && <=0.11
3839
, http-media >=0.8 && <=0.9
3940
, mtl >=2.2.2 && <=2.3

primer-service/src/Primer/OpenAPI.hs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
{-# LANGUAGE UndecidableInstances #-}
12
{-# OPTIONS_GHC -fno-warn-orphans #-}
23

34
module Primer.OpenAPI (
45
-- * Orphan instances
56
-- $orphanInstances
67
) where
78

8-
import Data.OpenApi (ToSchema)
9-
import Data.Text (Text)
10-
import Data.Typeable (Typeable)
9+
import Data.OpenApi (fromAesonOptions)
10+
import Data.OpenApi.Internal.Schema
11+
import Deriving.Aeson (AesonOptions (aesonOptions))
12+
import Foreword
1113
import Primer.API (Def, Module, NodeBody, NodeFlavor, Prog, Tree)
1214
import Primer.Core (GlobalName, ID (..), LVarName, ModuleName)
1315
import Primer.Database (Session, SessionName)
16+
import Primer.JSON (CustomJSON)
1417
import Primer.Name (Name)
1518

1619
-- $orphanInstances
@@ -20,6 +23,13 @@ import Primer.Name (Name)
2023
-- build primer with ghcjs, because openapi3 transitively depends on network,
2124
-- which ghcjs currently cannot build.
2225

26+
-- Suitable for deriving via, when the ToJSON instance is via PrimerJSON
27+
instance
28+
(Typeable a, Generic a, GToSchema (Rep a), Typeable os, Typeable ks, AesonOptions os) =>
29+
ToSchema (CustomJSON (os :: ks) a)
30+
where
31+
declareNamedSchema _ = genericDeclareNamedSchema (fromAesonOptions (aesonOptions @os)) (Proxy @a)
32+
2333
instance ToSchema SessionName
2434
instance ToSchema Session
2535

primer/primer.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ library
2929
Primer.Eval
3030
Primer.EvalFull
3131
Primer.Examples
32+
Primer.JSON
3233
Primer.Module
3334
Primer.Name
3435
Primer.Name.Fresh
@@ -45,7 +46,6 @@ library
4546
hs-source-dirs: src
4647
other-modules:
4748
Control.Monad.NestedError
48-
Primer.JSON
4949

5050
default-language: GHC2021
5151
default-extensions:

0 commit comments

Comments
 (0)