papsign / Ktor-OpenAPI-Generator

Ktor OpenAPI/Swagger 3 Generator
Apache License 2.0
241 stars 42 forks source link

@Path not setting proper route #35

Closed mintu19 closed 4 years ago

mintu19 commented 4 years ago

I am using @Path like

@Path("/{deviceId}")
data class MyParams(
    @PathParam("Device ID") val deviceId: String
)

When i use it with route, proper openapi definitions are created but ktor route is not working. I listed routes and checked... It doesn't show as parameterized route and also none of post method works...

route("images") {
                post<MyParams, PResponse, PRequest>(
                    info("Add to server", "Add link")
           ) { pr, req ->
                    respond(pr(true))
          }

route listing on ktor show

19-04-2020 17:47:04.633 [main] INFO  Application - route: /(method:GET)
19-04-2020 17:47:04.633 [main] INFO  Application - route: /(authenticate "default")/users/agent/(method:GET)
19-04-2020 17:47:04.633 [main] INFO  Application - route: /(authenticate "default")/images/(method:POST)

Sending port request to images/1 or images/ routes produces error 404

19-04-2020 18:00:01.192 [nioEventLoopGroup-4-3] INFO  Application - Unhandled: POST - /images/
19-04-2020 18:00:32.333 [nioEventLoopGroup-4-3] INFO  Application - Unhandled: POST - /images/1

Why route is shown unhandled and it is generated as above without parameters

Wicpar commented 4 years ago

On non authenticated routes it does work on the experimental branch.

I find it odd that you declare a post on an authenticated route without giving it the principal type, are you authenticating the route, and if yes why is it not the OpenAPIAuthenticatedRoute<A> context that requires you to give the authentication type parameter ? fun <reified P : Any, reified R : Any, reified B : Any, A> OpenAPIAuthenticatedRoute<A>.post

mintu19 commented 4 years ago

yes i am using authenticated route. I missed writing proper signature while posting here. It is like this:

post<MyParams, PResponse, PRequest, IdPrincipal>(

what is the issue and why route is not properly generated?

Wicpar commented 4 years ago

What version are you using ? 0.1 or 0.2 experimental ?

mintu19 commented 4 years ago

i am using implementation 'com.github.papsign:Ktor-OpenAPI-Generator:-SNAPSHOT'

Wicpar commented 4 years ago

The snapshots might be unstable, can you try the tagged versions to see if there is one that works ?

Le dim. 19 avr. 2020 à 16:32, Akshit Bhandari notifications@github.com a écrit :

i am using implementation 'com.github.papsign:Ktor-OpenAPI-Generator:-SNAPSHOT'

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/papsign/Ktor-OpenAPI-Generator/issues/35#issuecomment-616148542, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJHT75OZRJNS5HYRB7FVPDRNMDPTANCNFSM4MLYXD4Q .

mintu19 commented 4 years ago

using implementation 'com.github.papsign:Ktor-OpenAPI-Generator:0.2-beta.2-experimental'

getting:

19-04-2020 21:0
5:12.678 [main] INFO  Application - route: /openapi.json/(method:GET)
19-04-2020 21:05:12.678 [main] INFO  Application - route: /(method:GET)
19-04-2020 21:05:12.679 [main] INFO  Application - route: /(authenticate "default")/com.papsign.ktor.openapigen.route.util.ConstantRouteSelector@646c0a67/users/agent/(method:GET)
19-04-2020 21:05:12.679 [main] INFO  Application - route: /(authenticate "default")/com.papsign.ktor.openapigen.route.util.ConstantRouteSelector@3804a9a8/images/(method:POST)

it doesn't look good

Wicpar commented 4 years ago

What is the import you use for the path annotation, is it the one from the library ?

mintu19 commented 4 years ago

yes

import com.papsign.ktor.openapigen.annotations.Path

mintu19 commented 4 years ago

I have verified it is not working without auth also

@Path("/my/{id}")
data class TestParam(@PathParam("id") val id: String)
apiRouting {
        head<TestParam, Boolean>(
            info("id")
        ) {
            respond(true)
        }
    }

This will generate 19-04-2020 23:52:15.117 [main] INFO Application - route: /(method:HEAD) instead of route /my/{id}

if i use get instead of head no new route is generated (as get root is already there in normal ktor routes i created).

Path annotation is not working as expected

Wicpar commented 4 years ago

You can debug the function package com.papsign.ktor.openapigen.route.preHandle at this line:

    val path = P::class.findAnnotation<Path>()

this is common code, it should be called by both normal and authenticated routes, it should not be null, and should contain your path string. This is the only relevant code to this issue, if there is a problem it can be seen here...

It could be:

In case it cannot be found you can specify the params in the route directly instead of the annotation:

route("{param}").post()

If you have a code snippet (with all the imports) i can clone that recreates the issue it would be great so i can debug it.

mintu19 commented 4 years ago

i am not able to debug this via intellij idea. IDE hangs on debug that code... I will try to make a sample for you to debug...

mintu19 commented 4 years ago

Using this will generate doc properly?

route("{param}").post()

and how to use in call inside post method to get parameter as call is not available

Wicpar commented 4 years ago

Yes, the path annotation is just a convenience.

mintu19 commented 4 years ago

Resolved the issue while creating example project. Route was created properly. It was not displayed in log because ktor have no way to list routes so routes were logged based on selector. I printed all routes and then my routes were visible. Given routes may or may not have handler but there is no way to check as handlers are internal field. So have to list all routes.

Also the api was not called because content Type header was missing while sending. It gets added in postman auto but dont know why it was't. Tracing routes helped with that.

Anyways it works now.Thanks