git.iko.soy/deriving-openapi3
on master help sign in
Added description
29e7d7eiko
package.yaml+6
@@ -10,6 +10,9 @@ dependencies:
10 - openapi3
10 - openapi3
11 - deriving-aeson
11 - deriving-aeson
12 - aeson
12 - aeson
13+ - servant
14+ - lens
15+ - text
13library:
16library:
14 source-dirs: src
17 source-dirs: src
15default-extensions:
18default-extensions:
@@ -21,3 +24,6 @@ default-extensions:
21 - FlexibleInstances
24 - FlexibleInstances
22 - TypeApplications
25 - TypeApplications
23 - UndecidableInstances
26 - UndecidableInstances
27+ - TypeFamilies
28+ - PolyKinds
29+ - OverloadedStrings
src/Deriving/OpenApi.hs+29−1
@@ -5,32 +5,60 @@ module Deriving.OpenApi
5 DatatypeNameModifier,
5 DatatypeNameModifier,
6 ToSchema,
6 ToSchema,
7 module Deriving.Aeson,
7 module Deriving.Aeson,
8+ Extending,
8 )
9 )
9where
10where
10
11
12+import Control.Lens
11import qualified Data.Aeson.Types as A
13import qualified Data.Aeson.Types as A
12import Data.Kind
14import Data.Kind
13import Data.OpenApi
15import Data.OpenApi
14import Data.OpenApi.Internal.Schema
16import Data.OpenApi.Internal.Schema
15import Data.Proxy
17import Data.Proxy
18+import Data.Text (Text)
19+import qualified Data.Text as T
16import Deriving.Aeson
20import Deriving.Aeson
17import GHC.Generics
21import GHC.Generics
18import GHC.TypeLits
22import GHC.TypeLits
23+import Servant.API
19
24
20type CustomOpenApi = CustomJSON
25type CustomOpenApi = CustomJSON
21
26
22instance (OpenApiOptions xs, GToSchema (Rep x), Generic x) => ToSchema (CustomJSON xs x) where
27instance (OpenApiOptions xs, GToSchema (Rep x), Generic x) => ToSchema (CustomJSON xs x) where
28+ declareNamedSchema Proxy =
23− declareNamedSchema Proxy = genericDeclareNamedSchema (openApiOptions @xs) (Proxy @x)
29+ openApiSchemaModifier @xs <$> genericDeclareNamedSchema (openApiOptions @xs) (Proxy @x)
24
30
25data DatatypeNameModifier t
31data DatatypeNameModifier t
26
32
27instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (DatatypeNameModifier f ': xs) where
33instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (DatatypeNameModifier f ': xs) where
28 openApiOptions = (openApiOptions @xs) {datatypeNameModifier = getStringModifier @f}
34 openApiOptions = (openApiOptions @xs) {datatypeNameModifier = getStringModifier @f}
29
35
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+
30-- deriving-aeson-based instances
56-- deriving-aeson-based instances
31
57
32class OpenApiOptions (xs :: [Type]) where
58class OpenApiOptions (xs :: [Type]) where
33 openApiOptions :: SchemaOptions
59 openApiOptions :: SchemaOptions
60+ openApiSchemaModifier :: NamedSchema -> NamedSchema
61+ openApiSchemaModifier = id
34
62
35instance OpenApiOptions '[] where
63instance OpenApiOptions '[] where
36 openApiOptions = defaultSchemaOptions
64 openApiOptions = defaultSchemaOptions