git.iko.soy/deriving-openapi3
on master help sign in
Made servant conditional
commitf4bfff11a22762f728b63cfcb498e54b5c4d05d4 authoriko <ilyakooo0@gmail.com> dateSun 9 Jan 2022, 19:30 (5 years ago) parent4c35d5a
3 files changed+45−15
.github/workflows/build.yaml+6 package.yaml+21−3 src/Deriving/OpenApi.hs+18−12
.github/workflows/build.yaml+6
@@ -42,3 +42,9 @@ jobs:
4242 shell: bash
4343 run: |
4444 stack build
45+
46+ - name: Check Formatting
47+ shell: bash
48+ run: |
49+ stack install ormolu
50+ ormolu -o -XTypeApplications -o -XCPP -m check $(find . -type f -name "*.hs")
package.yaml+21−3
@@ -1,18 +1,31 @@
11name: deriving-openapi3
22version: 0.1.0.0
33synopsis: DerivingVia for OpenAPI 3
4−# description:
4+description: See <https://github.com/ilyakooo0/deriving-openapi3 GitHub> for more details.
55maintainer: Ilya Kostyuchenko <ilyakooo0@gmail.com>
6+category: JSON, Generics
7+license: MIT
68github: ilyakooo0/deriving-openapi3
7−ghc-options: -Wall
9+ghc-options: -Weverything
10+ -Wno-star-is-type
11+ -Wno-unsafe
12+ -Wno-prepositive-qualified-module
13+ -Wno-safe
14+ -Wno-missing-safe-haskell-mode
15+ -Wno-missing-import-lists
16+ -Wno-implicit-prelude
817dependencies:
918 - base
1019 - openapi3
1120 - deriving-aeson
1221 - aeson
13− - servant
1422 - lens
1523 - text
24+when:
25+ - condition: flag(servant-description)
26+ dependencies:
27+ - servant
28+ cpp-options: -DSERVANT_DESCRIPTION
1629library:
1730 source-dirs: src
1831default-extensions:
@@ -27,3 +40,8 @@ default-extensions:
2740 - TypeFamilies
2841 - PolyKinds
2942 - OverloadedStrings
43+flags:
44+ servant-description:
45+ description: Create instances for the 'Description' type from 'servant'
46+ default: true
47+ manual: false
src/Deriving/OpenApi.hs+18−12
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# OPTIONS_GHC -Wno-orphans #-}
23
34module Deriving.OpenApi
@@ -15,13 +16,28 @@ import Data.Kind
1516import Data.OpenApi
1617import Data.OpenApi.Internal.Schema
1718import Data.Proxy
18−import Data.Text (Text)
19−import qualified Data.Text as T
2019import Data.Typeable
2120import Deriving.Aeson
2221import GHC.Generics
2322import GHC.TypeLits
23+
24+#ifdef SERVANT_DESCRIPTION
25+
2426import Servant.API
27+import Data.Text (Text)
28+import qualified Data.Text as T
29+
30+instance (AesonOptions xs) => AesonOptions (Description f ': xs) where
31+ aesonOptions = aesonOptions @xs
32+
33+instance (OpenApiOptions xs, KnownSymbol t) => OpenApiOptions (Description t ': xs) where
34+ openApiOptions = openApiOptions @xs
35+ openApiSchemaModifier = schema . description <>~ Just (toTextLine @t)
36+
37+toTextLine :: forall s. KnownSymbol s => Text
38+toTextLine = "\n\n" <> T.pack (symbolVal (Proxy @s))
39+
40+#endif
2541
2642type CustomOpenApi = CustomJSON
2743
@@ -37,16 +53,6 @@ instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (DatatypeNameMo
3753instance (AesonOptions xs) => AesonOptions (DatatypeNameModifier f ': xs) where
3854 aesonOptions = aesonOptions @xs
3955
40−instance (AesonOptions xs) => AesonOptions (Description f ': xs) where
41− aesonOptions = aesonOptions @xs
42−
43−instance (OpenApiOptions xs, KnownSymbol t) => OpenApiOptions (Description t ': xs) where
44− openApiOptions = openApiOptions @xs
45− openApiSchemaModifier = schema . description <>~ Just (toTextLine @t)
46−
47−toTextLine :: forall s. KnownSymbol s => Text
48−toTextLine = "\n\n" <> T.pack (symbolVal (Proxy @s))
49−
5056type family (++) (x :: [k]) (y :: [k]) :: [k] where
5157 (++) '[] ys = ys
5258 (++) (x ': xs) ys = xs ++ (x ': ys)