git.iko.soy/deriving-openapi3
on master help sign in
Initial commit
1dfae52iko
.gitignorenew file+1
@@ -0,0 +1 @@
1+openapi3-deriving.cabal
package.yamlnew file+23
@@ -0,0 +1,23 @@
1+name: openapi3-deriving
2+version: 0.1.0
3+synopsis: DerivingVia for OpenAPI 3
4+# description:
5+maintainer: Ilya Kostyuchenko <ilyakooo0@gmail.com>
6+github: ilyakooo0/openapi3-deriving
7+ghc-options: -Wall
8+dependencies:
9+ - base
10+ - openapi3
11+ - deriving-aeson
12+ - aeson
13+library:
14+ source-dirs: src
15+default-extensions:
16+ - AllowAmbiguousTypes
17+ - ScopedTypeVariables
18+ - KindSignatures
19+ - DataKinds
20+ - TypeOperators
21+ - FlexibleInstances
22+ - TypeApplications
23+ - UndecidableInstances
src/Deriving/OpenApi.hsnew file+71
@@ -0,0 +1,71 @@
1+{-# OPTIONS_GHC -Wno-orphans #-}
2+
3+module Deriving.OpenApi
4+ ( CustomOpenApi,
5+ DatatypeNameModifier,
6+ module Deriving.Aeson,
7+ )
8+where
9+
10+import qualified Data.Aeson.Types as A
11+import Data.Kind
12+import Data.OpenApi
13+import Data.OpenApi.Internal.Schema
14+import Data.Proxy
15+import Deriving.Aeson
16+import GHC.Generics
17+import GHC.TypeLits
18+
19+type CustomOpenApi = CustomJSON
20+
21+instance (OpenApiOptions xs, GToSchema (Rep x), Generic x) => ToSchema (CustomJSON xs x) where
22+ declareNamedSchema Proxy = genericDeclareNamedSchema (openApiOptions @xs) (Proxy @x)
23+
24+data DatatypeNameModifier t
25+
26+instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (DatatypeNameModifier f ': xs) where
27+ openApiOptions = (openApiOptions @xs) {datatypeNameModifier = getStringModifier @f}
28+
29+-- deriving-aeson-based instances
30+
31+class OpenApiOptions (xs :: [Type]) where
32+ openApiOptions :: SchemaOptions
33+
34+instance OpenApiOptions '[] where
35+ openApiOptions = defaultSchemaOptions
36+
37+instance OpenApiOptions xs => OpenApiOptions (UnwrapUnaryRecords ': xs) where
38+ openApiOptions = (openApiOptions @xs) {unwrapUnaryRecords = True}
39+
40+instance OpenApiOptions xs => OpenApiOptions (OmitNothingFields ': xs) where
41+ openApiOptions = openApiOptions @xs
42+
43+instance OpenApiOptions xs => OpenApiOptions (RejectUnknownFields ': xs) where
44+ openApiOptions = openApiOptions @xs
45+
46+instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (FieldLabelModifier f ': xs) where
47+ openApiOptions = (openApiOptions @xs) {fieldLabelModifier = getStringModifier @f}
48+
49+instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (ConstructorTagModifier f ': xs) where
50+ openApiOptions = (openApiOptions @xs) {constructorTagModifier = getStringModifier @f}
51+
52+instance
53+ (OpenApiOptions xs, TypeError ( 'Text "openapi3-deriving does not currently the `TagSingleConstructors` modifier.")) =>
54+ OpenApiOptions (TagSingleConstructors ': xs)
55+ where
56+ openApiOptions = undefined
57+
58+instance OpenApiOptions xs => OpenApiOptions (NoAllNullaryToStringTag ': xs) where
59+ openApiOptions = (openApiOptions @xs) {allNullaryToStringTag = False}
60+
61+instance (KnownSymbol t, KnownSymbol c, OpenApiOptions xs) => OpenApiOptions (SumTaggedObject t c ': xs) where
62+ openApiOptions = (openApiOptions @xs) {sumEncoding = A.TaggedObject (symbolVal (Proxy @t)) (symbolVal (Proxy @c))}
63+
64+instance (OpenApiOptions xs) => OpenApiOptions (SumUntaggedValue ': xs) where
65+ openApiOptions = (openApiOptions @xs) {sumEncoding = A.UntaggedValue}
66+
67+instance (OpenApiOptions xs) => OpenApiOptions (SumObjectWithSingleField ': xs) where
68+ openApiOptions = (openApiOptions @xs) {sumEncoding = A.ObjectWithSingleField}
69+
70+instance (OpenApiOptions xs) => OpenApiOptions (SumTwoElemArray ': xs) where
71+ openApiOptions = (openApiOptions @xs) {sumEncoding = A.TwoElemArray}
stack.yamlnew file+7
@@ -0,0 +1,7 @@
1+resolver: lts-16.26
2+
3+packages:
4+ - .
5+
6+extra-deps:
7+ - openapi3-3.0.0.1@sha256:205669d14c5b48d2275e0bac41d78c9bfdb925ef9a8e3265662ce51a9e6d8139,4641