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

SwaggerHttp4s does not work with relative paths, only absolutes #319

Closed
kdrakon opened this issue Nov 20, 2019 · 3 comments
Closed

SwaggerHttp4s does not work with relative paths, only absolutes #319

kdrakon opened this issue Nov 20, 2019 · 3 comments

Comments

@kdrakon
Copy link
Contributor

kdrakon commented Nov 20, 2019

As per the documented use:

  * Usage: add `new SwaggerHttp4s(yaml).routes[F]` to your http4s router. For example:
  * `Router("/docs" -> new SwaggerHttp4s(yaml).routes[IO])`.
  *
  * When using a custom `contextPath` is used, replace `/docs` with that value.

The problem I've encountered with this is when you go to the redirect path, /docs in this case, the url query parameter is an absolute path to /docs/docs.yaml:

Uri.fromString(s"/$contextPath/index.html?url=/$contextPath/$yamlName")

At my work, we have our application deployed in a kubernetes infrastructure where the REST-based microservices are put under URI namespaces. For example, locally, I could reach my documentation at http://localhost:8000/docs, but in production it would be https://example.com/my-service/docs. The fixed and absolute path above would mean it would try to load the OpenAPI yaml at https://example.com/docs/docs.yaml, resulting in a 404.

In #318, I've made a suggested change so that the contextPath (normally defaulted to docs) is made relative to the http4s Root, thus addressing my above illustrated problem. Furthermore, it allows for the following routing:

Router("/v1" -> (new MyServiceRoutes[IO] <+> new SwaggerHttp4s(yaml).routes[IO]))

So that your docs can sit at http://localhost:8000/v1/docs. This however means, if you wanted the docs at the root, you'd have to now instead do the following:

Router("/" -> new SwaggerHttp4s(yaml).routes[IO]) // routes to /docs

IMO, this makes more sense, especially since it gives more meaning to the parameter name contextPath.

@kdrakon
Copy link
Contributor Author

kdrakon commented Nov 20, 2019

Pull request ready #318

@kdrakon
Copy link
Contributor Author

kdrakon commented Nov 20, 2019

Extra note:
To further my point of the issue, this is how I put my docs under a different relative path in version 0.12.2:

Router(
  "/v1" -> new MyServiceRoutes[IO]
  "/v1/doc" -> new SwaggerHttp4s(yaml, contextPath = "v1/doc").routes[IO])
)

Having to correctly state the path like this twice is a code smell.

@adamw
Copy link
Member

adamw commented Nov 20, 2019

Released in 0.12.3

@adamw adamw closed this as completed Nov 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants