Fix(core): Prevent redundant schema resolution by fixing AnnotatedType equality #4975
+214
−39
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.
Pull Request
Description
This pull request resolves a performance issue where the ModelConverterContext fails to cache AnnotatedType instances, leading to redundant schema resolutions.
The root cause was a faulty equals() and hashCode() implementation in the AnnotatedType class. These methods incorrectly included contextual fields (e.g., propertyName), causing logically identical types to be treated as different objects by the cache (processedTypes Set). This bug resulted in the same types being processed multiple times during schema generation, causing significant performance degradation.
This commit corrects the equals() and hashCode() implementations to only consider fields that define a type's intrinsic identity, ensuring the cache functions as expected.
Closes: #4965
Type of Change
Checklist
Screenshots / Additional Context
Test Case Explanation:
A new test, AnnotatedTypeCachingTest.java, has been added to reproduce the bug and verify the fix.
Why a DummyModelConverter is used: The standard ModelResolver has internal optimizations for simple types like String, which prevents it from recursively calling the ModelConverterContext. This interferes with testing the context's caching behavior. To solve this, the test uses a DummyModelConverter that simulates the recursive resolution of a model in a controlled environment, allowing us to test the caching logic in isolation.
How it Works:
Before the fix: The test fails because the faulty equals() implementation causes multiple AnnotatedType instances for the String type to be added to the cache. The assertion assertEquals(stringTypeCount, 1) fails because the actual count is greater than 1.
After the fix: The test passes because the corrected equals() implementation ensures that only one AnnotatedType instance for the String type is stored in the cache. The assertion assertEquals(stringTypeCount, 1) succeeds.
This test provides clear, automated proof that the caching issue is resolved.