-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds code-gen support for OpenAPI discriminators using Tagged Unions
Although the OpenAPI spec allows for more complicated scenarios, I've restricted this to discriminator unions with an explicit mapping using only schema types that are not referenced elsewhere. This allows us to generate nearly the exact same code as we would write by hand for handling our own tagged union schemas.
- Loading branch information
Showing
19 changed files
with
678 additions
and
87 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
json-fleece-openapi3/examples/test-cases/TestCases/Types/Bar.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
|
||
module TestCases.Types.Bar | ||
( Bar(..) | ||
, barSchema | ||
) where | ||
|
||
import Fleece.Core ((#+), Object) | ||
import qualified Fleece.Core as FC | ||
import Prelude (Eq, Maybe, Show) | ||
import qualified TestCases.Types.Bar.BarName as BarName | ||
|
||
data Bar = Bar | ||
{ barName :: Maybe BarName.BarName | ||
} | ||
deriving (Eq, Show) | ||
|
||
barSchema :: FC.Fleece schema => Object schema Bar Bar | ||
barSchema = | ||
FC.constructor Bar | ||
#+ FC.optional "barName" barName BarName.barNameSchema |
17 changes: 17 additions & 0 deletions
17
json-fleece-openapi3/examples/test-cases/TestCases/Types/Bar/BarName.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
|
||
module TestCases.Types.Bar.BarName | ||
( BarName(..) | ||
, barNameSchema | ||
) where | ||
|
||
import qualified Data.Text as T | ||
import qualified Fleece.Core as FC | ||
import Prelude (Eq, Show) | ||
|
||
newtype BarName = BarName T.Text | ||
deriving (Show, Eq) | ||
|
||
barNameSchema :: FC.Fleece schema => schema BarName | ||
barNameSchema = | ||
FC.coerceSchema FC.text |
17 changes: 17 additions & 0 deletions
17
json-fleece-openapi3/examples/test-cases/TestCases/Types/Bar/Type.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
|
||
module TestCases.Types.Bar.Type | ||
( Type(..) | ||
, typeSchema | ||
) where | ||
|
||
import qualified Data.Text as T | ||
import qualified Fleece.Core as FC | ||
import Prelude (Eq, Show) | ||
|
||
newtype Type = Type T.Text | ||
deriving (Show, Eq) | ||
|
||
typeSchema :: FC.Fleece schema => schema Type | ||
typeSchema = | ||
FC.coerceSchema FC.text |
21 changes: 21 additions & 0 deletions
21
json-fleece-openapi3/examples/test-cases/TestCases/Types/Baz.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
|
||
module TestCases.Types.Baz | ||
( Baz(..) | ||
, bazSchema | ||
) where | ||
|
||
import Fleece.Core ((#+), Object) | ||
import qualified Fleece.Core as FC | ||
import Prelude (Eq, Maybe, Show) | ||
import qualified TestCases.Types.Baz.BazName as BazName | ||
|
||
data Baz = Baz | ||
{ bazName :: Maybe BazName.BazName | ||
} | ||
deriving (Eq, Show) | ||
|
||
bazSchema :: FC.Fleece schema => Object schema Baz Baz | ||
bazSchema = | ||
FC.constructor Baz | ||
#+ FC.optional "bazName" bazName BazName.bazNameSchema |
17 changes: 17 additions & 0 deletions
17
json-fleece-openapi3/examples/test-cases/TestCases/Types/Baz/BazName.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
|
||
module TestCases.Types.Baz.BazName | ||
( BazName(..) | ||
, bazNameSchema | ||
) where | ||
|
||
import qualified Data.Text as T | ||
import qualified Fleece.Core as FC | ||
import Prelude (Eq, Show) | ||
|
||
newtype BazName = BazName T.Text | ||
deriving (Show, Eq) | ||
|
||
bazNameSchema :: FC.Fleece schema => schema BazName | ||
bazNameSchema = | ||
FC.coerceSchema FC.text |
17 changes: 17 additions & 0 deletions
17
json-fleece-openapi3/examples/test-cases/TestCases/Types/Baz/Type.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
|
||
module TestCases.Types.Baz.Type | ||
( Type(..) | ||
, typeSchema | ||
) where | ||
|
||
import qualified Data.Text as T | ||
import qualified Fleece.Core as FC | ||
import Prelude (Eq, Show) | ||
|
||
newtype Type = Type T.Text | ||
deriving (Show, Eq) | ||
|
||
typeSchema :: FC.Fleece schema => schema Type | ||
typeSchema = | ||
FC.coerceSchema FC.text |
17 changes: 17 additions & 0 deletions
17
json-fleece-openapi3/examples/test-cases/TestCases/Types/Foo.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
|
||
module TestCases.Types.Foo | ||
( Foo(..) | ||
, fooSchema | ||
) where | ||
|
||
import Fleece.Core (Object) | ||
import qualified Fleece.Core as FC | ||
import Prelude (Eq, Show) | ||
|
||
data Foo = Foo | ||
deriving (Eq, Show) | ||
|
||
fooSchema :: FC.Fleece schema => Object schema Foo Foo | ||
fooSchema = | ||
FC.constructor Foo |
17 changes: 17 additions & 0 deletions
17
json-fleece-openapi3/examples/test-cases/TestCases/Types/Foo/Type.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
|
||
module TestCases.Types.Foo.Type | ||
( Type(..) | ||
, typeSchema | ||
) where | ||
|
||
import qualified Data.Text as T | ||
import qualified Fleece.Core as FC | ||
import Prelude (Eq, Show) | ||
|
||
newtype Type = Type T.Text | ||
deriving (Show, Eq) | ||
|
||
typeSchema :: FC.Fleece schema => schema Type | ||
typeSchema = | ||
FC.coerceSchema FC.text |
34 changes: 34 additions & 0 deletions
34
json-fleece-openapi3/examples/test-cases/TestCases/Types/OneOfWithDiscriminator.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE ExplicitNamespaces #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module TestCases.Types.OneOfWithDiscriminator | ||
( OneOfWithDiscriminator(..) | ||
, oneOfWithDiscriminatorSchema | ||
) where | ||
|
||
import Fleece.Core ((#@)) | ||
import qualified Fleece.Core as FC | ||
import Prelude (($), Eq, Show) | ||
import Shrubbery (type (@=)) | ||
import qualified Shrubbery as Shrubbery | ||
import qualified TestCases.Types.Bar as Bar | ||
import qualified TestCases.Types.Baz as Baz | ||
import qualified TestCases.Types.Foo as Foo | ||
|
||
newtype OneOfWithDiscriminator = OneOfWithDiscriminator (Shrubbery.TaggedUnion | ||
'[ "bar" @= Bar.Bar | ||
, "baz" @= Baz.Baz | ||
, "foo" @= Foo.Foo | ||
]) | ||
deriving (Show, Eq) | ||
|
||
oneOfWithDiscriminatorSchema :: FC.Fleece schema => schema OneOfWithDiscriminator | ||
oneOfWithDiscriminatorSchema = | ||
FC.coerceSchema $ | ||
FC.taggedUnionNamed (FC.qualifiedName "TestCases.Types.OneOfWithDiscriminator" "OneOfWithDiscriminator") "type" $ | ||
FC.taggedUnionMember @"bar" Bar.barSchema | ||
#@ FC.taggedUnionMember @"baz" Baz.bazSchema | ||
#@ FC.taggedUnionMember @"foo" Foo.fooSchema |
2 changes: 1 addition & 1 deletion
2
json-fleece-openapi3/examples/test-cases/TestCases/Types/OneOfWithNullable.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
json-fleece-openapi3/examples/test-cases/TestCases/Types/ReferenceOneOfInsideOneOf.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
json-fleece-openapi3/examples/test-cases/TestCases/Types/TopLevelOneOf.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
json-fleece-openapi3/examples/test-cases/TestCases/Types/TopLevelOneOfOneOption.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.