Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Bug: resolve_references does not resolve references held in list fields

Đang mở Phù hợp với người mới
#43 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
84/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
data

Hướng nghiên cứu

Bắt đầu tại rune/runtime/base_data_class.py, trong resolve_references: bước đệ quy đã xử lý các trường danh sách, trong khi bước phân giải tham chiếu chỉ kiểm tra isinstance(obj, (UnresolvedReference, Reference)) trên chính giá trị property. Chạy trước test tái tạo tự chứa trong issue để xác nhận lỗi, sau đó mở rộng bước phân giải để duyệt qua các phần tử danh sách và ghi kết quả lại theo index, vượt qua tính bất biến của Pydantic theo cách _bind_property_to làm với các tham chiếu scalar. Hoàn thành nghĩa là cả hai assertion trong bản tái tạo đều pass, và bộ test hiện có vẫn pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
  1. 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 @ref metadata):

    type Party:
        [metadata key]
        name string (1..1)
    
    type Contract:
        parties Party (2..*)
            [metadata reference]
    
  2. Generate Python from the model. The parties field 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)
    
  3. Deserialize a JSON document where the list field contains @ref entries:

    {
      "@type": "...",
      "party": [
        {"identifier": [{"id": {"@data": "p1"}}], "name": "Buyer"},
        {"identifier": [{"id": {"@data": "p2"}}], "name": "Seller"}
      ],
      "contract": {
        "@type": "...",
        "parties": [
          {"@ref": "p1"},
          {"@ref": "p2"}
        ]
      }
    }
    
  4. 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.

Ngôn ngữ chính
Python
Star
0
Fork
3
Merge trung bình
8 giờ 25 phút
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của finos/rune-python-runtime

Tất cả issue của finos/rune-python-runtime

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.