Skip to content

Add leading slash to methodPath (while generating code) #161

@caio-northfleet-neon

Description

@caio-northfleet-neon

The ProtocolClient provided as input to the generated <service>.connect.swift file has a host attribute in its initialization, and the underlying code ends up calling:

let url = URL(string: path, relativeTo: URL(string: self.config.host))!

Issue is that it does not allow for extra intermediary path elements to stand between host and path. So, for example, exposing the gRPC service under https://my.company.com works fine, but under https://my.company.com/services does not. When using the solely exposed host: "https://my.company.com/services" property to overcome the issue, unfortunately the underlying URL(string, relativeTo: URL) implementation "swallows" that extra /services path in the process.
The workaround, on top of the first workaround used to add the extra path to the host attribute, is to add an extra trailing slash to everything, so host: "https://my.company.com/services/" does work, the extra /services path keeps there and calls work just fine.

I noticed the plugin code below does not include a leading slash to the generated methodPath, what would pretty much solve the issue. Note that this leading slash is indeed added to the Go generated code, but not for the Swift and Kotlin generated code using Connect plugins.

Go generated code:

const (
	MyService_MyMethod_FullMethodName = "/path.to.MySergice/MyMethod"
)

Connect plugin code generator not including the leading slash:
https://github.com/bufbuild/connect-swift/blob/61b79789c5a3179c6cd06b62f41a22e91bceea87/Plugins/ConnectSwiftPlugin/ConnectClientGenerator.swift#L191

Options here are:

  • In the plugin, add a leading slash to the generated code.
  • In ProtocolClient, expose an extra initialization attribute for extra path.
  • In ProtocolClient, change the host initialization attribute name to baseUrl and call let url = URL(baseUrl+path) and make sure there's a slash between baseUrl and path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions