I’m trying out type safe routing and I have diffic...
# ktor
j
I’m trying out type safe routing and I have difficulties with this case :
Copy code
@Serializable
@Resource("/search")
class Search(
    val query: String? = null,
    val offset: Offset? = null,
    val limit: Limit? = null,
) : ApiResource() {
    @Serializable
    @Resource("type1")
    class Type1(
        val parent: Search = Search(),
        val query: String,
        val offset: Offset,
        val limit: Limit,
    )

    @Serializable
    @Resource("type2")
    class Type2(
        val parent: Search = Search(),
        val query: String,
        val offset: Offset,
        val limit: Limit,
    )

...

get<ApiResource.Search> { request ->
    call.respond("search works. Query: ${request.query}") // This works
}

get<ApiResource.Search.Type1> { request ->
    call.respond("search type1 works. Query: ${request.query}") // this gives me a 404 error
}
What am I supposed to change to make it work? The parameter
query
is used to search by name in a database.
/query
will search for both
type1
and
type2
while
search/type1
should return only
type1
results
a
Since
query
,
offset
and
limit
are required and you don’t list those parameters in the
Resource
annotation (
@Resource("type1")
) you have to make a request with corresponding query parameters, e.g.
/search/type1?query=123&offset=1&limit=10
, otherwise that route won’t be matched.
j
of course 🤦 Is there a better way to manage
query
,
offset
and
limit
without repeating it for each child?
153 Views