Skip to content

fix: write whole lists in rune_serialize instead of raising Attribute… - #71

Merged
dschwartznyc merged 1 commit into
mainfrom
fix-serialization-error
Nov 11, 2026
Merged

dschwartznyc merged 1 commit into
mainfrom
fix-serialization-error

Conversation

@dschwartznyc

Copy link
Copy Markdown
Contributor

Write whole lists in rune_serialize instead of raising AttributeError

Fixes bug 4 in #42.

Problem

rune_serialize() failed on any list attribute whose serializer wraps the whole list:

PydanticSerializationError: Error serializing to JSON: PydanticSerializationError:
Error calling function serialise: AttributeError: 'list' object has no attribute 'serialise_meta'

The generator puts the serializer and validator around the whole list for a list attribute with metadata, such as parties Party (0..*) where Party has [metadata key], and for every list attribute in a generated bundle. The list then reaches serialise in a single call, and was treated as one object. Reading the same data already worked, because deserialize handles a list.

Change

In src/rune/runtime/metadata.py, the serializers for complex types, basic types with metadata, and enums with metadata:

  • write a whole list item by item, each with its own metadata;
  • write None as None;
  • for complex types, write a reference in a list, resolved or not, as {"@ref": ...};
  • declare return_type=Any, since they can now return a list.

For an enum with metadata, a single value is still written with handler(obj.enum_instance), as on main. The handler of a list attribute serializes the list's type, so it can't be used for one item; each item in a list writes its enum value directly.

New test/test_list_cardinality_serialise.py, 9 tests, using the classes rune-python-generator produces, in the form used in a generated bundle. It covers lists of objects, objects with keys, strings with metadata and enums with metadata, an empty list, an absent list, a list built in Python, an item of a subtype written with its @type, and a list of references written back as references.

Testing

  • test/test_list_cardinality_serialise.py: all 9 pass. Run against main without this change, 8 of the 9 fail.
  • Full suite in a clean environment (pip install .[dev] plus pytest, pytest-cov and pytest-mock, as in run-tests.yml): 440 passed, 20 skipped. The skips are the tests that need the generated serialization package or a CDM package.
  • With the serialization package generated from rune-common's round-trip suite installed, test/serializer-round-trip/test_metascheme.py::test_enum_list now passes. The 2 remaining failures there, in test_extension.py, are the subtype problem in Assigning an object to an attribute should store a copy that keeps its actual type #66.
  • ruff check passes on metadata.py.

Notes

  • Python mode: in model_dump(), a single enum with metadata gives an Enum member in @data, and an item in a list gives the enum's value as a string. JSON output is the same for both.
  • Choices: a list of choices in Java's flattened @type format needs fix-match-java-choice-serialization as well, and isn't tested here. Both branches change ComplexTypeMetaDataMixin.serialise, so whichever merges second needs that function merged by hand.
  • Related: Bug: resolve_references does not resolve references held in list fields #43, where references in lists aren't resolved when read. With this change, they are at least written back as references.

@github-actions

Copy link
Copy Markdown

Test Results

460 tests  +9   440 ✅ +9   1s ⏱️ ±0s
  1 suites ±0    20 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 06b677a. ± Comparison against base commit 6cd39ce.

@dschwartznyc
dschwartznyc merged commit 06b677a into main Nov 11, 2026
6 checks passed
@dschwartznyc
dschwartznyc deleted the fix-serialization-error branch November 12, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants