Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isolate the instantiation of an annotation

I have a huge @OpenApi annotation (basically it's documentation of a Javalin/Kotlin endpoint) which occupies a lot of lines:

@OpenApi(
   summary = "",
   description = "Lists all customers",
   path = "customers",
   queryParams =
   // ...........
   // ...........
   // etc
)
override fun handle(context: Context) {
   // body of the REST handler
}

I have to scroll a lot to see the actual handler. Hence, I'd like to isolate it somehow like:

@GetCustomersDoc
override fun handle(context: Context) {
   // body of the REST handler
}

I'm OK with other solutions that make the documentation go elsewhere.

This would make the code cleaner and the docs segregated.

like image 569
Luís Soares Avatar asked Sep 12 '19 22:09

Luís Soares


1 Answers

You could define separate Annotations:

annotation class MyOwnApi(
    val openApi: OpenApi = OpenApi(
          summary = "",
          description = "Lists all customers",
          path = "customers",
          queryParams =
          // ...........
          // ...........
          // etc
        )
)

annotation class UserOpenApi(
        val openApi: OpenApi = OpenApi(
              summary = "Something",
              description = "Lists all users",
              // ...........
              // ...........
              // etc
            )
    )

Pros:

  • Code separation
  • Reusable annontation classes
  • You could even make a builder class and test it

Cons:

  • Confusing
  • Annotations can't inherit, extend classes or implement Interfaces
  • May not be possible to implement or require complex code changes, if the classes/objects are checked for @OpenApi directly. In this case, you would need another reflective search to pull the annotation from a field!
like image 59
Neo Avatar answered Oct 15 '22 18:10

Neo