Add OpenApi support.

This commit is contained in:
Nick 2023-01-24 00:29:57 +00:00
parent c506d9d43b
commit 4101a201c2
4 changed files with 35 additions and 0 deletions

View File

@ -19,6 +19,11 @@ flag aeson
default: False
manual: True
flag openapi
description: Enable OpenAPI instances
default: False
manual: True
flag rel8
description: Enable Rel8 support
default: False
@ -38,6 +43,10 @@ library
other-modules: Data.Money.JSON
build-depends: aeson >= 2.0 && < 3.0
ghc-options: -DUSE_AESON
if flag(openapi)
other-modules: Data.Money.OpenApi
build-depends: openapi3
ghc-options: -DUSE_OPENAPI
if flag(rel8)
other-modules: Data.Money.Rel8
build-depends: rel8 >= 1.4,

View File

@ -7,6 +7,9 @@ import Data.Money.Internal as Export
#ifdef USE_AESON
import Data.Money.JSON as Export ()
#endif
#ifdef USE_OPENAPI
import Data.Money.OpenApi as Export ()
#endif
#ifdef USE_REL8
import Data.Money.Rel8 as Export ()
#endif

22
src/Data/Money/OpenApi.hs Normal file
View File

@ -0,0 +1,22 @@
{-# OPTIONS_GHC -Wno-orphans #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
module Data.Money.OpenApi where
import Data.Fixed
import Data.Money.Internal
import Data.OpenApi
import Data.Typeable
instance (KnownCurrency c, Typeable (Money c)) => ToSchema (Money c) where
declareNamedSchema _ = do
centiSchema <- declareSchemaRef (Proxy :: Proxy Centi)
strSchema <- declareSchemaRef (Proxy :: Proxy String)
return $ NamedSchema (Just "Money") $
mempty { _schemaType = Just OpenApiObject
, _schemaProperties = [ ("currency", strSchema)
, ("amount", centiSchema) ]
, _schemaRequired = [ "currency", "amount" ]
}

View File

@ -6,6 +6,7 @@ packages:
flags:
bucks:
aeson: true
openapi: true
rel8: true
nix: