git.iko.soy/deriving-openapi3
on master help sign in
Added description
commit29e7d7ec58aeda490ecfd9c0670fc469e7e8528d authoriko <ilyakooo0@gmail.com> dateSat 16 Jan 2021, 16:47 (6 years ago) parent86a9e5b
2 files changed+35−1
package.yaml+6 src/Deriving/OpenApi.hs+29−1
package.yaml+6
@@ -10,6 +10,9 @@ dependencies:
1010 - openapi3
1111 - deriving-aeson
1212 - aeson
13+ - servant
14+ - lens
15+ - text
1316library:
1417 source-dirs: src
1518default-extensions:
@@ -21,3 +24,6 @@ default-extensions:
2124 - FlexibleInstances
2225 - TypeApplications
2326 - UndecidableInstances
27+ - TypeFamilies
28+ - PolyKinds
29+ - OverloadedStrings
src/Deriving/OpenApi.hs+29−1
@@ -5,32 +5,60 @@ module Deriving.OpenApi
55 DatatypeNameModifier,
66 ToSchema,
77 module Deriving.Aeson,
8+ Extending,
89 )
910where
1011
12+import Control.Lens
1113import qualified Data.Aeson.Types as A
1214import Data.Kind
1315import Data.OpenApi
1416import Data.OpenApi.Internal.Schema
1517import Data.Proxy
18+import Data.Text (Text)
19+import qualified Data.Text as T
1620import Deriving.Aeson
1721import GHC.Generics
1822import GHC.TypeLits
23+import Servant.API
1924
2025type CustomOpenApi = CustomJSON
2126
2227instance (OpenApiOptions xs, GToSchema (Rep x), Generic x) => ToSchema (CustomJSON xs x) where
23− declareNamedSchema Proxy = genericDeclareNamedSchema (openApiOptions @xs) (Proxy @x)
28+ declareNamedSchema Proxy =
29+ openApiSchemaModifier @xs <$> genericDeclareNamedSchema (openApiOptions @xs) (Proxy @x)
2430
2531data DatatypeNameModifier t
2632
2733instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (DatatypeNameModifier f ': xs) where
2834 openApiOptions = (openApiOptions @xs) {datatypeNameModifier = getStringModifier @f}
2935
36+instance (AesonOptions xs) => AesonOptions (DatatypeNameModifier f ': xs) where
37+ aesonOptions = aesonOptions @xs
38+
39+instance (AesonOptions xs) => AesonOptions (Description f ': xs) where
40+ aesonOptions = aesonOptions @xs
41+
42+instance (OpenApiOptions xs, KnownSymbol t) => OpenApiOptions (Description t ': xs) where
43+ openApiOptions = openApiOptions @xs
44+ openApiSchemaModifier = schema . description <>~ Just (toTextLine @t)
45+
46+toTextLine :: forall s. KnownSymbol s => Text
47+toTextLine = "\n\n" <> T.pack (symbolVal (Proxy @s))
48+
49+type family (++) (x :: [k]) (y :: [k]) :: [k] where
50+ (++) '[] ys = ys
51+ (++) (x ': xs) ys = xs ++ (x ': ys)
52+
53+type family Extending (c :: * -> *) (ee :: [*]) :: * -> * where
54+ Extending (CustomJSON a) ee = CustomJSON (a ++ ee)
55+
3056-- deriving-aeson-based instances
3157
3258class OpenApiOptions (xs :: [Type]) where
3359 openApiOptions :: SchemaOptions
60+ openApiSchemaModifier :: NamedSchema -> NamedSchema
61+ openApiSchemaModifier = id
3462
3563instance OpenApiOptions '[] where
3664 openApiOptions = defaultSchemaOptions