ammachado commented on code in PR #24122: URL: https://github.com/apache/camel/pull/24122#discussion_r3447770509
########## 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: Confirmed. `SimpleFunctionExpression` is the only writer of raw (unprefixed) keys to the shared cache, and it always writes the correct expression for that function name. No framework path seeds a poison `"body"` entry, so the scenario this test exercises cannot occur in practice. Removing it along with the rest of the change. _Claude Code on behalf of Adriano Machado_ -- 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]
