papsign / Ktor-OpenAPI-Generator

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

Validation annotations aren't working after 0.2-beta.14 release #94

Closed msramosjr closed 3 years ago

msramosjr commented 3 years ago

While trying to migrate from 0.2-beta.13 to 0.2-beta.15, I found that my validators stopped working. I then tried 0.2-beta.14 and found out that the issue was happening there too. Apparently, all validators stopped working. I've tested @MinLength, @MaxLength, @Min, @Max. They are simply not being applied.

In order to show how to reproduce the problem. I changed Minimal.kt example on line 120 and added a @MinLength validator. data class StringUsable(@MinLength(10) val str: String)

When calling curl -X POST "http://localhost:8080/block" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"str\":\"Ho\"}"

In 0.2-beta.13 the response is an HTTP 500 and a stacktrace is logged on server, as expected:

com.papsign.ktor.openapigen.annotations.type.string.length.LengthConstraintViolation: Constraint violation: the length of the string should be at least 10, but it is 2
    at com.papsign.ktor.openapigen.annotations.type.string.length.LengthConstraintProcessor$LengthConstraintValidator.validate(LengthConstraintProcessor.kt:29)
    at com.papsign.ktor.openapigen.validation.ValidationHandler$transform$1.invoke(ValidationHandler.kt:37)
    at com.papsign.ktor.openapigen.validation.ValidationHandler.handle(ValidationHandler.kt:262)
    at com.papsign.ktor.openapigen.validation.ValidationHandler$12.invoke(ValidationHandler.kt:243)
    at com.papsign.ktor.openapigen.validation.ValidationHandler.handle(ValidationHandler.kt:262)
    at com.papsign.ktor.openapigen.route.OpenAPIRoute$handle$$inlined$apply$lambda$1$2$1.invokeSuspend(OpenAPIRoute.kt:74)
    at com.papsign.ktor.openapigen.route.OpenAPIRoute$handle$$inlined$apply$lambda$1$2$1.invoke(OpenAPIRoute.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:183)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:27)
    at io.ktor.routing.Routing.executeResult(Routing.kt:147)
    at io.ktor.routing.Routing.interceptor(Routing.kt:34)
    at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:99)
    at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
    at io.ktor.features.ContentNegotiation$Feature$install$1.invokeSuspend(ContentNegotiation.kt:107)
    at io.ktor.features.ContentNegotiation$Feature$install$1.invoke(ContentNegotiation.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:183)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:27)
    at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invokeSuspend(DefaultEnginePipeline.kt:120)
    at io.ktor.server.engine.DefaultEnginePipelineKt$defaultEnginePipeline$2.invoke(DefaultEnginePipeline.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:318)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:163)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:183)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:27)
    at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invokeSuspend(NettyApplicationCallHandler.kt:40)
    at io.ktor.server.netty.NettyApplicationCallHandler$handleRequest$1.invoke(NettyApplicationCallHandler.kt)
    at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55)
    at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:111)
    at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
    at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:54)
    at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
    at io.ktor.server.netty.NettyApplicationCallHandler.handleRequest(NettyApplicationCallHandler.kt:30)
    at io.ktor.server.netty.NettyApplicationCallHandler.channelRead(NettyApplicationCallHandler.kt:24)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377)
    at io.netty.channel.AbstractChannelHandlerContext.access$600(AbstractChannelHandlerContext.java:59)
    at io.netty.channel.AbstractChannelHandlerContext$7.run(AbstractChannelHandlerContext.java:368)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)

But in 0.2-beta.14 (and 0.2-beta.15), @MinLength annotation is ignored and server response is 200 with no stacktrace on server and the following response body:

{"str":"Ho"}
msramosjr commented 3 years ago

@Wicpar do you have any thoughts about this?

darkxanter commented 3 years ago

The problem appeared after adding AnnotationTarget.VALUE_PARAMETER to annotations and for some reason these annotations became missing in the annotations list for parameters.

Wicpar commented 3 years ago

I remember some issue i had with something like that when i originally implemented it. It has to do with the way annotations are bound to diferent parts depending on what kind they are.

Wicpar commented 3 years ago

Fixed it, pushing update

msramosjr commented 3 years ago

Thank you guys! @Wicpar could you please release another version?

Wicpar commented 3 years ago

@msramosjr done

msramosjr commented 3 years ago

@Wicpar thanks!