on master help sign in
Add OpenApi support.
commit4101a201c279cfc1da83fd91c82ccbcbab9c6393 authorNick <nick@typeable.io> dateTue 24 Jan 2023, 00:29 (4 years ago) parentc506d9d
4 files changed+35
bucks.cabal+9 src/Data/Money.hs+3 src/Data/Money/OpenApi.hs+22 stack.yaml+1
bucks.cabal+9
@@ -19,6 +19,11 @@ flag aeson
1919 default: False
2020 manual: True
2121
22+flag openapi
23+ description: Enable OpenAPI instances
24+ default: False
25+ manual: True
26+
2227flag rel8
2328 description: Enable Rel8 support
2429 default: False
@@ -38,6 +43,10 @@ library
3843 other-modules: Data.Money.JSON
3944 build-depends: aeson >= 2.0 && < 3.0
4045 ghc-options: -DUSE_AESON
46+ if flag(openapi)
47+ other-modules: Data.Money.OpenApi
48+ build-depends: openapi3
49+ ghc-options: -DUSE_OPENAPI
4150 if flag(rel8)
4251 other-modules: Data.Money.Rel8
4352 build-depends: rel8 >= 1.4,
src/Data/Money.hs+3
@@ -7,6 +7,9 @@ import Data.Money.Internal as Export
77#ifdef USE_AESON
88import Data.Money.JSON as Export ()
99#endif
10+#ifdef USE_OPENAPI
11+import Data.Money.OpenApi as Export ()
12+#endif
1013#ifdef USE_REL8
1114import Data.Money.Rel8 as Export ()
1215#endif
src/Data/Money/OpenApi.hsnew file+22
@@ -0,0 +1,22 @@
1+{-# OPTIONS_GHC -Wno-orphans #-}
2+{-# LANGUAGE FlexibleContexts #-}
3+{-# LANGUAGE MonoLocalBinds #-}
4+{-# LANGUAGE OverloadedLists #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+module Data.Money.OpenApi where
7+
8+import Data.Fixed
9+import Data.Money.Internal
10+import Data.OpenApi
11+import Data.Typeable
12+
13+instance (KnownCurrency c, Typeable (Money c)) => ToSchema (Money c) where
14+ declareNamedSchema _ = do
15+ centiSchema <- declareSchemaRef (Proxy :: Proxy Centi)
16+ strSchema <- declareSchemaRef (Proxy :: Proxy String)
17+ return $ NamedSchema (Just "Money") $
18+ mempty { _schemaType = Just OpenApiObject
19+ , _schemaProperties = [ ("currency", strSchema)
20+ , ("amount", centiSchema) ]
21+ , _schemaRequired = [ "currency", "amount" ]
22+ }
stack.yaml+1
@@ -6,6 +6,7 @@ packages:
66flags:
77 bucks:
88 aeson: true
9+ openapi: true
910 rel8: true
1011
1112nix: