v79
05/30/2024, 3:44 PMschemas
in OpenAPI parlance. The user of the library would annotate a data class with @APISchema
and my library would generate the appropriate yaml.
I've tried this before, using KSP, but it was very hacky and it wasn't a separate library.
So I am not sure what is the best way - a kapt plugin to generate the yaml, or ksp, or maybe just do runtime reflection? (I'm running this in AWS lambda and reflection seems to be a bad idea).
Some sample code in thread...v79
05/30/2024, 3:48 PMoverride val router: Router = lambdaRouter {
get("/warm") { _: Request<Unit> ->
println("NEW API: Ping received; warming"); Response.ok(
"Warmed"
)
}.supplies(MimeType.plainText)
put("/wibble") { request: Request<Wibble> -> { println("Wibble: ${request.body}") }; Response.ok(
"Wibble"
) }.expects(MimeType.json)
group("/project") {
auth(cognitoJWTAuthorizer) {
get("/load/{projectKey}", projectController::getProject)
}
}
}
v79
05/30/2024, 3:48 PMWibble
would be a data class with my APISchema
annotation.v79
05/30/2024, 3:53 PMget("/openAPI") { request<Unit> -> Response.ok(router.openAPI()) }.supplies(MimeType.YAML)
which would, at run time, provide a complete OpenAPI Yaml specification for all the routes defined in the lambdaRouter {...}
block.v79
05/30/2024, 4:00 PMschemas:
Wibble:
type: object
properties:
id:
type: integer
name:
type: string
Jiaxiang
05/30/2024, 5:26 PMWibble
inside a lambda body? If that’s the case, KSP won’t work, I am afraid kapt won’t work either.v79
05/30/2024, 5:51 PMJiaxiang
05/30/2024, 5:52 PMv79
05/30/2024, 5:52 PMyigit
05/30/2024, 9:11 PMyigit
05/30/2024, 9:12 PMv79
05/31/2024, 5:31 PMput("/wibble") { request: Request<Wibble> -> Response.OK("Wibble created") }
And I've accepted that. However, most of the time I won't be using trailing lambdas to declare my API routes - instead, I'd be passing a named function:
put("/wibble", wibbleController::createNewWibble)
That function would be declared separately, something like this:
class WibbleController {
@OpenAPIRoute
fun createNewWibble(request: Request<Wibble>): Response<String> {
// create a new Wibble in the database...
return Response("Wibble created")
}
}
And with that OpenAPIRoute
annotation on that function body, KSP can get me the information I need about the Request<Wibble>
and the Response<String>
.
tl;dr - I can get KSP to do what I want for the more realistic scenario, but it cannot and well never help with the trailing lambda scenario. I think I can live with that.yigit
06/01/2024, 10:13 AM@Get("/driver/profile/{id}")
suspend fun searchDrivers(
@Doc("The customer id of the driver")
id: Int
): IRDriverProfile? {
return repo.getDriverProfile(id)
}
@Doc
is a custom annotation to adds documentation to the open-api schema. the response types all have the same annotation in their properties.
to generate the schema, i simply reflect through all methods and traverse them.
and the same code also adds them as routes to ktorhfhbd
06/01/2024, 1:07 PM