0

I'm using swagger annotations to document an api, and they're quite verbose. I find myself repeating certain annotations verbatim. like:

public Response myAPI(
        @Parameter(
                description = "Some long long description",
                required = true,
                schema = @Schema(allowableValues = {"abc", "def", "hij"}, defaultValue = "abc"))
        @QueryParam("foo") String foo) {
}

What I'd love to have is:

static Parameter myRepeatingParam = @Parameter(
              description = "Some long long description",
              required = true,
              schema = @Schema(allowableValues = {"abc", "def", "hij"}, 
              defaultValue = "abc"));

public Response myAPI01(
        @myRepeatingParam
        @QueryParam("foo") String foo) {
}

public Response myAPI02(
        @myRepeatingParam
        @QueryParam("foo") String foo) {
}

Update

Using the meta-annotations works some places, but not others. For instance, given:

@Target({PARAMETER, METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Parameter(in = ParameterIn.QUERY, name = "foo")
public @interface TestSwagger {
}

This compiles:

    public Response myAPI(
            @TestSwagger
            @QueryParam("foo") String foo) {
        return null;
    }

But in this use case it does not, because @TestSwagger isn't a @Parameter and there appears to be no polymorphism in this case.

    @Operation(
            parameters = { // array of @Parameters
                    @TestSwagger,
                    @Parameter(in = ParameterIn.QUERY, name = "bar")},
            )
    public Response myAPINope(
            @QueryParam("foo") String foo,
            @QueryParam("bar") String bar) {
        return null;
    }

This 2nd case is more what I'm going after and doesn't seem possible.

marathon
  • 7,881
  • 17
  • 74
  • 137
  • Not natively, but it's doable at the framework level, like Jackson does: https://stackoverflow.com/questions/13401581/how-to-create-an-annotation-that-is-a-group-of-jackson-annotations – shmosel Nov 15 '21 at 18:53
  • Does this answer your question? [swagger combine some repeating annotations](https://stackoverflow.com/questions/46324478/swagger-combine-some-repeating-annotations) – M. Prokhorov Nov 15 '21 at 18:59
  • @M.Prokhorov - the "meta" annotation answer looked promising, but it doesn't actually compile for annotations nested inside other annotations, which is my actual use case. (params are inside of `ApiResponse` – marathon Nov 15 '21 at 19:27
  • At least Java allows you to extract primitives, strings, and arrays of both to static finals. – Miha_x64 Nov 15 '21 at 19:30
  • @marathon, what do you mean "doesn't compile"? If you meant code from your original snippet and that doesn't compile - it's the correct compiler behavior, you can't assign annotation to a variable - you have to define a proper `@interface`. Otherwise, please show your actual code and what doesn't compile. – M. Prokhorov Nov 17 '21 at 15:33
  • @M.Prokhorov - no, not my code snippet from the question. See the update at the bottom of my question. – marathon Nov 17 '21 at 20:56

0 Answers0