Generating Swagger for Haskell union type

Viewed 77

I have a data structure of the following form:

data MyType =
   Foo Int String
   | Bar Int Int
   | Baz String

I've manually generated an aeson ToJSON instance:

instance ToJSON MyType where
   toJSON (Foo i s) = object [
      "tag" .= ("foo" :: Text),
      "intField" .= i,
      "stringField" = s] 
   -- and so on.

Now I want a Swagger schema for it. Because I have a custom ToJSON instance I'm going to have to define a matching instance of ToSchema. I could just declare it as a list of mostly-optional field names and a mandatory tag string, but it really ought to list the possible tag values and associate them with different fields.

The OpenAPI specification talks about the discriminator object for doing this, and I've found the corresponding function in the Haskell swagger2 package. But I can't see how to get the mapping from discriminator value to sub-schema. Does anyone have any examples of how to do this?

0 Answers
Related