Skip to content

fix(template): treat invalid expression pattern modifier as literal - #3459

Closed
seonwooj0810 wants to merge 1 commit into
OpenFeign:masterfrom
seonwooj0810:fix/issue-2739-invalid-pattern-modifier
Closed

seonwooj0810 wants to merge 1 commit into
OpenFeign:masterfrom
seonwooj0810:fix/issue-2739-invalid-pattern-modifier

Conversation

@seonwooj0810

Copy link
Copy Markdown
Contributor

Fixes #2739

Root cause

A header-map value (or any template fragment) such as {test:[abc} is parsed by Expressions.create as a {name:pattern} expression, where the part after : is treated as a regex value modifier. Expression's constructor compiles that modifier eagerly via Pattern.compile(...), so when the modifier is not a valid regular expression ([abc → Unclosed character class) a PatternSyntaxException was thrown during template construction. With a Map header value containing brackets/colons this surfaces as the stack trace in the issue (HeaderTemplate.create → Template → Expressions.create → Pattern.compile).

Change

Expressions.create already returns null to signal "this is not a usable expression — treat the chunk as a literal" (e.g. for nested expressions and non-simple candidates). This extends that existing contract: a PatternSyntaxException raised while building the expression is caught and create returns null, so the fragment falls back to a Literal in Template.parseFragment instead of failing template construction. Valid expressions such as {id:[0-9]+} are unaffected.

Test evidence

Added ExpressionsTest.invalidPatternModifierIsTreatedAsLiteral — Expressions.create("{test:[abc}") previously threw PatternSyntaxException; it now returns null (literal).

Verification done: built and ran the affected tests with the project toolchain — ./mvnw -pl core test -Dtest='feign.template.**' → 80 tests pass (including the new one), no regression; code formatting validated via git-code-format-maven-plugin:validate-code-format (BUILD SUCCESS); commit carries a DCO Signed-off-by trailer.

A header-map (or any template) value such as "{test:[abc}" parses as a
{name:pattern} expression, but the ':' value modifier ("[abc") is not a
valid regular expression. Expression's constructor compiles it eagerly
with Pattern.compile, so an invalid modifier threw PatternSyntaxException
during template construction (OpenFeigngh-2739) instead of the value being used
as-is.

Expressions.create already returns null to signal "this is not a usable
expression, treat the chunk as a literal". Extend that contract to a
PatternSyntaxException from the value modifier: catch it and return null
so the chunk falls back to a literal. Valid expressions such as
{id:[0-9]+} are unaffected.

Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
@seonwooj0810

Copy link
Copy Markdown
Contributor Author

Closing this as already fixed upstream.

While rebasing onto current master I found that 5cd6f57 ("treat invalid expression value modifier as a literal", #3437) already lands the same change: the Expressions.create(...) body is wrapped in a try that catches PatternSyntaxException and returns null, so an invalid ':' value modifier is treated as a literal instead of failing template construction. The only remaining difference on my branch was comment wording.

Test coverage there is a superset of mine too — invalidValueModifierIsTreatedAsLiteral asserts {range:[1:10} throws nothing, that {a:[} and {a:(} return null, and that a valid {id:[0-9]+} still produces an expression, whereas my test only covered the single {test:[abc} case from #2739.

Nothing left to do here — thanks to whoever picked it up. Closing to keep the review queue clean.

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

Successfully merging this pull request may close these issues.

When using FeignClient, an error occurs when a value with a special structure is passed in the header map

1 participant