Bug: resolve_references does not resolve references held in list fields
まだ誰も着手していません。
評価
調査の方向性
rune/runtime/base_data_class.py の resolve_references から着手する。再帰ステップはすでにリストフィールドを処理しているが、参照解決ステップはプロパティ値自体に対して isinstance(obj, (UnresolvedReference, Reference)) を確認するだけである。まず issue に添付された自己完結の再現テストを実行して失敗を確認し、その後、解決ステップを拡張してリスト要素を反復処理し、結果をインデックスで書き戻すようにする。スカラー参照に対して _bind_property_to が行うのと同様に Pydantic の不変性を回避すること。再現テストの両アサーションが通り、既存のテストスートも引き続き通ることで完了とする。
索引モデルが 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分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
finos/rune-python-runtime のほかの issue
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
finos/rune-python-runtime#42 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 62/100
finos/rune-python-runtime#27 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 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
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
KalicoCrew/kalico#1012 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
esphome/esphome.io#7657 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
VNG-Realisatie/gemma-zaken#2658 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
comp/gateway comp/plugins duplicate P3 type/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
NousResearch/hermes-agent#137019 ·
メンテナーはふだん 1 日以内に返信