-
Notifications
You must be signed in to change notification settings - Fork 47
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
Fix encoding of plain text bodies #9
Fix encoding of plain text bodies #9
Conversation
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.
Nice find! I think we can remove some of the optional and force casting though.
let decoded: T | ||
if let myType = T.self as? _StringParameterConvertible.Type { | ||
guard | ||
let stringValue = String(data: data, encoding: .utf8), |
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.
Please avoid String(data:encoding:), especially in networked software as it has 2 failure modes (return nil or insert replacement characters for invalid UTF8 -- which one it chooses depends on what's broken).
The correct and faster way is String(decoding: data, as: UTF8.self)
which also doesn't require Foundation and is not fallible
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.
Thanks, broke out into apple/swift-openapi-generator#42
@@ -128,6 +139,12 @@ extension Converter { | |||
) throws -> Data { | |||
let body = transform(value) | |||
headerFields.add(name: "content-type", value: body.contentType) | |||
if let value = value as? _StringParameterConvertible { | |||
guard let data = value.description.data(using: .utf8) else { |
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.
Same here, the correct and fast way is Data(value.utf8)
which can't fail and is faster. You should audit your code to make sure String(data
and data(using
aren't present
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.
Motivation
When the content type
text/plain
is used, we had a bug where we were encoding the text usingCodable
into a JSON fragment, i.e.hello
->"hello"
(note the added quotes), which is incorrect. We should be usingLosslessStringConvertible
for primitive types, so thathello
is sent ashello
(without the quotes).Modifications
Fixed the body conversion methods to use the string conversion if possible, and only use the
Codable
implementation for complex types.Result
When content type is
text/plain
(and other plain text variants),hello
is sent ashello
, not"hello"
.Test Plan
Updated unit tests and added new ones to cover these cases.