Skip to content

Commit

Permalink
Make nullary schemas valid
Browse files Browse the repository at this point in the history
Resolves #167
  • Loading branch information
michalrus committed Sep 27, 2018
1 parent c8ccdd7 commit b4ce7b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Data/Swagger/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,10 @@ instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
[ "collectionFormat" .= fmt
, "items" .= schema ]
toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
toJSON (SwaggerItemsArray []) = object -- Nullary schema, cf. <https://github.com/GetShopTV/swagger2/issues/167>.
[ "items" .= object []
, "maxItems" .= (0 :: Int)
, "example" .= ([] :: [()]) ]
toJSON (SwaggerItemsArray x) = object [ "items" .= x ]

instance ToJSON Host where
Expand Down Expand Up @@ -1162,7 +1166,13 @@ instance FromJSON SecurityScheme where
parseJSON = sopSwaggerGenericParseJSON

instance FromJSON Schema where
parseJSON = sopSwaggerGenericParseJSON
parseJSON = fmap nullaryCleanup . sopSwaggerGenericParseJSON
where nullaryCleanup :: Schema -> Schema
nullaryCleanup s@Schema{_schemaParamSchema=ps} =
if _paramSchemaItems ps == Just (SwaggerItemsArray [])
then s { _schemaExample = Nothing
, _schemaParamSchema = ps { _paramSchemaMaxItems = Nothing } }
else s

instance FromJSON Header where
parseJSON = sopSwaggerGenericParseJSON
Expand All @@ -1178,6 +1188,7 @@ instance FromJSON (SwaggerItems 'SwaggerKindParamOtherSchema) where
<*> ((o .: "items" >>= parseJSON) <|> fail ("foo" ++ show o))

instance FromJSON (SwaggerItems 'SwaggerKindSchema) where
parseJSON js | js == object [] = pure $ SwaggerItemsArray [] -- Nullary schema.
parseJSON js@(Object _) = SwaggerItemsObject <$> parseJSON js
parseJSON js@(Array _) = SwaggerItemsArray <$> parseJSON js
parseJSON _ = empty
Expand Down
4 changes: 2 additions & 2 deletions test/Data/Swagger/CommonTestTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ lightSchemaJSON = [aesonQQ|
"type": "object",
"properties":
{
"NoLight": { "type": "array", "items": [] },
"NoLight": { "type": "array", "items": {}, "maxItems": 0, "example": [] },
"LightFreq": { "type": "number", "format": "double" },
"LightColor": { "$ref": "#/definitions/Color" },
"LightWaveLength": { "type": "number", "format": "double" }
Expand All @@ -568,7 +568,7 @@ lightInlinedSchemaJSON = [aesonQQ|
"type": "object",
"properties":
{
"NoLight": { "type": "array", "items": [] },
"NoLight": { "type": "array", "items": {}, "maxItems": 0, "example": [] },
"LightFreq": { "type": "number", "format": "double" },
"LightColor":
{
Expand Down

0 comments on commit b4ce7b4

Please sign in to comment.