Bug: resolve_references does not resolve references held in list fields
还没有人认领这个 Issue。
评估
调研方向
从 rune/runtime/base_data_class.py 的 resolve_references 开始:递归步骤已经处理列表字段,而引用解析步骤仅对属性值本身检查 isinstance(obj, (UnresolvedReference, Reference))。先运行 issue 中自包含的复现测试以确认失败,然后扩展解析步骤以遍历列表元素并按索引写回结果,绕过 Pydantic 的不可变性,方式与 _bind_property_to 对标量引用的处理相同。完成的标准是复现中的两个断言都通过,并且现有测试套件仍然通过。
由索引模型根据 Issue 内容生成。
描述
Bug Report
BaseDataClass.resolve_references only resolves references that are the direct value of a property. When a property holds a list, references within that list are silently skipped, leaving them as UnresolvedReference objects rather than the resolved instances.
Steps to Reproduce
-
Define a Rune model with a type that has a multi-cardinality reference field (i.e., a field typed as a list of another type, decorated with
@refmetadata):type Party: [metadata key] name string (1..1) type Contract: parties Party (2..*) [metadata reference] -
Generate Python from the model. The
partiesfield will be typed as a list with a reference validator:parties: Annotated[ list[Party | BaseReference | None], Party.serializer(), Party.validator(('@key', '@key:external', '@ref', '@ref:external')) ] = Field(..., min_length=2) -
Deserialize a JSON document where the list field contains
@refentries:{ "@type": "...", "party": [ {"identifier": [{"id": {"@data": "p1"}}], "name": "Buyer"}, {"identifier": [{"id": {"@data": "p2"}}], "name": "Seller"} ], "contract": { "@type": "...", "parties": [ {"@ref": "p1"}, {"@ref": "p2"} ] } } -
Call
BaseDataClass.rune_deserialize(json_str).
The following self-contained test reproduces the failure without a generator or CDM dependency:
from typing import Annotated, Optional
from pydantic import Field
from rune.runtime.base_data_class import BaseDataClass
from rune.runtime.metadata import BaseReference, UnresolvedReference
# --- minimal model ---
class Observation(BaseDataClass):
value: str = Field(...)
class Reset(BaseDataClass):
# single-cardinality reference — works correctly
primaryObservation: Optional[Annotated[
Observation,
Observation.serializer(),
Observation.validator(('@key', '@ref')),
]] = Field(None)
# multi-cardinality reference — broken
observations: Annotated[
list[Observation | BaseReference | None],
Observation.serializer(),
Observation.validator(('@key', '@ref')),
] = Field(default_factory=list)
# --- JSON: one object defines @key, the other field references it via @ref ---
data = {
'primaryObservation': {'@key': 'obs-1', 'value': '1.234'},
'observations': [
{'@ref': 'obs-1'}, # should resolve to the Observation above
],
}
model = Reset.rune_deserialize(data, validate_model=False)
# Scalar reference: resolved correctly
assert isinstance(model.primaryObservation, Observation), \
f'Expected Observation, got {type(model.primaryObservation)}'
# List reference: NOT resolved — this assertion FAILS
assert isinstance(model.observations[0], Observation), \
f'Expected Observation, got {type(model.observations[0])}'
# -> AssertionError: Expected Observation, got <class 'rune.runtime.metadata.UnresolvedReference'>
Expected Result
Reference resolution completes successfully. Each element of contract.parties is resolved to its corresponding Party instance — the same outcome as for a singular reference field.
Actual Result
The references in contract.parties are silently left as UnresolvedReference objects. No exception is raised during deserialization, but any attempt to use the field values fails at runtime because the elements are UnresolvedReference instances rather than Party instances.
Root cause: BaseDataClass.resolve_references (in base_data_class.py) iterates self.__dict__ and resolves properties whose value is directly an UnresolvedReference:
for prop_nm, obj in self.__dict__.items():
if isinstance(obj, (UnresolvedReference, Reference)): # <-- checks the property value
refs.append((prop_nm, obj.get_reference(self)))
For a list field, obj is the list itself — not an UnresolvedReference — so the condition is False and none of the references inside the list are ever resolved.
The earlier loop in the same method already handles lists correctly for the recursion step (finding BaseDataClass children to recurse into), but the reference-resolution step does not apply the same pattern.
Environment
- rune-python-runtime current main
- Observed during ingestion of CDM production samples
Additional Context
The fix requires iterating list elements in the resolution step, analogous to the existing recursion step:
for prop_nm, obj in self.__dict__.items():
if isinstance(obj, (UnresolvedReference, Reference)):
refs.append((prop_nm, obj.get_reference(self)))
elif isinstance(obj, (MutableSequence, tuple)):
for i, item in enumerate(obj):
if isinstance(item, (UnresolvedReference, Reference)):
list_refs.append((prop_nm, i, item.get_reference(self)))
Resolved items must then be written back into the list by index, and the binding must bypass Pydantic's immutability the same way the scalar case does via _bind_property_to.
- 主要语言
- Python
- 星标
- 0
- 派生
- 3
- 平均合并
- 8 小时 25 分钟
- 30 天内合并 PR
- 3
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
finos/rune-python-runtime 的其他 Issue
-
Serialisation fails with pydantic 2.14: wrap serialisers pass SerializationInfo as index_key可能已有人在做 @hugohills-regnosys 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
finos/rune-python-runtime#45 ·
-
难度 5/5 一周以上 新手友好度 35/100
finos/rune-python-runtime#42 ·
-
难度 3/5 1-2 天 新手友好度 62/100
finos/rune-python-runtime#27 ·
-
难度 5/5 一周以上 新手友好度 25/100
finos/rune-python-runtime#16 ·
-
难度 4/5 3-5 天 新手友好度 35/100
finos/rune-python-runtime#15 · 1 条评论 ·
查看 finos/rune-python-runtime 的全部 Issue
相似的 Issue
-
vis.image() reports "Unsupported number of image channels: 224" for an HxWxC image可能已有人在做 @piyush182004 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 62/100
fossasia/visdom#1997 · 3 条评论 ·
维护者通常 6 天内回复
-
Byse new domain未关闭
难度 2/5 1-3 小时 新手友好度 62/100
Gujal00/ResolveURL#1525 ·
维护者通常 1 天内回复
-
[Bug]: LangGraph Store sync put bypasses namespace validation可能已有人在做 @ktz03 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
volcengine/OpenViking#6050 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
hummingbot/condor#283 ·
维护者通常 1 天内回复
-
curriculum documentation quality
难度 2/5 1-3 小时 新手友好度 78/100
githubnext/gh-aw-workshop#4571 ·
维护者通常 1 天内回复