babyfish-ct / jimmer

A revolutionary ORM framework for both java and kotlin.
Apache License 2.0
876 stars 88 forks source link

TransientResolver未正确覆写getDefaultValue时的报错不明确 #792

Closed SWQXDBA closed 2 days ago

SWQXDBA commented 5 days ago

TransientResolver要求对非空属性返回一个非null 的getDefaultValue实现 但是如果没有提供,会在源码中直接写入一个 Collections.emptyList()

在AbstractDataLoader.java中


   private Map<Object, Object> resolveWithDefaultValue(
            TransientResolver<Object, Object> resolver,
            Collection<Object> ids
    ) {
        Map<Object, Object> valueMap = resolver.resolve(ids);
        if (valueMap.keySet().containsAll(ids)) {
            return valueMap;
        }
        Object defaultValue = resolver.getDefaultValue();
        if (defaultValue == null || (prop.isReferenceList(TargetLevel.OBJECT))) {
            defaultValue = Collections.emptyList();
        }
        for (Object id : ids) {
            if (!valueMap.containsKey(id)) {
                valueMap.put(id, defaultValue);
            }
        }
        return valueMap;
    }

这可能是不合适的行为 因为会抛出这样的报错提示

class java.util.Collections$EmptyList cannot be cast to class java.lang.Integer (java.util.Collections$EmptyList and java.lang.Integer are in module java.base of loader 'bootstrap') java.lang.ClassCastException: class java.util.Collections$EmptyList cannot be cast to class java.lang.Integer (java.util.Collections$EmptyList and java.lang.Integer are in module java.base of loader 'bootstrap') at com.xxx.xxx.modules.task.TaskDraft$$$DraftImpl.__set(TaskDraft.kt:1591) at org.babyfish.jimmer.sql.fetcher.impl.FetcherTask.setDraftProp(FetcherTask.java:207)

实际上会走到draft的set属性的地方遇到强制类型转换时再抛出错误 QQ_1732090707299

babyfish-ct commented 2 days ago

Try 0.9.20