javalin / javalin

A simple and modern Java and Kotlin web framework

Home Page:https://javalin.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Confusing `Ws{{Action}}Context.matchedPath()` functionality

LoonyRules opened this issue · comments

Describe your issue
When I was updating my Request Logger to log WebSockets, I noticed that the Ws{{Action}}Context.matchedPath() was returning * instead of /ws that I would've expected.

For HTTP requests, via the Context class, .matchedPath() would return the endpoint path that you registered via:

config.get("/resource/{resource_id}/`, ctx -> { ... });

when being called from inside of the RequestLogger, meaning, that the Context.matchedPath() result is /resource/{resource_id}/, not *.

The Request Logger is always going to match on ALL requests, it's kind of its point, of which is how it's getting the * value in the first place. So I'm more confused as to why the implementation for WebSockets ever uses * as the matchedPath as it's never useful to the end user in the first place.

To @tipsy, this feels more like an API design flaw in the naming conventions and is something that is up for discussion in Javalin 7 over at #2091, but to me as a user, it sounds more like an implementation flaw that wasn't caught or questioned early on simply due to the fact that not a lot of Javalin users use the WebSockets functionality in the first place. How this issue is determined (api flaw or implementation flaw) isn't all that important, just outlining the confusion is important here.

What I really wanted for my request logging was:

public void onWsConnect(final @NotNull WsConnectContext ctx) {
  String matchedPath = ctx.getUpgradeCtx$javalin().matchedPath();
}

I have raised a separate issue for making this getter method nicer to read for Java developers.

What would be nice, would be to have a Ws{{Action}}Context.path() method, but for metric logging (eg: Prometheus) I need the matched path, so I'm still reliant on the upgrade context for my usage. You typically want the full path .path() for console logging, but then the matchedPath for timeseries.