git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #168 from michalrus/nullary-schema-valid

Make nullary schemas valid

commit6fc1496b8f24c3c52a12ad88c3e4f41f2d1f0c4d authorOleg Grenrus <oleg.grenrus@iki.fi> dateSat 24 Nov 2018, 19:36 (8 years ago) parent042c4eb9d48823
2 files changed+23−3
src/Data/Swagger/Internal.hs+21−1 test/Data/Swagger/CommonTestTypes.hs+2−2
src/Data/Swagger/Internal.hs+21−1
@@ -65,6 +65,9 @@ import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
6565#define DEFINE_TOENCODING
6666#endif
6767
68+-- $setup
69+-- >>> import Data.Aeson
70+
6871-- | A list of definitions that can be used in references.
6972type Definitions = InsOrdHashMap Text
7073
@@ -1050,11 +1053,21 @@ instance ToJSON Header where
10501053 toJSON = sopSwaggerGenericToJSON
10511054 DEFINE_TOENCODING
10521055
1056+-- | As for nullary schema for 0-arity type constructors, see
1057+-- <https://github.com/GetShopTV/swagger2/issues/167>.
1058+--
1059+-- >>> encode (SwaggerItemsArray [])
1060+-- "{\"example\":[],\"items\":{},\"maxItems\":0}"
1061+--
10531062instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
10541063 toJSON (SwaggerItemsPrimitive fmt schema) = object
10551064 [ "collectionFormat" .= fmt
10561065 , "items" .= schema ]
10571066 toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1067+ toJSON (SwaggerItemsArray []) = object
1068+ [ "items" .= object []
1069+ , "maxItems" .= (0 :: Int)
1070+ , "example" .= ([] :: [()]) ]
10581071 toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
10591072
10601073instance ToJSON Host where
@@ -1171,7 +1184,13 @@ instance FromJSON SecurityScheme where
11711184 parseJSON = sopSwaggerGenericParseJSON
11721185
11731186instance FromJSON Schema where
1174− parseJSON = sopSwaggerGenericParseJSON
1187+ parseJSON = fmap nullaryCleanup . sopSwaggerGenericParseJSON
1188+ where nullaryCleanup :: Schema -> Schema
1189+ nullaryCleanup s@Schema{_schemaParamSchema=ps} =
1190+ if _paramSchemaItems ps == Just (SwaggerItemsArray [])
1191+ then s { _schemaExample = Nothing
1192+ , _schemaParamSchema = ps { _paramSchemaMaxItems = Nothing } }
1193+ else s
11751194
11761195instance FromJSON Header where
11771196 parseJSON = sopSwaggerGenericParseJSON
@@ -1187,6 +1206,7 @@ instance FromJSON (SwaggerItems 'SwaggerKindParamOtherSchema) where
11871206 <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
11881207
11891208instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
1209+ parseJSON js | js == object [] = pure $ SwaggerItemsArray [] -- Nullary schema.
11901210 parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
11911211 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
11921212 parseJSON _ = empty
test/Data/Swagger/CommonTestTypes.hs+2−2
@@ -552,7 +552,7 @@ lightSchemaJSON = [aesonQQ|
552552 "type": "object",
553553 "properties":
554554 {
555− "NoLight": { "type": "array", "items": [] },
555+ "NoLight": { "type": "array", "items": {}, "maxItems": 0, "example": [] },
556556 "LightFreq": { "type": "number", "format": "double" },
557557 "LightColor": { "$ref": "#/definitions/Color" },
558558 "LightWaveLength": { "type": "number", "format": "double" }
@@ -568,7 +568,7 @@ lightInlinedSchemaJSON = [aesonQQ|
568568 "type": "object",
569569 "properties":
570570 {
571− "NoLight": { "type": "array", "items": [] },
571+ "NoLight": { "type": "array", "items": {}, "maxItems": 0, "example": [] },
572572 "LightFreq": { "type": "number", "format": "double" },
573573 "LightColor":
574574 {