question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support swagger generation for ktor app

See original GitHub issue

[question] Code review of my initial ktor generator at swagger-codegen?

Sorry if this isn’t the correct place to request community feedback. Please redirect me if there’s a better place to ask… I’d usually ask in a community’s gitter chat room, but I can’t find one for ktor.

I’m a core team member on Swagger Codegen. If you’re not familiar with the project, it converts OpenAPI 2.0 (previously known as Swagger 2.0) specifications into different types of projects: clients, servers, documentation, and scripts.

I’ve just implemented the start of a ktor server generator for swagger codegen. It doesn’t support swagger spec generation 100%, but the output and examples would provide a quick stubbed server which can easily be modified.

I was wondering if I could get some feedback on some of the patterns I’ve used in the generated code? The pull request is at swagger-api/swagger-codegen#7412, and generated output is under the samples/ directory.

There were two questions I have currently:

  1. Is there a way in the locations api to specify query string delimiters to split values into an array? OpenAPI 2.0 supports multi (which I see ktor supports, but I haven’t added support for in the linked PR). But OpenAPI 2.0 also supports different delimiters (comma, space, pipe, and tab)… is there a way to automatically split these into an Array[T]?
  2. Because the code is generated at the api endpoint level, and restructuring the swagger specification to group by path isn’t entirely trivial (the spec supports different authentication per endpoint), I’ve had to code a workaround for authentication. Specifically, if I have a GET /pet and a POST /pet where both have authentication, I’ve created a route registration like this for both:
    route("/pet") {
        put {
            val principal = call.authentication.principal<OAuthAccessTokenResponse>()
            
            if (principal == null) {
                call.respond(HttpStatusCode.Unauthorized)
            } else {
                call.respond(HttpStatusCode.NotImplemented)
            }
        }
    }
    .apply {
        try {
            authentication {
                oauth(client, ApplicationExecutors.asCoroutineDispatcher(), { ApplicationAuthProviders["petstore_auth"] }, {
                    // TODO: define a callback url here.
                    "/"
                })
            }
        } catch(e: io.ktor.application.DuplicateApplicationFeatureException){
            application.environment.log.warn("authentication block for '/pet' is duplicated in code. " +
            "Generated endpoints may need to be merged under a 'route' entry.")
        }
    }

I’ve wrap the authentication invocation in a try/catch because GET /pet has already registered the Authentication feature. Is there a way to key these separately in the event that our users may have, for example, different API keys for read vs. write APIs?

Thanks!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
orangycommented, Jan 22, 2018

I will take a look at generated code later, but for authentication we plan to change it like this:

  • install(Authentication) { … } for configuration
  • authenticate { … } builder for introducing scope

Something like this:

authenticate {
   post("foo") {}
   put("bar") {}
   get("secret") {}
}

get("public") {}

Do you think it will work for you?

0reactions
oleg-larshincommented, Aug 10, 2020

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Swagger UI - Ktor
Ktor allows you to generate and serve Swagger UI for your project based on the existing OpenAPI specification. With Swagger UI, you can ......
Read more >
SMILEY4/ktor-swagger-ui: Kotlin Ktor plugin to ... - GitHub
This library provides a Ktor plugin to document routes, generate an OpenApi Specification and serve a Swagger UI. It is meant to be...
Read more >
OpenAPI Generation with Ktor. Introduction | Nerd For Tech
If you already have an API and only need the documentation, use one of the various Swagger tools (like Swagger Inspector, Swagger Editor...
Read more >
Intergate Swagger UI Hosting as Ktor Feature : KTOR-774
It would be really nice if Ktor could support hosting a Swagger UI that is ... newest version of Ktor and also use...
Read more >
Swagger/OpenApi Documentation in ktor - Reddit
hi, guys, someone knows how create a api documentation of a ktor application using openapi-generator? Thanks for you help.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found