From e9df59694146d0b919e9e037eb7469f9498d5f49 Mon Sep 17 00:00:00 2001 From: dschwartznyc Date: Tue, 10 Nov 2026 15:50:41 -0500 Subject: [PATCH 1/2] fix: read and write choice types in Java's flattened @type format --- src/rune/runtime/base_data_class.py | 120 ++++++- src/rune/runtime/metadata.py | 27 +- test/test_choice_serialization.py | 539 ++++++++++++++++++++++++++++ 3 files changed, 682 insertions(+), 4 deletions(-) create mode 100644 test/test_choice_serialization.py diff --git a/src/rune/runtime/base_data_class.py b/src/rune/runtime/base_data_class.py index f405e8c..51c61b2 100644 --- a/src/rune/runtime/base_data_class.py +++ b/src/rune/runtime/base_data_class.py @@ -1,4 +1,5 @@ '''Base class for all Rune type classes''' +import contextvars import copy import importlib import json @@ -47,9 +48,27 @@ def _annotation_contains(annotation: Any, base_types: tuple[type, ...], *, unkno if isinstance(annotation, type) and issubclass(annotation, base_types): return True return any(_annotation_contains(arg, base_types, unknown=unknown) for arg in get_args(annotation)) +_CONSTRUCTING = contextvars.ContextVar('rune_constructing', default=False) -class BaseDataClass(BaseModel, ComplexTypeMetaDataMixin): +class _RuneModelMeta(type(BaseModel)): + '''Records that an object is being built directly in Python. + + The validator of a choice can't otherwise tell `ChoiceBasic(string='x')` + from the JSON `{"string": "x"}`. This has to be done here: defining + `__init__` on the model would make pydantic build nested models through + it, so that JSON would look like direct construction as well. + ''' + def __call__(cls, *args, **kwargs): + token = _CONSTRUCTING.set(True) + try: + return super().__call__(*args, **kwargs) + finally: + _CONSTRUCTING.reset(token) + + +class BaseDataClass(BaseModel, ComplexTypeMetaDataMixin, + metaclass=_RuneModelMeta): ''' A base class for all cdm generated classes. It is derived from `pydantic.BaseModel` which provides type checking at object creation for all cdm classes. It provides as well the `validate_model`, @@ -208,6 +227,8 @@ def _serialize_refs(self, serializer, info): root_metadata = self.__dict__.get(ROOT_CONTAINER) if not refs and not reference_fields: result = serializer(self) + if self._is_rune_choice(): + result = self._flatten_choice(result) return root_metadata | result if root_metadata else result updates = {} list_fields = {} @@ -263,6 +284,8 @@ def _serialize_refs(self, serializer, info): key, key_type = reference written[position] = {key_type.rune_ref_tag: key} res[output_name] = written + if self._is_rune_choice(): + res = self._flatten_choice(res) return root_metadata | res if root_metadata else res @staticmethod @@ -284,6 +307,99 @@ def _serialized_field_name(self, name: str, info) -> str: field = type(self).model_fields[name] return (field.serialization_alias or name) if by_alias else name + @classmethod + def _is_rune_choice(cls) -> bool: + '''True for a class generated from a Rune `choice`.''' + return getattr(cls, '_RUNE_CHOICE', False) + + @classmethod + def _option_class(cls, name: str): + '''The data class declared for a choice option. None if the option + is a basic type or an enum.''' + def find(tp): + if isinstance(tp, type): + return tp if issubclass(tp, BaseDataClass) else None + for arg in get_args(tp): + if found := find(arg): + return found + return None + return find(cls.model_fields[name].annotation) + + def _flatten_choice(self, res: dict) -> dict: + '''Rewrites a serialized choice in the Rune JSON format: `@type` names + the selected option and its fields sit directly on the choice.''' + selected = [name for name in type(self).model_fields + if self.__dict__.get(name) is not None] + if not selected: + return {} + if len(selected) > 1: + raise ValueError(f'Multiple choice options selected for ' + f'{self._fqrtn()}: {selected}') + name = selected[0] + value = self.__dict__[name] + option = res[name] + declared = self._option_class(name) + if (isinstance(value, BaseDataClass) and declared is not None + and type(value) is not declared): + raise ValueError(f'Choice option {name} of {self._fqrtn()} holds ' + f'{type(value)}, but must be exactly {declared}') + option_cls = type(value) if isinstance(value, BaseDataClass) else declared + if option_cls is None: # basic type or enum + if isinstance(option, dict) and '@data' in option: + return {'@type': name} | option + return {'@type': name, '@data': option} + # pylint: disable=protected-access + if option_cls._is_rune_choice(): + # A nested choice is already flat, unless the option has metadata + # or is a reference + if '@data' in option or any(k.startswith('@ref') for k in option): + return {'@type': option_cls._fqrtn()} | option + return option + return {'@type': option_cls._fqrtn()} | { + k: v for k, v in option.items() if k != '@type'} + + @classmethod + def _choice_has_option(cls, rune_type: str) -> bool: + '''True if `rune_type` is an option of this choice, or of a choice + nested in it.''' + # pylint: disable=protected-access + for name in cls.model_fields: + option = cls._option_class(name) + if option is None: + if name == rune_type: + return True + elif option._fqrtn() == rune_type or ( + option._is_rune_choice() and option._choice_has_option(rune_type)): + return True + return False + + @classmethod + def _choice_to_fields(cls, data: dict) -> dict: + '''Converts a choice in the Rune JSON format to this class's fields, + with the option selected by `@type`.''' + if '@type' not in data: + if any(k.startswith('@ref') for k in data): + return data # a reference to a choice + if '@data' in data: + metadata = {k: v for k, v in data.items() if k != '@data'} + return metadata | cls._choice_to_fields(data['@data']) + raise ValueError(f"Missing required '@type' for choice " + f'{cls._fqrtn()}') + rune_type = data['@type'] + body = {k: v for k, v in data.items() if k != '@type'} + # pylint: disable=protected-access + for name in cls.model_fields: + option = cls._option_class(name) + if option is None: + if name == rune_type: + return {name: body['@data']} + elif option._fqrtn() == rune_type: + return {name: body} + elif option._is_rune_choice() and option._choice_has_option(rune_type): + return {name: data} + raise ValueError(f"'{rune_type}' is not an option of choice " + f'{cls._fqrtn()}') + @model_validator(mode='wrap') @classmethod def _deserialize_refs(cls, data: Any, @@ -291,6 +407,8 @@ def _deserialize_refs(cls, data: Any, '''should resolve refs after creation''' metadata = {} if isinstance(data, dict): + if cls._is_rune_choice() and not _CONSTRUCTING.get(): + data = cls._choice_to_fields(data) metadata = {k: v for k, v in data.items() if k.startswith('@')} if aux := cls._create_unresolved_ref(metadata): return aux # type: ignore diff --git a/src/rune/runtime/metadata.py b/src/rune/runtime/metadata.py index bb26a3b..3d5406c 100644 --- a/src/rune/runtime/metadata.py +++ b/src/rune/runtime/metadata.py @@ -479,7 +479,11 @@ def serialise(cls, obj) -> dict | list: if isinstance(obj, list): return [None if item is None else cls.serialise(item) for item in obj] res = obj.serialise_meta() - res |= obj.model_dump(exclude_unset=True, exclude_defaults=True) + dumped = obj.model_dump(exclude_unset=True, exclude_defaults=True) + if obj._is_rune_choice(): # pylint: disable=protected-access + # Metadata of the attribute sits beside the flattened choice + return res | {'@data': dumped} if res else dumped + res |= dumped if cls != obj.__class__: # pylint: disable=protected-access # res = {'@type': obj._FQRTN} | res @@ -511,10 +515,27 @@ def deserialize(cls, obj, allowed_meta: set[str]): for k, v in obj.items(): (metadata if k.startswith('@') else fields)[k] = v - # References deserialization treatment - if aux := cls._create_unresolved_ref(metadata): + # pylint: disable=protected-access + # References deserialization treatment. A reference beside `@type` + # belongs to the selected choice option, not to this attribute. + is_option_of_choice = cls._is_rune_choice() and '@type' in metadata + if not is_option_of_choice and (aux := cls._create_unresolved_ref(metadata)): return aux + if cls._is_rune_choice(): + if '@data' in metadata and '@type' not in metadata: + # Metadata of the attribute, beside the flattened choice + model = cls.model_validate(metadata.pop('@data')) + else: + # `@type` selects the option, and any metadata is the option's + model = cls.model_validate(obj) + metadata = {} + model.__dict__[META_CONTAINER] = metadata + if cls.meta_checks_enabled(): + model._init_meta(allowed_meta) + model._register_keys(metadata) + return model + # Model creation rune_cls = cls._type_to_cls(metadata) if rune_cls != cls and not issubclass(rune_cls, cls): diff --git a/test/test_choice_serialization.py b/test/test_choice_serialization.py new file mode 100644 index 0000000..bdfc928 --- /dev/null +++ b/test/test_choice_serialization.py @@ -0,0 +1,539 @@ +'''Rune choice types are read and written in the Rune JSON format. + +The selected option is named by `@type` and its fields sit directly on the +choice, as in rune-common's `RuneChoiceTypeSerializer` and +`RuneChoiceTypeDeserializer`. The nested form, `{"A": {...}}`, is not accepted. + +Testing the following Rune definitions: + +type A: + fieldA string (1..1) + +type ExtA extends A: + fieldExt string (1..1) + +type B: + fieldB string (1..1) + +type Located: + fieldL string (1..1) + +type Keyed: + [metadata key] + fieldK string (1..1) + +type PaymentDate: + [metadata key] + day string (1..1) + +type WithDate: + paymentDate PaymentDate (0..1) + +choice ChoiceData: + A + B + +choice ChoiceBasic: + string + int + +choice ChoiceNested: + ChoiceData + ChoiceBasic + +choice ChoiceDeep: + ChoiceNested + +choice ChoiceWithMeta: + Located + [metadata location] + Keyed + [metadata reference] + +choice ChoiceLocatedOption: + ChoiceData + [metadata location] + string + +choice ChoiceRefOption: + ChoiceData + [metadata address "pointsTo"=Root->located] + B + +choice ChoiceDated: + WithDate + B + +type Root: + data ChoiceData (0..1) + basic ChoiceBasic (0..1) + nested ChoiceNested (0..1) + deep ChoiceDeep (0..1) + entries ChoiceData (0..*) + located ChoiceData (0..1) + [metadata location] + withMeta ChoiceWithMeta (0..1) + locatedOption ChoiceLocatedOption (0..1) + dated ChoiceDated (0..1) + dateRef PaymentDate (0..1) + [metadata reference] + keyed Keyed (0..1) + refOption ChoiceRefOption (0..1) + withMetaHooked ChoiceWithMeta (0..1) + choiceRef ChoiceData (0..1) + [metadata address "pointsTo"=Root->located] + +withMetaHooked has the runtime's serializer and validator although it has no +metadata, as every field of a class in the generated bundle does. +''' +import json +from typing import Annotated, ClassVar, Optional + +import pytest +from pydantic import Field + +from rune.runtime.base_data_class import BaseDataClass +from rune.runtime.metadata import BaseReference + +NS = 'test_choice_serialization' + + +class A(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.A' + fieldA: str = Field(..., description='') + + +class ExtA(A): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.ExtA' + fieldExt: str = Field(..., description='') + + +class B(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.B' + fieldB: str = Field(..., description='') + + +class Located(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.Located' + fieldL: str = Field(..., description='') + + +class Keyed(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.Keyed' + _ALLOWED_METADATA = {'@key', '@key:external'} + fieldK: str = Field(..., description='') + + +class PaymentDate(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.PaymentDate' + _ALLOWED_METADATA = {'@key', '@key:external'} + day: str = Field(..., description='') + + +# As in generated code: an attribute named after its type needs an alias, +# or the annotation would resolve to the attribute itself. +_A, _B, _Located, _Keyed, _int = A, B, Located, Keyed, int + +KEYS = ('@key', '@key:external') +REFS = ('@key', '@key:external', '@ref', '@ref:external') + + +class WithDate(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.WithDate' + paymentDate: Annotated[Optional[PaymentDate], PaymentDate.serializer(), + PaymentDate.validator(KEYS)] = Field(None) + + +_WithDate = WithDate + + +class ChoiceData(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.ChoiceData' + _RUNE_CHOICE: ClassVar[bool] = True + A: Optional[_A] = Field(None) + B: Optional[_B] = Field(None) + + +_ChoiceData = ChoiceData + + +class ChoiceBasic(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.ChoiceBasic' + _RUNE_CHOICE: ClassVar[bool] = True + string: Optional[str] = Field(None) + int: Optional[_int] = Field(None) + + +_ChoiceBasic = ChoiceBasic + + +class ChoiceNested(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.ChoiceNested' + _RUNE_CHOICE: ClassVar[bool] = True + ChoiceData: Optional[_ChoiceData] = Field(None) + ChoiceBasic: Optional[_ChoiceBasic] = Field(None) + + +_ChoiceNested = ChoiceNested + + +class ChoiceDeep(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.ChoiceDeep' + _RUNE_CHOICE: ClassVar[bool] = True + ChoiceNested: Optional[_ChoiceNested] = Field(None) + + +class ChoiceWithMeta(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.ChoiceWithMeta' + _RUNE_CHOICE: ClassVar[bool] = True + Located: Annotated[Optional[_Located], _Located.serializer(), + _Located.validator(('@key:scoped', ))] = Field(None) + Keyed: Annotated[Optional[_Keyed | BaseReference], _Keyed.serializer(), + _Keyed.validator(REFS)] = Field(None) + _KEY_REF_CONSTRAINTS = { + 'Located': {'@key:scoped'}, + 'Keyed': {'@ref', '@ref:external'} + } + + +class ChoiceLocatedOption(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.ChoiceLocatedOption' + _RUNE_CHOICE: ClassVar[bool] = True + ChoiceData: Annotated[Optional[_ChoiceData], _ChoiceData.serializer(), + _ChoiceData.validator(('@key:scoped', ))] = Field(None) + string: Optional[str] = Field(None) + _KEY_REF_CONSTRAINTS = {'ChoiceData': {'@key:scoped'}} + + +class ChoiceRefOption(BaseDataClass): + '''An option that is itself a choice, and is a reference.''' + _FQRTN: ClassVar[str] = f'{NS}.ChoiceRefOption' + _RUNE_CHOICE: ClassVar[bool] = True + ChoiceData: Annotated[Optional[_ChoiceData | BaseReference], + _ChoiceData.serializer(), + _ChoiceData.validator(('@ref:scoped', ))] = Field(None) + B: Optional[_B] = Field(None) + _KEY_REF_CONSTRAINTS = {'ChoiceData': {'@ref:scoped'}} + + +class ChoiceDated(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.ChoiceDated' + _RUNE_CHOICE: ClassVar[bool] = True + WithDate: Optional[_WithDate] = Field(None) + B: Optional[_B] = Field(None) + + +class OneOfHolder(BaseDataClass): + '''A type with optional attributes is not a choice: it has no marker.''' + _FQRTN: ClassVar[str] = f'{NS}.OneOfHolder' + A: Optional[_A] = Field(None) + B: Optional[_B] = Field(None) + + +class Root(BaseDataClass): + '''no doc''' + _FQRTN: ClassVar[str] = f'{NS}.Root' + data: Optional[ChoiceData] = Field(None) + basic: Optional[ChoiceBasic] = Field(None) + nested: Optional[ChoiceNested] = Field(None) + deep: Optional[ChoiceDeep] = Field(None) + entries: Optional[list[ChoiceData]] = Field(None) + located: Annotated[Optional[ChoiceData], _ChoiceData.serializer(), + _ChoiceData.validator(('@key:scoped', ))] = Field(None) + withMeta: Optional[ChoiceWithMeta] = Field(None) + locatedOption: Optional[ChoiceLocatedOption] = Field(None) + dated: Optional[ChoiceDated] = Field(None) + dateRef: Annotated[Optional[PaymentDate | BaseReference], + PaymentDate.serializer(), + PaymentDate.validator(REFS)] = Field(None) + keyed: Annotated[Optional[Keyed], _Keyed.serializer(), + _Keyed.validator(KEYS)] = Field(None) + holder: Optional[OneOfHolder] = Field(None) + withMetaHooked: Annotated[Optional[ChoiceWithMeta], + ChoiceWithMeta.serializer(), + ChoiceWithMeta.validator()] = Field(None) + refOption: Annotated[Optional[ChoiceRefOption], ChoiceRefOption.serializer(), + ChoiceRefOption.validator()] = Field(None) + choiceRef: Annotated[Optional[ChoiceData | BaseReference], + _ChoiceData.serializer(), + _ChoiceData.validator(('@ref:scoped', ))] = Field(None) + _KEY_REF_CONSTRAINTS = { + 'located': {'@key:scoped'}, + 'dateRef': {'@ref', '@ref:external'}, + 'choiceRef': {'@ref:scoped'} + } + + +def _written(model: BaseDataClass, attribute: str): + '''what a model writes for one of its attributes''' + return json.loads(model.rune_serialize(validate_model=False))[attribute] + + +A_JSON = {'@type': f'{NS}.A', 'fieldA': 'foo'} +B_JSON = {'@type': f'{NS}.B', 'fieldB': 'bar'} + + +# --------------------------------------------------------------------------- +# Writing +# --------------------------------------------------------------------------- + +@pytest.mark.parametrize('model, attribute, expected', [ + pytest.param(Root(data=ChoiceData(A=A(fieldA='foo'))), + 'data', A_JSON, id='data-option'), + pytest.param(Root(data=ChoiceData(B=B(fieldB='bar'))), + 'data', B_JSON, id='other-data-option'), + pytest.param(Root(basic=ChoiceBasic(string='foo')), + 'basic', {'@type': 'string', '@data': 'foo'}, id='string'), + pytest.param(Root(basic=ChoiceBasic(int=10)), + 'basic', {'@type': 'int', '@data': 10}, id='int'), + pytest.param(Root(nested=ChoiceNested( + ChoiceData=ChoiceData(A=A(fieldA='foo')))), + 'nested', A_JSON, id='nested-choice'), + pytest.param(Root(nested=ChoiceNested( + ChoiceBasic=ChoiceBasic(string='foo'))), + 'nested', {'@type': 'string', '@data': 'foo'}, + id='nested-basic'), + pytest.param(Root(deep=ChoiceDeep(ChoiceNested=ChoiceNested( + ChoiceData=ChoiceData(B=B(fieldB='bar'))))), + 'deep', B_JSON, id='deep-nested-choice'), + pytest.param(Root(entries=[ChoiceData(A=A(fieldA='foo')), + ChoiceData(B=B(fieldB='bar'))]), + 'entries', [A_JSON, B_JSON], id='list'), +]) +def test_write_choice(model, attribute, expected): + '''the selected option is written with @type, on the choice itself''' + assert _written(model, attribute) == expected + + +@pytest.mark.parametrize('attribute, value', [ + pytest.param('located', + {'@key:scoped': 'outer', '@data': A_JSON}, + id='choice-attribute-with-metadata'), + pytest.param('withMeta', + {'@type': f'{NS}.Located', '@key:scoped': 'loc', 'fieldL': 'x'}, + id='option-with-location'), + pytest.param('locatedOption', + {'@type': f'{NS}.ChoiceData', '@key:scoped': 'inner', + '@data': A_JSON}, + id='nested-choice-option-with-metadata'), +]) +def test_round_trip_with_metadata(attribute, value): + '''metadata is kept, and written where Java writes it''' + model = Root.rune_deserialize({attribute: value}, validate_model=False) + assert _written(model, attribute) == value + + +def test_round_trip_option_is_a_reference(): + '''an option that is a reference is written with @type and the reference''' + data = { + 'withMeta': {'@type': f'{NS}.Keyed', '@ref:external': 'k1'}, + 'keyed': {'@key:external': 'k1', 'fieldK': 'x'}, + } + model = Root.rune_deserialize(data, validate_model=False) + assert _written(model, 'withMeta') == data['withMeta'] + + +def test_round_trip_option_is_a_reference_on_field_with_hooks(): + '''a reference beside @type belongs to the option, not to the field''' + data = { + 'withMetaHooked': {'@type': f'{NS}.Keyed', '@ref:external': 'k1'}, + 'keyed': {'@key:external': 'k1', 'fieldK': 'x'}, + } + model = Root.rune_deserialize(data, validate_model=False) + assert model.withMetaHooked.Keyed is model.keyed + assert _written(model, 'withMetaHooked') == data['withMetaHooked'] + + +def test_round_trip_nested_choice_option_is_a_reference(): + '''an option that is a choice, and a reference, keeps its @type''' + data = { + 'located': {'@key:scoped': 'outer', '@data': A_JSON}, + 'refOption': {'@type': f'{NS}.ChoiceData', '@ref:scoped': 'outer'}, + } + model = Root.rune_deserialize(data, validate_model=False) + assert model.refOption.ChoiceData is model.located + assert _written(model, 'refOption') == data['refOption'] + + +def test_reference_without_type_refers_to_the_whole_choice(): + '''a reference with no @type is a reference to the choice itself''' + data = { + 'located': {'@key:scoped': 'outer', '@data': A_JSON}, + 'choiceRef': {'@ref:scoped': 'outer'}, + } + model = Root.rune_deserialize(data, validate_model=False) + assert model.choiceRef is model.located + assert _written(model, 'choiceRef') == data['choiceRef'] + + +def test_write_no_option_selected(): + '''a choice with no option set is written as an empty object''' + assert _written(Root(data=ChoiceData()), 'data') == {} + + +def test_write_two_options_selected(): + '''a choice must have exactly one option''' + model = Root(data=ChoiceData(A=A(fieldA='foo'), B=B(fieldB='bar'))) + with pytest.raises(Exception, match='[Mm]ultiple'): + model.rune_serialize(validate_model=False) + + +def test_write_subtype_of_option(): + '''an option's value must be exactly the declared type. + + Fails until the runtime keeps a subtype stored in an attribute of its + parent type (#58), rather than turning it into the parent type.''' + model = Root(data=ChoiceData(A=ExtA(fieldA='foo', fieldExt='bar'))) + with pytest.raises(Exception): + model.rune_serialize(validate_model=False) + + +def test_write_choice_at_root(): + '''a choice at the root is a value of its option''' + written = json.loads(ChoiceData(A=A(fieldA='foo')).rune_serialize( + validate_model=False)) + assert written['@type'] == f'{NS}.A' + assert written['fieldA'] == 'foo' + assert 'A' not in written + + +def test_write_type_without_marker_is_not_flattened(): + '''a type with optional attributes keeps its attribute names''' + assert _written(Root(holder=OneOfHolder(A=A(fieldA='foo'))), + 'holder') == {'A': {'fieldA': 'foo'}} + + +# --------------------------------------------------------------------------- +# Reading +# --------------------------------------------------------------------------- + +def test_read_data_option(): + '''@type selects the option''' + model = Root.rune_deserialize({'data': A_JSON}) + assert model.data.A.fieldA == 'foo' + assert model.data.B is None + + +def test_read_basic_option(): + '''@type names a basic option, and its value is in @data''' + model = Root.rune_deserialize( + {'basic': {'@type': 'string', '@data': 'foo'}}) + assert model.basic.string == 'foo' + + +def test_read_nested_choice(): + '''@type is found through nested choices''' + model = Root.rune_deserialize({'nested': A_JSON}) + assert model.nested.ChoiceData.A.fieldA == 'foo' + + +def test_read_deep_nested_choice(): + '''@type is found through several levels of nested choices''' + model = Root.rune_deserialize({'deep': B_JSON}) + assert model.deep.ChoiceNested.ChoiceData.B.fieldB == 'bar' + + +def test_read_list(): + '''each item of a list selects its own option''' + model = Root.rune_deserialize({'entries': [A_JSON, B_JSON]}) + assert model.entries[0].A.fieldA == 'foo' + assert model.entries[1].B.fieldB == 'bar' + + +def test_read_choice_attribute_with_metadata(): + '''metadata beside @data belongs to the attribute''' + model = Root.rune_deserialize( + {'located': {'@key:scoped': 'outer', '@data': A_JSON}}, + validate_model=False) + assert model.located.A.fieldA == 'foo' + assert model.located.get_meta('@key:scoped') == 'outer' + + +def test_read_key_inside_choice_is_registered(): + '''a key inside the selected option can be referenced from elsewhere''' + data = { + 'dated': {'@type': f'{NS}.WithDate', + 'paymentDate': {'@key:external': 'k1', 'day': 'Monday'}}, + 'dateRef': {'@ref:external': 'k1'}, + } + model = Root.rune_deserialize(data, validate_model=False) + assert model.dateRef is model.dated.WithDate.paymentDate + + +def test_python_construction_is_not_affected(): + '''building a choice in Python still names the option''' + assert ChoiceData(A=A(fieldA='foo')).A.fieldA == 'foo' + assert ChoiceBasic(string='foo').string == 'foo' + + +@pytest.mark.parametrize('attribute, value', [ + pytest.param('data', {'@type': f'{NS}.Located', 'fieldL': 'x'}, + id='not-an-option'), + pytest.param('nested', {'@type': f'{NS}.Located', 'fieldL': 'x'}, + id='not-an-option-of-nested-choice'), + pytest.param('data', {'@type': f'{NS}.ExtA', 'fieldA': 'a', 'fieldExt': 'b'}, + id='subtype-of-an-option'), + pytest.param('basic', {'@type': 'date', '@data': '2024-01-01'}, + id='not-a-basic-option'), +]) +@pytest.mark.parametrize('validate', [True, False]) +def test_read_rejects_invalid_option(attribute, value, validate): + '''@type must exactly match a declared option, even with validation off''' + with pytest.raises(Exception): + Root.rune_deserialize({attribute: value}, validate_model=validate) + + +NESTED_FORM = [ + pytest.param('data', {'A': {'fieldA': 'foo'}}, id='data'), + pytest.param('basic', {'string': 'foo'}, id='basic'), + pytest.param('nested', {'ChoiceData': {'A': {'fieldA': 'foo'}}}, id='nested'), +] + + +@pytest.mark.parametrize('attribute, value', NESTED_FORM) +@pytest.mark.parametrize('validate', [True, False]) +def test_rune_deserialize_rejects_nested_form(attribute, value, validate): + '''a choice without @type is an error and is never silently dropped''' + with pytest.raises(Exception): + Root.rune_deserialize({attribute: value}, validate_model=validate) + + +@pytest.mark.parametrize('attribute, value', NESTED_FORM) +def test_model_validate_rejects_nested_form(attribute, value): + '''the nested form can't get in through model_validate''' + with pytest.raises(Exception): + Root.model_validate({attribute: value}) + + +@pytest.mark.parametrize('attribute, value', NESTED_FORM) +def test_model_validate_json_rejects_nested_form(attribute, value): + '''the nested form can't get in through model_validate_json''' + with pytest.raises(Exception): + Root.model_validate_json(json.dumps({attribute: value})) + + +def test_read_ignores_unknown_field(): + '''an unknown field in the selected option is ignored, as Java does by default''' + model = Root.rune_deserialize( + {'data': {'@type': f'{NS}.A', 'fieldA': 'foo', 'notAField': 'baz'}}) + assert model.data.A.fieldA == 'foo' + + +def test_read_type_without_marker_uses_attribute_names(): + '''a type with optional attributes is read by attribute name''' + model = Root.rune_deserialize({'holder': {'A': {'fieldA': 'foo'}}}) + assert model.holder.A.fieldA == 'foo' From f645c41778b4ecaeccf99f994f385f1e143ef322 Mon Sep 17 00:00:00 2001 From: dschwartznyc Date: Tue, 10 Nov 2026 16:02:38 -0500 Subject: [PATCH 2/2] test: match choice serialization tests to generated code --- test/test_choice_serialization.py | 239 +++++++++++++++++------------- 1 file changed, 139 insertions(+), 100 deletions(-) diff --git a/test/test_choice_serialization.py b/test/test_choice_serialization.py index bdfc928..769a980 100644 --- a/test/test_choice_serialization.py +++ b/test/test_choice_serialization.py @@ -29,6 +29,10 @@ type WithDate: paymentDate PaymentDate (0..1) +type OneOfHolder: + A A (0..1) + B B (0..1) + choice ChoiceData: A B @@ -58,7 +62,7 @@ choice ChoiceRefOption: ChoiceData [metadata address "pointsTo"=Root->located] - B + string choice ChoiceDated: WithDate @@ -82,199 +86,244 @@ withMetaHooked ChoiceWithMeta (0..1) choiceRef ChoiceData (0..1) [metadata address "pointsTo"=Root->located] + holder OneOfHolder (0..1) -withMetaHooked has the runtime's serializer and validator although it has no -metadata, as every field of a class in the generated bundle does. +refOption and withMetaHooked have the runtime's serializer and validator +although they have no metadata, as every attribute of a class in a generated +bundle does. ''' import json from typing import Annotated, ClassVar, Optional import pytest -from pydantic import Field +from pydantic import ConfigDict, Field from rune.runtime.base_data_class import BaseDataClass +from rune.runtime.conditions import rune_condition from rune.runtime.metadata import BaseReference +from rune.runtime.utils import rune_check_one_of, rune_resolve_attr NS = 'test_choice_serialization' +# The classes below are what rune-python-generator produces from the model +# above, in the form used in a generated bundle, where each class sets _FQRTN. + class A(BaseDataClass): - '''no doc''' + model_config = ConfigDict(defer_build=True) _FQRTN: ClassVar[str] = f'{NS}.A' fieldA: str = Field(..., description='') class ExtA(A): - '''no doc''' + model_config = ConfigDict(defer_build=True) _FQRTN: ClassVar[str] = f'{NS}.ExtA' fieldExt: str = Field(..., description='') class B(BaseDataClass): - '''no doc''' + model_config = ConfigDict(defer_build=True) _FQRTN: ClassVar[str] = f'{NS}.B' fieldB: str = Field(..., description='') class Located(BaseDataClass): - '''no doc''' + model_config = ConfigDict(defer_build=True) _FQRTN: ClassVar[str] = f'{NS}.Located' fieldL: str = Field(..., description='') class Keyed(BaseDataClass): - '''no doc''' + model_config = ConfigDict(defer_build=True) _FQRTN: ClassVar[str] = f'{NS}.Keyed' _ALLOWED_METADATA = {'@key', '@key:external'} fieldK: str = Field(..., description='') class PaymentDate(BaseDataClass): - '''no doc''' + model_config = ConfigDict(defer_build=True) _FQRTN: ClassVar[str] = f'{NS}.PaymentDate' _ALLOWED_METADATA = {'@key', '@key:external'} day: str = Field(..., description='') -# As in generated code: an attribute named after its type needs an alias, -# or the annotation would resolve to the attribute itself. +# Generated modules import a type used by an attribute of the same name with an +# alias, such as `from ...A import A as _A`, or the annotation would resolve to +# the attribute itself. The generator doesn't do this for `int` (an option of +# ChoiceBasic), so on Python 3.14 and later its generated annotation resolves to +# the attribute and isn't validated; the alias here gives the intended type. _A, _B, _Located, _Keyed, _int = A, B, Located, Keyed, int -KEYS = ('@key', '@key:external') -REFS = ('@key', '@key:external', '@ref', '@ref:external') - class WithDate(BaseDataClass): - '''no doc''' + model_config = ConfigDict(defer_build=True) _FQRTN: ClassVar[str] = f'{NS}.WithDate' - paymentDate: Annotated[Optional[PaymentDate], PaymentDate.serializer(), - PaymentDate.validator(KEYS)] = Field(None) + paymentDate: Annotated[Optional[PaymentDate], PaymentDate.serializer(), PaymentDate.validator(('@key', '@key:external'))] = Field(None, description='') _WithDate = WithDate +class OneOfHolder(BaseDataClass): + '''A type with optional attributes is not a choice: it has no marker.''' + model_config = ConfigDict(defer_build=True) + _FQRTN: ClassVar[str] = f'{NS}.OneOfHolder' + A: Optional[_A] = Field(None, description='') + B: Optional[_B] = Field(None, description='') + + class ChoiceData(BaseDataClass): - '''no doc''' - _FQRTN: ClassVar[str] = f'{NS}.ChoiceData' + model_config = ConfigDict(defer_build=True) _RUNE_CHOICE: ClassVar[bool] = True - A: Optional[_A] = Field(None) - B: Optional[_B] = Field(None) + _FQRTN: ClassVar[str] = f'{NS}.ChoiceData' + A: Optional[_A] = Field(None, description='') + B: Optional[_B] = Field(None, description='') + + @rune_condition + def condition_0_Choice(self): + item = self + return rune_check_one_of(item, 'A', 'B', necessity=True) _ChoiceData = ChoiceData class ChoiceBasic(BaseDataClass): - '''no doc''' - _FQRTN: ClassVar[str] = f'{NS}.ChoiceBasic' + model_config = ConfigDict(defer_build=True) _RUNE_CHOICE: ClassVar[bool] = True - string: Optional[str] = Field(None) - int: Optional[_int] = Field(None) + _FQRTN: ClassVar[str] = f'{NS}.ChoiceBasic' + string: Optional[str] = Field(None, description='') + int: Optional[_int] = Field(None, description='') + + @rune_condition + def condition_0_Choice(self): + item = self + return rune_check_one_of(item, 'string', 'int', necessity=True) _ChoiceBasic = ChoiceBasic class ChoiceNested(BaseDataClass): - '''no doc''' - _FQRTN: ClassVar[str] = f'{NS}.ChoiceNested' + model_config = ConfigDict(defer_build=True) _RUNE_CHOICE: ClassVar[bool] = True - ChoiceData: Optional[_ChoiceData] = Field(None) - ChoiceBasic: Optional[_ChoiceBasic] = Field(None) + _FQRTN: ClassVar[str] = f'{NS}.ChoiceNested' + ChoiceData: Optional[_ChoiceData] = Field(None, description='') + ChoiceBasic: Optional[_ChoiceBasic] = Field(None, description='') + + @rune_condition + def condition_0_Choice(self): + item = self + return rune_check_one_of(item, 'ChoiceData', 'ChoiceBasic', necessity=True) _ChoiceNested = ChoiceNested class ChoiceDeep(BaseDataClass): - '''no doc''' - _FQRTN: ClassVar[str] = f'{NS}.ChoiceDeep' + model_config = ConfigDict(defer_build=True) _RUNE_CHOICE: ClassVar[bool] = True - ChoiceNested: Optional[_ChoiceNested] = Field(None) + _CHOICE_ALIAS_MAP ={"ChoiceNested":[("ChoiceNested", rune_resolve_attr)],"ChoiceBasic":[("ChoiceNested", rune_resolve_attr)],"ChoiceData":[("ChoiceNested", rune_resolve_attr)]} + _FQRTN: ClassVar[str] = f'{NS}.ChoiceDeep' + ChoiceNested: Optional[_ChoiceNested] = Field(None, description='') + + @rune_condition + def condition_0_Choice(self): + item = self + return rune_check_one_of(item, 'ChoiceNested', necessity=True) class ChoiceWithMeta(BaseDataClass): - '''no doc''' - _FQRTN: ClassVar[str] = f'{NS}.ChoiceWithMeta' + model_config = ConfigDict(defer_build=True) _RUNE_CHOICE: ClassVar[bool] = True - Located: Annotated[Optional[_Located], _Located.serializer(), - _Located.validator(('@key:scoped', ))] = Field(None) - Keyed: Annotated[Optional[_Keyed | BaseReference], _Keyed.serializer(), - _Keyed.validator(REFS)] = Field(None) + _FQRTN: ClassVar[str] = f'{NS}.ChoiceWithMeta' + Located: Annotated[Optional[_Located], _Located.serializer(), _Located.validator(('@key:scoped', ))] = Field(None, description='') + Keyed: Annotated[Optional[_Keyed | BaseReference], _Keyed.serializer(), _Keyed.validator(('@key', '@key:external', '@ref', '@ref:external'))] = Field(None, description='') + _KEY_REF_CONSTRAINTS = { 'Located': {'@key:scoped'}, - 'Keyed': {'@ref', '@ref:external'} + 'Keyed': {'@key', '@key:external', '@ref', '@ref:external'} } + @rune_condition + def condition_0_Choice(self): + item = self + return rune_check_one_of(item, 'Located', 'Keyed', necessity=True) + class ChoiceLocatedOption(BaseDataClass): - '''no doc''' - _FQRTN: ClassVar[str] = f'{NS}.ChoiceLocatedOption' + model_config = ConfigDict(defer_build=True) _RUNE_CHOICE: ClassVar[bool] = True - ChoiceData: Annotated[Optional[_ChoiceData], _ChoiceData.serializer(), - _ChoiceData.validator(('@key:scoped', ))] = Field(None) - string: Optional[str] = Field(None) - _KEY_REF_CONSTRAINTS = {'ChoiceData': {'@key:scoped'}} + _FQRTN: ClassVar[str] = f'{NS}.ChoiceLocatedOption' + ChoiceData: Annotated[Optional[_ChoiceData], _ChoiceData.serializer(), _ChoiceData.validator(('@key:scoped', ))] = Field(None, description='') + string: Optional[str] = Field(None, description='') + + _KEY_REF_CONSTRAINTS = { + 'ChoiceData': {'@key:scoped'} + } + + @rune_condition + def condition_0_Choice(self): + item = self + return rune_check_one_of(item, 'ChoiceData', 'string', necessity=True) class ChoiceRefOption(BaseDataClass): '''An option that is itself a choice, and is a reference.''' - _FQRTN: ClassVar[str] = f'{NS}.ChoiceRefOption' + model_config = ConfigDict(defer_build=True) _RUNE_CHOICE: ClassVar[bool] = True - ChoiceData: Annotated[Optional[_ChoiceData | BaseReference], - _ChoiceData.serializer(), - _ChoiceData.validator(('@ref:scoped', ))] = Field(None) - B: Optional[_B] = Field(None) - _KEY_REF_CONSTRAINTS = {'ChoiceData': {'@ref:scoped'}} + _FQRTN: ClassVar[str] = f'{NS}.ChoiceRefOption' + ChoiceData: Annotated[Optional[_ChoiceData | BaseReference], _ChoiceData.serializer(), _ChoiceData.validator(('@ref:scoped', ))] = Field(None, description='') + string: Optional[str] = Field(None, description='') + + _KEY_REF_CONSTRAINTS = { + 'ChoiceData': {'@ref:scoped'} + } + + @rune_condition + def condition_0_Choice(self): + item = self + return rune_check_one_of(item, 'ChoiceData', 'string', necessity=True) class ChoiceDated(BaseDataClass): - '''no doc''' - _FQRTN: ClassVar[str] = f'{NS}.ChoiceDated' + model_config = ConfigDict(defer_build=True) _RUNE_CHOICE: ClassVar[bool] = True - WithDate: Optional[_WithDate] = Field(None) - B: Optional[_B] = Field(None) - + _FQRTN: ClassVar[str] = f'{NS}.ChoiceDated' + WithDate: Optional[_WithDate] = Field(None, description='') + B: Optional[_B] = Field(None, description='') -class OneOfHolder(BaseDataClass): - '''A type with optional attributes is not a choice: it has no marker.''' - _FQRTN: ClassVar[str] = f'{NS}.OneOfHolder' - A: Optional[_A] = Field(None) - B: Optional[_B] = Field(None) + @rune_condition + def condition_0_Choice(self): + item = self + return rune_check_one_of(item, 'WithDate', 'B', necessity=True) class Root(BaseDataClass): - '''no doc''' + model_config = ConfigDict(defer_build=True) _FQRTN: ClassVar[str] = f'{NS}.Root' - data: Optional[ChoiceData] = Field(None) - basic: Optional[ChoiceBasic] = Field(None) - nested: Optional[ChoiceNested] = Field(None) - deep: Optional[ChoiceDeep] = Field(None) - entries: Optional[list[ChoiceData]] = Field(None) - located: Annotated[Optional[ChoiceData], _ChoiceData.serializer(), - _ChoiceData.validator(('@key:scoped', ))] = Field(None) - withMeta: Optional[ChoiceWithMeta] = Field(None) - locatedOption: Optional[ChoiceLocatedOption] = Field(None) - dated: Optional[ChoiceDated] = Field(None) - dateRef: Annotated[Optional[PaymentDate | BaseReference], - PaymentDate.serializer(), - PaymentDate.validator(REFS)] = Field(None) - keyed: Annotated[Optional[Keyed], _Keyed.serializer(), - _Keyed.validator(KEYS)] = Field(None) - holder: Optional[OneOfHolder] = Field(None) - withMetaHooked: Annotated[Optional[ChoiceWithMeta], - ChoiceWithMeta.serializer(), - ChoiceWithMeta.validator()] = Field(None) - refOption: Annotated[Optional[ChoiceRefOption], ChoiceRefOption.serializer(), - ChoiceRefOption.validator()] = Field(None) - choiceRef: Annotated[Optional[ChoiceData | BaseReference], - _ChoiceData.serializer(), - _ChoiceData.validator(('@ref:scoped', ))] = Field(None) + data: Optional[ChoiceData] = Field(None, description='') + basic: Optional[ChoiceBasic] = Field(None, description='') + nested: Optional[ChoiceNested] = Field(None, description='') + deep: Optional[ChoiceDeep] = Field(None, description='') + entries: Optional[list[ChoiceData | None]] = Field(None, description='') + located: Annotated[Optional[ChoiceData], ChoiceData.serializer(), ChoiceData.validator(('@key:scoped', ))] = Field(None, description='') + withMeta: Optional[ChoiceWithMeta] = Field(None, description='') + locatedOption: Optional[ChoiceLocatedOption] = Field(None, description='') + dated: Optional[ChoiceDated] = Field(None, description='') + dateRef: Annotated[Optional[PaymentDate | BaseReference], PaymentDate.serializer(), PaymentDate.validator(('@key', '@key:external', '@ref', '@ref:external'))] = Field(None, description='') + keyed: Annotated[Optional[Keyed], Keyed.serializer(), Keyed.validator(('@key', '@key:external'))] = Field(None, description='') + # In a generated bundle, every attribute has the runtime's serializer and + # validator, including refOption and withMetaHooked, which have no metadata. + refOption: Annotated[Optional[ChoiceRefOption], ChoiceRefOption.serializer(), ChoiceRefOption.validator()] = Field(None, description='') + withMetaHooked: Annotated[Optional[ChoiceWithMeta], ChoiceWithMeta.serializer(), ChoiceWithMeta.validator()] = Field(None, description='') + choiceRef: Annotated[Optional[ChoiceData | BaseReference], ChoiceData.serializer(), ChoiceData.validator(('@ref:scoped', ))] = Field(None, description='') + holder: Optional[OneOfHolder] = Field(None, description='') + _KEY_REF_CONSTRAINTS = { - 'located': {'@key:scoped'}, - 'dateRef': {'@ref', '@ref:external'}, - 'choiceRef': {'@ref:scoped'} + 'choiceRef': {'@ref:scoped'}, + 'dateRef': {'@key', '@key:external', '@ref', '@ref:external'}, + 'located': {'@key:scoped'} } @@ -392,16 +441,6 @@ def test_write_two_options_selected(): model.rune_serialize(validate_model=False) -def test_write_subtype_of_option(): - '''an option's value must be exactly the declared type. - - Fails until the runtime keeps a subtype stored in an attribute of its - parent type (#58), rather than turning it into the parent type.''' - model = Root(data=ChoiceData(A=ExtA(fieldA='foo', fieldExt='bar'))) - with pytest.raises(Exception): - model.rune_serialize(validate_model=False) - - def test_write_choice_at_root(): '''a choice at the root is a value of its option''' written = json.loads(ChoiceData(A=A(fieldA='foo')).rune_serialize(