Fix KSP type extraction for Comparable and Number types with @Convert #1453
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.
resolves #1452
Additional context
Root Cause:
The issue stems from a fundamental difference in how Java APT and KSP code generators handle type extraction:
Java APT behavior: The APT implementation (
ExtendedTypeFactory) does not consider the presence of@Convertannotations when determining path types. It solely analyzes the declared type's hierarchy (e.g., whether it implementsComparableor extendsNumber) to determine the appropriate path type.KSP behavior (before fix): The KSP
TypeExtractorhad auserType()method that checked for annotations including@Convert,@Type, and@JdbcTypeCode. When these annotations were present, the method would returnUnknowntype, which was then rendered asSimplePath<T>.Execution order issue: The
fallbackType()method inTypeExtractorcontains logic to check if a type implementsComparableor extendsNumber. Additionally, thereferenceType()method handles enum types by generatingEnumPath<T>. However, when@Convertannotation was present, theuserType()method was called first in the type extraction chain (beforefallbackType()andreferenceType()), causing it to returnUnknowntype immediately. This prevented the type hierarchy analysis infallbackType()and enum detection inreferenceType()from ever being executed, resulting in:SimplePath<T>being generated instead of the correctComparablePath<T>orNumberPath<T>for Comparable/Number typesSimplePath<T>being generated instead ofEnumPath<T>for enum types with@Convertannotation (as documented in PR #1411)The removal of the
userType()method resolves both issues, allowing proper type detection based on type hierarchy and enum identification.The Fix:
The fix removes the annotation-based type detection (
userType()method) fromTypeExtractor, allowing the type extraction logic to proceed directly to type hierarchy analysis. This ensures that:Comparableare correctly identified and generateComparablePath<T>(e.g.,YearMonth)Numberand implementingComparableare correctly identified and generateNumberPath<T>(e.g.,Money)EnumPath<T>(even when@Convertannotation is present), enabling enum-specific operations likecoalesce()Related Pull Requests:
fallbackType()method to handle unknown types by checking if they implementComparable(generatesComparablePath) or fallback toSimplePathotherwise.userType()method to support@JdbcTypeCodeannotation.@Convertannotation generatingSimplePathinstead ofEnumPathby removing theuserType()method that was preventing proper enum detection inreferenceType().