servant-swagger
servant-swagger copied to clipboard
validateEveryToJSON wants ToJSON even for ReqBody
With the following API
type UserAPI
= (Capture "user_id" UserId :> Get '[JSON] User)
:<|> (Capture "new" :> ReqBody '[JSON] NewUser :> Post '[JSON] UserId)
and only the following instances
newtype UserId = ...
instance FromHttpApiData UserId
instance ToJSON UserId
data User = ...
instance ToJSON User
data NewUser = ...
instance FromJSON NewUser
It is not possible to use validateEveryToJSON because it expects instance ToJSON NewUser even though the UserAPI is perfectly usable as NewUser is only used in ReqBody and thus only requires a FromJSON instance.
Of course an easy fix is to just add a ToJSON instance, which is low risk compared to adding FromJSON instances for things you only want to send out.
Looks like changing BodyTypes is actually relatively easy, but swagger2 only has validateToJSON and not validateFromJSON.
Seems like implementation was started though: https://github.com/GetShopTV/swagger2/pull/162
Excuse that, I'm blind! The PR was merged and the function exists: https://hackage.haskell.org/package/swagger2-2.3.1.1/docs/Data-Swagger-Schema-Generator.html#v:validateFromJSON