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

Provide a way to define custom metadata for each endpoint

dzikoysk opened this issue · comments

About

Currently, there's no option to attach any custom data to an endpoint. It's a bit limiting for plugins that could benefit of such a feature, such as e.g. OpenAPI:

As suggested on Discord, we can also move current endpoint roles here.

API preview

For now, we'd add a new field to Endpoint class & new addHttpHandler signature to the default routing that provides a possibility to specify list of custom objects:

router.addHttpHandler(GET, "/monke", handler, openApiDoc, roles, [...])

I think we should contain it fully to endpoint, so for example:

router.addEndpoint {
    method = HttpHandler.GET
    path = "/"
    handler = handler
    metaData = setOf(obj1, obj2, obj3)
}

For roles, this could be:

router.addEndpoint {
    method = HttpHandler.GET
    path = "/"
    handler = handler
    metaData = setOf(roles) // we key on class, so `roles` can't be a `Set<Role>`, but something like `RoleData`
}
routing
  .get(ctx -> ctx.result("abc"))
  .put(ctx -> ctx.result("abc"))
  .addEndpoint(GET, "/")
    .metadata(new Roles(ANONYMOUS))
    .metadata(
      OpenApiDocumentation.create()
        .summary('Home')
        .description('Home page')
    )
    .handler(ctx -> ctx.result("Hello, world!"))
  .addEndpoint(GET, "/api/v1/health")
    .metadata(new Roles(ADMIN))
    .metadata(
      OpenApiDocumentation.create()
        .summary('Health check')
        .description('Check the health of the app')
    )
    .handler(ctx -> ctx.result("abc"))
  .before(ctx -> ctx.result("abc"))
  .after(ctx -> ctx.result("abc"))
      
routing
  .get(ctx -> ctx.result("abc"))
  .put(ctx -> ctx.result("abc"))
  .addEndpoint(endpoint -> {
    endpoint.method = HttpHandler.GET;
    endpoint.path = "/";
    endpoint.handler = ctx -> ctx.result("abc");
    endpoint.metadata = Set.of(
      new Roles(ADMIN),
      OpenApiDocumentation.create()
        .summary('Home')
        .description('Home page')
    );
  })
  .addEndpoint(endpoint -> {
    endpoint.method = HttpHandler.GET;
    endpoint.path = "/";
    endpoint.handler = ctx -> it.result("abc");
    endpoint.metadata = Set.of(
      new Roles(ADMIN),
      OpenApiDocumentation.create()
        .summary('Home')
        .description('Home page')
    )
  })
  .before(ctx -> ctx.result("abc"))
  .after(ctx -> ctx.result("abc"))