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.37k stars 110 forks source link

URLSession and AHC transports double-encode path parameters #251

Closed sliemeobn closed 1 year ago

sliemeobn commented 1 year ago

When using String path parameters, the generated client (correctly) URL-encodes the values to be included in the path. However, the server handler just passes the the "raw" string value in the input.path structure.

Eg: using the generated client:

        let response = try await client.createRole(.init(
            path: .init(roleId: "A role"),
            ...
        ))

server code

    func createRole(_ input: Operations.createRole.Input) async throws -> 
      //input.path.roleId is "A%20role"
}

I would expect this to be symmetric and arrive decoded in the handler.

czechboy0 commented 1 year ago

Thanks for filing, yes that looks like a bug.

czechboy0 commented 1 year ago

I tried to reproduce in a unit test, but seems to be behaving correctly: https://github.com/apple/swift-openapi-runtime/pull/49

Possibly this could be caused by the client using 0.2 and server 0.1? I could see that leaving the client as escaped, but not being unescaped on the server.

sliemeobn commented 1 year ago

found it, it is actually a bug in the URLSessionTransport's initializer of URLRequest

The URLComponents.url adds another round of percent encoding, basically sending an double-encoded path parameter to the server.

czechboy0 commented 1 year ago

Ok both the URLSession and AHC transports have been released with this fix in 0.2.2.

Thank you @sliemeobn for doing the heavy lifting on this one! 🙏