git.iko.soy/typeable/swagger2
on master help sign in
Added percent encoding
779c587iko
src/Data/Swagger/Internal.hs+18−4
@@ -55,7 +55,9 @@ import Data.Swagger.Internal.AesonUtils (sopSwaggerGenericToJSON
55 ,AesonDefaultValue(..)
55 ,AesonDefaultValue(..)
56 ,mkSwaggerAesonOptions
56 ,mkSwaggerAesonOptions
57 ,saoAdditionalPairs
57 ,saoAdditionalPairs
58− ,saoSubObject)
58+ ,saoSubObject
59+ ,percentEncodeT
60+ ,percentDecodeT)
59import Data.Swagger.Internal.Utils
61import Data.Swagger.Internal.Utils
60
62
61#if MIN_VERSION_aeson(0,10,0)
63#if MIN_VERSION_aeson(0,10,0)
@@ -1039,8 +1041,14 @@ instance ToJSON SecuritySchemeType where
1039 <+> object [ "type" .= ("oauth2" :: Text) ]
1041 <+> object [ "type" .= ("oauth2" :: Text) ]
1040
1042
1041instance ToJSON Swagger where
1043instance ToJSON Swagger where
1042− toJSON = sopSwaggerGenericToJSON
1044+ toJSON = sopSwaggerGenericToJSON . percentEncodeSwagger
1043− DEFINE_TOENCODING
1045+ toEncoding = sopSwaggerGenericToEncoding . percentEncodeSwagger
1046+
1047+percentEncodeSwagger :: Swagger -> Swagger
1048+percentEncodeSwagger s = s
1049+ { _swaggerDefinitions =
1050+ InsOrdHashMap.mapKeys percentEncodeT $ _swaggerDefinitions s
1051+ }
1044
1052
1045instance ToJSON SecurityScheme where
1053instance ToJSON SecurityScheme where
1046 toJSON = sopSwaggerGenericToJSON
1054 toJSON = sopSwaggerGenericToJSON
@@ -1180,7 +1188,13 @@ instance FromJSON SecuritySchemeType where
1180 parseJSON _ = empty
1188 parseJSON _ = empty
1181
1189
1182instance FromJSON Swagger where
1190instance 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+ }
1184
1198
1185instance FromJSON SecurityScheme where
1199instance FromJSON SecurityScheme where
1186 parseJSON = sopSwaggerGenericParseJSON
1200 parseJSON = sopSwaggerGenericParseJSON
src/Data/Swagger/Internal/AesonUtils.hs+57
@@ -24,6 +24,11 @@ module Data.Swagger.Internal.AesonUtils (
24 saoPrefix,
24 saoPrefix,
25 saoAdditionalPairs,
25 saoAdditionalPairs,
26 saoSubObject,
26 saoSubObject,
27+ -- * Percent encoding
28+ percentEncodeS,
29+ percentEncodeT,
30+ percentDecodeS,
31+ percentDecodeT,
27 ) where
32 ) where
28
33
29import Prelude ()
34import Prelude ()
@@ -330,3 +335,55 @@ sopSwaggerGenericToEncoding'' (SwaggerAesonOptions prefix _ sub) = go
330 where (x, y) = span isUpper s
335 where (x, y) = span isUpper s
331
336
332#endif
337#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