git.iko.soy/typeable/openapi3
on bb1c64f help sign in
typeable/openapi3/src/Data/OpenApi/SchemaOptions.hs
76 lines 2.7 kB history raw
1{-# LANGUAGE RecordWildCards #-}
2-- |
3-- Module: Data.OpenApi.SchemaOptions
4-- Maintainer: Nickolay Kudasov <nickolay@getshoptv.com>
5-- Stability: experimental
6--
7-- Generic deriving options for @'ToParamSchema'@ and @'ToSchema'@.
8module Data.OpenApi.SchemaOptions where
10import qualified Data.Aeson.Types as Aeson
12-- | Options that specify how to encode your type to Swagger schema.
13data SchemaOptions = SchemaOptions
14 { -- | Function applied to field labels. Handy for removing common record prefixes for example.
15 fieldLabelModifier :: String -> String
16 -- | Function applied to constructor tags which could be handy for lower-casing them for example.
17 , constructorTagModifier :: String -> String
18 -- | Function applied to datatype name.
19 , datatypeNameModifier :: String -> String
20 -- | If @'True'@ the constructors of a datatype, with all nullary constructors,
21 -- will be encoded to a string enumeration schema with the constructor tags as possible values.
22 , allNullaryToStringTag :: Bool
23 -- | Hide the field name when a record constructor has only one field, like a newtype.
24 , unwrapUnaryRecords :: Bool
25 -- | Specifies how to encode constructors of a sum datatype.
26 , sumEncoding :: Aeson.SumEncoding
27 }
29-- | Default encoding @'SchemaOptions'@.
30--
31-- @
32-- 'SchemaOptions'
33-- { 'fieldLabelModifier' = id
34-- , 'constructorTagModifier' = id
35-- , 'datatypeNameModifier' = id
36-- , 'allNullaryToStringTag' = True
37-- , 'unwrapUnaryRecords' = False
38-- , 'sumEncoding' = 'Aeson.defaultTaggedObject'
39-- }
40-- @
41defaultSchemaOptions :: SchemaOptions
42defaultSchemaOptions = SchemaOptions
43 { fieldLabelModifier = id
44 , constructorTagModifier = id
45 , datatypeNameModifier = id
46 , allNullaryToStringTag = True
47 , unwrapUnaryRecords = False
48 , sumEncoding = Aeson.defaultTaggedObject
49 }
51-- | Convert 'Aeson.Options' to 'SchemaOptions'.
52--
53-- Specifically the following fields get copied:
54--
55-- * 'fieldLabelModifier'
56-- * 'constructorTagModifier'
57-- * 'allNullaryToStringTag'
58-- * 'unwrapUnaryRecords'
59--
60-- Note that these fields have no effect on `SchemaOptions`:
61--
62-- * 'Aeson.omitNothingFields'
63-- * 'Aeson.tagSingleConstructors'
64--
65-- The rest is defined as in 'defaultSchemaOptions'.
66--
67-- @since 2.2.1
68--
69fromAesonOptions :: Aeson.Options -> SchemaOptions
70fromAesonOptions opts = defaultSchemaOptions
71 { fieldLabelModifier = Aeson.fieldLabelModifier opts
72 , constructorTagModifier = Aeson.constructorTagModifier opts
73 , allNullaryToStringTag = Aeson.allNullaryToStringTag opts
74 , unwrapUnaryRecords = Aeson.unwrapUnaryRecords opts
75 , sumEncoding = Aeson.sumEncoding opts
76 }