git.iko.soy/typeable/openapi3
on c121663 help sign in
typeable/openapi3/src/Data/Swagger/SchemaOptions.hs
72 lines 2.4 kB history raw
1{-# LANGUAGE RecordWildCards #-}
2-- |
3-- Module: Data.Swagger.SchemaOptions
4-- Maintainer: Nickolay Kudasov <nickolay@getshoptv.com>
5-- Stability: experimental
6--
7-- Generic deriving options for @'ToParamSchema'@ and @'ToSchema'@.
8module Data.Swagger.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 }
27-- | Default encoding @'SchemaOptions'@.
28--
29-- @
30-- 'SchemaOptions'
31-- { 'fieldLabelModifier' = id
32-- , 'constructorTagModifier' = id
33-- , 'datatypeNameModifier' = id
34-- , 'allNullaryToStringTag' = True
35-- , 'unwrapUnaryRecords' = False
36-- }
37-- @
38defaultSchemaOptions :: SchemaOptions
39defaultSchemaOptions = SchemaOptions
40 { fieldLabelModifier = id
41 , constructorTagModifier = id
42 , datatypeNameModifier = id
43 , allNullaryToStringTag = True
44 , unwrapUnaryRecords = False
45 }
47-- | Convert 'Aeson.Options' to 'SchemaOptions'.
48--
49-- Specifically the following fields get copied:
50--
51-- * 'fieldLabelModifier'
52-- * 'constructorTagModifier'
53-- * 'allNullaryToStringTag'
54-- * 'unwrapUnaryRecords'
55--
56-- Note that these fields have no effect on `SchemaOptions`:
57--
58-- * 'Aeson.omitNothingFields'
59-- * 'Aeson.sumEncoding'
60-- * 'Aeson.tagSingleConstructors'
61--
62-- The rest is defined as in 'defaultSchemaOptions'.
63--
64-- @since 2.2.1
65--
66fromAesonOptions :: Aeson.Options -> SchemaOptions
67fromAesonOptions opts = defaultSchemaOptions
68 { fieldLabelModifier = Aeson.fieldLabelModifier opts
69 , constructorTagModifier = Aeson.constructorTagModifier opts
70 , allNullaryToStringTag = Aeson.allNullaryToStringTag opts
71 , unwrapUnaryRecords = Aeson.unwrapUnaryRecords opts
72 }