jooby icon indicating copy to clipboard operation
jooby copied to clipboard

[FR] MVC Add method meta data to route

Open agentgt opened this issue 1 year ago • 2 comments

For 3.0 it would be very helpful if the Route had more metadata for MVC handlers. For example which class and method is the handler. Currently the MVC annotation processor does add the path as well annotation metadata but it would be helpful if it added:

  • Class name
  • Method name
  • Method parameters

The idea for are use case is in dev mode (or not) you could print that information out for diagnostic purposes. e.g. perhaps a meta tag on the page that says what view and controller rendered it. (the view is easy and doable today but the other meta data like class is not)

In other frameworks this information is available because they are using reflection and give you the the java reflect Method that handled the request. We do not want to do that but should give enough information to effectively get the Method (or MethodHandle) if one desires so. That is static strings like how annotations are added to the route should be enough I think.

I can put in a PR in 3.0 if you are interested?

agentgt avatar Sep 15 '22 14:09 agentgt

Does it add value? I don't think so. Also, if we do it for mvc, we need to same on lambda routes... which isn't as easy as MVC.

jknack avatar Sep 15 '22 14:09 jknack

Does it add value? I don't think so.

It is exceptionally valuable for us as it was easily possibly in Spring and others. Look at the bottom of this Javascript library's web page.

https://demo.unpoly.com/

It shows the name of the controller and view. That isn't easily available in Jooby.

I had to do lots of things to port our existing metrics and logging to jooby. I had to use the route path which is often not clear at all where that path's controller is.

It is valuable for:

  • Metrics
  • Diagnostics
  • AOP programming
  • Logging

Even if the parameters are not there this would still be valuable:

route.attribute("io.jooby.controller.class", Controller.class);
route.attribute("io.jooby.controller.method.name", "getSomething");

Also, if we do it for mvc, we need to same on lambda routes...

Annotations are not really supported on lambdas either. I think it's fine that isn't available on lambdas. Lambdas can wire whatever name they want manually since they have the route.

In MVC there is way no way to edit aka post process the route after it is created.

I suppose if you don't want to modify the route attributes you could provide an SPI such that you call:

public interface MVCRouteProcessor {
    public void processRoute(Route route, Class<?> controller, String methodName, String.... parameterNames, paramtersTypes... etc);

}

Then call the above from the generated code... I'm kidding the above is disgusting.

Please just add the Controller Class<?> (not the synthetic one) and method name to the attributes should be good enough.

Of course you could add setMvcMethodName and setMvcControllerClass on the Route itself but that seems pretty nasty as well.

agentgt avatar Sep 15 '22 23:09 agentgt