-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Percent encoding #205
base: master
Are you sure you want to change the base?
Percent encoding #205
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use urlEncode. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking through the source code, it doesn't escape all reserved characters: source. It doesn't escape the I think the desired behaviour is for mainstream tools (like https://editor.swagger.io) to accept all output as valid. |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to me the wrong place to do this. The text inside the
Reference
should really be a type that can only be turned into a string by encoding it; the fact that the string is also encoded if it appears inside a json object is coincidental.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what you are trying to say.
Reference
references some other type, as you have alluded to.IMO the Haskell data type should contain the literate string, which is the name of the referenced type.
So, if the type referenced is
User'
, then the literal string contained in theReference
should be"User'"
. However, swagger requires all URIs (references) to be a proper URI, which requires special characters (such as'
) to be escaped with percent-encoding. So, percent-encoding really is just the way theReference
type is converted to swagger.And, as far as I understand it, in the scope of this library,
ToJSON
instances on swagger-related datatypes should produce a valid swagger description.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will fix the tests (if that will be necessary), once we come to a common understanding.