swift-openapi-generator icon indicating copy to clipboard operation
swift-openapi-generator copied to clipboard

Error when implementing a middleware with SWIFT_APPROACHABLE_CONCURRENCY

Open joaocolaco opened this issue 8 months ago • 2 comments

Question

Since Xcode 26 beta 5 i cannot comply with the ClientMiddleware protocol of swift-openapi-runtime. The error that I got is Candidate has non-matching type '(HTTPRequest, HTTPBody?, URL, String, nonisolated(nonsending) @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)) async throws -> (HTTPResponse, HTTPBody?)' for a structure that seems (and was) to be well "made":

public struct ApiKeyMiddleware: ClientMiddleware {

   //The API key to login
    private let apiKey: String
    
    
    public init(apiKey: String) { self.apiKey = apiKey }

    public func intercept(
        _ request: HTTPRequest,
        body: HTTPBody?,
        baseURL: URL,
        operationID: String,
        next:  @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (
            HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?
        )
    ) async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?) {
        var request = request
        request.headerFields[.init("x-goog-api-key")!] = self.apiKey
        return try await next(request, body, baseURL)
    }
}

Is this an Xcode/Swift bug or is there something that I'm missing?

Thanks

joaocolaco avatar Aug 07 '25 17:08 joaocolaco

running into the exact same problem. There doesn't seem to be a way to prevent Xcode from automatically adding nonisolated(nonsending) to the implementation as far as I can tell.

BrentMifsud avatar Aug 07 '25 21:08 BrentMifsud

Ok, seems to be happening only with the SWIFT_APPROACHABLE_CONCURRENCY=YES build setting. While we investigate this, one workaround is to create a dedicated target for your middleware/generated code, and ensure the build setting is turned off in that target only.

Edit: An easier workaround is to add @concurrent to the next parameter, before @Sendable, making it:

next: @concurrent @Sendable (HTTPRequest, HTTPBody?, URL) ...

czechboy0 avatar Aug 08 '25 07:08 czechboy0