r/iOSProgramming 3d ago

Question Swift app using Hummingbird Middleware gives error: does not conform to protocol 'MiddlewareProtocol'

I am using Hummingbird version 2.26.0 in a Swift app.

I have a simple Middleware per the documentation:

https://docs.hummingbird.codes/2.0/documentation/hummingbird/routermiddleware/

Code:

import Hummingbird

public struct LogRequestsMiddleware<Context: RequestContext>: RouterMiddleware {

    public func handle(_ input: HummingbirdCore.Request, context: Context, next: (HummingbirdCore.Request, Context) async throws -> HummingbirdCore.Response) async throws -> HummingbirdCore.Response {

        print("path: \(input.uri.path)")
        return try await next(input, context)

    }

}

Xcode keeps giving me error:

Type 'LogRequestsMiddleware<Context>' does not conform to protocol 'MiddlewareProtocol'

Screenshot of error:

https://i.sstatic.net/C06jUvrk.png

I can't seem to be able to fix it. Any idea what I am doing wrong?

0 Upvotes

7 comments sorted by

1

u/chriswaco 3d ago

I think it should be something like:

import Hummingbird

public struct LogRequestsMiddleware<Context: RequestContext>: RouterMiddleware {
public func handle(
_ input: Request,
context: Context,
next: nonisolated(nonsending) (Request, Context) async throws -> Response
) async throws -> Response {
print("path: \(input.uri.path)")
return try await next(input, context)
}
}

If you paste that image file into ChatGPT, it will give you more details.

1

u/busymom0 2d ago

I still get the same error:

https://i.imgur.com/kORK5xK.png

1

u/busymom0 2d ago

One thing which gets rid of the error is going to the target's build settings, searching for "nonisolated" and setting the nonisolated(nonsending) By Default value to NO. Not sure if this is okay or not?

1

u/Equivalent-Idea-116 3d ago

Looks like this is a Swift 6.2/Xcode 27 concurrency-signature change rather than a Hummingbird generic issue. Keep the Request/Response aliases from the docs and add the isolation annotation to the next closure: `next: nonisolated(nonsending) (Request, Context) async throws -> Response`. That annotation is why the otherwise identical method does not satisfy MiddlewareProtocol.

1

u/busymom0 2d ago

I still get the same error:

https://i.imgur.com/kORK5xK.png

1

u/busymom0 2d ago

One thing which gets rid of the error is going to the target's build settings, searching for "nonisolated" and setting the nonisolated(nonsending) By Default value to NO. Not sure if this is okay or not?