feign icon indicating copy to clipboard operation
feign copied to clipboard

Error in RequestTemplate#uri when there're both query and fragment

Open lijinliangyihao opened this issue 1 year ago • 0 comments

In method RequestTemplate uri(String uri, boolean append), we process query first, then fragment. Code like this

@Test
  public void testFragmentAndQueries() {
    RequestTemplate template = new RequestTemplate().method(HttpMethod.GET)
        .uri("/path?name=bbb#aaa", true);

    System.out.println(template.fragment);
    System.out.println(template.queries());
  }

will produce:

null
{name=[bbb#aaa]}

This is clearly not what we want. But if we reverse the order of query processing and fragment processing like this, in method RequestTemplate#uri:

    // other code ...
    // move this part up!
    int fragmentIndex = uri.indexOf('#');
    if (fragmentIndex > -1) {
      fragment = uri.substring(fragmentIndex);
      uri = uri.substring(0, fragmentIndex);
    }
    /*
     * templates may provide query parameters. since we want to manage those explicitly, we will need
     * to extract those out, leaving the uriTemplate with only the path to deal with.
     */
    Matcher queryMatcher = QUERY_STRING_PATTERN.matcher(uri);
    if (queryMatcher.find()) {
      String queryString = uri.substring(queryMatcher.start() + 1);

      /* parse the query string */
      this.extractQueryTemplates(queryString, append);

      /* reduce the uri to the path */
      uri = uri.substring(0, queryMatcher.start());
    }

    // other code ...

The answer is as we expected:

#aaa
{name=[bbb]}

lijinliangyihao avatar Dec 20 '23 09:12 lijinliangyihao