@@ -1,139 +1,108 @@
11{-# LANGUAGE CPP #-}
22{-# OPTIONS_GHC -Wno-orphans #-}
33
4−-- | __Example:__
5−--
6−-- >>> :set -XDerivingStrategies -XDerivingVia -XDeriveGeneric
7−--
8−-- >>> :{
9−-- data User = User
10−-- { userFirstName :: String,
11−-- userAge :: Maybe Integer
12−-- }
13−-- deriving stock (Generic)
14−-- deriving (FromJSON, ToJSON, ToSchema)
15−-- via CustomJSON
16−-- '[FieldLabelModifier '[StripPrefix "user", CamelToSnake], RejectUnknownFields]
17−-- User
18−-- showYaml = Data.ByteString.Char8.putStr . Data.Yaml.encode
19−-- :}
20−--
21−-- >>> showYaml $ toSchema (Proxy :: Proxy User)
22−-- type: object
23−-- properties:
24−-- first_name:
25−-- type: string
26−-- age:
27−-- type: integer
28−-- required:
29−-- - first_name
30−-- additionalProperties: false
314module Deriving.OpenApi
325 ( CustomOpenApi,
336 DatatypeNameModifier,
347 ToSchema,
358 module Deriving.Aeson,
9+ Extending,
3610 )
3711where
3812
3913import Control.Lens
4014import qualified Data.Aeson.Types as A
15+import Data.Kind
4116import Data.OpenApi
42−import Data.OpenApi.Internal.ParamSchema
4317import Data.OpenApi.Internal.Schema
4418import Data.Proxy
45−import Data.Text (Text)
46−import qualified Data.Text as T
4719import Data.Typeable
4820import Deriving.Aeson
4921import GHC.Generics
5022import GHC.TypeLits
5123
52−data Description (desc :: Symbol) = Description
24+#ifdef SERVANT_DESCRIPTION
25+
26+import Servant.API
27+import Data.Text (Text)
28+import qualified Data.Text as T
5329
5430instance (AesonOptions xs) => AesonOptions (Description f ': xs) where
5531 aesonOptions = aesonOptions @xs
5632
57−instance KnownSymbol t => OpenApiOptionModifier (Description t) where
33+instance (OpenApiOptions xs, KnownSymbol t) => OpenApiOptions (Description t ': xs) where
34+ openApiOptions = openApiOptions @xs
5835 openApiSchemaModifier = schema . description <>~ Just (toTextLine @t)
5936
6037toTextLine :: forall s. KnownSymbol s => Text
6138toTextLine = "\n\n" <> T.pack (symbolVal (Proxy @s))
6239
63−-- $setup
64−-- >>> import qualified Data.Yaml
65−-- >>> import qualified Data.ByteString.Char8
40+#endif
6641
6742type CustomOpenApi = CustomJSON
6843
69−instance
70− (OpenApiOptionModifier xs, GToSchema (Rep x), Generic x, Typeable x, Typeable xs, Typeable k) =>
71− ToSchema (CustomJSON (xs :: k) x)
72− where
44+instance (OpenApiOptions xs, GToSchema (Rep x), Generic x, Typeable x, Typeable xs) => ToSchema (CustomJSON xs x) where
7345 declareNamedSchema Proxy =
74− openApiSchemaModifier @xs
75− <$> genericDeclareNamedSchema (openApiOptionsModifier @xs defaultSchemaOptions) (Proxy @x)
76−
77−instance
78− (OpenApiOptionModifier xs, GToParamSchema (Rep x), Generic x) =>
79− ToParamSchema (CustomJSON (xs :: k) x)
80− where
81− toParamSchema Proxy =
82− genericToParamSchema (openApiOptionsModifier @xs defaultSchemaOptions) (Proxy @x)
83−
84−class OpenApiOptionModifier x where
85− openApiOptionsModifier :: SchemaOptions -> SchemaOptions
86− openApiOptionsModifier = id
87− openApiSchemaModifier :: NamedSchema -> NamedSchema
88− openApiSchemaModifier = id
46+ openApiSchemaModifier @xs <$> genericDeclareNamedSchema (openApiOptions @xs) (Proxy @x)
8947
9048data DatatypeNameModifier t
9149
92−instance (StringModifier f) => OpenApiOptionModifier (DatatypeNameModifier f) where
93− openApiOptionsModifier o = o {datatypeNameModifier = getStringModifier @f}
50+instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (DatatypeNameModifier f ': xs) where
51+ openApiOptions = (openApiOptions @xs) {datatypeNameModifier = getStringModifier @f}
9452
9553instance (AesonOptions xs) => AesonOptions (DatatypeNameModifier f ': xs) where
9654 aesonOptions = aesonOptions @xs
9755
56+type family (++) (x :: [k]) (y :: [k]) :: [k] where
57+ (++) '[] ys = ys
58+ (++) (x ': xs) ys = xs ++ (x ': ys)
59+
60+type family Extending (c :: * -> *) (ee :: [*]) :: * -> * where
61+ Extending (CustomJSON a) ee = CustomJSON (a ++ ee)
62+
9863-- deriving-aeson-based instances
9964
100−instance OpenApiOptionModifier UnwrapUnaryRecords where
101− openApiOptionsModifier o = o {unwrapUnaryRecords = True}
65+class OpenApiOptions (xs :: [Type]) where
66+ openApiOptions :: SchemaOptions
67+ openApiSchemaModifier :: NamedSchema -> NamedSchema
68+ openApiSchemaModifier = id
69+
70+instance OpenApiOptions '[] where
71+ openApiOptions = defaultSchemaOptions
72+
73+instance OpenApiOptions xs => OpenApiOptions (UnwrapUnaryRecords ': xs) where
74+ openApiOptions = (openApiOptions @xs) {unwrapUnaryRecords = True}
10275
103−instance OpenApiOptionModifier OmitNothingFields
76+instance OpenApiOptions xs => OpenApiOptions (OmitNothingFields ': xs) where
77+ openApiOptions = openApiOptions @xs
10478
105−#if MIN_VERSION_deriving_aeson(0,2,4)
106−instance OpenApiOptionModifier RejectUnknownFields where
79+instance OpenApiOptions xs => OpenApiOptions (RejectUnknownFields ': xs) where
80+ openApiOptions = openApiOptions @xs
10781 openApiSchemaModifier = schema . additionalProperties .~ Just (AdditionalPropertiesAllowed False)
108−#endif
10982
110−instance StringModifier f => OpenApiOptionModifier (FieldLabelModifier f) where
111− openApiOptionsModifier o = o {fieldLabelModifier = getStringModifier @f}
83+instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (FieldLabelModifier f ': xs) where
84+ openApiOptions = (openApiOptions @xs) {fieldLabelModifier = getStringModifier @f}
11285
113−instance StringModifier f => OpenApiOptionModifier (ConstructorTagModifier f) where
114− openApiOptionsModifier o = o {constructorTagModifier = getStringModifier @f}
86+instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (ConstructorTagModifier f ': xs) where
87+ openApiOptions = (openApiOptions @xs) {constructorTagModifier = getStringModifier @f}
11588
11689instance
117− TypeError ('Text "deriving-openapi3 does not currently the `TagSingleConstructors` modifier.") =>
118− OpenApiOptionModifier TagSingleConstructors
119−
120−instance OpenApiOptionModifier NoAllNullaryToStringTag where
121− openApiOptionsModifier o = o {allNullaryToStringTag = False}
122−
123−instance (KnownSymbol t, KnownSymbol c) => OpenApiOptionModifier (SumTaggedObject t c) where
124− openApiOptionsModifier o = o {sumEncoding = A.TaggedObject (symbolVal (Proxy @t)) (symbolVal (Proxy @c))}
90+ (OpenApiOptions xs, TypeError ('Text "openapi3-deriving does not currently the `TagSingleConstructors` modifier.")) =>
91+ OpenApiOptions (TagSingleConstructors ': xs)
92+ where
93+ openApiOptions = undefined
12594
126−instance OpenApiOptionModifier SumUntaggedValue where
127− openApiOptionsModifier o = o {sumEncoding = A.UntaggedValue}
95+instance OpenApiOptions xs => OpenApiOptions (NoAllNullaryToStringTag ': xs) where
96+ openApiOptions = (openApiOptions @xs) {allNullaryToStringTag = False}
12897
129−instance OpenApiOptionModifier SumObjectWithSingleField where
130− openApiOptionsModifier o = o {sumEncoding = A.ObjectWithSingleField}
98+instance (KnownSymbol t, KnownSymbol c, OpenApiOptions xs) => OpenApiOptions (SumTaggedObject t c ': xs) where
99+ openApiOptions = (openApiOptions @xs) {sumEncoding = A.TaggedObject (symbolVal (Proxy @t)) (symbolVal (Proxy @c))}
131100
132−instance OpenApiOptionModifier SumTwoElemArray where
133− openApiOptionsModifier o = o {sumEncoding = A.TwoElemArray}
101+instance (OpenApiOptions xs) => OpenApiOptions (SumUntaggedValue ': xs) where
102+ openApiOptions = (openApiOptions @xs) {sumEncoding = A.UntaggedValue}
134103
135−instance OpenApiOptionModifier '[]
104+instance (OpenApiOptions xs) => OpenApiOptions (SumObjectWithSingleField ': xs) where
105+ openApiOptions = (openApiOptions @xs) {sumEncoding = A.ObjectWithSingleField}
136106
137−instance (OpenApiOptionModifier x, OpenApiOptionModifier xs) => OpenApiOptionModifier (x ': xs) where
138− openApiOptionsModifier = openApiOptionsModifier @xs . openApiOptionsModifier @x
139− openApiSchemaModifier = openApiSchemaModifier @xs . openApiSchemaModifier @x
107+instance (OpenApiOptions xs) => OpenApiOptions (SumTwoElemArray ': xs) where
108+ openApiOptions = (openApiOptions @xs) {sumEncoding = A.TwoElemArray}