-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modelling sum types #80
Comments
Can Swagger2 represent those at all? AFAIK the best you can do is mark both fields optional. ping @fizruk |
Ah I see. Thanks @phadej - how do you mark the fields as optional? I also tried doing an explanation, but you can't see |
So yeah, true sum types are not supported well by Swagger 2.0. Note: sum types can be supported in OpenAPI 3.0, via Still, you can do as @phadej suggested — mark both fields optional. Additionally you can specify that there should be exactly 1 field (no more, no less) using You can actually see that approach in an existing instance for
If you look at the definition, you'll find that it's actually a derived instance (ToSchema a, ToSchema b) => ToSchema (Either a b) That means you can achieve the same for your type. Note that you can use data WithdrawlResult =
WithdrawlError ClientError -- ^ Error with http client error
| WithdrawlSuccess Transaction -- ^ Success with transaction details
deriving (Show, Typeable, Generic)
wdDesc :: Text
wdDesc = "An object with either a success field containing the transaction or "
<> "an error field containing the ClientError from the wallet as a string"
instance ToSchema WithdrawlResult where
declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
{ constructorTagModifier = map toLower . drop (length "Withdrawl") }
& mapped.mapped.schema.description ?~ wdDesc You can check that it works in GHCi:
|
Nice! Thanks a lot!
…On Thu, 28 Jun 2018 at 14:52, Nickolay Kudasov ***@***.***> wrote:
So yeah, true sum types are not supported well by Swagger 2.0 (and OpenAPI
3.0 AFAIK).
Still, you can do as @phadej <https://github.com/phadej> suggested — mark
both fields optional. Additionally you can specify that there should be
exactly 1 field (no more, no less) using minProperties and maxProperties
("properties" is what fields are called in a JSON object).
You can actually see that approach in an existing instance for Either a b:
>>> BSL8.putStrLn . encodePretty $ toSchema (Proxy :: Proxy (Either Integer String))
{
"minProperties": 1,
"maxProperties": 1,
"type": "object",
"properties": {
"Left": {
"type": "integer"
},
"Right": {
"type": "string"
}
}
}
If you look at the definition, you'll find that it's actually a derived
Generic-based implementation:
instance (ToSchema a, ToSchema b) => ToSchema (Either a b)
That means you can achieve the same for your type. Note that you can use
genericDeclareNamedSchema explicitly to specify property names for
constructors:
data WithdrawlResult =
WithdrawlError ClientError -- ^ Error with http client error
| WithdrawlSuccess Transaction -- ^ Success with transaction details
deriving (Show, Typeable, Generic)
wdDesc :: Text
wdDesc = "An object with either a success field containing the transaction or "
<> "an error field containing the ClientError from the wallet as a string"
instance ToSchema WithdrawlResult where
declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
{ constructorTagModifier = map toLower . drop (length "Withdrawl") }
& mapped.mapped.schema.description ?~ wdDesc
You can check that it works in GHCi:
>>> BSL8.putStrLn . encodePretty $ toSchema (Proxy :: Proxy WithdrawlResult)
{
"minProperties": 1,
"maxProperties": 1,
"type": "object",
"description": "An object with either a success field containing the transaction or an error field containing the ClientError from the wallet as a string",
"properties": {
"error": {
...
},
"success": {
...
}
}
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#80 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAFW1Hw__gQ329y231XvKBozMfV9dn5Pks5uBN-sgaJpZM4U7Cod>
.
--
Regards,
Ben Ford
[email protected]
+447540722690
|
See conversation here haskell-servant/servant-swagger#80
Hm, I thought OpenAPI 3.0 supported them (with |
@neongreen yes, you are right, I will edit my previous comment. |
Is there a plan for supporting OpenAPI 3.0 in this module? I think that the |
@AnthonySuper see #99 |
Now that #99 has been closed, can we also close this one? |
Hi, Thanks in advance! |
How do you go about expressing a sum type like this:
Ideally I'd want to express that I'm expecting a JSON object with either
{ success: {...}}
or{ error: "Some message" }
I can't quite figure out how to do that...The text was updated successfully, but these errors were encountered: