Skip to content

Fix OAuth2ResourceServerLambdaDsl for Kotlin code#916

Merged
timtebeek merged 5 commits intomainfrom
MBoegers/oauth2-kotlin-lambda-dsl
Jan 28, 2026
Merged

Fix OAuth2ResourceServerLambdaDsl for Kotlin code#916
timtebeek merged 5 commits intomainfrom
MBoegers/oauth2-kotlin-lambda-dsl

Conversation

@MBoegers
Copy link
Contributor

@MBoegers MBoegers commented Jan 27, 2026

Summary

  • Fix recipe to correctly handle Kotlin configuration code
  • Prevent duplicate transformations when recipe runs multiple cycles
  • Enable the previously failing Kotlin test

Problem

The OAuth2ResourceServerLambdaDsl recipe was incorrectly transforming Kotlin code. When run on Kotlin files like:

server.jwt().jwkSetUri("...").and().opaqueToken().introspectionUri("...")

The recipe would mangle the output, nesting opaqueToken inside jwt's lambda and losing the jwkSetUri configuration. The root cause was that after the first transformation cycle, subsequent cycles would incorrectly detect already-transformed methods (with lambda arguments) as still applicable for transformation.

Solution

  1. Added a check in isApplicableMethod() to skip methods that already have a lambda argument, preventing duplicate transformations in subsequent recipe cycles
  2. Enhanced isAndMethod() with fallback type checking using the select's method return type for cases where Kotlin return types aren't fully resolved

Known Limitation

The generated lambdas use parenthesized syntax .jwt({jwt -> ...}) instead of Kotlin's idiomatic trailing lambda syntax .jwt { jwt -> ... }. This is because the recipe creates J.Lambda (Java AST) nodes which print with parentheses in Kotlin.

A future improvement would be to detect Kotlin source files and create K.FunctionLiteral nodes instead, producing idiomatic trailing lambda syntax. The current output is valid Kotlin and functionally correct, just not stylistically ideal.

Test plan

  • Existing Java tests pass (OAuth2ResourceServerLambdaDslTest.advanced())
  • Kotlin test now passes (OAuth2ResourceServerLambdaDslTest.Kotlin.preservesCustomJwtConfiguration())
  • Related recipes using ConvertToSecurityDslVisitor still work (HttpSecurityLambdaDsl, OAuth2ClientLambdaDsl, etc.)

Fixes moderneinc/customer-requests#1765

The recipe was incorrectly transforming Kotlin code when run multiple
times. After the first cycle transformed methods like jwt() and
opaqueToken() to lambda DSL style, subsequent cycles would incorrectly
detect them as applicable for transformation again because the method
type information didn't reflect the added lambda argument.

This caused the recipe to nest opaqueToken inside jwt's lambda and lose
the jwkSetUri configuration.

Changes:
- Add check in isApplicableMethod() to skip methods that already have a
  lambda argument, preventing duplicate transformations
- Enhance isAndMethod() with fallback type checking using the select's
  method return type for cases where return types aren't fully resolved
- Update test to remove @ExpectedToFail and match actual recipe output

Fixes moderneinc/customer-requests#1765
@MBoegers MBoegers marked this pull request as draft January 27, 2026 16:11
The Kotlin parser doesn't provide complete type information for
method invocations like server.jwt(), causing LST type validation
to fail. Other Kotlin tests in this file already use TypeValidation.none()
to work around this limitation.
@MBoegers MBoegers marked this pull request as ready for review January 28, 2026 11:11
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Jan 28, 2026
@timtebeek timtebeek merged commit f5c948d into main Jan 28, 2026
2 checks passed
@timtebeek timtebeek deleted the MBoegers/oauth2-kotlin-lambda-dsl branch January 28, 2026 11:22
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants