git.iko.soy/typeable/openapi3
on master help sign in
Add "format" field for uint32 and uint64 (#215)

* Add failing test for unsigned integers * Update ToParamSchema instances for Word32/Word64

commit023e2ac2cb5ba2b08928826c68d6d8d9ca3c4326 authorPaul Young <84700+paulyoung@users.noreply.github.com> dateFri 15 May 2020, 00:22 (6 years ago) parent5289fb6
3 files changed+35−3
src/Data/Swagger/Internal/ParamSchema.hs+6−2 test/Data/Swagger/CommonTestTypes.hs+28−1 test/Data/Swagger/SchemaSpec.hs+1
src/Data/Swagger/Internal/ParamSchema.hs+6−2
@@ -147,8 +147,12 @@ instance ToParamSchema Int64 where
147147instance ToParamSchema Word where toParamSchema = toParamSchemaBoundedIntegral
148148instance ToParamSchema Word8 where toParamSchema = toParamSchemaBoundedIntegral
149149instance ToParamSchema Word16 where toParamSchema = toParamSchemaBoundedIntegral
150−instance ToParamSchema Word32 where toParamSchema = toParamSchemaBoundedIntegral
151−instance ToParamSchema Word64 where toParamSchema = toParamSchemaBoundedIntegral
150+
151+instance ToParamSchema Word32 where
152+ toParamSchema proxy = toParamSchemaBoundedIntegral proxy & format ?~ "int32"
153+
154+instance ToParamSchema Word64 where
155+ toParamSchema proxy = toParamSchemaBoundedIntegral proxy & format ?~ "int64"
152156
153157-- | Default plain schema for @'Bounded'@, @'Integral'@ types.
154158--
test/Data/Swagger/CommonTestTypes.hs+28−1
@@ -15,6 +15,7 @@ import Data.Map (Map)
1515import Data.Proxy
1616import Data.Set (Set)
1717import qualified Data.Text as Text
18+import Data.Word
1819import GHC.Generics
1920
2021import Data.Swagger
@@ -676,4 +677,30 @@ timeOfDayParamSchemaJSON = [aesonQQ|
676677 "type": "string",
677678 "format": "hh:MM:ss"
678679}
679−|]
680+|]
681+
682+
683+-- ========================================================================
684+-- UnsignedInts
685+-- ========================================================================
686+data UnsignedInts = UnsignedInts
687+ { unsignedIntsUint32 :: Word32
688+ , unsignedIntsUint64 :: Word64
689+ } deriving (Generic)
690+
691+instance ToSchema UnsignedInts where
692+ declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
693+ { fieldLabelModifier = map toLower . drop (length "unsignedInts") }
694+
695+unsignedIntsSchemaJSON :: Value
696+unsignedIntsSchemaJSON = [aesonQQ|
697+{
698+ "type": "object",
699+ "properties":
700+ {
701+ "uint32": { "type": "integer", "format": "int32", "minimum": 0, "maximum": 4294967295 },
702+ "uint64": { "type": "integer", "format": "int64", "minimum": 0, "maximum": 18446744073709551615 }
703+ },
704+ "required": ["uint32", "uint64"]
705+}
706+|]
test/Data/Swagger/SchemaSpec.hs+1
@@ -85,6 +85,7 @@ spec = do
8585 context "Status (sum of unary constructors)" $ checkToSchema (Proxy :: Proxy Status) statusSchemaJSON
8686 context "Character (ref and record sum)" $ checkToSchema (Proxy :: Proxy Character) characterSchemaJSON
8787 context "Light (sum with unwrapUnaryRecords)" $ checkToSchema (Proxy :: Proxy Light) lightSchemaJSON
88+ context "UnsignedInts" $ checkToSchema (Proxy :: Proxy UnsignedInts) unsignedIntsSchemaJSON
8889 context "Schema name" $ do
8990 context "String" $ checkSchemaName Nothing (Proxy :: Proxy String)
9091 context "(Int, Float)" $ checkSchemaName Nothing (Proxy :: Proxy (Int, Float))