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

DiscoveryClient Route Definition Locator predicate parse exception after 5.0.2 upgrade

Open
#4,216 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

for team discussion
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

In testing our Spring Cloud Gateway (WebFlux server) implementation after upgrading from Spring Cloud 2025.1.1 to 2025.1.2, I receive the following exception, because of which no service instances can be discovered:

DiscoveryClientRouteDefinitionLocator : Unable to parse '/api-v5/' + serviceId.replaceFirst('-v(\d+)', '/v$1') + '/**'

    org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method replaceFirst(java.lang.String,java.lang.String) cannot be found on type java.lang.String

Our Path predicate is setup to match URIs of the form "/api-v5/{apiName}/{apiMajorVersion}/**" to instances of Consul services registered with serviceId = {apiName}-{apiMajorVersion}:

spring:
  cloud:
    gateway:
      server:
        webflux:
          discovery:
            locator:
              enabled: true
              predicates:
              - name: Path
                args:
                  pattern: "'/api-v5/' + serviceId.replaceFirst('-v(\\d+)', '/v$1') + '/**'"
              # etc.

I traced the root cause to the removal of the withInstanceMethods() builder method for the SimpleEvaluationContext instance field:
https://github.com/spring-cloud/spring-cloud-gateway/blame/8b7549c1a9a0340b66316c99807747d253aae354/spring-cloud-gateway-server-webflux/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocator.java#L78. I confirmed previous functionality was restored by replacing this version of the Bean with a custom class mimicking the prior version using withInstanceMethods.

Would it be possible to restore withInstanceMethods when building the SimpleEvaluationContext? If there are legitimate reasons not to do so, is there another way I can define my Path predicate to map URIs the way I need?

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 spring-cloud-gateway-server-webflux/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocator.java at the SimpleEvaluationContext construction, then compare the prior version referenced in the issue's blame link. Verify how the configured Path predicate expression is evaluated; done means the supplied replaceFirst expression can generate the intended discovery route, or the supported alternative is clearly established.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.