apple / swift-openapi-generator

Generate Swift client and server code from an OpenAPI document.
https://swiftpackageindex.com/apple/swift-openapi-generator/documentation
Apache License 2.0
1.21k stars 87 forks source link

registerHandlers should throw an error if serverURL doesn't match documentation #528

Closed ladiesman218 closed 3 months ago

ladiesman218 commented 3 months ago

Motivation

In

@main struct HelloWorldVaporServer {
    static func main() async throws {
        let app = Vapor.Application()

        let transport = VaporTransport(routesBuilder: app)
        let handler = Handler()
        try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!)
        try await app.execute()
    }
}

If I made a typo in serverURL parameter, I can use the mis-typed url instead of the one defined in openapi.yaml file. Is it better to throw an error if this happens? Say for example in my openapi.yaml file, I have

servers:
  - url: https://example.com/api

But in the above function I typed /aoi, I could still use 127.0.0.1:8080/aoi to access the endpoint instead of getting an error.

Proposed solution

Throw error if this happens

Alternatives considered

No response

Additional information

No response

czechboy0 commented 3 months ago

Hi @ladiesman218,

if you have a server defined in the OpenAPI document, you can use it from Swift code as follows:

try handler.registerHandlers(on: transport, serverURL: Servers.server1())

Only pass in a manually constructed URL if you need to provide a server URL not defined in the OpenAPI document.

Does that help?

ladiesman218 commented 3 months ago

Definitely. Thx I used this: https://github.com/apple/swift-openapi-generator/blob/main/Examples/hello-world-vapor-server-example/Sources/HelloWorldVaporServer/HelloWorldVaporServer.swift 😂

Will close this