Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

ilyakooo0
Copy link

closes #204

@fisx
Copy link
Collaborator

fisx commented Mar 19, 2020

There is a test finding String "#/definitions/myrosetree%27". Did you forget to escape somewhere?

@ilyakooo0
Copy link
Author

It seems like the test is finding it in a URI, which should be escaped. Am I wrong?

Copy link
Collaborator

@fisx fisx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may be right about the test case, but I'm not sure any more what you're trying to accomplish. I've left some comments, but I'll also look into the code a bit more soon.

@@ -344,3 +349,55 @@ sopSwaggerGenericToEncoding'' (SwaggerAesonOptions prefix _ sub) = go
where (x, y) = span isUpper s

#endif

percentEncodeS :: String -> String
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use urlEncode.

Copy link
Author

Choose a reason for hiding this comment

The 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 sub-delims set of characters from RFC 3986, which isn't strictly necessary and is scheme-specific.

I think the desired behaviour is for mainstream tools (like https://editor.swagger.io) to accept all output as valid.

@@ -1150,10 +1153,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 ]
Copy link
Collaborator

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.

Copy link
Author

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 the Reference 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 the Reference 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.

Copy link
Author

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.

src/Data/Swagger/Internal.hs Outdated Show resolved Hide resolved
@fisx
Copy link
Collaborator

fisx commented Mar 20, 2020

Thanks, btw! :)

@ilyakooo0
Copy link
Author

BTW, the way I found out about the need to percent-encode is by importing a generated swagger file, containing a type with a ' into the swagger editor (https://editor.swagger.io). It gave errors about needing to escape URIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Escape URI
2 participants