chore: simplify TristateModule type-resolution and serializer wiring#184
Merged
Conversation
Extract the repeated JavaType-resolution boilerplate in the Tristate deserializer path into three file-private helpers — ANY_TYPE (the Object fallback for raw/Tristate<*>), JavaType.firstContainedOrNull(), and JavaType.isTristate() — so the inner-type lookup reads the same way at each of its call sites instead of being open-coded three times. Drop the no-op ContextualSerializer from TristateSerializer: its createContextual just returned this, which is how Jackson treats a non-contextual serializer anyway. Payload typing is already resolved per value through the dynamic PropertySerializerMap, so there is no per-property type to capture. The deserializer's ContextualDeserializer is genuine and stays. Rewrite TristateSerializerModifier.changeProperties with List.replaceAll so the wrap-or-keep decision is a single expression rather than an indexed loop mutating the list as it iterates. All three are behavior-preserving; every type involved is internal, so there is no public-API change.
Relocate ANY_TYPE, JavaType.firstContainedOrNull(), and JavaType.isTristate() out of TristateModule.kt into a dedicated Extensions.kt, and drop the now-unused TypeFactory import from TristateModule.kt. The helpers stay internal — they are module-private implementation details, not public API.
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
Three behavior-preserving cleanups to
TristateModule.ktinsdk-serde-jackson. They cluster around the same file — repeatedJavaTyperesolution boilerplate and a serializer hook that earns nothing.The
Tristate<T>round-trip contract is untouched:Absentomits the key,Nullserializes as JSONnull,Present(v)serializes asv, and inner-type resolution is identical. Every class involved isinternal, so there is no public-API change andapiCheckis unaffected.Changes
Extract
ANY_TYPE,firstContainedOrNull(),isTristate()helpers. The deserializer side resolvedJavaTypes in three places (findBeanDeserializer,createContextual,deserialize), each repeating the same trio: theTristateassignability guard, the "first contained type or fall back toObject" lookup, and theconstructType(Object)fallback. Naming each step once removes the copy-paste and makes the type-resolution intent obvious at every call site.Drop the no-op
ContextualSerializerfromTristateSerializer. ItscreateContextualjust returnedthis, which is exactly how Jackson treats a non-contextual serializer. The serializer already resolves the payload type per value through itsPropertySerializerMap, so there is no per-property type to capture at contextualization time. The unused import is removed in the same change. The deserializer'sContextualDeserializeris genuine (it captures the property'sT) and stays.Rewrite
changePropertieswithList.replaceAll. The wrap-or-keep decision now reads as one expression instead of aforEachIndexedloop that mutates the list it is iterating.Verification
./gradlew :sdk-serde-jackson:buildpasses — tests, ktlint, detekt, andapiCheckall green.Closes #181