git.iko.soy/typeable/openapi3
on 340f666 help sign in
typeable/openapi3/test/Data/Swagger/ParamSchemaSpec.hs
114 lines 3.0 kB history raw
1{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE TemplateHaskell #-}
4{-# LANGUAGE QuasiQuotes #-}
5module Data.Swagger.ParamSchemaSpec where
7import Data.Aeson
8import Data.Aeson.QQ
9import Data.Char
10import Data.Proxy
11import GHC.Generics
13import Data.Swagger
15import SpecCommon
16import Test.Hspec
18checkToParamSchema :: ToParamSchema a => Proxy a -> Value -> Spec
19checkToParamSchema proxy js = (toParamSchema proxy :: ParamSchema Param) <=> js
21spec :: Spec
22spec = do
23 describe "Generic ToParamSchema" $ do
24 context "Unit" $ checkToParamSchema (Proxy :: Proxy Unit) unitSchemaJSON
25 context "Color (bounded enum)" $ checkToParamSchema (Proxy :: Proxy Color) colorSchemaJSON
26 context "Status (constructorTagModifier)" $ checkToParamSchema (Proxy :: Proxy Status) statusSchemaJSON
27 context "Unary records" $ do
28 context "Email (unary record)" $ checkToParamSchema (Proxy :: Proxy Email) emailSchemaJSON
29 context "UserId (non-record newtype)" $ checkToParamSchema (Proxy :: Proxy UserId) userIdSchemaJSON
31main :: IO ()
32main = hspec spec
34-- ========================================================================
35-- Unit type
36-- ========================================================================
38data Unit = Unit deriving (Generic)
39instance ToParamSchema Unit
41unitSchemaJSON :: Value
42unitSchemaJSON = [aesonQQ|
44 "type": "string",
45 "enum": ["Unit"]
47|]
49-- ========================================================================
50-- Color (enum)
51-- ========================================================================
52data Color
53 = Red
54 | Green
55 | Blue
56 deriving (Generic)
57instance ToParamSchema Color
59colorSchemaJSON :: Value
60colorSchemaJSON = [aesonQQ|
62 "type": "string",
63 "enum": ["Red", "Green", "Blue"]
65|]
67-- ========================================================================
68-- Status (constructorTagModifier)
69-- ========================================================================
71data Status = StatusOk | StatusError deriving (Generic)
73instance ToParamSchema Status where
74 toParamSchema = genericToParamSchema defaultSchemaOptions
75 { constructorTagModifier = map toLower . drop (length "Status") }
77statusSchemaJSON :: Value
78statusSchemaJSON = [aesonQQ|
80 "type": "string",
81 "enum": ["ok", "error"]
83|]
85-- ========================================================================
86-- Email (newtype with unwrapUnaryRecords set to True)
87-- ========================================================================
89newtype Email = Email { getEmail :: String }
90 deriving (Generic)
91instance ToParamSchema Email
93emailSchemaJSON :: Value
94emailSchemaJSON = [aesonQQ|
96 "type": "string"
98|]
100-- ========================================================================
101-- UserId (non-record newtype)
102-- ========================================================================
104newtype UserId = UserId Integer
105 deriving (Generic)
106instance ToParamSchema UserId
108userIdSchemaJSON :: Value
109userIdSchemaJSON = [aesonQQ|
111 "type": "integer"
113|]