|
| 1 | +{-# LANGUAGE DerivingStrategies #-} |
| 2 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 3 | + |
| 4 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 5 | +module Web.Template.Servant.Swagger |
| 6 | + ( WithDescription(..) |
| 7 | + , AsEnum(..) |
| 8 | + ) where |
| 9 | + |
| 10 | +import Control.Lens (_Just, (%~), (&), (?~)) |
| 11 | +import Data.Aeson (FromJSON, ToJSON, Value (..)) |
| 12 | +import Data.OpenApi (ToSchema (..), defaultSchemaOptions, description, enum_, |
| 13 | + genericDeclareNamedSchema, schema) |
| 14 | +import Data.OpenApi.Internal.Schema (GToSchema, rename) |
| 15 | +import Data.Override (Override) |
| 16 | +import Data.Override.Aeson () |
| 17 | +import Data.Override.Internal (Overridden, Using) |
| 18 | +import Data.Proxy (Proxy (..)) |
| 19 | +import Data.Text (Text, pack) |
| 20 | +import Data.Typeable (Typeable) |
| 21 | +import GHC.Generics (Generic (..)) |
| 22 | +import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) |
| 23 | +import Type.Reflection (typeRep) |
| 24 | + |
| 25 | +-- | Add a description to any field in a record. |
| 26 | +-- |
| 27 | +-- Intended to be used with 'Data.Override.Override' from @generic-override@ package like this: |
| 28 | +-- |
| 29 | +-- @ |
| 30 | +-- data Foo = |
| 31 | +-- { fooSomeField :: Text |
| 32 | +-- , fooOtherField :: Int |
| 33 | +-- } |
| 34 | +-- deriving (Generic) |
| 35 | +-- deriving (ToJSON, FromJSON, ToSchema) via |
| 36 | +-- 'Web.Template.Servant.Aeson.CamelCaseAeson' ('Data.Override.Override' Foo |
| 37 | +-- '[ "fooSomeField" `'Data.Override.With'` 'WithDescription' "Some text describing a Foo" |
| 38 | +-- , "fooOtherField" `'Data.Override.With'` 'WithDescription' "A number of Foos" |
| 39 | +-- ]) |
| 40 | +-- @ |
| 41 | +-- |
| 42 | +-- For techincal reasons, @CamelCaseAeson@ must be on the outer layer. |
| 43 | +newtype WithDescription (descr :: Symbol) a |
| 44 | + = WithDescription a |
| 45 | + deriving (Eq, Show) |
| 46 | + deriving newtype (ToJSON, FromJSON) |
| 47 | + |
| 48 | +instance (Typeable a, KnownSymbol descr, ToSchema a) => ToSchema (WithDescription descr a) where |
| 49 | + declareNamedSchema _ = do |
| 50 | + sch <- declareNamedSchema @a Proxy |
| 51 | + return $ sch & schema . description ?~ pack (symbolVal @descr Proxy) |
| 52 | + |
| 53 | +-- | Describe possible enumeration values for a 'Text' field. |
| 54 | +-- |
| 55 | +-- To be used with 'Data.Override.Override': |
| 56 | +-- |
| 57 | +-- @ |
| 58 | +-- data Foo = |
| 59 | +-- { fooColor :: Text |
| 60 | +-- } |
| 61 | +-- deriving (Generic) |
| 62 | +-- deriving (ToJSON, FromJSON, ToSchema) via |
| 63 | +-- 'Web.Template.Servant.Aeson.CamelCaseAeson' ('Data.Override.Override' Foo |
| 64 | +-- '[ "fooColor" `'Data.Override.As`' 'AsEnum' '["red", "black", "white"] |
| 65 | +-- ]) |
| 66 | +-- @ |
| 67 | +newtype AsEnum (vals :: [Symbol]) |
| 68 | + = AsEnum Text |
| 69 | + deriving (Eq, Show) |
| 70 | + deriving newtype (ToJSON, FromJSON) |
| 71 | + |
| 72 | +instance ToSchema (AsEnum '[]) where |
| 73 | + declareNamedSchema _ = do |
| 74 | + sch <- declareNamedSchema @Text Proxy |
| 75 | + return $ sch & schema . enum_ ?~ [] |
| 76 | + |
| 77 | +instance (KnownSymbol val, Typeable vals, ToSchema (AsEnum vals)) => ToSchema (AsEnum (val ': vals)) where |
| 78 | + declareNamedSchema _ = do |
| 79 | + sch <- declareNamedSchema @(AsEnum vals) Proxy |
| 80 | + return $ sch & schema . enum_ . _Just %~ (String (pack $ symbolVal @val Proxy) :) |
| 81 | + |
| 82 | +instance |
| 83 | + ( Typeable a |
| 84 | + , Typeable xs |
| 85 | + , Generic (Override a xs) |
| 86 | + , GToSchema (Rep (Override a xs)) |
| 87 | + ) => ToSchema (Override a xs) where |
| 88 | + declareNamedSchema p = |
| 89 | + -- Prevent "Override" from showing up in schema name. |
| 90 | + rename (Just $ pack $ show $ typeRep @a) <$> genericDeclareNamedSchema defaultSchemaOptions p |
| 91 | + |
| 92 | +instance |
| 93 | + ( Typeable a |
| 94 | + , Typeable xs |
| 95 | + , Typeable ms |
| 96 | + , ToSchema (Using ms a xs) |
| 97 | + ) => ToSchema (Overridden ms a xs) where |
| 98 | + declareNamedSchema _ = declareNamedSchema @(Using ms a xs) Proxy |
0 commit comments