Annoying annotation io.swagger.v3.oas.annotations.Operation which is too popular and duplicate with application side class name

Springboot uses some annotations for REST api, such as

 @PostMapping @Operation(summary = "an example api") public void forceDone(@RequestBody @Valid final Example example) { ... 

The @Operation is io.swagger.v3.oas.annotations.Operation.

But this name is too popular, the application side also have a class (not @interface) named Operation, such as

class MyApp.Operation { ... 

Sure I can replace one side of them with full class name,

but I wonder

  • is there anyway to tell Java to differentiate the @interface and class name? such as (pseudo code)

import io.swagger.v3.oas.annotations.@Operation import MyApp.Operation ... @Operation(...) ... { Operation myAppOperation = ... // not related to the @Operation. } 

(I understand that annotation can not be inherited, but still hoping a better solution).

EDIT: I have seen springboot internal source code searching for io.swagger.v3.oas.annotations.Operation, such as in org.springdoc.api.AbstractOpenApiResource#calculatePath(..)

 if (apiOperation == null || StringUtils.isBlank(apiOperation.operationId())) apiOperation = AnnotatedElementUtils.findMergedAnnotation(method, io.swagger.v3.oas.annotations.Operation.class); 

so obviously there seems no way to use an alternative annotation, I did not expect that.

But I come up with another thought:

  • is there any way to reference annotation name by short name? such as

annotations.Operation 

instead of

io.swagger.v3.oas.annotations.Operation 

?

1

1 Answer

Two ideas came to my mind.

Idea 1:

Create a meta annotation for @Operation with a different name and use this instead in your code base. This works only of the framework supports meta annotations / composed annotations. A quick test in my own Spring Swagger application looked promising.

e.g.:

import io.swagger.v3.oas.annotations.Operation; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Operation public @interface OpenApiOperation { String summary() default ""; String description() default ""; // copy needed signatures of original @Operation here } 

Then use @OpenApiOperation instead of @Operation in your code base.

Idea 2:

Make your own Operation class an inner class and import it via the outer class name.

e.g.:

public class MyApp { public static class Operation { // methods } } 

Then use it like this:

import MyApp ... @Operation(...) ... { MyApp.Operation myAppOperation = ... } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like