Skip to content

Generic ToSchema does not match generic ToAeson #241

@PaulJohnson

Description

@PaulJohnson

By default the generic ToJSON instance uses a tagged field, but the generic ToSchema uses a single constructor:

module Main where

import Data.Aeson
import qualified Data.ByteString.Lazy as BL
import Data.Char
import Data.Proxy
import Data.Swagger
import GHC.Generics


data Foo =
     One Integer Integer
     | Two Integer String
     deriving (Generic)

instance ToJSON Foo
instance ToSchema Foo

aFoo :: Foo
aFoo = Two 3 "Example"


main = do
     putStrLn "Schema for Foo:"
     putStrLn $ map (chr . fromIntegral) $ BL.unpack $ encode $ toSchema (Proxy :: Proxy Foo)
     putStrLn "\n\nValue of Foo:"
     putStrLn $ map (chr . fromIntegral) $ BL.unpack $ encode aFoo
Schema for Foo:
{"properties":{"One":{"items":[{"type":"integer"},{"type":"integer"}],"maxItems":2,"minItems":2,"type":"array"},"Two":{"items":[{"type":"integer"},{"type":"string"}],"maxItems":2,"minItems":2,"type":"array"}},"maxProperties":1,"minProperties":1,"type":"object"}


Value of Foo:
{"contents":[3,"Example"],"tag":"Two"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions