Skip to content

methods().that().areAnnotatedWith() does not match methods which have multiple instances of the same annotation type #419

Closed
@m-ismar

Description

@m-ismar

Hello,

I don't know if this is due to me misconfiguring something, or due to ArchUnit, but I have a case where areAnnotatedWith() is not finding all the instances in a class.

In the following arch test:

    ArchCondition<JavaMethod> haveDescriptionInResponseDefinition =
            new ArchCondition<JavaMethod>(
                    "not have description in @ApiResponse definition") {
                @Override
                public void check(JavaMethod javaMethod, ConditionEvents conditionEvents) {

                    if (Util.readApiResponseDescription(javaMethod).isEmpty()) {
                        String message = String.format(
                                "@ApiResponse %s in method %s must have a description",
                                javaMethod.getAnnotationOfType(ApiResponse.class).responseCode(),
                                javaMethod.getFullName()
                        );
                        conditionEvents.add(SimpleConditionEvent.violated(javaMethod, message));
                    }
                }
            };
   
    @ArchTest
    private final ArchRule no_description_in_api_response = methods().that().
            areAnnotatedWith(ApiResponse.class)
            .should(haveDescriptionInResponseDefinition);

Not all methods get matched:

    //Recognized
    @GET
    @RolesAllowed(ROLE_RESTCLIENT)
    @Path("/{id}")
    @Produces({IMAGE_JPEG, IMAGE_JPG})
    @Operation(description = "Provides downloading images.")
    @ApiResponse(responseCode = "200", description = "The image binary.")
    public File getImage(
            @PathParam("id")
            String imageId, 
           @Context MessageContext context
     ) {...}

    //Not recognized
    @PUT
    @RolesAllowed(ROLE_RESTCLIENT)
    @Path("/{id}")
    @Consumes(MULTIPART_FORM_DATA)
    @Produces(APPLICATION_JSON)
    @Operation(description = "Handles upload of images.")
    @ApiResponse(responseCode = "204", description = "Successfully uploaded image")
    @ApiResponse(responseCode = "401", description = "Missing authorization for putting image")
    @ApiResponse(responseCode = "400", description = "A bad request, e.g., an invalid image ID")
    public Response putImage(
            @PathParam("id")
            @Parameter(description = "The ID (client-generated UUID) of the image to upload.") String imageId,
            MultipartBody body
    ) {...}

I figured that that was because putImage() has multiple @ApiResponse annotations, but I'm not sure why that happens and what can I do to avoid it.

Is this intended ArchUnit behaviour or am I missing something?

Sorry if this question was asked before, but I found nothing on the topic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions