git.iko.soy/typeable/openapi3
on master help sign in
Tune nullary schema implementation
commit24c08faaf75a4b78a533c8966d9336fbd96ffdcd authorOleg Grenrus <oleg.grenrus@iki.fi> dateSat 24 Nov 2018, 20:00 (8 years ago) parent86658dd
1 file changed+17−3
src/Data/Swagger/Internal.hs+17−3
src/Data/Swagger/Internal.hs+17−3
@@ -66,6 +66,7 @@ import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
6666#endif
6767
6868-- $setup
69+-- >>> :seti -XDataKinds
6970-- >>> import Data.Aeson
7071
7172-- | A list of definitions that can be used in references.
@@ -1067,7 +1068,8 @@ instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
10671068 toJSON (SwaggerItemsArray []) = object
10681069 [ "items" .= object []
10691070 , "maxItems" .= (0 :: Int)
1070− , "example" .= ([] :: [()]) ]
1071+ , "example" .= Array mempty
1072+ ]
10711073 toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
10721074
10731075instance ToJSON Host where
@@ -1205,9 +1207,21 @@ instance FromJSON (SwaggerItems 'SwaggerKindParamOtherSchema) where
12051207 <$> o .:? "collectionFormat"
12061208 <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
12071209
1210+-- |
1211+--
1212+-- >>> decode "{}" :: Maybe (SwaggerItems 'SwaggerKindSchema)
1213+-- Just (SwaggerItemsArray [])
1214+--
1215+-- >>> eitherDecode "{\"$ref\":\"#/definitions/example\"}" :: Either String (SwaggerItems 'SwaggerKindSchema)
1216+-- Right (SwaggerItemsObject (Ref (Reference {getReference = "example"})))
1217+--
1218+-- >>> eitherDecode "[{\"$ref\":\"#/definitions/example\"}]" :: Either String (SwaggerItems 'SwaggerKindSchema)
1219+-- Right (SwaggerItemsArray [Ref (Reference {getReference = "example"})])
1220+--
12081221instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
1209− parseJSON js | js == object [] = pure $ SwaggerItemsArray [] -- Nullary schema.
1210− parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1222+ parseJSON js@(Object obj)
1223+ | null obj = pure $ SwaggerItemsArray [] -- Nullary schema.
1224+ | otherwise = SwaggerItemsObject <$> parseJSON js
12111225 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
12121226 parseJSON _ = empty
12131227