Skip to content

Commit

Permalink
Aeson 2 for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbiehl committed Mar 24, 2022
1 parent 3fc3da8 commit 4793b3c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/Data/OpenApi/Aeson/Compat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ lookupKey = KeyMap.lookup . Key.fromText

hasKey :: T.Text -> KeyMap.KeyMap a -> Bool
hasKey = KeyMap.member . Key.fromText

filterKeys :: (Key -> Bool) -> KeyMap.KeyMap a -> KeyMap.KeyMap a
filterKeys p = KeyMap.filterWithKey (\key _ -> p key)
#else
deleteKey :: T.Text -> HM.HashMap T.Text v -> HM.HashMap T.Text v
deleteKey = HM.delete
Expand Down Expand Up @@ -73,4 +76,7 @@ lookupKey = HM.lookup

hasKey :: T.Text -> HM.HashMap T.Text a -> Bool
hasKey = HM.member

filterKeys :: (T.Text -> Bool) -> HM.HashMap T.Text a -> HM.HashMap T.Text a
filterKeys p = HM.filterWithKey (\key _ -> p key)
#endif
36 changes: 18 additions & 18 deletions src/Data/OpenApi/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import Text.Read (readMaybe)
import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap

import Data.OpenApi.Aeson.Compat (deleteKey)
import Data.OpenApi.Aeson.Compat (deleteKey, filterKeys, keyToText, objectToList)
import Data.OpenApi.Internal.AesonUtils (AesonDefaultValue (..), HasSwaggerAesonOptions (..),
mkSwaggerAesonOptions, saoAdditionalPairs, saoSubObject,
sopSwaggerGenericParseJSON, sopSwaggerGenericToEncoding,
Expand Down Expand Up @@ -649,7 +649,7 @@ data ParamLocation
| ParamCookie
deriving (Eq, Ord, Show, Generic, Data, Typeable)

instance Hashable ParamLocation
instance Hashable ParamLocation

type Format = Text

Expand Down Expand Up @@ -1018,7 +1018,7 @@ data Referenced a
instance IsString a => IsString (Referenced a) where
fromString = Inline . fromString

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

data AdditionalProperties
Expand Down Expand Up @@ -1555,19 +1555,19 @@ instance FromJSON Param where
parseJSON = sopSwaggerGenericParseJSON

instance FromJSON Responses where
parseJSON (Object o) = Responses
<$> o .:? "default"
<*> parseJSON
( Object
( HashMap.filterWithKey (\k _ -> not $ isExt k)
$ HashMap.delete "default" o
)
)
<*> case HashMap.filterWithKey (\k _ -> isExt k) o of
exts
| HashMap.null exts -> pure (SpecificationExtensions mempty)
| otherwise -> parseJSON (Object exts)
parseJSON (Object o) = Responses
<$> o .:? "default"
<*> parseJSON
( Object
( filterKeys (not . isExt . keyToText) $
deleteKey "default" o
)
)
<*> case filterKeys (isExt . keyToText) o of
exts
| null exts -> pure (SpecificationExtensions mempty)
| otherwise -> parseJSON (Object exts)

parseJSON _ = empty

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


instance FromJSON Info where
Expand Down Expand Up @@ -1695,7 +1695,7 @@ instance HasSwaggerAesonOptions SecurityScheme where
instance HasSwaggerAesonOptions Schema where
swaggerAesonOptions _ = mkSwaggerAesonOptions "schema" & saoSubObject .~ ["paramSchema", "extensions"]
instance HasSwaggerAesonOptions OpenApi where
swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
swaggerAesonOptions _ = mkSwaggerAesonOptions "swagger" & saoAdditionalPairs .~ [("openapi", "3.0.0")]
& saoSubObject .~ ["extensions"]
instance HasSwaggerAesonOptions Example where
swaggerAesonOptions _ = mkSwaggerAesonOptions "example" & saoSubObject .~ ["extensions"]
Expand Down

0 comments on commit 4793b3c

Please sign in to comment.