Skip to content

Conversation

@jbelkins
Copy link
Contributor

@jbelkins jbelkins commented Oct 22, 2025

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.

  • The guard that handles the empty-response case now has some commentary to explain why it's there.
  • Our JSON parser was set to allow fragments. This is not needed for SDK use and is removed. (A couple tests had to have their setup altered to accommodate this, since they were using fragments for simplicity of setup.)
  • We were catching the NSError that is thrown when JSON is invalid, and converting it to a successful response. That catch is now removed.
  • Empty bodies are explicitly allowed by JSON-based AWS protocols, and are generally to be treated as nil. That behavior is unchanged.
  • Similarly, SmithyXML now throws a parse error when the XML cannot be parsed.
  • JSON and XML parsers are now tested for correct behavior when the parsed response is invalid.

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

let traceInterceptor = TraceInterceptor<TestInput, TestOutput, HTTPRequest, HTTPResponse>(trace: trace)
let builder = OrchestratorBuilder<TestInput, TestOutput, HTTPRequest, HTTPResponse>()
.attributes(attributes)
.serialize({ input, builder, _ in
Copy link
Contributor Author

@jbelkins jbelkins Oct 22, 2025

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.

@jbelkins jbelkins changed the title fix: Throw error on invalid JSON in response fix!: Throw error on invalid JSON in response Oct 24, 2025
@jbelkins jbelkins marked this pull request as ready for review November 24, 2025 17:16
} catch let error as NSError where error.domain == "NSCocoaErrorDomain" && error.code == 3840 {
return try Reader(nodeInfo: "", jsonObject: [:])
} catch {
throw InvalidEncodingError(wrapped: error)
Copy link
Contributor Author

@jbelkins jbelkins Nov 24, 2025

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)
}
Copy link
Contributor Author

@jbelkins jbelkins Nov 24, 2025

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.

@jbelkins jbelkins changed the title fix!: Throw error on invalid JSON in response fix!: Throw error on invalid encoded data in response Nov 24, 2025
XCTAssertThrowsError(try Reader.from(data: invalidXML)) { error in
XCTAssert(error is ResponseEncodingError)
}
}
Copy link
Contributor Author

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 @@
//
Copy link
Contributor

@sichanyoo sichanyoo Nov 24, 2025

Choose a reason for hiding this comment

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

  1. Should this file name be changed from InvalidEncodingError to match the error struct name
  2. This may be wrong but shouldn't the error name be ResponseDecodingError instead of ResponseEncodingError since SDK client is decoding response rather than encoding it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed as recommended

@jbelkins jbelkins merged commit 3f41237 into main Nov 25, 2025
33 checks passed
@jbelkins jbelkins deleted the jbe/invalid_json_fix branch November 25, 2025 22:17
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.

3 participants