git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #196 from anneloreegger/87_add_instance_for_TimeOfDay

#87 add instance for time of day

commit1874b986d23d8a83d0aad091bd1c2e94bddd90b3 authorfisx <mf@zerobuzz.net> dateSat 9 Nov 2019, 21:41 (7 years ago) parentc1216633089c78
5 files changed+49−1
src/Data/Swagger/Internal/ParamSchema.hs+6 src/Data/Swagger/Internal/Schema.hs+4 test/Data/Swagger/CommonTestTypes.hs+29 test/Data/Swagger/ParamSchemaSpec.hs+6−1 test/Data/Swagger/SchemaSpec.hs+4
src/Data/Swagger/Internal/ParamSchema.hs+6
@@ -193,6 +193,12 @@ timeParamSchema fmt = mempty
193193instance ToParamSchema Day where
194194 toParamSchema _ = timeParamSchema "date"
195195
196+-- |
197+-- >>> toParamSchema (Proxy :: Proxy TimeOfDay) ^. format
198+-- Just "hh:MM:ss"
199+instance ToParamSchema TimeOfDay where
200+ toParamSchema _ = timeParamSchema "hh:MM:ss"
201+
196202-- |
197203-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. format
198204-- Just "yyyy-mm-ddThh:MM:ss"
src/Data/Swagger/Internal/Schema.hs+4
@@ -147,6 +147,10 @@ class ToSchema a where
147147 Proxy a -> Declare (Definitions Schema) NamedSchema
148148 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
149149
150+instance ToSchema TimeOfDay where
151+ declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152+ & example ?~ toJSON (TimeOfDay 12 33 15)
153+
150154-- | Convert a type into a schema and declare all used schema definitions.
151155declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
152156declareSchema = fmap _namedSchemaSchema . declareNamedSchema
test/Data/Swagger/CommonTestTypes.hs+29
@@ -648,3 +648,32 @@ singleMaybeFieldSchemaJSON = [aesonQQ|
648648 }
649649}
650650|]
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 @@
33{-# LANGUAGE ScopedTypeVariables #-}
44{-# LANGUAGE TemplateHaskell #-}
55{-# LANGUAGE QuasiQuotes #-}
6+{-# LANGUAGE OverloadedStrings #-}
67module Data.Swagger.ParamSchemaSpec where
78
8−import Data.Aeson (Value)
9+import Data.Aeson
910import Data.Aeson.QQ.Simple
1011import Data.Char
1112import Data.Proxy
@@ -17,6 +18,9 @@ import Data.Swagger.Internal (SwaggerKind(..))
1718import Data.Swagger.CommonTestTypes
1819import SpecCommon
1920import Test.Hspec
21+import Data.Time.LocalTime
22+
23+import qualified Data.HashMap.Strict as HM
2024
2125checkToParamSchema :: ToParamSchema a => Proxy a -> Value -> Spec
2226checkToParamSchema proxy js = (toParamSchema proxy :: ParamSchema ('SwaggerKindNormal Param)) <=> js
@@ -30,6 +34,7 @@ spec = do
3034 context "Unary records" $ do
3135 context "Email (unary record)" $ checkToParamSchema (Proxy :: Proxy Email) emailSchemaJSON
3236 context "UserId (non-record newtype)" $ checkToParamSchema (Proxy :: Proxy UserId) userIdSchemaJSON
37+ context "TimeOfDay" $ checkToParamSchema (Proxy :: Proxy Data.Time.LocalTime.TimeOfDay) timeOfDayParamSchemaJSON
3338
3439main :: IO ()
3540main = hspec spec
test/Data/Swagger/SchemaSpec.hs+4
@@ -21,6 +21,9 @@ import Data.Swagger.CommonTestTypes
2121import SpecCommon
2222import Test.Hspec
2323
24+import qualified Data.HashMap.Strict as HM
25+import Data.Time.LocalTime
26+
2427checkToSchema :: ToSchema a => Proxy a -> Value -> Spec
2528checkToSchema proxy js = toSchema proxy <=> js
2629
@@ -105,6 +108,7 @@ spec = do
105108 context "MyRoseTree' (inlineNonRecursiveSchemas)" $ checkInlinedRecSchema (Proxy :: Proxy MyRoseTree') myRoseTreeSchemaJSON'
106109 describe "Bounded Enum key mapping" $ do
107110 context "ButtonImages" $ checkToSchema (Proxy :: Proxy ButtonImages) buttonImagesSchemaJSON
111+ context "TimeOfDay" $ checkToSchema (Proxy :: Proxy Data.Time.LocalTime.TimeOfDay) timeOfDaySchemaJSON
108112
109113main :: IO ()
110114main = hspec spec