diff --git a/src/Data/Swagger/Internal.hs b/src/Data/Swagger/Internal.hs index d7b1a5b..560b6ec 100644 --- a/src/Data/Swagger/Internal.hs +++ b/src/Data/Swagger/Internal.hs @@ -56,7 +56,9 @@ import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON ,AesonDefaultValue(..) ,mkSwaggerAesonOptions ,saoAdditionalPairs - ,saoSubObject) + ,saoSubObject + ,percentEncodeT + ,percentDecodeT) import Data.Swagger.Internal.Utils #if MIN_VERSION_aeson(0,10,0) @@ -1150,10 +1152,11 @@ instance ToJSON SecurityDefinitions where toJSON (SecurityDefinitions sd) = toJSON sd instance ToJSON Reference where - toJSON (Reference ref) = object [ "$ref" .= ref ] + toJSON (Reference ref) = object [ "$ref" .= percentEncodeT ref ] referencedToJSON :: ToJSON a => Text -> Referenced a -> Value -referencedToJSON prefix (Ref (Reference ref)) = object [ "$ref" .= (prefix <> ref) ] +referencedToJSON prefix (Ref (Reference ref)) = + object [ "$ref" .= (prefix <> percentEncodeT ref) ] referencedToJSON _ (Inline x) = toJSON x instance ToJSON (Referenced Schema) where toJSON = referencedToJSON "#/definitions/" @@ -1314,7 +1317,7 @@ instance FromJSON SecurityDefinitions where parseJSON js = SecurityDefinitions <$> parseJSON js instance FromJSON Reference where - parseJSON (Object o) = Reference <$> o .: "$ref" + parseJSON (Object o) = Reference . percentDecodeT <$> o .: "$ref" parseJSON _ = empty referencedParseJSON :: FromJSON a => Text -> Value -> JSON.Parser (Referenced a) diff --git a/src/Data/Swagger/Internal/AesonUtils.hs b/src/Data/Swagger/Internal/AesonUtils.hs index 3193670..3733866 100644 --- a/src/Data/Swagger/Internal/AesonUtils.hs +++ b/src/Data/Swagger/Internal/AesonUtils.hs @@ -24,6 +24,11 @@ module Data.Swagger.Internal.AesonUtils ( saoPrefix, saoAdditionalPairs, saoSubObject, + -- * Percent encoding + percentEncodeS, + percentEncodeT, + percentDecodeS, + percentDecodeT, ) where import Prelude () @@ -344,3 +349,55 @@ sopSwaggerGenericToEncoding'' (SwaggerAesonOptions prefix _ sub) = go where (x, y) = span isUpper s #endif + +percentEncodeS :: String -> String +percentEncodeS "" = "" +percentEncodeS ('!' : s) = "%21" <> percentEncodeS s +percentEncodeS ('#' : s) = "%23" <> percentEncodeS s +percentEncodeS ('$' : s) = "%24" <> percentEncodeS s +percentEncodeS ('%' : s) = "%25" <> percentEncodeS s +percentEncodeS ('&' : s) = "%26" <> percentEncodeS s +percentEncodeS ('\'' : s) = "%27" <> percentEncodeS s +percentEncodeS ('(' : s) = "%28" <> percentEncodeS s +percentEncodeS (')' : s) = "%29" <> percentEncodeS s +percentEncodeS ('*' : s) = "%2A" <> percentEncodeS s +percentEncodeS ('+' : s) = "%2B" <> percentEncodeS s +percentEncodeS (',' : s) = "%2C" <> percentEncodeS s +percentEncodeS ('/' : s) = "%2F" <> percentEncodeS s +percentEncodeS (':' : s) = "%3A" <> percentEncodeS s +percentEncodeS (';' : s) = "%3B" <> percentEncodeS s +percentEncodeS ('=' : s) = "%3D" <> percentEncodeS s +percentEncodeS ('?' : s) = "%3F" <> percentEncodeS s +percentEncodeS ('@' : s) = "%40" <> percentEncodeS s +percentEncodeS ('[' : s) = "%5B" <> percentEncodeS s +percentEncodeS (']' : s) = "%5D" <> percentEncodeS s +percentEncodeS (c : s) = c : percentEncodeS s + +percentEncodeT :: Text -> Text +percentEncodeT = T.pack . percentEncodeS . T.unpack + +percentDecodeS :: String -> String +percentDecodeS "" = "" +percentDecodeS ('%':'2':'1':s) = '!' : percentDecodeS s +percentDecodeS ('%':'2':'3':s) = '#' : percentDecodeS s +percentDecodeS ('%':'2':'4':s) = '$' : percentDecodeS s +percentDecodeS ('%':'2':'5':s) = '%' : percentDecodeS s +percentDecodeS ('%':'2':'6':s) = '&' : percentDecodeS s +percentDecodeS ('%':'2':'7':s) = '\'' : percentDecodeS s +percentDecodeS ('%':'2':'8':s) = '(' : percentDecodeS s +percentDecodeS ('%':'2':'9':s) = ')' : percentDecodeS s +percentDecodeS ('%':'2':'A':s) = '*' : percentDecodeS s +percentDecodeS ('%':'2':'B':s) = '+' : percentDecodeS s +percentDecodeS ('%':'2':'C':s) = ',' : percentDecodeS s +percentDecodeS ('%':'2':'F':s) = '/' : percentDecodeS s +percentDecodeS ('%':'3':'A':s) = ':' : percentDecodeS s +percentDecodeS ('%':'3':'B':s) = ';' : percentDecodeS s +percentDecodeS ('%':'3':'D':s) = '=' : percentDecodeS s +percentDecodeS ('%':'3':'F':s) = '?' : percentDecodeS s +percentDecodeS ('%':'4':'0':s) = '@' : percentDecodeS s +percentDecodeS ('%':'5':'B':s) = '[' : percentDecodeS s +percentDecodeS ('%':'5':'D':s) = ']' : percentDecodeS s +percentDecodeS (c : s) = c : percentDecodeS s + +percentDecodeT :: Text -> Text +percentDecodeT = T.pack . percentDecodeS . T.unpack