Skip to content

Commit 4793b3c

Browse files
committed
Aeson 2 for extensions
1 parent 3fc3da8 commit 4793b3c

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/Data/OpenApi/Aeson/Compat.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ lookupKey = KeyMap.lookup . Key.fromText
4343

4444
hasKey :: T.Text -> KeyMap.KeyMap a -> Bool
4545
hasKey = KeyMap.member . Key.fromText
46+
47+
filterKeys :: (Key -> Bool) -> KeyMap.KeyMap a -> KeyMap.KeyMap a
48+
filterKeys p = KeyMap.filterWithKey (\key _ -> p key)
4649
#else
4750
deleteKey :: T.Text -> HM.HashMap T.Text v -> HM.HashMap T.Text v
4851
deleteKey = HM.delete
@@ -73,4 +76,7 @@ lookupKey = HM.lookup
7376

7477
hasKey :: T.Text -> HM.HashMap T.Text a -> Bool
7578
hasKey = HM.member
79+
80+
filterKeys :: (T.Text -> Bool) -> HM.HashMap T.Text a -> HM.HashMap T.Text a
81+
filterKeys p = HM.filterWithKey (\key _ -> p key)
7682
#endif

src/Data/OpenApi/Internal.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import Text.Read (readMaybe)
5050
import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
5151
import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
5252

53-
import Data.OpenApi.Aeson.Compat (deleteKey)
53+
import Data.OpenApi.Aeson.Compat (deleteKey, filterKeys, keyToText, objectToList)
5454
import Data.OpenApi.Internal.AesonUtils (AesonDefaultValue (..), HasSwaggerAesonOptions (..),
5555
mkSwaggerAesonOptions, saoAdditionalPairs, saoSubObject,
5656
sopSwaggerGenericParseJSON, sopSwaggerGenericToEncoding,
@@ -649,7 +649,7 @@ data ParamLocation
649649
| ParamCookie
650650
deriving (Eq, Ord, Show, Generic, Data, Typeable)
651651

652-
instance Hashable ParamLocation
652+
instance Hashable ParamLocation
653653

654654
type Format = Text
655655

@@ -1018,7 +1018,7 @@ data Referenced a
10181018
instance IsString a => IsString (Referenced a) where
10191019
fromString = Inline . fromString
10201020

1021-
newtype URL = URL { getUrl :: Text }
1021+
newtype URL = URL { getUrl :: Text }
10221022
deriving (Eq, Ord, Show, Hashable, ToJSON, FromJSON, Data, Typeable, AesonDefaultValue)
10231023

10241024
data AdditionalProperties
@@ -1555,19 +1555,19 @@ instance FromJSON Param where
15551555
parseJSON = sopSwaggerGenericParseJSON
15561556

15571557
instance FromJSON Responses where
1558-
parseJSON (Object o) = Responses
1559-
<$> o .:? "default"
1560-
<*> parseJSON
1561-
( Object
1562-
( HashMap.filterWithKey (\k _ -> not $ isExt k)
1563-
$ HashMap.delete "default" o
1564-
)
1565-
)
1566-
<*> case HashMap.filterWithKey (\k _ -> isExt k) o of
1567-
exts
1568-
| HashMap.null exts -> pure (SpecificationExtensions mempty)
1569-
| otherwise -> parseJSON (Object exts)
1570-
1558+
parseJSON (Object o) = Responses
1559+
<$> o .:? "default"
1560+
<*> parseJSON
1561+
( Object
1562+
( filterKeys (not . isExt . keyToText) $
1563+
deleteKey "default" o
1564+
)
1565+
)
1566+
<*> case filterKeys (isExt . keyToText) o of
1567+
exts
1568+
| null exts -> pure (SpecificationExtensions mempty)
1569+
| otherwise -> parseJSON (Object exts)
1570+
15711571
parseJSON _ = empty
15721572

15731573
isExt :: Text -> Bool
@@ -1642,7 +1642,7 @@ instance FromJSON SpecificationExtensions where
16421642
parseJSON = withObject "SpecificationExtensions" extFieldsParser
16431643
where
16441644
extFieldsParser = pure . SpecificationExtensions . InsOrdHashMap.fromList . catMaybes . filterExtFields
1645-
filterExtFields = fmap (\(k, v) -> (, v) <$> Text.stripPrefix "x-" k) . HashMap.toList
1645+
filterExtFields = fmap (\(k, v) -> (, v) <$> Text.stripPrefix "x-" (keyToText k)) . objectToList
16461646

16471647

16481648
instance FromJSON Info where
@@ -1695,7 +1695,7 @@ instance HasSwaggerAesonOptions SecurityScheme where
16951695
instance HasSwaggerAesonOptions Schema where
16961696
swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject .~ ["paramSchema", "extensions"]
16971697
instance HasSwaggerAesonOptions OpenApi where
1698-
swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
1698+
swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
16991699
& saoSubObject .~ ["extensions"]
17001700
instance HasSwaggerAesonOptions Example where
17011701
swaggerAesonOptions _ = mkSwaggerAesonOptions "example" & saoSubObject .~ ["extensions"]

0 commit comments

Comments
 (0)