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

Fix env package name in expression language type identifier #11418

Open
wants to merge 1 commit into
base: 4.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import io.micronaut.expressions.parser.token.Tokenizer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.micronaut.expressions.parser.token.TokenType.*;
Expand Down Expand Up @@ -469,7 +470,7 @@ private TypeIdentifier typeIdentifier(boolean wrapped) {
parts.add(eat(IDENTIFIER).value());
while (lookahead != null && lookahead.type() == DOT) {
eat(DOT);
parts.add(eat(IDENTIFIER).value());
parts.add(eat(IDENTIFIER, ENVIRONMENT).value());
}

if (wrapped) {
Expand Down Expand Up @@ -559,4 +560,21 @@ private Token eat(TokenType tokenType) {
lookahead = tokenizer.getNextToken();
return token;
}

private Token eat(TokenType... tokenTypes) {
if (lookahead == null) {
throw new ExpressionParsingException("Unexpected end of input. Expected any of: '" + Arrays.toString(tokenTypes) + "'");
}

Token token = lookahead;

for (TokenType type : tokenTypes) {
if (token.type() == type) {
lookahead = tokenizer.getNextToken();
return token;
}
}

throw new ExpressionParsingException("Unexpected token: " + token.value() + ". Expected any of: '" + Arrays.toString(tokenTypes) + "'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ public Token getNextToken() {
if (!hasMoreTokens()) {
return null;
}

// TODO for things like ctx[io.micronaut.core.context.env.Environment] this
// will return the ENVIRONMENT token instead of the IDENTIFIER token because of the env package name.
// Order of the map above matters if multiple things can match it
remaining = expression.substring(cursor);
for (TokenPattern pattern: PATTERNS) {
Token token = pattern.matches(remaining);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ class TypeIdentifierExpressionsSpec extends AbstractEvaluatedExpressionsSpec {
expr2 instanceof Class && expr2 == Object.class
}

void "test type identifier including env in package naming"(){
given:
Object expr1 = evaluateAgainstContext("#{ #getType(T(io.micronaut.context.env.Environment)) }",
"""
@jakarta.inject.Singleton
class Context {
Class<?> getType(Class<?> type) {
return type;
}
}
""")

expect:
expr1 instanceof Class && expr1 == Environment.class
}

}
Loading