Fix keyOf to handle unresolved type variables#3675
Merged
Conversation
The recent change to use TypeLiteral<T>() in keyOf breaks callers that pass generic types with non-reified type parameters from enclosing classes (e.g., keyOf<DecodingBatchCloudEventReceiver<T>>() where T is a class type parameter). TypeLiteral captures the unresolved TypeVariable, causing Guice to reject it with KeyNotFullySpecified. This adds a runtime check: if the TypeLiteral's type contains any TypeVariable, fall back to the old T::class.java approach. Fully specified generic types still benefit from the TypeLiteral improvement.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a regression in keyOf<T>() introduced in PR #3668, where changing from T::class.java to TypeLiteral broke modules that use generic class type parameters (non-reified). The fix adds runtime detection of unresolved TypeVariables and falls back to the erased class when needed, while still preserving full generic type information for fully-specified types.
Changes:
- Modified
keyOfto conditionally useTypeLiteralor erased class based on whether unresolved type variables are present - Added
containsTypeVariable()extension function onTypeto recursively detectTypeVariableinstances - Added comprehensive test coverage for both the fix and the helper function
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| misk-inject/src/main/kotlin/misk/inject/Guice.kt | Implements conditional type handling in keyOf and adds containsTypeVariable() helper function |
| misk-inject/src/test/kotlin/misk/inject/KeyOfTest.kt | Comprehensive test suite covering simple types, fully-specified generics, unresolved type variables, and the containsTypeVariable() function |
| misk-inject/api/misk-inject.api | Exposes new containsTypeVariable function as public API |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mateuszmrozewski
approved these changes
Feb 17, 2026
jvmakine
approved these changes
Feb 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
keyOf<T>()to fall back to the erased class (T::class.java) when the resolvedTypeLiteralcontains unresolvedTypeVariableskeyOf<List<String>>()) still useTypeLiteralfor proper generic type preservationProblem
PR #3668 changed
keyOffromKey.get(T::class.java, ...)toKey.get(object : TypeLiteral<T>() {}, ...). This breaks callers that pass generic types containing non-reified type parameters from enclosing classes, such as:Guice rejects the resulting key with
KeyNotFullySpecifiedbecauseDecodingBatchCloudEventReceiver<T>contains an unresolvedTypeVariable.This breaks
InjectorTestin cash-server's Yolodex service (and any other service usingBatchReceiverModule,SuspendingBatchReceiverModule, orSuspendingReceiverModule).Fix
Added a
containsTypeVariable()extension onjava.lang.reflect.Typethat recursively checks forTypeVariableinstances.keyOfnow checks theTypeLiteral's type — if it contains any type variables, it falls back to the old erased-class behavior. Otherwise, it uses theTypeLiteralfor full generic type preservation.Test plan
InjectorTestin cash-server Yolodex service fails with the current Misk version (2026.02.14.023725-1db1eee) — 3/3 tests fail withKeyNotFullySpecifiedlocal.propertiesinclude