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:
- 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]
? - 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 aPOST /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:
- Created 6 years ago
- Reactions:2
- Comments:10 (6 by maintainers)
Top GitHub Comments
I will take a look at generated code later, but for
authentication
we plan to change it like this:install(Authentication) { … }
for configurationauthenticate { … }
builder for introducing scopeSomething like this:
Do you think it will work for you?
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.