-
Notifications
You must be signed in to change notification settings - Fork 35
fix!: Throw error on invalid encoded data in response #984
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
Conversation
| let traceInterceptor = TraceInterceptor<TestInput, TestOutput, HTTPRequest, HTTPResponse>(trace: trace) | ||
| let builder = OrchestratorBuilder<TestInput, TestOutput, HTTPRequest, HTTPResponse>() | ||
| .attributes(attributes) | ||
| .serialize({ input, builder, _ in |
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.
The tests below used JSON fragments (i.e. a JSON string only) for serde since fragments were allowed and it kept the test setup simpler. Tests now serialize JSON objects so the allowFragments option on the JSON reader can be disabled.
…swift into jbe/invalid_json_fix
| } catch let error as NSError where error.domain == "NSCocoaErrorDomain" && error.code == 3840 { | ||
| return try Reader(nodeInfo: "", jsonObject: [:]) | ||
| } catch { | ||
| throw InvalidEncodingError(wrapped: error) |
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.
Above:
- Explanatory comments are added
- JSON fragments are no longer allowed (no real services use them)
ResponseEncodingError(which wraps the spacific error thrown by the decoding implementation) is thrown when the JSON cannot be decoded
| guard let doc = xmlReadMemory(buffer.pointee.content, Int32(count), "", "UTF-8", 0) else { return Reader() } | ||
| guard let doc = xmlReadMemory(buffer.pointee.content, Int32(count), "", "UTF-8", 0) else { | ||
| throw InvalidEncodingError(wrapped: XMLError.parsingError) | ||
| } |
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.
Instead of returning an empty parsed body, throw an ResponseEncodingError when the XML is not valid.
| XCTAssertThrowsError(try Reader.from(data: invalidXML)) { error in | ||
| XCTAssert(error is ResponseEncodingError) | ||
| } | ||
| } |
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.
Test above verifies that ResponseEncodingError is thrown for invalid XML.
| @@ -0,0 +1,28 @@ | |||
| // | |||
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.
- Should this file name be changed from
InvalidEncodingErrorto match the error struct name - This may be wrong but shouldn't the error name be
ResponseDecodingErrorinstead ofResponseEncodingErrorsince SDK client is decoding response rather than encoding it
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.
Renamed as recommended
Description of changes
Currently, when a response contains invalid JSON, the error is caught & not rethrown, and the response body is treated as an empty JSON object
{}.This can lead to misleading responses, i.e. leading the caller to believe that the request succeeded but returned no results versus that it was not parseable.
The error that is now thrown for an unparseable response is
SmithySerialization.ResponseEncodingError. This error "wraps" the specific error thrown by the parsing implementation, which hides both the implementation & the specific encoding in use. This module & name were chosen to be compatible with future planned changes to smithy-swift.guardthat handles the empty-response case now has some commentary to explain why it's there.NSErrorthat is thrown when JSON is invalid, and converting it to a successful response. That catch is now removed.nil. That behavior is unchanged.This is a breaking change in that it alters the SDK's behavior from "empty success" to throwing when unparseable data is encountered in a response. The impact should be minimal because invalidly encoded responses should never be happening on AWS services. (This was found in a cross-SDK investigation.)
Scope
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.