diff --git a/primer-service/src/Primer/OpenAPI.hs b/primer-service/src/Primer/OpenAPI.hs index 58c001a67..26359c723 100644 --- a/primer-service/src/Primer/OpenAPI.hs +++ b/primer-service/src/Primer/OpenAPI.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE StandaloneDeriving #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module Primer.OpenAPI ( @@ -6,9 +7,10 @@ module Primer.OpenAPI ( ) where import Data.OpenApi (ToSchema) +import Data.Text (Text) import Primer.API (APIDef, APIProg, Tree) import Primer.App (InitialApp) -import Primer.Core (ID) +import Primer.Core (ID (..)) import Primer.Database (Session, SessionName) import Primer.Name (Name) @@ -22,8 +24,14 @@ import Primer.Name (Name) instance ToSchema SessionName instance ToSchema Session instance ToSchema InitialApp -instance ToSchema ID -instance ToSchema Name + +-- We need to GND the ID instance to matche its To/FromJSON instances +deriving newtype instance ToSchema ID + +-- We can't GND derive for Name as it is an opaque class +-- But the JSON instance is done by GND, so we must match here... +-- This instance works because the parameter has a phantom role! +deriving via Text instance (ToSchema Name) instance ToSchema Tree instance ToSchema APIDef instance ToSchema APIProg diff --git a/primer/src/Primer/API.hs b/primer/src/Primer/API.hs index 18987f76d..fb464a069 100644 --- a/primer/src/Primer/API.hs +++ b/primer/src/Primer/API.hs @@ -277,7 +277,7 @@ getProgram sid = withSession' sid $ QueryApp $ viewProg . handleGetProgramReques -- just enough information to render nicely. -- (NB: currently this is just a first draft, and is expected to evolve.) data Tree = Tree - { nodeId :: ID -- REVIEW: here, and in APIDef we maybe want a raw Int, as IDs serialise as '{unID: 5}' + { nodeId :: ID , label :: Text , children :: [Tree] } diff --git a/primer/src/Primer/Core.hs b/primer/src/Primer/Core.hs index 6e7859dcc..40e8f0a39 100644 --- a/primer/src/Primer/Core.hs +++ b/primer/src/Primer/Core.hs @@ -64,7 +64,7 @@ newtype ID = ID {unID :: Int} -- The Ord and Enum instances are useful for tests but we may remove them in -- future, so don't use them in app code. deriving newtype (Show, Num, Ord, Enum) - deriving (FromJSON, ToJSON) via VJSON ID + deriving newtype (FromJSON, ToJSON) instance ToJSONKey ID diff --git a/primer/src/Primer/Name.hs b/primer/src/Primer/Name.hs index 1c2ec2804..654a73af5 100644 --- a/primer/src/Primer/Name.hs +++ b/primer/src/Primer/Name.hs @@ -23,7 +23,7 @@ import Primer.JSON newtype Name = Name {unName :: Text} deriving (Eq, Ord, Generic, Data) deriving newtype (Show, IsString) - deriving (FromJSON, ToJSON) via VJSON Name + deriving newtype (FromJSON, ToJSON) -- | Construct a name from a Text. This is called unsafe because there are no -- guarantees about whether the name refers to anything that is in scope.