git.iko.soy/typeable/swagger2
on master help sign in
Fix #138 schema type becomes optional

According to OpenAPI as it mentioned in #131, "type" might not be specified. It is implemented currently in Kubernetes. Thus, interface is changing with this fix. CHANGELOG should be also updated.

commit0ccfe218225916d71e681062f9bea0e06d400603 authorAndrey Prokopenko <andy@getshoptv.com> dateWed 22 Aug 2018, 16:03 (8 years ago) parentcf4b03b
4 files changed+41−42
examples/hackage.hs+1−1 src/Data/Swagger/Internal.hs+1−1 src/Data/Swagger/Internal/Schema.hs+1−1 src/Data/Swagger/Internal/Schema/Validation.hs+38−39
examples/hackage.hs+1−1
@@ -27,7 +27,7 @@ instance ToSchema UserSummary where
2727 usernameSchema <- declareSchemaRef (Proxy :: Proxy Username)
2828 useridSchema <- declareSchemaRef (Proxy :: Proxy Int)
2929 return $ NamedSchema (Just "UserSummary") $ mempty
30− & type_ .~ SwaggerObject
30+ & type_ ?~ SwaggerObject
3131 & properties .~
3232 [ ("summaryUsername", usernameSchema )
3333 , ("summaryUserid" , useridSchema )
src/Data/Swagger/Internal.hs+1−1
@@ -610,7 +610,7 @@ data ParamSchema (t :: SwaggerKind *) = ParamSchema
610610 , _paramSchemaMultipleOf :: Maybe Scientific
611611 } deriving (Eq, Show, Generic, Typeable)
612612
613−deriving instance (Typeable k, Data (SwaggerType k), Data (SwaggerItems k)) => Data (ParamSchema k)
613+deriving instance (Typeable k, Data (Maybe (SwaggerType k)), Data (SwaggerItems k)) => Data (ParamSchema k)
614614
615615data Xml = Xml
616616 { -- | Replaces the name of the element/attribute used for the described schema property.
src/Data/Swagger/Internal/Schema.hs+1−1
@@ -335,7 +335,7 @@ sketchSchema = sketch . toJSON
335335
336336 go Null = mempty & type_ ?~ SwaggerNull
337337 go (Bool _) = mempty & type_ ?~ SwaggerBoolean
338− go (String _) = mempty & type_ ?~ SwaggerString
338+ go (String _) = mempty & type_ ?~ SwaggerString
339339 go (Number _) = mempty & type_ ?~ SwaggerNumber
340340 go (Array xs) = mempty
341341 & type_ ?~ SwaggerArray
src/Data/Swagger/Internal/Schema/Validation.hs+38−39
@@ -382,45 +382,44 @@ inferParamSchemaTypes sch = concat
382382
383383validateSchemaType :: Value -> Validation Schema ()
384384validateSchemaType value = withSchema $ \sch ->
385− case sch ^. type_ of
386− Just explicitType -> validateSchemaTypeAs explicitType value
387− Nothing ->
388− case inferSchemaTypes sch of
389− [t] -> validateSchemaTypeAs t value
390− [] -> invalid $ "unable to infer type for schema, please provide type explicitly"
391− ts -> invalid $ "unable to infer type for schema, possible candidates: " ++ intercalate ", " (map show ts)
392−
393−validateSchemaTypeAs
394− :: SwaggerType 'SwaggerKindSchema -> Value -> Validation Schema ()
395−validateSchemaTypeAs t value =
396− case (t, value) of
397− (SwaggerNull, Null) -> valid
398− (SwaggerBoolean, Bool _) -> valid
399− (SwaggerInteger, Number n) -> sub_ paramSchema (validateInteger n)
400− (SwaggerNumber, Number n) -> sub_ paramSchema (validateNumber n)
401− (SwaggerString, String s) -> sub_ paramSchema (validateString s)
402− (SwaggerArray, Array xs) -> sub_ paramSchema (validateArray xs)
403− (SwaggerObject, Object o) -> validateObject o
404− _ -> invalid $ "expected JSON value of type " ++ show t
385+ case (sch ^. type_, value) of
386+ (Just SwaggerNull, Null) -> valid
387+ (Just SwaggerBoolean, Bool _) -> valid
388+ (Just SwaggerInteger, Number n) -> sub_ paramSchema (validateInteger n)
389+ (Just SwaggerNumber, Number n) -> sub_ paramSchema (validateNumber n)
390+ (Just SwaggerString, String s) -> sub_ paramSchema (validateString s)
391+ (Just SwaggerArray, Array xs) -> sub_ paramSchema (validateArray xs)
392+ (Just SwaggerObject, Object o) -> validateObject o
393+ (Nothing, Null) -> valid
394+ (Nothing, Bool _) -> valid
395+ -- Number by default
396+ (Nothing, Number n) -> sub_ paramSchema (validateNumber n)
397+ (Nothing, String s) -> sub_ paramSchema (validateString s)
398+ (Nothing, Array xs) -> sub_ paramSchema (validateArray xs)
399+ (Nothing, Object o) -> validateObject o
400+ param@(t, _) -> invalid $ "expected JSON value of type " ++ showType param
405401
406402validateParamSchemaType :: Value -> Validation (ParamSchema t) ()
407403validateParamSchemaType value = withSchema $ \sch ->
408− case sch ^. type_ of
409− Just explicitType -> validateParamSchemaTypeAs explicitType value
410− Nothing ->
411− case inferParamSchemaTypes sch of
412− [t] -> validateParamSchemaTypeAs t value
413− [] -> invalid $ "unable to infer type for schema, please provide type explicitly"
414− ts -> invalid $ "unable to infer type for schema, possible candidates: " ++ intercalate ", " (map show ts)
415−
416−validateParamSchemaTypeAs
417− :: SwaggerType t -> Value -> Validation (ParamSchema t) ()
418−validateParamSchemaTypeAs t value =
419− case (t, value) of
420− (SwaggerBoolean, Bool _) -> valid
421− (SwaggerInteger, Number n) -> validateInteger n
422− (SwaggerNumber, Number n) -> validateNumber n
423− (SwaggerString, String s) -> validateString s
424− (SwaggerArray, Array xs) -> validateArray xs
425− _ -> invalid $ "expected JSON value of type " ++ show t
426−
404+ case (sch ^. type_, value) of
405+ (Just SwaggerBoolean, Bool _) -> valid
406+ (Just SwaggerInteger, Number n) -> validateInteger n
407+ (Just SwaggerNumber, Number n) -> validateNumber n
408+ (Just SwaggerString, String s) -> validateString s
409+ (Just SwaggerArray, Array xs) -> validateArray xs
410+ (Nothing, Bool _) -> valid
411+ -- Number by default
412+ (Nothing, Number n) -> validateNumber n
413+ (Nothing, String s) -> validateString s
414+ (Nothing, Array xs) -> validateArray xs
415+ (t, _) -> invalid $ "expected JSON value of type " ++ show t
416+ param@(t, _) -> invalid $ "expected JSON value of type " ++ showType param
417+
418+showType :: (Maybe (SwaggerType t), Value) -> String
419+showType (Just type_, _) = show type_
420+showType (Nothing, Null) = "SwaggerNull"
421+showType (Nothing, Bool _) = "SwaggerBoolean"
422+showType (Nothing, Number _) = "SwaggerNumber"
423+showType (Nothing, String _) = "SwaggerString"
424+showType (Nothing, Array _) = "SwaggerArray"
425+showType (Nothing, Object _) = "SwaggerObject"