Skip to content

Keep Hooks.enableAutomaticContextPropagation() when the module has no application configuration file - #1306

Open
slayyyyer wants to merge 4 commits into
openrewrite:mainfrom
slayyyyer:fix-1201-b8-hooks-without-config-file
Open

slayyyyer wants to merge 4 commits into
openrewrite:mainfrom
slayyyyer:fix-1201-b8-hooks-without-config-file

Conversation

@slayyyyer

@slayyyyer slayyyyer commented Nov 18, 2026 •

Copy link
Copy Markdown

Part of #1201 — item B8.

MigrateHooksToReactorContextProperty removed Hooks.enableAutomaticContextPropagation() from a module and then added spring.reactor.context-propagation=auto to whatever application.properties/application.yml existed in the project. A module with no application configuration file of its own, or with one only under src/test/resources or another source set, lost the call while gaining nothing: automatic context propagation was silently turned off. That is item B8 in #1201.

What this changes

  • The scanning recipe records, per JavaProject, that the module has an application configuration file in its main source set (SourceSet marker plus SpringExecutionContextView's default application configuration paths). The call is replaced only in projects that have both the hook and such a file.
  • RemoveMethodInvocationsVisitor went with the enclosing call when the invocation was the only statement in a lambda body. The removal is now a block-statement removal, so stream.forEach(arg -> { Hooks.enableAutomaticContextPropagation(); }) keeps its forEach and ends up as stream.forEach(arg -> { });.
  • The description states that the call is kept when the module has no application configuration file to add the property to.
  • recipes.csv regenerated; only this recipe's row changed.

Is this the right change?

  • Do we already have it? No. The recipe itself comes from Fix spring.reactor.context-propagation property value from 'true' to 'auto' #938 (issue SB 3.2 - Use Reactor Context-Propagation property #464) and is included by spring-boot-32.yml; this PR only narrows when it acts. No other recipe in the repo or the catalog has the same intent.
  • Is it common? Yes. The shape it targets — the hook in main next to SpringApplication.run — is what the recipe documents, and a module without an application.* of its own (a library module, or a multi-module build where configuration lives elsewhere) is ordinary. Those modules are the ones losing behaviour today.
  • Could it be declarative? No. The decision is per module and needs a scan over that module's parsed files, which a chain of Change* recipes cannot express. The property addition itself stays AddSpringProperty.
  • Is it verified upstream? Yes. spring.reactor.context-propagation is a Spring Boot 3.2 property (ReactorProperties is @since 3.2.0), documented in Observability — Context Propagation and listed under Properties in the Spring Boot 3.2 release notes. The recipe sits in the 3.2 composite.
  • Is it complete enough to ship? Yes. A module the recipe can migrate still gets both halves of the migration, and a module it cannot migrate safely now makes no change instead of a damaging one.
  • Does it do no harm? That is the point of the PR. The recipe is more conservative afterwards: no change without a place to put the property, existing configuration respected by AddSpringProperty, and the lambda case no longer deletes surrounding code.
  • Is it opinionated? No. It is part of the 3.2 upgrade composite, not a best-practice recipe.

Tests

MigrateHooksToReactorContextPropertyTest, 9 tests, all passing:

  • replaceMethodCallWithProperty, replaceMethodCallWithPropertyInYaml — the documented shape, for application.properties and application.yml.
  • replacesHookOnlyWhereAnApplicationConfigurationFileExists — two modules, only the configured one changes.
  • keepsHookWhenThereIsNoApplicationConfigurationFile (B8), keepsHookWhenConfigurationIsOnlyInTestSources, keepsHookWhenConfigurationIsInAnotherSourceSet — no change.
  • replaceMethodCallThatIsTheOnlyStatementInALambdaBody, keepsTheEnclosingCallWhenTheLambdaBodyBecomesEmpty — the visitor bug.
  • shouldNotAddPropertyWhenNoHooksPresent — no change.

The YAML, lambda and custom-source-set tests were each checked to fail with the corresponding part of the fix reverted.

Note on the local run

The declared build plugin (org.openrewrite.build 2.27.4, from artifacts.codegenomeproject.org) answers 401 anonymously from this machine, so the run used plugin 2.20.0 (rewrite 8.90.x) with the three test sources that need a newer rewrite excluded. On that setup the full suite matches main: 64 failures, identical per class, all failing the same way on unmodified main. CI will run the declared versions.

…on configuration

The call was removed from every file in a module that used it, but `AddSpringProperty` only edits application configuration files that already exist, so in a module without one the behaviour disappeared silently. Record the modules that have a main source set application configuration file during the scan, and only replace the call with `spring.reactor.context-propagation=auto` there. A configuration file in a test source set does not count, since the running application would not read it.
The description now states that the call is only replaced in a module that has an application configuration file to add the property to, and that a module without one keeps the call. `recipes.csv` regenerated for the changed description.
`application.yml` in the main source set now asserts that the call is replaced and the property added as nested YAML, so the YAML branch of the configuration check is exercised, not only `application.properties`.
…wn statement

`RemoveMethodInvocationsVisitor` also removed the call around a lambda whose body became empty, so a registration or `forEach(..)` lost its invocation. Removing only the statement leaves that call in place. Only the `main` source set, or a file without a source set marker, now counts as the application's configuration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant