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

Add "format" field for uint32 and uint64 #215

Merged
merged 2 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Data/Swagger/Internal/ParamSchema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ instance ToParamSchema Int64 where
instance ToParamSchema Word where toParamSchema = toParamSchemaBoundedIntegral
instance ToParamSchema Word8 where toParamSchema = toParamSchemaBoundedIntegral
instance ToParamSchema Word16 where toParamSchema = toParamSchemaBoundedIntegral
instance ToParamSchema Word32 where toParamSchema = toParamSchemaBoundedIntegral
instance ToParamSchema Word64 where toParamSchema = toParamSchemaBoundedIntegral

instance ToParamSchema Word32 where
toParamSchema proxy = toParamSchemaBoundedIntegral proxy & format ?~ "int32"

instance ToParamSchema Word64 where
toParamSchema proxy = toParamSchemaBoundedIntegral proxy & format ?~ "int64"

-- | Default plain schema for @'Bounded'@, @'Integral'@ types.
--
Expand Down
29 changes: 28 additions & 1 deletion test/Data/Swagger/CommonTestTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Data.Map (Map)
import Data.Proxy
import Data.Set (Set)
import qualified Data.Text as Text
import Data.Word
import GHC.Generics

import Data.Swagger
Expand Down Expand Up @@ -676,4 +677,30 @@ timeOfDayParamSchemaJSON = [aesonQQ|
"type": "string",
"format": "hh:MM:ss"
}
|]
|]


-- ========================================================================
-- UnsignedInts
-- ========================================================================
data UnsignedInts = UnsignedInts
{ unsignedIntsUint32 :: Word32
, unsignedIntsUint64 :: Word64
} deriving (Generic)

instance ToSchema UnsignedInts where
declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
{ fieldLabelModifier = map toLower . drop (length "unsignedInts") }

unsignedIntsSchemaJSON :: Value
unsignedIntsSchemaJSON = [aesonQQ|
{
"type": "object",
"properties":
{
"uint32": { "type": "integer", "format": "int32", "minimum": 0, "maximum": 4294967295 },
"uint64": { "type": "integer", "format": "int64", "minimum": 0, "maximum": 18446744073709551615 }
},
"required": ["uint32", "uint64"]
}
|]
1 change: 1 addition & 0 deletions test/Data/Swagger/SchemaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ spec = do
context "Status (sum of unary constructors)" $ checkToSchema (Proxy :: Proxy Status) statusSchemaJSON
context "Character (ref and record sum)" $ checkToSchema (Proxy :: Proxy Character) characterSchemaJSON
context "Light (sum with unwrapUnaryRecords)" $ checkToSchema (Proxy :: Proxy Light) lightSchemaJSON
context "UnsignedInts" $ checkToSchema (Proxy :: Proxy UnsignedInts) unsignedIntsSchemaJSON
context "Schema name" $ do
context "String" $ checkSchemaName Nothing (Proxy :: Proxy String)
context "(Int, Float)" $ checkSchemaName Nothing (Proxy :: Proxy (Int, Float))
Expand Down