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

Serve swagger-ui from WebJar instead of unpkg.com #193

Closed
netknight opened this issue Feb 15, 2022 · 7 comments · Fixed by #215
Closed

Serve swagger-ui from WebJar instead of unpkg.com #193

netknight opened this issue Feb 15, 2022 · 7 comments · Fixed by #215
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@netknight
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I just can't imagine how current implementation can work with OAuth authentication for REST calls.

To use OAuth there is must be oauth2-redirect.html specified on the OAuth server side and it'll redirect to this URL after successful authentication. Currently this url is located on external unpkg.com site. So there is no way to redirect there and complete authentication flow executed by swagger-ui.

Moreover many projects just don't want to serve anything from 3-rd party mirrors.

Describe the solution you'd like
The better approach would be to instal KTor WebJars extension and serve all static like JS & CSS from there .

Additionally there must be:
oauth2RedirectUrl specified, generally it is done in swagger-config.json
and clientId & clientSecretId should be provided like described here (see the bottom of the page: "initOAuth"):
https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/

Describe alternatives you've considered
Don't see any good alternatives, because AFAIK all external bundles just can't work well with external authentication.

Additional context
Having bundled Swagger-UI via WebJar and custom extended implementation for index.html & swagger-config.json there will be a better tuning possibilities for Swagger.
Moreover there is no any need to write full implementation for index.html, you can just replace dynamic part like this:
this::class.java.getResource("/META-INF/resources/webjars/swagger-ui/$swaggerVersion/index.html")?.readText()?.replaceFirst("window.ui = ui", "YOUR SCRIPT CODE that ends with replaced string")

@brizzbuzz
Copy link
Contributor

Hi... you can do this.

There is a module called kompendium-swagger-ui. The module is deprecated in favor of the new approach, and will likely be removed in a future version, but until then feel free to use it. There were a number of identified bugs with the webjar approach however.

I would recommend looking at that implementation and porting the code necessary over to your own application.

Ultimately, the Swagger and ReDoc routes that Kompendium provides are mostly for ease of use. Any custom requirements that you have would fall under the requirements for your application, and can be solved on your end, due to the decoupled nature of the library.

I'm going to close this as I don't think there is any work to do here.

@brizzbuzz
Copy link
Contributor

On second thought, the OAuth use case is compelling... I'll leave this open.

I'm not going to be working on this personally, but if you'd like to handle the necessary work to get the swagger web jar module fully functional, then I'd welcome a PR :)

@brizzbuzz brizzbuzz reopened this Feb 15, 2022
@brizzbuzz brizzbuzz added help wanted Extra attention is needed good first issue Good for newcomers labels Feb 15, 2022
@netknight
Copy link
Contributor Author

Can you add some details about:

There were a number of identified bugs with the webjar approach however.
Maybe I've already solved some of these problems in my project.

@brizzbuzz
Copy link
Contributor

This is one #128

There was another one that I'm blanking on as well.

If you can get a PR running that leverages webjars and points to the correct openapi spec on launch, then I think that should be enough to consider the webjar module functional. Then we can file any other bugs that users run into.

@netknight
Copy link
Contributor Author

This shouldn't be a problem, but I believe there must be also a problem with context path if project launched like this:
http(s)://domain/context_path/

I'll try to adopt my code but I have only implementation for OAuth authentication, there are multiple others will remain.

@netknight
Copy link
Contributor Author

I was able to launch Swagger-UI with OAuth using WebJar with kompendium and it works well for me.
But there must be created intermediate swagger-config.json resource that specifies the very basic swagger configuration, and a set of api specs as external resources (those that are generated by kompendium as openapi.json).
Also it would be great to be able to configure the path for openapi.json resource (I believe that most of developers won't like having it in the root of their projects)

This will require to update this part of code in core:

var openApiJson: Routing.(OpenApiSpec) -> Unit = { spec ->
      route("/openapi.json") {
        get {
          call.respond(HttpStatusCode.OK, spec)
        }
      }
    }

If you agree with that I'll do pull request.

By the way is there any channel for this project? I like it and would love to contribute more.

@brizzbuzz
Copy link
Contributor

But there must be created intermediate swagger-config.json resource that specifies the very basic swagger configuration

Where would this live?

Also it would be great to be able to configure the path for openapi.json resource

You can :) Would look something like this

install(Kompendium) {
    spec = Util.baseSpec
    openApiJson = { spec ->
      route("/testing/testing/123/cool-doc.json") {
        get {
          call.respond(HttpStatusCode.OK, spec)
        }
      }
    }
  }
  // Configures the routes for our API
  routing {
    // This adds ReDoc support at the `/docs` endpoint.
    // By default, it will point at the `/openapi.json` created by Kompendium
    redoc(pageTitle = "Simple API Docs", specUrl = "/testing/testing/123/cool-doc.json")

By the way is there any channel for this project? I like it and would love to contribute more.

Thanks! Any PRs are welcome :) Even just submitting these issues is super useful. There isn't any kind of channel for this project, since it's mostly just me sitting alone in my castle 😂 Right now, the best thing to do is probably open a discussion for things that aren't directly related to an issue.

If this project grows to be more people, maybe we can start a Gitter or something for it, that would be cool :) but I think we'll save that for down the line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants