Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#87 add instance for time of day #196

Merged
merged 8 commits into from
Nov 9, 2019
6 changes: 6 additions & 0 deletions src/Data/Swagger/Internal/ParamSchema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ timeParamSchema fmt = mempty
instance ToParamSchema Day where
toParamSchema _ = timeParamSchema "date"

-- |
-- >>> toParamSchema (Proxy :: Proxy TimeOfDay) ^. format
-- Just "hh:MM:ss"
instance ToParamSchema TimeOfDay where
fisx marked this conversation as resolved.
Show resolved Hide resolved
toParamSchema _ = timeParamSchema "hh:MM:ss"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are hh, ss small, but MM is capitalized? Could you explain this in the haddocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I am not sure if it makes a difference if MM is capitalized or not. I just took the schema that was used for LocalTime ("yyyy-mm-ddThh:MM:ss") and removed the parts I didn't need.
So basically I left the M capitalized for being consistent, but I don't know if there is another reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm is month. MM is minutes.


-- |
-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. format
-- Just "yyyy-mm-ddThh:MM:ss"
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Swagger/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ class ToSchema a where
Proxy a -> Declare (Definitions Schema) NamedSchema
declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions

instance ToSchema TimeOfDay where
declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
& example ?~ toJSON (TimeOfDay 12 33 15)

-- | Convert a type into a schema and declare all used schema definitions.
declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
declareSchema = fmap _namedSchemaSchema . declareNamedSchema
Expand Down
29 changes: 29 additions & 0 deletions test/Data/Swagger/CommonTestTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,32 @@ singleMaybeFieldSchemaJSON = [aesonQQ|
}
}
|]


-- ========================================================================
-- TimeOfDay
-- ========================================================================
data TimeOfDay
= Int
| Pico
deriving (Generic)
instance ToSchema TimeOfDay
instance ToParamSchema TimeOfDay


timeOfDaySchemaJSON :: Value
timeOfDaySchemaJSON = [aesonQQ|
{
"example": "12:33:15",
"type": "string",
"format": "hh:MM:ss"
}
|]

timeOfDayParamSchemaJSON :: Value
timeOfDayParamSchemaJSON = [aesonQQ|
{
"type": "string",
"format": "hh:MM:ss"
}
|]
7 changes: 6 additions & 1 deletion test/Data/Swagger/ParamSchemaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
module Data.Swagger.ParamSchemaSpec where

import Data.Aeson (Value)
import Data.Aeson
import Data.Aeson.QQ.Simple
import Data.Char
import Data.Proxy
Expand All @@ -17,6 +18,9 @@ import Data.Swagger.Internal (SwaggerKind(..))
import Data.Swagger.CommonTestTypes
import SpecCommon
import Test.Hspec
import Data.Time.LocalTime

import qualified Data.HashMap.Strict as HM

checkToParamSchema :: ToParamSchema a => Proxy a -> Value -> Spec
checkToParamSchema proxy js = (toParamSchema proxy :: ParamSchema ('SwaggerKindNormal Param)) <=> js
Expand All @@ -30,6 +34,7 @@ spec = do
context "Unary records" $ do
context "Email (unary record)" $ checkToParamSchema (Proxy :: Proxy Email) emailSchemaJSON
context "UserId (non-record newtype)" $ checkToParamSchema (Proxy :: Proxy UserId) userIdSchemaJSON
context "TimeOfDay" $ checkToParamSchema (Proxy :: Proxy Data.Time.LocalTime.TimeOfDay) timeOfDayParamSchemaJSON

main :: IO ()
main = hspec spec
4 changes: 4 additions & 0 deletions test/Data/Swagger/SchemaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import Data.Swagger.CommonTestTypes
import SpecCommon
import Test.Hspec

import qualified Data.HashMap.Strict as HM
import Data.Time.LocalTime

checkToSchema :: ToSchema a => Proxy a -> Value -> Spec
checkToSchema proxy js = toSchema proxy <=> js

Expand Down Expand Up @@ -105,6 +108,7 @@ spec = do
context "MyRoseTree' (inlineNonRecursiveSchemas)" $ checkInlinedRecSchema (Proxy :: Proxy MyRoseTree') myRoseTreeSchemaJSON'
describe "Bounded Enum key mapping" $ do
context "ButtonImages" $ checkToSchema (Proxy :: Proxy ButtonImages) buttonImagesSchemaJSON
context "TimeOfDay" $ checkToSchema (Proxy :: Proxy Data.Time.LocalTime.TimeOfDay ) timeOfDaySchemaJSON
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context "TimeOfDay" $ checkToSchema (Proxy :: Proxy Data.Time.LocalTime.TimeOfDay ) timeOfDaySchemaJSON
context "TimeOfDay" $ checkToSchema (Proxy :: Proxy Data.Time.LocalTime.TimeOfDay) timeOfDaySchemaJSON


main :: IO ()
main = hspec spec