feign icon indicating copy to clipboard operation
feign copied to clipboard

PatternSyntaxException: Illegal character

Open myifeng opened this issue 4 years ago • 9 comments

I want to set the requestTemplate header value to an array, but an error occurs

public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
            requestTemplate.header("roles", "{\"roles\":[\"default-role\"]}");
        }
    }
}
java.util.regex.PatternSyntaxException: Illegal character range near index 10
["default-role"]
          ^
	at java.util.regex.Pattern.error(Pattern.java:1957)
	at java.util.regex.Pattern.range(Pattern.java:2657)
	at java.util.regex.Pattern.clazz(Pattern.java:2564)
	at java.util.regex.Pattern.sequence(Pattern.java:2065)
	at java.util.regex.Pattern.expr(Pattern.java:1998)
	at java.util.regex.Pattern.compile(Pattern.java:1698)
	at java.util.regex.Pattern.<init>(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at feign.template.Expression.lambda$new$0(Expression.java:35)
	at java.util.Optional.ifPresent(Optional.java:159)
	at feign.template.Expression.<init>(Expression.java:35)
	at feign.template.Expressions$SimpleExpression.<init>(Expressions.java:106)
	at feign.template.Expressions.create(Expressions.java:86)
	at feign.template.Template.parseFragment(Template.java:218)
	at feign.template.Template.parseTemplate(Template.java:202)
	at feign.template.Template.<init>(Template.java:61)
	at feign.template.HeaderTemplate.<init>(HeaderTemplate.java:110)
	at feign.template.HeaderTemplate.create(HeaderTemplate.java:72)
	at feign.RequestTemplate.lambda$appendHeader$3(RequestTemplate.java:758)
	at java.util.Map.compute(Map.java:1093)
	at feign.RequestTemplate.appendHeader(RequestTemplate.java:756)
	at feign.RequestTemplate.header(RequestTemplate.java:726)
	at feign.RequestTemplate.header(RequestTemplate.java:694)
	at com.commercial.consumer.config.FeignConfig.apply(FeignConfig.java:20)
......

How can I set it up so that it can work ?

myifeng avatar Sep 26 '21 05:09 myifeng

@myifeng I believe you can try with - eg: public class FeignConfig implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { requestTemplate.header("roles", "{"roles":["default-role"]}"); } } }

guptnis avatar Oct 14 '21 21:10 guptnis

@guptnis Thank you very much, but your answer does not seem to solve my problem.

"{\"roles\":[\"default-role\"]}" from JSONObject.toJSONString(user)

myifeng avatar Oct 19 '21 02:10 myifeng

I am also facing the same issue with feign and setting header value that contains JSON array object. Any help would be highly appreciated !!.

mr-rajeshrathod avatar Dec 01 '21 23:12 mr-rajeshrathod

I just discovered that this stopped working in v10.7.2 and later versions. So, It seems that this issue is happening because of the following commits.

https://github.com/OpenFeign/feign/pull/1138/commits https://github.com/OpenFeign/feign/pull/1139/commits/791f9e52d3cfcf71bf4e58dc121f2d44672374c5

mr-rajeshrathod avatar Dec 02 '21 02:12 mr-rajeshrathod

I found a solution: encode the value to prevent { , because feign treats a string beginning with { as an expression or a literal.

requestTemplate.header("roles", "{\"roles\":[\"default-role\"]}");	// It not work.
requestTemplate.header("roles", URLEncoder.encode("{\"roles\":[\"default-role\"]}"), StandardCharsets.UTF_8.name());	// It work.

But I think this is unfriendly to developers.

myifeng avatar Dec 02 '21 05:12 myifeng

@myifeng - This will also require decoding the header value at receiving end. So, I don't think this is the best approach.

@authors - Can you please look into this issue as its impacting our ability to use feign client for REST calls.

mr-rajeshrathod avatar Dec 02 '21 21:12 mr-rajeshrathod

Hi All - Is there any update on this issue? I am sure this is breaking for many consumer of feign library.

mr-rajeshrathod avatar Jun 28 '22 15:06 mr-rajeshrathod

Hi @mr-rajeshrathod , feign is mainly run by volunteers, so if that is impacting you, feel free to raise a PR and I will review for you.

velo avatar Jun 28 '22 21:06 velo

Here's another issue this looks like another json header issue fixed in master at HEAD now. Related to the fixes that came in for https://github.com/OpenFeign/feign/issues/1464

I believe this can be closed @velo

JKomoroski avatar Oct 22 '22 01:10 JKomoroski