Skip to content

Commit f1d502c

Browse files
committed
DynamoDB Enhanced Client Polymorphic Types Support
1 parent d1a5fe6 commit f1d502c

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

services-custom/dynamodb-enhanced/src/main/java/software/amazon/awssdk/enhanced/dynamodb/mapper/StaticPolymorphicTableSchema.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Map;
2525
import java.util.Optional;
2626
import java.util.function.Function;
27-
2827
import software.amazon.awssdk.annotations.SdkPublicApi;
2928
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter;
3029
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType;
@@ -255,9 +254,12 @@ public StaticPolymorphicTableSchema<T> build() {
255254
}
256255
}
257256

258-
// Sort subtypes: deeper (more specific) before shallower
257+
// Sort subtypes so that deeper subclasses (more specific) are checked first by resolveByInstance.
259258
List<StaticSubtype<? extends T>> ordered = new ArrayList<>(staticSubtypes);
260-
sortSubtypesMostSpecificFirst(ordered, root);
259+
ordered.sort((first, second) -> Integer.compare(
260+
inheritanceDepthFromRoot(second.tableSchema().itemType().rawClass(), root),
261+
inheritanceDepthFromRoot(first.tableSchema().itemType().rawClass(), root)
262+
));
261263

262264
return new StaticPolymorphicTableSchema<>(
263265
rootTableSchema,
@@ -269,18 +271,9 @@ public StaticPolymorphicTableSchema<T> build() {
269271
}
270272

271273
/**
272-
* Orders subtypes so that deeper subclasses (more specific) are checked first by resolveByInstance.
273-
*/
274-
private static <T> void sortSubtypesMostSpecificFirst(List<StaticSubtype<? extends T>> subs, Class<?> root) {
275-
subs.sort((first, second) -> Integer.compare(
276-
inheritanceDepthFromRoot(second.tableSchema().itemType().rawClass(), root),
277-
inheritanceDepthFromRoot(first.tableSchema().itemType().rawClass(), root)
278-
));
279-
}
280-
281-
/**
282-
* Counts how many superclass steps it takes to reach the given root. Example: if Manager extends Employee extends Person
283-
* (root), then Manager → depth 2, Employee → depth 1, Person → depth 0.
274+
* Counts how many superclass steps it takes to reach the given root.
275+
* Example: if Manager extends Employee extends Person (root), then:
276+
* Manager → depth 2, Employee → depth 1, Person → depth 0.
284277
*/
285278
private static int inheritanceDepthFromRoot(Class<?> type, Class<?> root) {
286279
int depth = 0;

0 commit comments

Comments
 (0)