Skip to content

Commit

Permalink
fix: Convert Swift extension to helper class for use by ObjC (#3973)
Browse files Browse the repository at this point in the history
Resolved a crash introduced with #3931.

Co-authored-by: Max Chuquimia <>
Co-authored-by: Philipp Hofmann <philipp.hofmann@sentry.io>
  • Loading branch information
maxchuquimia and philipphofmann committed May 14, 2024
1 parent 3912b16 commit 1ac58a6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 36 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fix retrieving GraphQL operation names crashing ([#3973](https://github.com/getsentry/sentry-cocoa/pull/3973))

## 8.26.0

### Features
Expand Down
16 changes: 8 additions & 8 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
15E0A8F22411A45A00F044E3 /* SentrySession.m in Sources */ = {isa = PBXBuildFile; fileRef = 15E0A8F12411A45A00F044E3 /* SentrySession.m */; };
33042A0D29DAF79A00C60085 /* SentryExtraContextProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 33042A0C29DAF79A00C60085 /* SentryExtraContextProvider.m */; };
33042A1729DC2C4300C60085 /* SentryExtraContextProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */; };
51B15F7E2BE88A7C0026A2F2 /* URLSessionTaskExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51B15F7D2BE88A7C0026A2F2 /* URLSessionTaskExtensions.swift */; };
51B15F802BE88D510026A2F2 /* URLSessionTaskTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51B15F7F2BE88D510026A2F2 /* URLSessionTaskTests.swift */; };
51B15F7E2BE88A7C0026A2F2 /* URLSessionTaskHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51B15F7D2BE88A7C0026A2F2 /* URLSessionTaskHelper.swift */; };
51B15F802BE88D510026A2F2 /* URLSessionTaskHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51B15F7F2BE88D510026A2F2 /* URLSessionTaskHelperTests.swift */; };
620379DB2AFE1415005AC0C1 /* SentryBuildAppStartSpans.h in Headers */ = {isa = PBXBuildFile; fileRef = 620379DA2AFE1415005AC0C1 /* SentryBuildAppStartSpans.h */; };
620379DD2AFE1432005AC0C1 /* SentryBuildAppStartSpans.m in Sources */ = {isa = PBXBuildFile; fileRef = 620379DC2AFE1432005AC0C1 /* SentryBuildAppStartSpans.m */; };
621D9F2F2B9B0320003D94DE /* SentryCurrentDateProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 621D9F2E2B9B0320003D94DE /* SentryCurrentDateProvider.swift */; };
Expand Down Expand Up @@ -1035,8 +1035,8 @@
33042A0B29DAF5F400C60085 /* SentryExtraContextProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryExtraContextProvider.h; sourceTree = "<group>"; };
33042A0C29DAF79A00C60085 /* SentryExtraContextProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryExtraContextProvider.m; sourceTree = "<group>"; };
33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryExtraContextProviderTests.swift; sourceTree = "<group>"; };
51B15F7D2BE88A7C0026A2F2 /* URLSessionTaskExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionTaskExtensions.swift; sourceTree = "<group>"; };
51B15F7F2BE88D510026A2F2 /* URLSessionTaskTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionTaskTests.swift; sourceTree = "<group>"; };
51B15F7D2BE88A7C0026A2F2 /* URLSessionTaskHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionTaskHelper.swift; sourceTree = "<group>"; };
51B15F7F2BE88D510026A2F2 /* URLSessionTaskHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionTaskHelperTests.swift; sourceTree = "<group>"; };
620379DA2AFE1415005AC0C1 /* SentryBuildAppStartSpans.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryBuildAppStartSpans.h; path = include/SentryBuildAppStartSpans.h; sourceTree = "<group>"; };
620379DC2AFE1432005AC0C1 /* SentryBuildAppStartSpans.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBuildAppStartSpans.m; sourceTree = "<group>"; };
621D9F2E2B9B0320003D94DE /* SentryCurrentDateProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCurrentDateProvider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2104,7 +2104,6 @@
isa = PBXGroup;
children = (
62872B622BA1B86100A4FA7D /* NSLockTests.swift */,
51B15F7F2BE88D510026A2F2 /* URLSessionTaskTests.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -3628,6 +3627,7 @@
D8AFC0582BDA899A00118BE1 /* RedactRegionTests.swift */,
D8F67AEF2BE0D31A00C9197B /* UIImageHelperTests.swift */,
D8F67AF22BE10F7600C9197B /* UIRedactBuilderTests.swift */,
51B15F7F2BE88D510026A2F2 /* URLSessionTaskHelperTests.swift */,
);
name = Tools;
sourceTree = "<group>";
Expand Down Expand Up @@ -3659,6 +3659,7 @@
D8AFC0192BD7A20B00118BE1 /* SentryViewScreenshotProvider.swift */,
D8AFC0562BDA895400118BE1 /* UIRedactBuilder.swift */,
D8F67AED2BE0D19200C9197B /* UIImageHelper.swift */,
51B15F7D2BE88A7C0026A2F2 /* URLSessionTaskHelper.swift */,
);
path = Tools;
sourceTree = "<group>";
Expand Down Expand Up @@ -3817,7 +3818,6 @@
children = (
D8F016B52B962548007B9AFB /* StringExtensions.swift */,
62872B5E2BA1B7F300A4FA7D /* NSLock.swift */,
51B15F7D2BE88A7C0026A2F2 /* URLSessionTaskExtensions.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -4424,7 +4424,7 @@
7B3B473825D6CC7E00D01640 /* SentryNSError.m in Sources */,
D8ACE3C82762187200F5A213 /* SentryNSDataTracker.m in Sources */,
7BE3C77D2446112C00A38442 /* SentryRateLimitParser.m in Sources */,
51B15F7E2BE88A7C0026A2F2 /* URLSessionTaskExtensions.swift in Sources */,
51B15F7E2BE88A7C0026A2F2 /* URLSessionTaskHelper.swift in Sources */,
D8B088B729C9E3FF00213258 /* SentryTracerConfiguration.m in Sources */,
8ECC674A25C23A20000E2BF6 /* SentryTransactionContext.mm in Sources */,
03BCC38C27E1C01A003232C7 /* SentryTime.mm in Sources */,
Expand Down Expand Up @@ -4763,7 +4763,7 @@
63FE721420DA66EC00CDBAE8 /* SentryCrashMemory_Tests.m in Sources */,
62885DA729E946B100554F38 /* TestConncurrentModifications.swift in Sources */,
63FE720520DA66EC00CDBAE8 /* FileBasedTestCase.m in Sources */,
51B15F802BE88D510026A2F2 /* URLSessionTaskTests.swift in Sources */,
51B15F802BE88D510026A2F2 /* URLSessionTaskHelperTests.swift in Sources */,
63EED6C32237989300E02400 /* SentryOptionsTest.m in Sources */,
7BBD18B22451804C00427C76 /* SentryRetryAfterHeaderParserTests.swift in Sources */,
7BD337E424A356180050DB6E /* SentryCrashIntegrationTests.swift in Sources */,
Expand Down
6 changes: 4 additions & 2 deletions Sources/Sentry/SentryNetworkTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ - (void)captureFailedRequests:(NSURLSessionTask *)sessionTask
context[@"response"] = response;

if (self.isGraphQLOperationTrackingEnabled) {
context[@"graphql_operation_name"] = [sessionTask getGraphQLOperationName];
context[@"graphql_operation_name"] =
[URLSessionTaskHelper getGraphQLOperationNameFrom:sessionTask];
}

event.context = context;
Expand Down Expand Up @@ -506,7 +507,8 @@ - (void)addBreadcrumbForSessionTask:(NSURLSessionTask *)sessionTask
[NSHTTPURLResponse localizedStringForStatusCode:responseStatusCode];

if (self.isGraphQLOperationTrackingEnabled) {
breadcrumbData[@"graphql_operation_name"] = [sessionTask getGraphQLOperationName];
breadcrumbData[@"graphql_operation_name"] =
[URLSessionTaskHelper getGraphQLOperationNameFrom:sessionTask];
}
}

Expand Down
19 changes: 0 additions & 19 deletions Sources/Swift/Extensions/URLSessionTaskExtensions.swift

This file was deleted.

20 changes: 20 additions & 0 deletions Sources/Swift/Tools/URLSessionTaskHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

@objcMembers
class URLSessionTaskHelper: NSObject {

static func getGraphQLOperationName(from task: URLSessionTask?) -> String? {
guard let task = task else { return nil }
guard task.originalRequest?.value(forHTTPHeaderField: "Content-Type") == "application/json" else { return nil }
guard let requestBody = task.originalRequest?.httpBody else { return nil }

let requestInfo = try? JSONDecoder().decode(GraphQLRequest.self, from: requestBody)

return requestInfo?.operationName
}

}

private struct GraphQLRequest: Decodable {
let operationName: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import Nimble
@testable import Sentry
import XCTest

final class URLSessionTaskTests: XCTestCase {
final class URLSessionTaskHelperTests: XCTestCase {

func testHTTPContentTypeInvalid() {
let task = makeTask(
headers: ["Content-Type": "image/jpeg"],
body: "8J+YiQo="
)

let operationName = task.getGraphQLOperationName()
let operationName = URLSessionTaskHelper.getGraphQLOperationName(from: task)

expect(operationName) == nil
}

Expand All @@ -22,7 +22,7 @@ final class URLSessionTaskTests: XCTestCase {
body: "not json"
)

let operationName = task.getGraphQLOperationName()
let operationName = URLSessionTaskHelper.getGraphQLOperationName(from: task)

expect(operationName) == nil
}
Expand All @@ -33,7 +33,7 @@ final class URLSessionTaskTests: XCTestCase {
body: nil
)

let operationName = task.getGraphQLOperationName()
let operationName = URLSessionTaskHelper.getGraphQLOperationName(from: task)

expect(operationName) == nil
}
Expand All @@ -52,14 +52,14 @@ final class URLSessionTaskTests: XCTestCase {
"""
)

let operationName = task.getGraphQLOperationName()
let operationName = URLSessionTaskHelper.getGraphQLOperationName(from: task)

expect(operationName) == "MyOperation"
}

}

private extension URLSessionTaskTests {
private extension URLSessionTaskHelperTests {

func makeTask(headers: [String: String], body: String?) -> URLSessionTask {
var request = URLRequest(url: URL(string: "https://anything.com")!)
Expand Down

0 comments on commit 1ac58a6

Please sign in to comment.