davsclaus opened a new pull request, #31336: URL: https://github.com/apache/camel/pull/31336
## What was wrong `AdviceWith` prepares the advised route again (`merged.markUnprepared(); routes.prepareRoute(merged)`). Preparing a route was not idempotent: the definitions that the first prepare merged in from the route configurations are in the route outputs, so the second prepare saw them as the route's own and the configurations were merged once more. Reproduced on main with a route using a route configuration (onException, onCompletion, interceptFrom, interceptSendToEndpoint, intercept): - the advised route model had the `onException`, `interceptFrom`, `interceptSendToEndpoint` and `intercept` twice (dumps and diagrams too); - at runtime the `interceptSendToEndpoint` ran twice per call, and the `intercept` ran 33 times instead of 11 when the advice is done before the context is started (`isUseAdviceWith`); - the context-scoped onCompletion was no longer recorded as context-scoped (`RouteDefinition.getContextScopedOnCompletions()`, used by the YAML dumper). The transacted/saga part of the ticket did not reproduce: the transacted rewiring is idempotent, only the onException of the configuration was doubled. The new test keeps it covered. ## What changed Option (a)/(c) of the ticket analysis, in its smallest form: `RouteDefinitionHelper.prepareRouteImp` now keeps a context-scoped definition once when the route outputs already have the same instance (identity, not equals): - `initOnExceptions`: a global onException already in the outputs is not added again; - `initInterceptors`: an intercept / interceptFrom / interceptSendToEndpoint from the outputs that is also in the context-scoped list is not added again (so it is still recorded as context-scoped); - `initOnCompletions`: a global onCompletion already in the outputs does not count as the route's own, so the globals are used (and recorded as context-scoped) as on the first prepare. Why not a deep copy of the route as written (option (b), the groundwork of CAMEL-25142): it is a much bigger change, and the definitions merged from the original RouteBuilder (builder-level onException etc.) are not re-provided by the advice builder, so removing the merged ones before re-preparing would lose them. Matching by instance keeps the order and the builder-level definitions as they are today, and the route configuration ones come back once (also after the intercept reifier removed the intercept from the outputs on a started context). First prepares are unchanged: a context-scoped definition is never in the outputs before the first merge. Out of scope: the RouteBuilder-level `intercept` that is lost after adviceWith (CAMEL-28100, separate PR). ## Tests New `AdviceWithRouteConfigurationTest` (camel-core): route configuration with onException, onCompletion, interceptFrom, interceptSendToEndpoint and intercept, adviceWith on a started and a not-started context; RouteBuilder-level onException, onCompletion, interceptFrom and interceptSendToEndpoint; a transacted route with a route configuration. Each definition is once in the model and runs once (the intercept count matches the same route without adviceWith). 3 of the 5 tests fail on main. Ran: camel-core `AdviceWith*`, `*RoutesConfiguration*`, `*RouteConfiguration*`, `Intercept*`, `*OnCompletion*`, `Saga*`, `*Transacted*`, `RouteChecksum*` (512 tests) and the packages `processor.interceptor`, `model`, `processor.onexception` (662 tests); camel-spring-xml `*AdviceWith*`, `*RouteConfiguration*`, `*Transacted*` (84); camel-yaml-dsl `*Dump*`, `*RouteConfiguration*`, `*Intercept*`, `*OnCompletion*` (50); camel-test-junit6 `*Advice*` (2). All green. ## Upgrade guide Yes, a short 4.25 entry: a test that expected the doubled counts of a route configuration intercept after adviceWith must be updated. Fixes https://issues.apache.org/jira/browse/CAMEL-28099 _Claude Code on behalf of davsclaus_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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]
