git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #168 from michalrus/nullary-schema-valid
6fc1496Oleg Grenrus
src/Data/Swagger/Internal.hs+21−1
@@ -65,6 +65,9 @@ import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
65#define DEFINE_TOENCODING
65#define DEFINE_TOENCODING
66#endif
66#endif
67
67
68+-- $setup
69+-- >>> import Data.Aeson
70+
68-- | A list of definitions that can be used in references.
71-- | A list of definitions that can be used in references.
69type Definitions = InsOrdHashMap Text
72type Definitions = InsOrdHashMap Text
70
73
@@ -1050,11 +1053,21 @@ instance ToJSON Header where
1050 toJSON = sopSwaggerGenericToJSON
1053 toJSON = sopSwaggerGenericToJSON
1051 DEFINE_TOENCODING
1054 DEFINE_TOENCODING
1052
1055
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+--
1053instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
1062instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
1054 toJSON (SwaggerItemsPrimitive fmt schema) = object
1063 toJSON (SwaggerItemsPrimitive fmt schema) = object
1055 [ "collectionFormat" .= fmt
1064 [ "collectionFormat" .= fmt
1056 , "items" .= schema ]
1065 , "items" .= schema ]
1057 toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1066 toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
1067+ toJSON (SwaggerItemsArray []) = object
1068+ [ "items" .= object []
1069+ , "maxItems" .= (0 :: Int)
1070+ , "example" .= ([] :: [()]) ]
1058 toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
1071 toJSON (SwaggerItemsArray x) = object [ "items" .= x ]
1059
1072
1060instance ToJSON Host where
1073instance ToJSON Host where
@@ -1171,7 +1184,13 @@ instance FromJSON SecurityScheme where
1171 parseJSON = sopSwaggerGenericParseJSON
1184 parseJSON = sopSwaggerGenericParseJSON
1172
1185
1173instance FromJSON Schema where
1186instance 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
1175
1194
1176instance FromJSON Header where
1195instance FromJSON Header where
1177 parseJSON = sopSwaggerGenericParseJSON
1196 parseJSON = sopSwaggerGenericParseJSON
@@ -1187,6 +1206,7 @@ instance FromJSON (SwaggerItems 'SwaggerKindParamOtherSchema) where
1187 <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
1206 <*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))
1188
1207
1189instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
1208instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
1209+ parseJSON js | js == object [] = pure $ SwaggerItemsArray [] -- Nullary schema.
1190 parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1210 parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
1191 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1211 parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
1192 parseJSON _ = empty
1212 parseJSON _ = empty
test/Data/Swagger/CommonTestTypes.hs+2−2
@@ -552,7 +552,7 @@ lightSchemaJSON = [aesonQQ|
552 "type": "object",
552 "type": "object",
553 "properties":
553 "properties":
554 {
554 {
555− "NoLight": { "type": "array", "items": [] },
555+ "NoLight": { "type": "array", "items": {}, "maxItems": 0, "example": [] },
556 "LightFreq": { "type": "number", "format": "double" },
556 "LightFreq": { "type": "number", "format": "double" },
557 "LightColor": { "$ref": "#/definitions/Color" },
557 "LightColor": { "$ref": "#/definitions/Color" },
558 "LightWaveLength": { "type": "number", "format": "double" }
558 "LightWaveLength": { "type": "number", "format": "double" }
@@ -568,7 +568,7 @@ lightInlinedSchemaJSON = [aesonQQ|
568 "type": "object",
568 "type": "object",
569 "properties":
569 "properties":
570 {
570 {
571− "NoLight": { "type": "array", "items": [] },
571+ "NoLight": { "type": "array", "items": {}, "maxItems": 0, "example": [] },
572 "LightFreq": { "type": "number", "format": "double" },
572 "LightFreq": { "type": "number", "format": "double" },
573 "LightColor":
573 "LightColor":
574 {
574 {