Maintainer | Nickolay Kudasov <nickolay@getshoptv.com> |
---|---|
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Types and functions for working with Swagger schema.
Synopsis
- class ToSchema a where
- declareNamedSchema :: Proxy a -> Declare (Definitions Schema) NamedSchema
- declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
- declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
- toSchema :: ToSchema a => Proxy a -> Schema
- toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
- schemaName :: ToSchema a => Proxy a -> Maybe Text
- toInlinedSchema :: ToSchema a => Proxy a -> Schema
- class ToSchema1 (f :: * -> *) where
- declareNamedSchema1 :: (Generic (f a), GToSchema (Rep (f a)), ToSchema a) => Proxy f -> Proxy a -> Declare (Definitions Schema) NamedSchema
- newtype BySchema1 f a = BySchema1 (f a)
- genericDeclareNamedSchema :: forall a. (Generic a, GToSchema (Rep a)) => SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
- genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
- genericDeclareNamedSchemaNewtype :: forall a d c s i inner. (Generic a, Datatype d, Rep a ~ D1 d (C1 c (S1 s (K1 i inner)))) => SchemaOptions -> (Proxy inner -> Declare (Definitions Schema) Schema) -> Proxy a -> Declare (Definitions Schema) NamedSchema
- genericNameSchema :: forall a d f. (Generic a, Rep a ~ D1 d f, Datatype d) => SchemaOptions -> Proxy a -> Schema -> NamedSchema
- genericToNamedSchemaBoundedIntegral :: forall a d f. (Bounded a, Integral a, Generic a, Rep a ~ D1 d f, Datatype d) => SchemaOptions -> Proxy a -> NamedSchema
- toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
- declareSchemaBoundedEnumKeyMapping :: forall map key value. (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value) => Proxy (map key value) -> Declare (Definitions Schema) Schema
- toSchemaBoundedEnumKeyMapping :: forall map key value. (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value) => Proxy (map key value) -> Schema
- paramSchemaToNamedSchema :: (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d) => SchemaOptions -> Proxy a -> NamedSchema
- paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
- sketchSchema :: ToJSON a => a -> Schema
- sketchStrictSchema :: ToJSON a => a -> Schema
- inlineNonRecursiveSchemas :: Data s => Definitions Schema -> s -> s
- inlineAllSchemas :: Data s => Definitions Schema -> s -> s
- inlineSchemas :: Data s => [Text] -> Definitions Schema -> s -> s
- inlineSchemasWhen :: Data s => (Text -> Bool) -> Definitions Schema -> s -> s
- data SchemaOptions = SchemaOptions {}
- defaultSchemaOptions :: SchemaOptions
- fromAesonOptions :: Options -> SchemaOptions
Encoding
class ToSchema a where Source #
Convert a type into
.Schema
An example type and instance:
{-# LANGUAGE OverloadedStrings #-} -- allows to writeText
literals {-# LANGUAGE OverloadedLists #-} -- allows to writeMap
andHashMap
as lists import Control.Lens import Data.Proxy import Data.OpenApi data Coord = Coord { x :: Double, y :: Double } instance ToSchema Coord where declareNamedSchema _ = do doubleSchema <- declareSchemaRef (Proxy :: Proxy Double) return $ NamedSchema (Just "Coord") $ mempty & type_ ?~ OpenApiObject & properties .~ [ ("x", doubleSchema) , ("y", doubleSchema) ] & required .~ [ "x", "y" ]
Instead of manually writing your
instance you can
use a default generic implementation of ToSchema
.declareNamedSchema
To do that, simply add deriving
clause to your datatype
and declare a Generic
instance for your datatype without
giving definition for ToSchema
.declareNamedSchema
For instance, the previous example can be simplified into this:
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic) data Coord = Coord { x :: Double, y :: Double } deriving Generic instance ToSchema Coord
Nothing
declareNamedSchema :: Proxy a -> Declare (Definitions Schema) NamedSchema Source #
Convert a type into an optionally named schema together with all used definitions. Note that the schema itself is included in definitions only if it is recursive (and thus needs its definition in scope).
default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => Proxy a -> Declare (Definitions Schema) NamedSchema Source #
Instances
declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema Source #
Convert a type into a schema and declare all used schema definitions.
declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema) Source #
Convert a type into a referenced schema if possible and declare all used schema definitions. Only named schemas can be referenced, nameless schemas are inlined.
Schema definitions are typically declared for every referenced schema.
If
returns a reference, a corresponding schema
will be declared (regardless of whether it is recusive or not).declareSchemaRef
toSchema :: ToSchema a => Proxy a -> Schema Source #
Convert a type into a schema.
>>>
BSL.putStrLn $ encode $ toSchema (Proxy :: Proxy Int8)
{"maximum":127,"minimum":-128,"type":"integer"}
>>>
BSL.putStrLn $ encode $ toSchema (Proxy :: Proxy [Day])
{"items":{"$ref":"#/components/schemas/Day"},"type":"array"}
toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema Source #
Convert a type into a referenced schema if possible. Only named schemas can be referenced, nameless schemas are inlined.
>>>
BSL.putStrLn $ encode $ toSchemaRef (Proxy :: Proxy Integer)
{"type":"integer"}
>>>
BSL.putStrLn $ encode $ toSchemaRef (Proxy :: Proxy Day)
{"$ref":"#/components/schemas/Day"}
schemaName :: ToSchema a => Proxy a -> Maybe Text Source #
Get type's schema name according to its
instance.ToSchema
>>>
schemaName (Proxy :: Proxy Int)
Nothing
>>>
schemaName (Proxy :: Proxy UTCTime)
Just "UTCTime"
toInlinedSchema :: ToSchema a => Proxy a -> Schema Source #
Convert a type into a schema without references.
>>>
BSL.putStrLn $ encode $ toInlinedSchema (Proxy :: Proxy [Day])
{"items":{"example":"2016-07-22","format":"date","type":"string"},"type":"array"}
WARNING:
will produce infinite schema
when inlining recursive schemas.toInlinedSchema
class ToSchema1 (f :: * -> *) where Source #
This class allows to generate schemas for polymorphic types (of kind Type -> Type
).
Intended usage:
>>>
data Foo a = Foo { foo :: a, bar :: Int } deriving (Eq, Show, Generic, ToSchema1)
>>>
:{
instance (ToSchema a, Typeable a) => ToSchema (Foo a) where declareNamedSchema _ = declareNamedSchema @(BySchema1 Foo a) Proxy :}
>>>
toNamedSchema @(Foo Int) Proxy ^. name
Just "Foo_Int">>>
toNamedSchema @(Foo Bool) Proxy ^. name
Just "Foo_Bool">>>
toNamedSchema @(Foo (Foo T.Text)) Proxy ^. name
Just "Foo_(Foo_Text)"
Nothing
declareNamedSchema1 :: (Generic (f a), GToSchema (Rep (f a)), ToSchema a) => Proxy f -> Proxy a -> Declare (Definitions Schema) NamedSchema Source #
default declareNamedSchema1 :: forall a. (ToSchema a, Generic (f a), GToSchema (Rep (f a))) => Proxy f -> Proxy a -> Declare (Definitions Schema) NamedSchema Source #
newtype BySchema1 f a Source #
For GHC 8.6+ it's more convenient to use DerivingVia
to derive instances of ToSchema
using ToSchema1
instance, like this:
>>>
data Foo a = Foo { foo :: a, bar :: Int } deriving (Eq, Show, Generic, ToSchema1)
>>>
deriving via BySchema1 Foo a instance (ToSchema a, Typeable a) => ToSchema (Foo a)
BySchema1 (f a) |
Instances
(ToSchema1 f, Generic (f a), GToSchema (Rep (f a)), Typeable (f a), ToSchema a) => ToSchema (BySchema1 f a) Source # | |
Defined in Data.OpenApi.Internal.Schema declareNamedSchema :: Proxy (BySchema1 f a) -> Declare (Definitions Schema) NamedSchema Source # |
Generic schema encoding
genericDeclareNamedSchema :: forall a. (Generic a, GToSchema (Rep a)) => SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema Source #
A configurable generic
creator.
This function applied to NamedSchema
is used as the default for defaultSchemaOptions
when the type is an instance of declareNamedSchema
.Generic
genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema Source #
A configurable generic
creator.Schema
genericDeclareNamedSchemaNewtype Source #
:: forall a d c s i inner. (Generic a, Datatype d, Rep a ~ D1 d (C1 c (S1 s (K1 i inner)))) | |
=> SchemaOptions | How to derive the name. |
-> (Proxy inner -> Declare (Definitions Schema) Schema) | How to create a schema for the wrapped type. |
-> Proxy a | |
-> Declare (Definitions Schema) NamedSchema |
Declare a named schema for a newtype
wrapper.
genericNameSchema :: forall a d f. (Generic a, Rep a ~ D1 d f, Datatype d) => SchemaOptions -> Proxy a -> Schema -> NamedSchema Source #
Bounded
Integral
genericToNamedSchemaBoundedIntegral :: forall a d f. (Bounded a, Integral a, Generic a, Rep a ~ D1 d f, Datatype d) => SchemaOptions -> Proxy a -> NamedSchema Source #
Bounded
Enum
key mappings
declareSchemaBoundedEnumKeyMapping :: forall map key value. (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value) => Proxy (map key value) -> Declare (Definitions Schema) Schema Source #
Declare Schema
for a mapping with Bounded
Enum
keys.
This makes a much more useful schema when there aren't many options for key values.
>>>
data ButtonState = Neutral | Focus | Active | Hover | Disabled deriving (Show, Bounded, Enum, Generic)
>>>
instance ToJSON ButtonState
>>>
instance ToSchema ButtonState
>>>
instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
>>>
type ImageUrl = T.Text
>>>
BSL.putStrLn $ encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
{"type":"object","properties":{"Focus":{"type":"string"},"Disabled":{"type":"string"},"Active":{"type":"string"},"Neutral":{"type":"string"},"Hover":{"type":"string"}}}
Note: this is only useful when key
is encoded with ToJSONKeyText
.
If it is encoded with ToJSONKeyValue
then a regular schema for [(key, value)]
is used.
toSchemaBoundedEnumKeyMapping :: forall map key value. (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value) => Proxy (map key value) -> Schema Source #
A Schema
for a mapping with Bounded
Enum
keys.
This makes a much more useful schema when there aren't many options for key values.
>>>
data ButtonState = Neutral | Focus | Active | Hover | Disabled deriving (Show, Bounded, Enum, Generic)
>>>
instance ToJSON ButtonState
>>>
instance ToSchema ButtonState
>>>
instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
>>>
type ImageUrl = T.Text
>>>
BSL.putStrLn $ encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
{"type":"object","properties":{"Focus":{"type":"string"},"Disabled":{"type":"string"},"Active":{"type":"string"},"Neutral":{"type":"string"},"Hover":{"type":"string"}}}
Note: this is only useful when key
is encoded with ToJSONKeyText
.
If it is encoded with ToJSONKeyValue
then a regular schema for [(key, value)]
is used.
Reusing ToParamSchema
paramSchemaToNamedSchema :: (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d) => SchemaOptions -> Proxy a -> NamedSchema Source #
Construct NamedSchema
usinng ToParamSchema
.
paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema Source #
Construct Schema
usinng ToParamSchema
.
Sketching Schema
s using ToJSON
Schema
ToJSON
sketchSchema :: ToJSON a => a -> Schema Source #
Make an unrestrictive sketch of a
based on a Schema
instance.
Produced schema can be used for further refinement.ToJSON
>>>
BSL.putStrLn $ encode $ sketchSchema "hello"
{"example":"hello","type":"string"}
>>>
BSL.putStrLn $ encode $ sketchSchema (1, 2, 3)
{"example":[1,2,3],"items":{"type":"number"},"type":"array"}
>>>
BSL.putStrLn $ encode $ sketchSchema ("Jack", 25)
{"example":["Jack",25],"items":[{"type":"string"},{"type":"number"}],"type":"array"}
>>>
data Person = Person { name :: String, age :: Int } deriving (Generic)
>>>
instance ToJSON Person
>>>
BSL.putStrLn $ encode $ sketchSchema (Person "Jack" 25)
{"example":{"age":25,"name":"Jack"},"required":["age","name"],"type":"object","properties":{"age":{"type":"number"},"name":{"type":"string"}}}
sketchStrictSchema :: ToJSON a => a -> Schema Source #
Make a restrictive sketch of a
based on a Schema
instance.
Produced schema uses as much constraints as possible.ToJSON
>>>
BSL.putStrLn $ encode $ sketchStrictSchema "hello"
{"maxLength":5,"pattern":"hello","minLength":5,"type":"string","enum":["hello"]}
>>>
BSL.putStrLn $ encode $ sketchStrictSchema (1, 2, 3)
{"minItems":3,"uniqueItems":true,"items":[{"maximum":1,"minimum":1,"multipleOf":1,"type":"number","enum":[1]},{"maximum":2,"minimum":2,"multipleOf":2,"type":"number","enum":[2]},{"maximum":3,"minimum":3,"multipleOf":3,"type":"number","enum":[3]}],"maxItems":3,"type":"array","enum":[[1,2,3]]}
>>>
BSL.putStrLn $ encode $ sketchStrictSchema ("Jack", 25)
{"minItems":2,"uniqueItems":true,"items":[{"maxLength":4,"pattern":"Jack","minLength":4,"type":"string","enum":["Jack"]},{"maximum":25,"minimum":25,"multipleOf":25,"type":"number","enum":[25]}],"maxItems":2,"type":"array","enum":[["Jack",25]]}
>>>
data Person = Person { name :: String, age :: Int } deriving (Generic)
>>>
instance ToJSON Person
>>>
BSL.putStrLn $ encode $ sketchStrictSchema (Person "Jack" 25)
{"minProperties":2,"required":["age","name"],"maxProperties":2,"type":"object","enum":[{"age":25,"name":"Jack"}],"properties":{"age":{"maximum":25,"minimum":25,"multipleOf":25,"type":"number","enum":[25]},"name":{"maxLength":4,"pattern":"Jack","minLength":4,"type":"string","enum":["Jack"]}}}
Inlining Schema
s
Schema
inlineNonRecursiveSchemas :: Data s => Definitions Schema -> s -> s Source #
Inline all non-recursive schemas for which the definition
can be found in
.Definitions
inlineAllSchemas :: Data s => Definitions Schema -> s -> s Source #
Inline all schema references for which the definition
can be found in
.Definitions
WARNING:
will produce infinite schemas
when inlining recursive schemas.inlineAllSchemas
inlineSchemas :: Data s => [Text] -> Definitions Schema -> s -> s Source #
Inline any referenced schema if its name is in the given list.
NOTE: if a referenced schema is not found in definitions it stays referenced even if it appears in the list of names.
WARNING:
will produce infinite schemas
when inlining recursive schemas.inlineSchemas
inlineSchemasWhen :: Data s => (Text -> Bool) -> Definitions Schema -> s -> s Source #
Inline any referenced schema if its name satisfies given predicate.
NOTE: if a referenced schema is not found in definitions the predicate is ignored and schema stays referenced.
WARNING:
will produce infinite schemas
when inlining recursive schemas.inlineSchemasWhen
Generic encoding configuration
data SchemaOptions Source #
Options that specify how to encode your type to Swagger schema.
SchemaOptions | |
|
defaultSchemaOptions :: SchemaOptions Source #
Default encoding
.SchemaOptions
SchemaOptions
{fieldLabelModifier
= id ,constructorTagModifier
= id ,datatypeNameModifier
= id ,allNullaryToStringTag
= True ,unwrapUnaryRecords
= False ,sumEncoding
=defaultTaggedObject
}
fromAesonOptions :: Options -> SchemaOptions Source #
Convert Options
to SchemaOptions
.
Specifically the following fields get copied:
Note that these fields have no effect on SchemaOptions
:
The rest is defined as in defaultSchemaOptions
.
Since: 2.2.1