davsclaus commented on code in PR #24122: URL: https://github.com/apache/camel/pull/24122#discussion_r3444038520
########## core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleFunctionCacheKeyTest.java: ########## @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.language.simple; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.ExchangeTestSupport; +import org.apache.camel.Expression; +import org.apache.camel.support.builder.ExpressionBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Regression tests for the function cache key prefix bug. SimpleFunctionExpression shares a cache map with the + * expression-level cache. Without an "@FUNC@" prefix, a function key like "body" would collide with any other cache + * entry stored under the same string, causing the wrong Expression to be returned. + */ +public class SimpleFunctionCacheKeyTest extends ExchangeTestSupport { + + /** + * The cache must store function entries under the "@FUNC@" prefix, not under the raw function name. A poison entry + * stored under the raw key "body" must not be picked up when evaluating ${body}. Without the prefix fix, the + * function would find the poison entry and return "WRONG" instead of the actual body value. + */ + @Test + public void testFunctionCacheKeyIsPrefixed() { + Map<String, Expression> sharedCache = new HashMap<>(); + sharedCache.put("body", ExpressionBuilder.constantExpression("WRONG")); // raw key: collision target without the fix + + exchange.getIn().setBody("correct"); + + SimpleExpressionParser parser = new SimpleExpressionParser(context, "${body}", true, sharedCache); Review Comment: This test seeds a raw `"body"` entry into the cache manually, but no code path in the framework writes raw unprefixed keys to the shared cache except `SimpleFunctionExpression` itself — which would write the *correct* body expression, not a poison value. The scenario being tested cannot occur through normal framework execution. ########## core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java: ########## @@ -97,6 +98,11 @@ private Expression createSimpleExpression(CamelContext camelContext, String func return exp; } + // Prefix that separates function-level cache entries from the top-level expression cache entries + // written by SimpleLanguage (which uses "@SIMPLE@" + fullExpression). The two keyspaces share the + // same LRUCache instance, so distinct prefixes are needed to prevent accidental collisions. + private static final String FUNCTION_CACHE_KEY_PREFIX = "@FUNC@"; Review Comment: The `@SIMPLE@` prefix on expression-level keys (added in CAMEL-17073) already prevents collision with raw function names stored here. The `@FUNC@` prefix adds a second layer of separation that is redundant with the existing design. Also, this constant is declared after the methods — Java convention typically places `private static final` constants near the top of the class, next to the `cacheExpression` field at line 42. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
