Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated spec servers url has trailing slash - this causes problems with tooling #1716

Open
2 of 4 tasks
amerrit2 opened this issue Nov 9, 2024 · 3 comments
Open
2 of 4 tasks
Labels
good first issue This issue could be an easy PR for those looking to help contribute help wanted

Comments

@amerrit2
Copy link

amerrit2 commented Nov 9, 2024

I discovered this issue while using swift-openapi-generator with the generated openapi json document. The resulting swift code fails to work since an extra slash is added into the path. This is because servers[idx].url has a trailing slash.

As far as I could find the openapi spec doesn't explicitly disallow this, however I found several resources/tools that disallow it for the exact reason that it doesn't work well with other tooling.

E.g.:

  • Redocly openapi lint rule
  • Vacuum openapi linter
    • Specifically says "the url value cannot end with a trailing slash. The addition of a slash will create invalid URI’s when consumed by tools."
  • Some style guide blog
    • "Must not have a trailing slash
      /users and /users/ are considered to be separate paths but it is bad practice for them to differ based only on a trailing slash. This can cause confusion among users of your API. It is usually preferred to not have a trailing slash."

Seems like there's a lot of agreement that there should not be a trailing slash.

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

No trailing slash:

"servers": [
		{
			"url": "http://localhost:3000"
		}
	]

Current Behavior

Has a trailing slash:

"servers": [
		{
			"url": "http://localhost:3000/"
		}
	]

Possible Solution

Remove the trailing slash

Steps to Reproduce

Not really necessary, if you include spec.host in the tsoa.json then you'll get the servers urls with trailing slashes.

Context (Environment)

Version of the library: 6.5.1
Version of NodeJS: 23.1.0

  • Confirm you were using yarn not npm: [x]

Detailed Description

There's not really any more detail I can offer.

Breaking change?

Since this changes the output it might be reasonable to include an opt-out mechanism. This would just be a `includeTrailingSlashForHosts config property.

Copy link

github-actions bot commented Nov 9, 2024

Hello there amerrit2 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

@amerrit2
Copy link
Author

amerrit2 commented Nov 9, 2024

Looks like this needs to be changed:

// specGenerator3.ts

// ...
private buildServers() {
    const basePath = normalisePath(this.config.basePath as string, '/', undefined, false);
    const scheme = this.config.schemes ? this.config.schemes[0] : 'https';
    const url = this.config.host ? `${scheme}://${this.config.host}${basePath}` : basePath;
    return [
      {
        url,
      } as Swagger.Server,
    ];
  }

to

// specGenerator3.ts

// ...
private buildServers() {
    const basePath = normalisePath(this.config.basePath as string, undefined, undefined, false);
    const scheme = this.config.schemes ? this.config.schemes[0] : 'https';
    const url = this.config.host ? `${scheme}://${this.config.host}${basePath}` : basePath;
    return [
      {
        url,
      } as Swagger.Server,
    ];
  }

(Disclaimer: I haven't tested this yet)

@amerrit2
Copy link
Author

Did a quick local test, and yeah that works.

@WoH WoH added help wanted good first issue This issue could be an easy PR for those looking to help contribute labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This issue could be an easy PR for those looking to help contribute help wanted
Projects
None yet
Development

No branches or pull requests

2 participants