Vapor testing can't find Swift OpenAPI Generator created endpoints

Hi everyone,

I'm playing around with the Swift OpenAPI Generator and Vapor to see if it can be an alternative to our TypeScript based tech stack.

So far I've created a simple toy project which uses an OpenAPI spec. I can get this one running and call the endpoints as per the expected specs. :white_check_mark:

Now I'm looking at testing the API. I followed the instructions on the Vapor forum: Vapor: Advanced → Testing

I have created the withApp method:

private func withApp(_ test: (Application) async throws -> ()) async throws {
    let app = try await Application.make(.testing)
    do {
        try await configure(app)
        try await test(app)
    }
    catch {
        try await app.asyncShutdown()
        throw error
    }
    try await app.asyncShutdown()
}

and a simple test:

        @Test("then it should return a status 200 (OK) message") func thenItShouldReturnAStatus20OKMessage() async throws {
            try await withApp { app in
                try await app.testing().test(.GET, "v1/rovers") { res async in
                    #expect(res.status == .ok)
                }
            }
        }

This test consistently fails with a 404 (not found) error. Though the logs shows indeed the /v1/rovers endpoint is called.

However, when I run the server and call the endpoint directly using curl http://localhost:8080/v1/rovers/ I do get the expected result back:

{
  "links" : [

  ],
  "rovers" : [
    {
      "id" : "BFE53D64-0DD9-4592-8936-BF52034D9C0F",
      "links" : [

      ],
      "pose" : {
        "heading" : "N",
        "x" : 1,
        "y" : 2
      }
    },
...

Am I missing something here? And/or am I supposed to test OpenAPI Generator based endpoints in a different way?

Where do you register the handlers? Is that done in your configure?

Hi Tim,

Yes, it's a very simple configure method:

// main.swift
let app = try await Vapor.Application.make()

func configure(_ app: Vapor.Application) async throws {
    let transport = VaporTransport(routesBuilder: app)
    let handler = MarsRoverAPIImplementation()
    
    try handler.registerHandlers(on: transport)
}

try await configure(app)
try await app.execute()

That looks like it should work fine - is there a project available to try and reproduce it?

Thank you Tim,

I'll try and make a stripped out version of the project tonight.

KR Maarten

OK, here's a sample project (hope this way of sharing works): iCloud Drive - Apple iCloud

Also, I notice now it no longer builds the tests (regular build is fine) and shows these errors (which may or may not be related):

I haven't checked the project but (regarding the errors in the image) off-hand I'd guess there is either a missing import or a missing dependency declaration in Package.swift. Perhaps "VaporTesting" or "Testing"?