Validate name of path param (@QueryValue) in Micronaut

400 Views Asked by At

I need to validate the @QueryValues in an endpoint in my controller class using Micronaut.

@Get("/files")
    @Produces({ "application/json" })
    FileSystemNodes readFiles(@Nullable
                              @QueryValue("project")
                              String project,
                              @Nullable
                              @QueryValue("store")
                              String store,
                              @Nullable
                              @QueryValue("path")
                              String path,
                              @NotNull
                              @Header("Authorization")
                              java.lang.String authorization,
                              @NotNull
                              @Header("TenantID")
                              java.lang.String tenantID);

The endpoint is supossed to work if either "project", "store" or "path" are send, also if the three of them are send or even if none of them are send as a part of the URL.

I have tried with Custom Constraint Validation but it validates the value of the path param, not the key value.

I understand that Custom Argument Binding helps to bind the value of the path param into an specific object, but something tells me that it would be the answer for this.

The endpoint should work normally in these escenarios:

/files?project=projectValue
/files?store=storeValue
/files?path=pathValue
/files?project=projectValue?store=storeValue?path=pathValue
/files?project=projectValue?store=storeValue
/files?project=projectValue?path=pathValue
/files?store=storeValue?path=pathValue
/files

But if I send and "unknown" path param, it should return 400 Bad Request

/files?xyz=xyzValue?path=pathValue
/files?abc=abcValue

What I want to achive is to filter the name of the path params to just accept project, store y path because right now if I hit the endpoint with /files?abcKey=abcValue it works, as if I send none of the path params expected (/files).

1

There are 1 best solutions below

0
jJavier52 On

I solved it adding a HttpFilter.

@Filter("/store/v1/files/systems/paths")
public class QueryParamFilter implements HttpServerFilter {

    @Override
    public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {

        Map<String, String> queryParams = request
                                        .getParameters()
                                        .asMap(String.class, String.class);

        if (queryParams.keySet().stream()
                                .anyMatch(key -> !key.equals("project")
                                                 && !key.equals("store")
                                                 && !key.equals("path"))) {
            MutableHttpResponse<?> response = HttpResponse.badRequest();
            response.body("Invalid query parameter");
            return Publishers.just(response);
        }
        return chain.proceed(request);
    }

}