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 Nov 22, 2018
1 parent c8ccdd7 commit dd47ca9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/Data/Swagger/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToEncoding)
#define DEFINE_TOENCODING
#endif

-- $setup
-- >>> import Data.Aeson

-- | A list of definitions that can be used in references.
type Definitions = InsOrdHashMap Text

Expand Down Expand Up @@ -1045,11 +1048,21 @@ instance ToJSON Header where
toJSON = sopSwaggerGenericToJSON
DEFINE_TOENCODING

-- | As for nullary schema for 0-arity type constructors, see
-- <https://github.com/GetShopTV/swagger2/issues/167>.
--
-- >>> encode (SwaggerItemsArray [])
-- "{\"example\":[],\"items\":{},\"maxItems\":0}"
--
instance ToJSON (ParamSchema t) => ToJSON (SwaggerItems t) where
toJSON (SwaggerItemsPrimitive fmt schema) = object
[ "collectionFormat" .= fmt
, "items" .= schema ]
toJSON (SwaggerItemsObject x) = object [ "items" .= x ]
toJSON (SwaggerItemsArray []) = object
[ "items" .= object []
, "maxItems" .= (0 :: Int)
, "example" .= ([] :: [()]) ]
toJSON (SwaggerItemsArray x) = object [ "items" .= x ]

instance ToJSON Host where
Expand Down Expand Up @@ -1162,7 +1175,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 +1197,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 dd47ca9

Please sign in to comment.