spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

Clarify or() behavior

Open fmunch opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe.

When using the fluent Java API the behavior of or() is not well documented when combined with and().

In these examples route-1 matches while route-2 does not.

.route("route-1", r -> r.predicate(swe -> false)
        .and().predicate(swe -> true)
        .or().predicate(swe -> true)
        .and().predicate(swe -> true)
        .and().predicate(swe -> true)
        .uri(...)
.route("route-2", r -> r.predicate(swe -> true)
        .and().predicate(swe -> true)
        .or().predicate(swe -> true)
        .and().predicate(swe -> true)
        .and().predicate(swe -> false)
        .uri(...)

I am guessing that the precedence is as follows:

((((false && true) || true) && true) && true) // => true
((((true && true) || true) && true) && false) // => false

Describe the solution you'd like

or() is only mentioned once in the documentation (unless I missed another section talking about it).

IMHO that precedence should be clearly documented since it might not be obvious for everyone and might cause configuration issues.

Describe alternatives you've considered

Is there any plan to add nested boolean expressions in the API? Something like:

.route("route", r -> r
        .nested(r.host(...).or().header(...))
        .and()
        .nested(r.predicate(...).or().predicate(...))
        .uri(...)

It might be less readable but would offer a more powerful API.

Additional context

What I originally wanted to do is check the hostname in either Host or X-ORIGINAL-HOST, something HostRoutePredicateFactory cannot do right now.

fmunch avatar Jul 27 '22 13:07 fmunch