mirror of
https://github.com/ilyakooo0/deriving-openapi3.git
synced 2024-09-11 10:25:55 +03:00
Added description
This commit is contained in:
parent
86a9e5bf33
commit
29e7d7ec58
@ -10,6 +10,9 @@ dependencies:
|
|||||||
- openapi3
|
- openapi3
|
||||||
- deriving-aeson
|
- deriving-aeson
|
||||||
- aeson
|
- aeson
|
||||||
|
- servant
|
||||||
|
- lens
|
||||||
|
- text
|
||||||
library:
|
library:
|
||||||
source-dirs: src
|
source-dirs: src
|
||||||
default-extensions:
|
default-extensions:
|
||||||
@ -21,3 +24,6 @@ default-extensions:
|
|||||||
- FlexibleInstances
|
- FlexibleInstances
|
||||||
- TypeApplications
|
- TypeApplications
|
||||||
- UndecidableInstances
|
- UndecidableInstances
|
||||||
|
- TypeFamilies
|
||||||
|
- PolyKinds
|
||||||
|
- OverloadedStrings
|
||||||
|
@ -5,32 +5,60 @@ module Deriving.OpenApi
|
|||||||
DatatypeNameModifier,
|
DatatypeNameModifier,
|
||||||
ToSchema,
|
ToSchema,
|
||||||
module Deriving.Aeson,
|
module Deriving.Aeson,
|
||||||
|
Extending,
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
|
import Control.Lens
|
||||||
import qualified Data.Aeson.Types as A
|
import qualified Data.Aeson.Types as A
|
||||||
import Data.Kind
|
import Data.Kind
|
||||||
import Data.OpenApi
|
import Data.OpenApi
|
||||||
import Data.OpenApi.Internal.Schema
|
import Data.OpenApi.Internal.Schema
|
||||||
import Data.Proxy
|
import Data.Proxy
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T
|
||||||
import Deriving.Aeson
|
import Deriving.Aeson
|
||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
import GHC.TypeLits
|
import GHC.TypeLits
|
||||||
|
import Servant.API
|
||||||
|
|
||||||
type CustomOpenApi = CustomJSON
|
type CustomOpenApi = CustomJSON
|
||||||
|
|
||||||
instance (OpenApiOptions xs, GToSchema (Rep x), Generic x) => ToSchema (CustomJSON xs x) where
|
instance (OpenApiOptions xs, GToSchema (Rep x), Generic x) => ToSchema (CustomJSON xs x) where
|
||||||
declareNamedSchema Proxy = genericDeclareNamedSchema (openApiOptions @xs) (Proxy @x)
|
declareNamedSchema Proxy =
|
||||||
|
openApiSchemaModifier @xs <$> genericDeclareNamedSchema (openApiOptions @xs) (Proxy @x)
|
||||||
|
|
||||||
data DatatypeNameModifier t
|
data DatatypeNameModifier t
|
||||||
|
|
||||||
instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (DatatypeNameModifier f ': xs) where
|
instance (StringModifier f, OpenApiOptions xs) => OpenApiOptions (DatatypeNameModifier f ': xs) where
|
||||||
openApiOptions = (openApiOptions @xs) {datatypeNameModifier = getStringModifier @f}
|
openApiOptions = (openApiOptions @xs) {datatypeNameModifier = getStringModifier @f}
|
||||||
|
|
||||||
|
instance (AesonOptions xs) => AesonOptions (DatatypeNameModifier f ': xs) where
|
||||||
|
aesonOptions = aesonOptions @xs
|
||||||
|
|
||||||
|
instance (AesonOptions xs) => AesonOptions (Description f ': xs) where
|
||||||
|
aesonOptions = aesonOptions @xs
|
||||||
|
|
||||||
|
instance (OpenApiOptions xs, KnownSymbol t) => OpenApiOptions (Description t ': xs) where
|
||||||
|
openApiOptions = openApiOptions @xs
|
||||||
|
openApiSchemaModifier = schema . description <>~ Just (toTextLine @t)
|
||||||
|
|
||||||
|
toTextLine :: forall s. KnownSymbol s => Text
|
||||||
|
toTextLine = "\n\n" <> T.pack (symbolVal (Proxy @s))
|
||||||
|
|
||||||
|
type family (++) (x :: [k]) (y :: [k]) :: [k] where
|
||||||
|
(++) '[] ys = ys
|
||||||
|
(++) (x ': xs) ys = xs ++ (x ': ys)
|
||||||
|
|
||||||
|
type family Extending (c :: * -> *) (ee :: [*]) :: * -> * where
|
||||||
|
Extending (CustomJSON a) ee = CustomJSON (a ++ ee)
|
||||||
|
|
||||||
-- deriving-aeson-based instances
|
-- deriving-aeson-based instances
|
||||||
|
|
||||||
class OpenApiOptions (xs :: [Type]) where
|
class OpenApiOptions (xs :: [Type]) where
|
||||||
openApiOptions :: SchemaOptions
|
openApiOptions :: SchemaOptions
|
||||||
|
openApiSchemaModifier :: NamedSchema -> NamedSchema
|
||||||
|
openApiSchemaModifier = id
|
||||||
|
|
||||||
instance OpenApiOptions '[] where
|
instance OpenApiOptions '[] where
|
||||||
openApiOptions = defaultSchemaOptions
|
openApiOptions = defaultSchemaOptions
|
||||||
|
Loading…
Reference in New Issue
Block a user