Sean Abbott
08/12/2021, 6:16 PMobject ExampleContractRoute {
// this specifies the route contract, including examples of the input and output body objects - they will
// get exploded into JSON schema in the OpenAPI docs
private val spec = "/echo" meta {
summary = "echoes the name and message sent to it"
receiving(nameAndMessageLens to NameAndMessage("jim", "hello!"))
returning(OK, nameAndMessageLens to NameAndMessage("jim", "hello!"))
} bindContract POST
// note that because we don't have any dynamic parameters, we can use a HttpHandler instance instead of a function
private val echo: HttpHandler = { request: Request ->
val received: NameAndMessage = nameAndMessageLens(request)
Response(OK).with(nameAndMessageLens of received)
}
operator fun invoke(): ContractRoute = spec to echo
}
which is being called inside contract in my app function
"/" bind contract {
renderer = OpenApi3(ApiInfo("Test API", "v1.0"), Jackson)
// Return Swagger API definition under /swagger.json
descriptionPath = "/swagger.json"
// Add contract routes
routes += ExampleContractRoute()
routes += HealthRoute()
},
and I'm trying to add a nested route to the ExampleContract route, like /echo/excited
just as a toy example What would be idiomatic way to do that?Sean Abbott
08/12/2021, 6:18 PMSean Abbott
08/12/2021, 6:26 PMdave
08/12/2021, 6:56 PMSean Abbott
08/12/2021, 7:04 PMGET /account/{id}
POST /account/{id}/task/{taskType}
And wanted both to be reflect in the swagger docs, could I just do a ContractRoute function for both of those?
like
fun acountRoute(): ContractRoute {
val spec = "/account" / <http://Path.int|Path.int>().of("accountId") meta {
summary = "retuns accoutn details"
} bindContract GET
return spec to ::greet
}
fun taskRoute(): ContractRote {
val spec = "/account" / <http://Path.int|Path.int>().of("accountId") / task / Path.string().of("taskType") meta {
...
}
// (elsewhere)
"/api" bind contract {
...
routes += accountRoute()
route += taskRoute()
}
Sean Abbott
08/12/2021, 7:04 PMdave
08/12/2021, 7:05 PMdave
08/12/2021, 7:06 PMSean Abbott
08/12/2021, 7:07 PMSean Abbott
08/12/2021, 7:08 PMdave
08/12/2021, 7:08 PMSean Abbott
08/12/2021, 7:09 PMSean Abbott
08/12/2021, 7:10 PMdave
08/12/2021, 7:10 PM