From 4101a201c279cfc1da83fd91c82ccbcbab9c6393 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 24 Jan 2023 00:29:57 +0000 Subject: [PATCH] Add OpenApi support. --- bucks.cabal | 9 +++++++++ src/Data/Money.hs | 3 +++ src/Data/Money/OpenApi.hs | 22 ++++++++++++++++++++++ stack.yaml | 1 + 4 files changed, 35 insertions(+) create mode 100644 src/Data/Money/OpenApi.hs diff --git a/bucks.cabal b/bucks.cabal index 5cd30ff..ddecc33 100644 --- a/bucks.cabal +++ b/bucks.cabal @@ -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, diff --git a/src/Data/Money.hs b/src/Data/Money.hs index c342a77..19275b2 100644 --- a/src/Data/Money.hs +++ b/src/Data/Money.hs @@ -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 diff --git a/src/Data/Money/OpenApi.hs b/src/Data/Money/OpenApi.hs new file mode 100644 index 0000000..2d12515 --- /dev/null +++ b/src/Data/Money/OpenApi.hs @@ -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" ] + } diff --git a/stack.yaml b/stack.yaml index e1954a0..2c81d0b 100644 --- a/stack.yaml +++ b/stack.yaml @@ -6,6 +6,7 @@ packages: flags: bucks: aeson: true + openapi: true rel8: true nix: