git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #196 from anneloreegger/87_add_instance_for_TimeOfDay
1874b98fisx
src/Data/Swagger/Internal/ParamSchema.hs+6
@@ -193,6 +193,12 @@ timeParamSchema fmt = mempty
193instance ToParamSchema Day where
193instance ToParamSchema Day where
194 toParamSchema _ = timeParamSchema "date"
194 toParamSchema _ = timeParamSchema "date"
195
195
196+-- |
197+-- >>> toParamSchema (Proxy :: Proxy TimeOfDay) ^. format
198+-- Just "hh:MM:ss"
199+instance ToParamSchema TimeOfDay where
200+ toParamSchema _ = timeParamSchema "hh:MM:ss"
201+
196-- |
202-- |
197-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. format
203-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. format
198-- Just "yyyy-mm-ddThh:MM:ss"
204-- Just "yyyy-mm-ddThh:MM:ss"
src/Data/Swagger/Internal/Schema.hs+4
@@ -147,6 +147,10 @@ class ToSchema a where
147 Proxy a -> Declare (Definitions Schema) NamedSchema
147 Proxy a -> Declare (Definitions Schema) NamedSchema
148 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
148 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
149
149
150+instance ToSchema TimeOfDay where
151+ declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152+ & example ?~ toJSON (TimeOfDay 12 33 15)
153+
150-- | Convert a type into a schema and declare all used schema definitions.
154-- | Convert a type into a schema and declare all used schema definitions.
151declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
155declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
152declareSchema = fmap _namedSchemaSchema . declareNamedSchema
156declareSchema = fmap _namedSchemaSchema . declareNamedSchema
test/Data/Swagger/CommonTestTypes.hs+29
@@ -648,3 +648,32 @@ singleMaybeFieldSchemaJSON = [aesonQQ|
648 }
648 }
649}
649}
650|]
650|]
651+
652+
653+-- ========================================================================
654+-- TimeOfDay
655+-- ========================================================================
656+data TimeOfDay
657+ = Int
658+ | Pico
659+ deriving (Generic)
660+instance ToSchema TimeOfDay
661+instance ToParamSchema TimeOfDay
662+
663+
664+timeOfDaySchemaJSON :: Value
665+timeOfDaySchemaJSON = [aesonQQ|
666+{
667+ "example": "12:33:15",
668+ "type": "string",
669+ "format": "hh:MM:ss"
670+}
671+|]
672+
673+timeOfDayParamSchemaJSON :: Value
674+timeOfDayParamSchemaJSON = [aesonQQ|
675+{
676+ "type": "string",
677+ "format": "hh:MM:ss"
678+}
679+|]
test/Data/Swagger/ParamSchemaSpec.hs+6−1
@@ -3,9 +3,10 @@
3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
4{-# LANGUAGE TemplateHaskell #-}
4{-# LANGUAGE TemplateHaskell #-}
5{-# LANGUAGE QuasiQuotes #-}
5{-# LANGUAGE QuasiQuotes #-}
6+{-# LANGUAGE OverloadedStrings #-}
6module Data.Swagger.ParamSchemaSpec where
7module Data.Swagger.ParamSchemaSpec where
7
8
8−import Data.Aeson (Value)
9+import Data.Aeson
9import Data.Aeson.QQ.Simple
10import Data.Aeson.QQ.Simple
10import Data.Char
11import Data.Char
11import Data.Proxy
12import Data.Proxy
@@ -17,6 +18,9 @@ import Data.Swagger.Internal (SwaggerKind(..))
17import Data.Swagger.CommonTestTypes
18import Data.Swagger.CommonTestTypes
18import SpecCommon
19import SpecCommon
19import Test.Hspec
20import Test.Hspec
21+import Data.Time.LocalTime
22+
23+import qualified Data.HashMap.Strict as HM
20
24
21checkToParamSchema :: ToParamSchema a => Proxy a -> Value -> Spec
25checkToParamSchema :: ToParamSchema a => Proxy a -> Value -> Spec
22checkToParamSchema proxy js = (toParamSchema proxy :: ParamSchema ('SwaggerKindNormal Param)) <=> js
26checkToParamSchema proxy js = (toParamSchema proxy :: ParamSchema ('SwaggerKindNormal Param)) <=> js
@@ -30,6 +34,7 @@ spec = do
30 context "Unary records" $ do
34 context "Unary records" $ do
31 context "Email (unary record)" $ checkToParamSchema (Proxy :: Proxy Email) emailSchemaJSON
35 context "Email (unary record)" $ checkToParamSchema (Proxy :: Proxy Email) emailSchemaJSON
32 context "UserId (non-record newtype)" $ checkToParamSchema (Proxy :: Proxy UserId) userIdSchemaJSON
36 context "UserId (non-record newtype)" $ checkToParamSchema (Proxy :: Proxy UserId) userIdSchemaJSON
37+ context "TimeOfDay" $ checkToParamSchema (Proxy :: Proxy Data.Time.LocalTime.TimeOfDay) timeOfDayParamSchemaJSON
33
38
34main :: IO ()
39main :: IO ()
35main = hspec spec
40main = hspec spec
test/Data/Swagger/SchemaSpec.hs+4
@@ -21,6 +21,9 @@ import Data.Swagger.CommonTestTypes
21import SpecCommon
21import SpecCommon
22import Test.Hspec
22import Test.Hspec
23
23
24+import qualified Data.HashMap.Strict as HM
25+import Data.Time.LocalTime
26+
24checkToSchema :: ToSchema a => Proxy a -> Value -> Spec
27checkToSchema :: ToSchema a => Proxy a -> Value -> Spec
25checkToSchema proxy js = toSchema proxy <=> js
28checkToSchema proxy js = toSchema proxy <=> js
26
29
@@ -105,6 +108,7 @@ spec = do
105 context "MyRoseTree' (inlineNonRecursiveSchemas)" $ checkInlinedRecSchema (Proxy :: Proxy MyRoseTree') myRoseTreeSchemaJSON'
108 context "MyRoseTree' (inlineNonRecursiveSchemas)" $ checkInlinedRecSchema (Proxy :: Proxy MyRoseTree') myRoseTreeSchemaJSON'
106 describe "Bounded Enum key mapping" $ do
109 describe "Bounded Enum key mapping" $ do
107 context "ButtonImages" $ checkToSchema (Proxy :: Proxy ButtonImages) buttonImagesSchemaJSON
110 context "ButtonImages" $ checkToSchema (Proxy :: Proxy ButtonImages) buttonImagesSchemaJSON
111+ context "TimeOfDay" $ checkToSchema (Proxy :: Proxy Data.Time.LocalTime.TimeOfDay) timeOfDaySchemaJSON
108
112
109main :: IO ()
113main :: IO ()
110main = hspec spec
114main = hspec spec