Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PatternSyntaxException: Illegal character #1512

Open
myifeng opened this issue Sep 26, 2021 · 9 comments
Open

PatternSyntaxException: Illegal character #1512

myifeng opened this issue Sep 26, 2021 · 9 comments

Comments

@myifeng
Copy link
Contributor

myifeng commented Sep 26, 2021

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 ?

@guptnis
Copy link

guptnis commented Oct 14, 2021

@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"]}");
}
}
}

@myifeng
Copy link
Contributor Author

myifeng commented Oct 19, 2021

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

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

@mr-rajeshrathod
Copy link

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
Copy link

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
791f9e5

@myifeng
Copy link
Contributor Author

myifeng commented Dec 2, 2021

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.

@mr-rajeshrathod
Copy link

@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
Copy link

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

@velo
Copy link
Member

velo commented Jun 28, 2022

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.

@JKomoroski
Copy link
Contributor

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 #1464

I believe this can be closed @velo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants