spring-cloud / spring-cloud/spring-cloud-gateway

How the BooleanSpec is suposed to work?

Open
#3,054 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feedback-provided
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

I am trying to create some routes based on specific Method and path, but the way BooleanSpec works quite is confusing.

My routes would be something like this:

graph LR;
    A[User]-->B[Critical route];
    B-->C['/get - GET'];
    B-->D['/post - POST'];
    C-->F[Rate limit - 1 req/s];
    D-->F;
    A[User]-->H[any other route];
    H-->G[Rate limit - 10 req/s];
    G-->T[API];
    F-->T;

In other words:

if (('/get' and 'GET') or ('/post' and 'POST')) {
    set rate limit = 1 req/s
} else {
    set rate limit = 10 req/s
}

When translating to the code and using the RouteLocatorBuilder, I created these routes:

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
            .route("critical", route -> route
                    .order(Ordered.HIGHEST_PRECEDENCE)
                    .path("/get").and().method(HttpMethod.GET)
                    .or()
                    .path("/post").and().method(HttpMethod.POST)
                    .filters(filter -> filter
                            .addResponseHeader("route", "critical")
                    )
                    .uri("https://httpbin.org/")
            )
            .route("default", route -> route
                    .order(Ordered.LOWEST_PRECEDENCE)
                    .path("/**")
                    .filters(filter -> filter
                            .addResponseHeader("route", "default")
                    )
                    .uri("https://httpbin.org/")
            )
            .build();
}

The problem is that when the route /get is called, the default route is used instead of critical. Am I doing something wrong or the RouteLocatorBuilder cannot handle this type of condition?

Some results
$ curl -X GET -I http://localhost:8080/get
HTTP/1.1 200 OK
Date: Tue, 12 Sep 2023 23:42:46 GMT
Content-Type: application/json
Content-Length: 419
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
route: default
$ curl -X POST -I http://localhost:8080/post
HTTP/1.1 200 OK
Date: Tue, 12 Sep 2023 23:42:55 GMT
Content-Type: application/json
Content-Length: 483
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
route: critical
$ curl -X DELETE -I http://localhost:8080/delete
HTTP/1.1 200 OK
Date: Tue, 12 Sep 2023 23:43:02 GMT
Content-Type: application/json
Content-Length: 457
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
route: default

The full code can be found here

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the RouteLocatorBuilder configuration shown in the issue and compare the GET /get, POST /post, and DELETE /delete results. Check how the BooleanSpec combines path and method predicates, then verify that the observed route headers match the intended critical-versus-default behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.