Skip to content

Commit 7b20aef

Browse files
committed
Improve error message for unsupported character in SpEL expression
Prior to this commit, when an unsupported character such as "ü" was encountered in a SpEL expression, the error message was: Cannot handle (252) 'ü' With this commit, the error message is now similar to: Unsupported character 'ü' (252) encountered at position 5 in expression. See gh-30580 Closes gh-30602
1 parent cdc4497 commit 7b20aef

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/standard/Tokenizer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -265,7 +265,9 @@ else if (isTwoCharToken(TokenKind.SAFE_NAVI)) {
265265
raiseParseException(this.pos, SpelMessage.UNEXPECTED_ESCAPE_CHAR);
266266
break;
267267
default:
268-
throw new IllegalStateException("Cannot handle (" + (int) ch + ") '" + ch + "'");
268+
throw new IllegalStateException(
269+
"Unsupported character '%s' (%d) encountered at position %d in expression."
270+
.formatted(ch, (int) ch, (this.pos + 1)));
269271
}
270272
}
271273
}

spring-expression/src/test/java/org/springframework/expression/spel/ParsingTests.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.springframework.expression.spel.standard.SpelExpressionParser;
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2728

2829
/**
2930
* Parse some expressions and check we get the AST we expect.
@@ -42,6 +43,58 @@ class ParsingTests {
4243
@Nested
4344
class Miscellaneous {
4445

46+
@Test
47+
void supportedCharactersInIdentifiers() {
48+
parseCheck("#var='value'");
49+
parseCheck("#Varz='value'");
50+
parseCheck("#VarZ='value'");
51+
parseCheck("#_var='value'");
52+
parseCheck("#$var='value'");
53+
parseCheck("#_$_='value'");
54+
55+
parseCheck("age");
56+
parseCheck("getAge()");
57+
parseCheck("get$age()");
58+
parseCheck("age");
59+
parseCheck("Age");
60+
parseCheck("__age");
61+
parseCheck("get__age()");
62+
63+
parseCheck("person.age");
64+
parseCheck("person.getAge()");
65+
parseCheck("person.get$age()");
66+
parseCheck("person$1.age");
67+
parseCheck("person_1.Age");
68+
parseCheck("person_1.__age");
69+
parseCheck("Person_1.get__age()");
70+
}
71+
72+
@Test
73+
void unsupportedCharactersInIdentifiers() {
74+
// Invalid syntax
75+
assertThatIllegalStateException()
76+
.isThrownBy(() -> parser.parseRaw("apple~banana"))
77+
.withMessage("Unsupported character '~' (126) encountered at position 6 in expression.");
78+
79+
// German characters
80+
assertThatIllegalStateException()
81+
.isThrownBy(() -> parser.parseRaw("begrüssung"))
82+
.withMessage("Unsupported character 'ü' (252) encountered at position 5 in expression.");
83+
assertThatIllegalStateException()
84+
.isThrownBy(() -> parser.parseRaw("Spaß"))
85+
.withMessage("Unsupported character 'ß' (223) encountered at position 4 in expression.");
86+
87+
// Spanish characters
88+
assertThatIllegalStateException()
89+
.isThrownBy(() -> parser.parseRaw("buenos_sueños"))
90+
.withMessage("Unsupported character 'ñ' (241) encountered at position 11 in expression.");
91+
92+
// Chinese characters
93+
assertThatIllegalStateException()
94+
.isThrownBy(() -> parser.parseRaw("have乐趣()"))
95+
.withMessage("Unsupported character '乐' (20048) encountered at position 5 in expression.");
96+
}
97+
4598
@Test
4699
void literalNull() {
47100
parseCheck("null");

0 commit comments

Comments
 (0)