Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sopSwaggerGenericToEncodingWithOpts and use it. #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Data/OpenApi/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
import Generics.SOP.TH (deriveGeneric)
import Data.OpenApi.Internal.AesonUtils (sopSwaggerGenericToJSON
,sopSwaggerGenericToJSONWithOpts
,sopSwaggerGenericToEncoding
,sopSwaggerGenericToEncodingWithOpts
,sopSwaggerGenericParseJSON
,HasSwaggerAesonOptions(..)
,AesonDefaultValue(..)
,mkSwaggerAesonOptions
,saoAdditionalPairs
,saoSubObject)
import Data.OpenApi.Internal.Utils
import Data.OpenApi.Internal.AesonUtils (sopSwaggerGenericToEncoding)

-- $setup
-- >>> :seti -XDataKinds
Expand Down Expand Up @@ -1316,6 +1317,8 @@ instance ToJSON SecurityScheme where
instance ToJSON Schema where
toJSON = sopSwaggerGenericToJSONWithOpts $
mkSwaggerAesonOptions "schema" & saoSubObject ?~ "items"
toEncoding = sopSwaggerGenericToEncodingWithOpts $
mkSwaggerAesonOptions "schema" & saoSubObject ?~ "items"

instance ToJSON Header where
toJSON = sopSwaggerGenericToJSON
Expand Down
21 changes: 20 additions & 1 deletion src/Data/OpenApi/Internal/AesonUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module Data.OpenApi.Internal.AesonUtils (
-- * Generic functions
AesonDefaultValue(..),
sopSwaggerGenericToJSON,
sopSwaggerGenericToEncoding,
sopSwaggerGenericToJSONWithOpts,
sopSwaggerGenericToEncoding,
sopSwaggerGenericToEncodingWithOpts,
sopSwaggerGenericParseJSON,
-- * Options
HasSwaggerAesonOptions(..),
Expand Down Expand Up @@ -267,6 +268,24 @@ sopSwaggerGenericToEncoding x =
proxy = Proxy :: Proxy a
opts = swaggerAesonOptions proxy

sopSwaggerGenericToEncodingWithOpts
:: forall a xs.
( HasDatatypeInfo a
, HasSwaggerAesonOptions a
, All2 ToJSON (Code a)
, All2 Eq (Code a)
, Code a ~ '[xs]
)
=> SwaggerAesonOptions
-> a
-> Encoding
sopSwaggerGenericToEncodingWithOpts opts x =
let ps = sopSwaggerGenericToEncoding' opts (from x) (datatypeInfo proxy) defs
in pairs (pairsToSeries (opts ^. saoAdditionalPairs) <> ps)
where
proxy = Proxy :: Proxy a
defs = hcpure (Proxy :: Proxy AesonDefaultValue) defaultValue

pairsToSeries :: [Pair] -> Series
pairsToSeries = foldMap (\(k, v) -> (k .= v))

Expand Down