Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent invocation of @Before (and similar) handlers with http method handlers #52

Closed
Itzdlg opened this issue Jan 16, 2025 · 1 comment

Comments

@Itzdlg
Copy link
Contributor

Itzdlg commented Jan 16, 2025

Actual behavior (the bug)
A before handler registered using @before and default values, alongside @endpoints, does not get invoked for all paths that a similarly-annotated method handler does.

Expected behavior
All paths that trigger a method handler should trigger the equivalently-registered @before, @BeforeMatched, @after, and @AfterMatched handlers.

To Reproduce

import io.javalin.Javalin
import io.javalin.community.routing.annotations.*
import io.javalin.http.Context
import java.util.*
import kong.unirest.Unirest.request
import org.assertj.core.api.Assertions.assertThat

fun main() {
    val app = Javalin.create { config ->
        config.router.mount(AnnotatedRouting) { routing ->
            routing.registerEndpoints(
                @Endpoints("/test/{id}")
                object {
                    @Before
                    fun before(ctx: Context, @Param id: UUID) {
                        ctx.header("before", id.toString())
                    }

                    @Get
                    fun get(ctx: Context, @Param id: UUID) {
                        ctx.header("get", id.toString())
                    }

                    @Get("/specific")
                    fun getSpecific(ctx: Context, @Param id: UUID) {
                        ctx.header("get", id.toString())
                    }
                }
            )
        }

        config.validation.register(UUID::class.java, UUID::fromString)
    }.start(8080)

    val id = UUID.randomUUID()
    val testPaths = setOf(
        "http://localhost:8080/test/$id",
        "http://localhost:8080/test/$id/",
        "http://localhost:8080/test/$id/specific",
    )

    for (path in testPaths) {
        println("Testing $path")

        val response = request("GET", path).asEmpty()
        assertThat(response.headers.getFirst("before")).isEqualTo(id.toString())
        assertThat(response.headers.getFirst("get")).isEqualTo(id.toString())
    }
}

Additional context
Discussion (Javalin discord, #help-needed)

@Itzdlg
Copy link
Contributor Author

Itzdlg commented Jan 16, 2025

Behavior resolved to pass test in To Reproduce by #50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant