Skip to content

Commit 92ff97d

Browse files
authored
fix: use non-failable UTF8View Data init when converting from String (#3430)
1 parent 006e045 commit 92ff97d

File tree

58 files changed

+125
-184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+125
-184
lines changed

Amplify/Categories/DataStore/Model/Internal/Model+Codable.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,7 @@ extension Model where Self: Codable {
3333
resolvedDecoder = JSONDecoder(dateDecodingStrategy: ModelDateFormatting.decodingStrategy)
3434
}
3535

36-
guard let data = json.data(using: .utf8) else {
37-
throw DataStoreError.decodingError(
38-
"Invalid JSON string. Could not convert the passed JSON string into a UTF-8 Data object",
39-
"Ensure the JSON doesn't contain any invalid UTF-8 data:\n\n\(json)"
40-
)
41-
}
42-
43-
return try resolvedDecoder.decode(Self.self, from: data)
36+
return try resolvedDecoder.decode(Self.self, from: Data(json.utf8))
4437
}
4538

4639
/// De-serialize a `Dictionary` into an instance of the concrete type that conforms

Amplify/Categories/DataStore/Model/Internal/Schema/ModelValueConverter.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ extension ModelValueConverter {
6363
/// application making any change to these `public` types should be backward compatible, otherwise it will be a
6464
/// breaking change.
6565
public static func fromJSON(_ value: String) throws -> Any? {
66-
guard let data = value.data(using: .utf8) else {
67-
return nil
68-
}
69-
return try JSONSerialization.jsonObject(with: data)
66+
return try JSONSerialization.jsonObject(with: Data(value.utf8))
7067
}
7168
}

AmplifyFunctionalTests/AmplifyConfigurationInitFromFileTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AmplifyConfigurationInitFromFileTests: XCTestCase {
2222
}
2323
"""
2424

25-
let configData = configString.data(using: .utf8)!
25+
let configData = Data(configString.utf8)
2626
let configURL = FileManager
2727
.default
2828
.temporaryDirectory

AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/AuthenticationTokenAuthInterceptor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class AuthenticationTokenAuthInterceptor: AuthInterceptorAsync {
5555
let authHeader = TokenAuthHeader(token: authToken, host: host)
5656
let base64Auth = AppSyncJSONHelper.base64AuthenticationBlob(authHeader)
5757

58-
let payloadData = SubscriptionConstants.emptyPayload.data(using: .utf8)
59-
let payloadBase64 = payloadData?.base64EncodedString()
58+
let payloadData = Data(SubscriptionConstants.emptyPayload.utf8)
59+
let payloadBase64 = payloadData.base64EncodedString()
6060

6161
guard var urlComponents = URLComponents(url: request.url, resolvingAgainstBaseURL: false) else {
6262
return request

AmplifyPlugins/API/Sources/AWSAPIPlugin/Interceptor/SubscriptionInterceptor/IAMAuthInterceptor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class IAMAuthInterceptor: AuthInterceptorAsync {
5454
}
5555
let base64Auth = AppSyncJSONHelper.base64AuthenticationBlob(authHeader)
5656

57-
let payloadData = payloadString.data(using: .utf8)
58-
let payloadBase64 = payloadData?.base64EncodedString()
57+
let payloadData = Data(payloadString.utf8)
58+
let payloadBase64 = payloadData.base64EncodedString()
5959

6060
guard var urlComponents = URLComponents(url: request.url, resolvingAgainstBaseURL: false) else {
6161
return request
@@ -91,7 +91,7 @@ class IAMAuthInterceptor: AuthInterceptorAsync {
9191
.withHeader(name: RealtimeProviderConstants.contentEncodingKey, value: RealtimeProviderConstants.iamEncoding)
9292
.withHeader(name: URLRequestConstants.Header.contentType, value: RealtimeProviderConstants.iamConentType)
9393
.withHeader(name: URLRequestConstants.Header.host, value: host)
94-
.withBody(.data(payload.data(using: .utf8)))
94+
.withBody(.data(Data(payload.utf8)))
9595

9696
/// 2. The request is SigV4 signed by using all the available headers on the request. By signing the request, the signature is added to
9797
/// the request headers as authorization and security token.

AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/AWSHTTPURLResponseTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import XCTest
1111
class AWSHTTPURLResponseTests: XCTestCase {
1212

1313
func testAWSHTTPURLResponse() throws {
14-
let body = "responseBody".data(using: .utf8)
14+
let body = Data("responseBody".utf8)
1515
let httpResponse = HTTPURLResponse(url: URL(string: "dummyString")!,
1616
statusCode: 200,
1717
httpVersion: "1.1",
@@ -35,7 +35,7 @@ class AWSHTTPURLResponseTests: XCTestCase {
3535
}
3636

3737
func testAWSHTTPURLResponseNSCoding() {
38-
let body = "responseBody".data(using: .utf8)
38+
let body = Data("responseBody".utf8)
3939
let httpResponse = HTTPURLResponse(url: URL(string: "dummyString")!,
4040
statusCode: 200,
4141
httpVersion: "1.1",

AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/GraphQLMutateCombineTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class GraphQLMutateCombineTests: OperationTestBase {
1616

1717
func testMutateSucceeds() throws {
1818
let testJSONData: JSONValue = ["foo": true]
19-
let sentData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
19+
let sentData = Data(#"{"data": {"foo": true}}"#.utf8)
2020
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)
2121

2222
let request = GraphQLRequest(document: testDocument, variables: nil, responseType: JSONValue.self)
@@ -52,7 +52,7 @@ class GraphQLMutateCombineTests: OperationTestBase {
5252
}
5353

5454
func testMutateHandlesResponseError() throws {
55-
let sentData = #"{"data": {"foo": true}, "errors": []}"# .data(using: .utf8)!
55+
let sentData = Data(#"{"data": {"foo": true}, "errors": []}"#.utf8)
5656
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)
5757

5858
let request = GraphQLRequest(document: testDocument, variables: nil, responseType: JSONValue.self)

AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/GraphQLQueryCombineTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class GraphQLQueryCombineTests: OperationTestBase {
1616

1717
func testQuerySucceeds() throws {
1818
let testJSONData: JSONValue = ["foo": true]
19-
let sentData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
19+
let sentData = Data(#"{"data": {"foo": true}}"#.utf8)
2020
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)
2121

2222
let request = GraphQLRequest(document: testDocument, variables: nil, responseType: JSONValue.self)
@@ -52,7 +52,7 @@ class GraphQLQueryCombineTests: OperationTestBase {
5252
}
5353

5454
func testQueryHandlesResponseError() throws {
55-
let sentData = #"{"data": {"foo": true}, "errors": []}"# .data(using: .utf8)!
55+
let sentData = Data(#"{"data": {"foo": true}, "errors": []}"#.utf8)
5656
try setUpPluginForSingleResponse(sending: sentData, for: .graphQL)
5757

5858
let request = GraphQLRequest(document: testDocument, variables: nil, responseType: JSONValue.self)

AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/GraphQLSubscribeCombineTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
7272
receivedDataValueError.isInverted = true
7373

7474
let testJSON: JSONValue = ["foo": true]
75-
let testData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
75+
let testData = Data(#"{"data": {"foo": true}}"#.utf8)
7676

7777
try await subscribe(expecting: testJSON)
7878
await fulfillment(of: [onSubscribeInvoked], timeout: 0.05)
@@ -117,7 +117,7 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
117117
}
118118

119119
func testDecodingError() async throws {
120-
let testData = #"{"data": {"foo": true}, "errors": []}"# .data(using: .utf8)!
120+
let testData = Data(#"{"data": {"foo": true}, "errors": []}"#.utf8)
121121
receivedCompletionFailure.isInverted = true
122122
receivedDataValueSuccess.isInverted = true
123123

@@ -134,7 +134,7 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
134134

135135
func testMultipleSuccessValues() async throws {
136136
let testJSON: JSONValue = ["foo": true]
137-
let testData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
137+
let testData = Data(#"{"data": {"foo": true}}"#.utf8)
138138
receivedCompletionFailure.isInverted = true
139139
receivedDataValueError.isInverted = true
140140
receivedDataValueSuccess.expectedFulfillmentCount = 2
@@ -152,8 +152,8 @@ class GraphQLSubscribeCombineTests: OperationTestBase {
152152
}
153153

154154
func testMixedSuccessAndErrorValues() async throws {
155-
let successfulTestData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
156-
let invalidTestData = #"{"data": {"foo": true}, "errors": []}"# .data(using: .utf8)!
155+
let successfulTestData = Data(#"{"data": {"foo": true}}"#.utf8)
156+
let invalidTestData = Data(#"{"data": {"foo": true}, "errors": []}"#.utf8)
157157
receivedCompletionFailure.isInverted = true
158158
receivedDataValueSuccess.expectedFulfillmentCount = 2
159159

AmplifyPlugins/API/Tests/AWSAPIPluginTests/Operation/GraphQLSubscribeTaskTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class GraphQLSubscribeTasksTests: OperationTestBase {
7676
receivedDataValueError.isInverted = true
7777

7878
let testJSON: JSONValue = ["foo": true]
79-
let testData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
79+
let testData = Data(#"{"data": {"foo": true}}"#.utf8)
8080

8181
try await subscribe(expecting: testJSON)
8282
await fulfillment(of: [onSubscribeInvoked], timeout: 0.05)
@@ -169,7 +169,7 @@ class GraphQLSubscribeTasksTests: OperationTestBase {
169169
}
170170

171171
func testDecodingError() async throws {
172-
let testData = #"{"data": {"foo": true}, "errors": []}"# .data(using: .utf8)!
172+
let testData = Data(#"{"data": {"foo": true}, "errors": []}"#.utf8)
173173
receivedCompletionFailure.isInverted = true
174174
receivedDataValueSuccess.isInverted = true
175175

@@ -186,7 +186,7 @@ class GraphQLSubscribeTasksTests: OperationTestBase {
186186

187187
func testMultipleSuccessValues() async throws {
188188
let testJSON: JSONValue = ["foo": true]
189-
let testData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
189+
let testData = Data(#"{"data": {"foo": true}}"#.utf8)
190190

191191
receivedCompletionFailure.isInverted = true
192192
receivedDataValueError.isInverted = true
@@ -205,8 +205,8 @@ class GraphQLSubscribeTasksTests: OperationTestBase {
205205
}
206206

207207
func testMixedSuccessAndErrorValues() async throws {
208-
let successfulTestData = #"{"data": {"foo": true}}"# .data(using: .utf8)!
209-
let invalidTestData = #"{"data": {"foo": true}, "errors": []}"# .data(using: .utf8)!
208+
let successfulTestData = Data(#"{"data": {"foo": true}}"#.utf8)
209+
let invalidTestData = Data(#"{"data": {"foo": true}, "errors": []}"#.utf8)
210210

211211
receivedCompletionFailure.isInverted = true
212212
receivedDataValueSuccess.expectedFulfillmentCount = 2

0 commit comments

Comments
 (0)