develop with

How nest annotations with kotlin

Nested annotations in Kotlin are a bit tricky if you don't know what to do.

If you haven’t had to use nested annotations in Kotlin, it can some as a surprise and a frustration when it doesn’t work the way you think it should. The original annotation on a method or class is defined with the @ prefix, while the sub-annotations that are nested within the original annotation are not prefixed with anything.

Here is a full example defining a method with a Swagger definition including Bearer authentication.

   @ApiOperation("list", response = MyObject::class, responseContainer = "list", authorizations = [
        Authorization("Bearer")
    ])
    fun listMyObjects() : Iterable<MyObject> {
      // TODO some stuff
    }

As you can see from the above example, ApiOperation is defined as it should be with the @ prefix. The Authorization nested annotation just refers to the annotation class similar to creating an object in Kotlin normally.

comments powered by Disqus

Want to see a topic covered? create a suggestion

Get more developer references and books in the developwith store.