git.iko.soy/typeable/swagger2
on master help sign in
Added percent encoding
commit779c587f548716e27868aa89346c8532057373e1 authoriko <ilyakooo0@gmail.com> dateThu 19 Mar 2020, 17:11 (7 years ago) parent71667d5
2 files changed+75−4
src/Data/Swagger/Internal.hs+18−4 src/Data/Swagger/Internal/AesonUtils.hs+57
src/Data/Swagger/Internal.hs+18−4
@@ -55,7 +55,9 @@ import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
5555 ,AesonDefaultValue(..)
5656 ,mkSwaggerAesonOptions
5757 ,saoAdditionalPairs
58− ,saoSubObject)
58+ ,saoSubObject
59+ ,percentEncodeT
60+ ,percentDecodeT)
5961import Data.Swagger.Internal.Utils
6062
6163#if MIN_VERSION_aeson(0,10,0)
@@ -1039,8 +1041,14 @@ instance ToJSON SecuritySchemeType where
10391041 <+> object [ "type" .= ("oauth2" :: Text) ]
10401042
10411043instance ToJSON Swagger where
1042− toJSON = sopSwaggerGenericToJSON
1043− DEFINE_TOENCODING
1044+ toJSON = sopSwaggerGenericToJSON . percentEncodeSwagger
1045+ toEncoding = sopSwaggerGenericToEncoding . percentEncodeSwagger
1046+
1047+percentEncodeSwagger :: Swagger -> Swagger
1048+percentEncodeSwagger s = s
1049+ { _swaggerDefinitions =
1050+ InsOrdHashMap.mapKeys percentEncodeT $ _swaggerDefinitions s
1051+ }
10441052
10451053instance ToJSON SecurityScheme where
10461054 toJSON = sopSwaggerGenericToJSON
@@ -1180,7 +1188,13 @@ instance FromJSON SecuritySchemeType where
11801188 parseJSON _ = empty
11811189
11821190instance FromJSON Swagger where
1183− parseJSON = sopSwaggerGenericParseJSON
1191+ parseJSON = fmap percentDecodeSwagger . sopSwaggerGenericParseJSON
1192+
1193+percentDecodeSwagger :: Swagger -> Swagger
1194+percentDecodeSwagger s = s
1195+ { _swaggerDefinitions =
1196+ InsOrdHashMap.mapKeys percentEncodeT $ _swaggerDefinitions s
1197+ }
11841198
11851199instance FromJSON SecurityScheme where
11861200 parseJSON = sopSwaggerGenericParseJSON
src/Data/Swagger/Internal/AesonUtils.hs+57
@@ -24,6 +24,11 @@ module Data.Swagger.Internal.AesonUtils (
2424 saoPrefix,
2525 saoAdditionalPairs,
2626 saoSubObject,
27+ -- * Percent encoding
28+ percentEncodeS,
29+ percentEncodeT,
30+ percentDecodeS,
31+ percentDecodeT,
2732 ) where
2833
2934import Prelude ()
@@ -330,3 +335,55 @@ sopSwaggerGenericToEncoding'' (SwaggerAesonOptions prefix _ sub) = go
330335 where (x, y) = span isUpper s
331336
332337#endif
338+
339+percentEncodeS :: String -> String
340+percentEncodeS "" = ""
341+percentEncodeS ('!' : s) = "%21" <> percentEncodeS s
342+percentEncodeS ('#' : s) = "%23" <> percentEncodeS s
343+percentEncodeS ('$' : s) = "%24" <> percentEncodeS s
344+percentEncodeS ('%' : s) = "%25" <> percentEncodeS s
345+percentEncodeS ('&' : s) = "%26" <> percentEncodeS s
346+percentEncodeS ('\'' : s) = "%27" <> percentEncodeS s
347+percentEncodeS ('(' : s) = "%28" <> percentEncodeS s
348+percentEncodeS (')' : s) = "%29" <> percentEncodeS s
349+percentEncodeS ('*' : s) = "%2A" <> percentEncodeS s
350+percentEncodeS ('+' : s) = "%2B" <> percentEncodeS s
351+percentEncodeS (',' : s) = "%2C" <> percentEncodeS s
352+percentEncodeS ('/' : s) = "%2F" <> percentEncodeS s
353+percentEncodeS (':' : s) = "%3A" <> percentEncodeS s
354+percentEncodeS (';' : s) = "%3B" <> percentEncodeS s
355+percentEncodeS ('=' : s) = "%3D" <> percentEncodeS s
356+percentEncodeS ('?' : s) = "%3F" <> percentEncodeS s
357+percentEncodeS ('@' : s) = "%40" <> percentEncodeS s
358+percentEncodeS ('[' : s) = "%5B" <> percentEncodeS s
359+percentEncodeS (']' : s) = "%5D" <> percentEncodeS s
360+percentEncodeS (c : s) = c : percentEncodeS s
361+
362+percentEncodeT :: Text -> Text
363+percentEncodeT = T.pack . percentEncodeS . T.unpack
364+
365+percentDecodeS :: String -> String
366+percentDecodeS "" = ""
367+percentDecodeS ('%':'2':'1':s) = '!' : percentDecodeS s
368+percentDecodeS ('%':'2':'3':s) = '#' : percentDecodeS s
369+percentDecodeS ('%':'2':'4':s) = '$' : percentDecodeS s
370+percentDecodeS ('%':'2':'5':s) = '%' : percentDecodeS s
371+percentDecodeS ('%':'2':'6':s) = '&' : percentDecodeS s
372+percentDecodeS ('%':'2':'7':s) = '\'' : percentDecodeS s
373+percentDecodeS ('%':'2':'8':s) = '(' : percentDecodeS s
374+percentDecodeS ('%':'2':'9':s) = ')' : percentDecodeS s
375+percentDecodeS ('%':'2':'A':s) = '*' : percentDecodeS s
376+percentDecodeS ('%':'2':'B':s) = '+' : percentDecodeS s
377+percentDecodeS ('%':'2':'C':s) = ',' : percentDecodeS s
378+percentDecodeS ('%':'2':'F':s) = '/' : percentDecodeS s
379+percentDecodeS ('%':'3':'A':s) = ':' : percentDecodeS s
380+percentDecodeS ('%':'3':'B':s) = ';' : percentDecodeS s
381+percentDecodeS ('%':'3':'D':s) = '=' : percentDecodeS s
382+percentDecodeS ('%':'3':'F':s) = '?' : percentDecodeS s
383+percentDecodeS ('%':'4':'0':s) = '@' : percentDecodeS s
384+percentDecodeS ('%':'5':'B':s) = '[' : percentDecodeS s
385+percentDecodeS ('%':'5':'D':s) = ']' : percentDecodeS s
386+percentDecodeS (c : s) = c : percentDecodeS s
387+
388+percentDecodeT :: Text -> Text
389+percentDecodeT = T.pack . percentDecodeS . T.unpack