diff --git a/src/rune/runtime/base_data_class.py b/src/rune/runtime/base_data_class.py index f405e8c..e992137 100644 --- a/src/rune/runtime/base_data_class.py +++ b/src/rune/runtime/base_data_class.py @@ -1,9 +1,11 @@ '''Base class for all Rune type classes''' +import contextvars import copy import importlib import json import logging -from collections.abc import Mapping, MutableSequence +from collections.abc import Generator, Mapping, MutableSequence +from contextlib import contextmanager from decimal import Decimal from enum import Enum from functools import cache @@ -13,8 +15,11 @@ BaseModel, ConfigDict, ModelWrapValidatorHandler, + PlainSerializer, TypeAdapter, ValidationError, + ValidationInfo, + WrapSerializer, model_serializer, model_validator, ) @@ -38,6 +43,17 @@ ROOT_CONTAINER = '__rune_root_metadata' _LIST_INDICES = TypeAdapter(list[int]) +_JSON_INPUT = contextvars.ContextVar('rune_json_input', default=False) + + +@contextmanager +def _rune_json_input(enabled: bool = True) -> Generator[None, None, None]: + '''Scope wire validation, allowing explicit native constructors and builders.''' + token = _JSON_INPUT.set(enabled) + try: + yield + finally: + _JSON_INPUT.reset(token) def _annotation_contains(annotation: Any, base_types: tuple[type, ...], *, unknown: bool = False) -> bool: @@ -49,7 +65,16 @@ def _annotation_contains(annotation: Any, base_types: tuple[type, ...], *, unkno return any(_annotation_contains(arg, base_types, unknown=unknown) for arg in get_args(annotation)) -class BaseDataClass(BaseModel, ComplexTypeMetaDataMixin): +class _RuneModelMeta(type(BaseModel)): + '''Distinguish explicit Python calls from Pydantic's nested wire validation.''' + def __call__(cls, *args, **kwargs): + if not _JSON_INPUT.get(): + return super().__call__(*args, **kwargs) + with _rune_json_input(False): + return super().__call__(*args, **kwargs) + + +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`, @@ -87,6 +112,7 @@ def model_rebuild(cls, *, force: bool = False, raise_errors: bool = True, cls._reference_fields.cache_clear() cls._containment_fields.cache_clear() cls._reference_constraints.cache_clear() + cls._choice_option_class.cache_clear() return super().model_rebuild(force=force, raise_errors=raise_errors, _parent_namespace_depth=_parent_namespace_depth + 1, _types_namespace=_types_namespace) @@ -201,13 +227,15 @@ def _unwrap_cow_inputs(cls, data: Any) -> Any: return cls._unwrap_cow_validation_input(data) @model_serializer(mode='wrap') - def _serialize_refs(self, serializer, info): - '''should replace objects with refs while serializing''' + def _serialize_rune_model(self, serializer, info): + '''Serialize fields, replace references, flatten choices, and add root metadata.''' refs = self._get_rune_refs_container() reference_fields = type(self)._reference_fields() 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, info) return root_metadata | result if root_metadata else result updates = {} list_fields = {} @@ -263,6 +291,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, info) return root_metadata | res if root_metadata else res @staticmethod @@ -284,13 +314,127 @@ 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 + @cache + def _choice_option_class(cls, name: str) -> type['BaseDataClass'] | None: + '''Find the declared data/choice class of the selected field only.''' + def find(annotation): + if isinstance(annotation, type) and issubclass(annotation, BaseDataClass): + return annotation + for argument in get_args(annotation): + if result := find(argument): + return result + return None + return find(cls.model_fields[name].annotation) + + @classmethod + def _choice_dispatch(cls) -> dict[str, str]: + '''Return generated dispatch metadata, raising RuntimeError when absent.''' + dispatch = getattr(cls, '_CHOICE_DISPATCH', None) + if dispatch is None: + raise RuntimeError(f'Choice {cls._fqrtn()} is missing _CHOICE_DISPATCH. ' + 'Regenerate the model with an updated Rune Python generator.') + return dispatch + + @classmethod + def _choice_to_fields(cls, data: dict[str, Any]) -> dict[str, Any]: + '''Route a wire discriminator to its immediate generated option field. + + Raise TypeError for malformed discriminator/wrapper types and ValueError + for missing or unsupported values. RuntimeError indicates missing + generated dispatch metadata. + ''' + if '@type' not in data: + if any(key.startswith('@ref') for key in data): + return data + if '@data' in data: + if not isinstance(data['@data'], dict): + raise TypeError("Choice '@data' must contain an object") + metadata = {key: value for key, value in data.items() if key != '@data'} + return metadata | cls._choice_to_fields(data['@data']) + raise ValueError(f"Missing required '@type' for choice {cls._fqrtn()}") + + tag = data['@type'] + if not isinstance(tag, str): + raise TypeError("Choice '@type' must be a string") + dispatch = cls._choice_dispatch() + if tag not in dispatch: + raise ValueError(f"'{tag}' is not an option of choice {cls._fqrtn()}") + name = dispatch[tag] + option_class = cls._choice_option_class(name) + body = {key: value for key, value in data.items() if key != '@type'} + if option_class is None: + wrapped = any(isinstance(item, (PlainSerializer, WrapSerializer)) + for item in cls.model_fields[name].metadata) + if '@data' in body and body['@data'] is None: + raise ValueError(f'Choice option {tag} must have a non-null value') + if '@data' not in body and not (wrapped and any(key.startswith('@ref') for key in body)): + raise ValueError(f"Missing required '@data' for choice option {tag}") + if wrapped: + value = body + else: + value = body['@data'] + elif tag == option_class._fqrtn(): + value = body + elif option_class._is_rune_choice(): + value = data + else: + raise ValueError(f"'{tag}' does not match declared option {option_class._fqrtn()}") + return {name: value} + + def _flatten_choice(self, serialized: dict[str, Any], info) -> dict[str, Any]: + '''Write the selected leaf, retaining valid option metadata/reference wrappers.''' + dispatch = type(self)._choice_dispatch() + 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 {self._fqrtn()}: {selected}') + name = selected[0] + output_name = self._serialized_field_name(name, info) + if output_name not in serialized: + return {} + option = serialized[output_name] + option_class = type(self)._choice_option_class(name) + if option_class is None: + tags = [tag for tag, field in dispatch.items() + if field == name] + if len(tags) != 1: + raise ValueError(f'Missing or ambiguous discriminator for choice option {name}') + if isinstance(option, dict) and ('@data' in option + or any(key.startswith('@ref') for key in option)): + return {'@type': tags[0]} | option + return {'@type': tags[0], '@data': option} + + value = rune_unwrap_proxy(self.__dict__[name]) + if isinstance(value, BaseDataClass) and type(value) is not option_class: + raise ValueError(f'Choice option {name} must be exactly {option_class._fqrtn()}') + if option_class._is_rune_choice(): + if '@type' not in option and ('@data' in option + or any(key.startswith('@ref') for key in option)): + return {'@type': option_class._fqrtn()} | option + if isinstance(value, BaseDataClass) and not any( + value.__dict__.get(field) is not None for field in type(value).model_fields): + raise ValueError(f'Nested choice {option_class._fqrtn()} has no selected option') + return option + return option | {'@type': option_class._fqrtn()} + @model_validator(mode='wrap') @classmethod - def _deserialize_refs(cls, data: Any, - handler: ModelWrapValidatorHandler[Self]) -> Self: - '''should resolve refs after creation''' + def _deserialize_rune_model(cls, data: Any, + handler: ModelWrapValidatorHandler[Self], info: ValidationInfo) -> Self: + '''Decode choices, validate fields, and initialize parents, keys, and references.''' + if info.mode == 'json' and not _JSON_INPUT.get(): + # TypeAdapter.validate_json also needs to mark nested Python revalidation. + with _rune_json_input(): + return cls._deserialize_rune_model(data, handler, info) # type: ignore metadata = {} if isinstance(data, dict): + if cls._is_rune_choice() and ( + _JSON_INPUT.get() or '@type' in data or '@data' in data + or any(key.startswith('@ref') for key in data)): + 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 @@ -439,7 +583,8 @@ def rune_serialize( root_meta['@version'] = self.get_model_version() # The top-level object isn't held in a field, so no field # serializer writes its own metadata - root_meta |= self.serialise_meta() # type: ignore + if not self._is_rune_choice(): + root_meta |= self.serialise_meta() # type: ignore return self.model_dump_json(indent=indent, include=include, @@ -507,9 +652,10 @@ def rune_deserialize(cls, rune_prefix = cls._get_rune_namespace_prefix() if rune_prefix is None and cls is BaseDataClass: rune_prefix = namespace_prefix - rune_cls = cls._type_to_cls(rune_dict, - namespace_prefix=rune_prefix) - model = rune_cls.model_validate(rune_dict, strict=strict) + rune_cls = cls if cls._is_rune_choice() else cls._type_to_cls( + rune_dict, namespace_prefix=rune_prefix) + with _rune_json_input(): + model = rune_cls.model_validate(rune_dict, strict=strict) model.resolve_references(ignore_dangling=False, recurse=True) if validate_model: model.validate_model( @@ -547,12 +693,13 @@ def model_validate_json(cls, by_name=by_name) # Decimal keeps every digit; a float would round long numbers data = json.loads(json_data, parse_float=Decimal) - return cls.model_validate(data, - strict=strict, - extra=extra, - context=context, - by_alias=by_alias, - by_name=by_name) + with _rune_json_input(): + return cls.model_validate(data, + strict=strict, + extra=extra, + context=context, + by_alias=by_alias, + by_name=by_name) def resolve_references(self, ignore_dangling=False, recurse=True): '''resolves all attributes which are references''' diff --git a/src/rune/runtime/metadata.py b/src/rune/runtime/metadata.py index 6003302..6f3cc88 100644 --- a/src/rune/runtime/metadata.py +++ b/src/rune/runtime/metadata.py @@ -458,6 +458,11 @@ def _get_rune_scope_type(cls): class ComplexTypeMetaDataMixin(BaseMetaDataMixin): '''metadata support for complex types''' + @classmethod + def _is_rune_choice(cls) -> bool: + '''Whether the generator explicitly marked this type as a Rune choice.''' + return getattr(cls, '_RUNE_CHOICE', False) + @classmethod def _type_to_cls(cls, metadata:dict[str, Any], @@ -481,7 +486,10 @@ 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(): + return res | {'@data': dumped} if res else dumped + res |= dumped if cls != obj.__class__: # pylint: disable=protected-access # res = {'@type': obj._FQRTN} | res @@ -514,14 +522,25 @@ def deserialize(cls, obj, allowed_meta: set[str]): (metadata if k.startswith('@') else fields)[k] = v # References deserialization treatment - if aux := cls._create_unresolved_ref(metadata): + is_choice = cls._is_rune_choice() + if not (is_choice and '@type' in metadata) and ( + aux := cls._create_unresolved_ref(metadata)): return aux - # Model creation - rune_cls = cls._type_to_cls(metadata) - if rune_cls != cls and not issubclass(rune_cls, cls): - raise ValueError(f'{rune_cls} has to be a child class of {cls}!') - model = rune_cls.model_validate(fields) # type: ignore + rune_cls = cls + if is_choice: + if '@data' in metadata and '@type' not in metadata: + fields = metadata.pop('@data') + else: + fields = obj + metadata = {} + else: + rune_cls = cls._type_to_cls(metadata) + if rune_cls != cls and not issubclass(rune_cls, cls): + raise ValueError(f'{rune_cls} has to be a child class of {cls}!') + + # Concrete models also inherit Pydantic's BaseModel, which supplies model_validate. + model = rune_cls.model_validate(fields) # pyright: ignore[reportAttributeAccessIssue] model.__dict__[META_CONTAINER] = metadata if cls.meta_checks_enabled(): model._init_meta(allowed_meta) # pylint: disable=protected-access diff --git a/src/rune/runtime/object_builder.py b/src/rune/runtime/object_builder.py index b992303..cf4795f 100644 --- a/src/rune/runtime/object_builder.py +++ b/src/rune/runtime/object_builder.py @@ -4,7 +4,7 @@ import copy from typing import Any -from rune.runtime.base_data_class import BaseDataClass +from rune.runtime.base_data_class import BaseDataClass, _rune_json_input from rune.runtime.reference_list import _ReferenceList from rune.runtime.utils import rune_mangle_name @@ -59,7 +59,8 @@ def to_model(self) -> BaseDataClass: raise ValueError('Draft has no model class configured.') data = self.to_dict() if hasattr(self._model_cls, 'model_validate'): - return self._model_cls.model_validate(data) + with _rune_json_input(False): + return self._model_cls.model_validate(data) return self._model_cls(**data) def __deepcopy__(self, memo): @@ -91,7 +92,7 @@ def __repr__(self): class _PathDraft: - __slots__ = ('_root', '_path') + __slots__ = ('_path', '_root') _root: ObjectBuilder _path: list[str] diff --git a/test/test_choice_serialization.py b/test/test_choice_serialization.py new file mode 100644 index 0000000..19fb9e8 --- /dev/null +++ b/test/test_choice_serialization.py @@ -0,0 +1,515 @@ +# Copyright (c) 2023-2026 CLOUDRISK Limited and FT Advisory LLC +# SPDX-License-Identifier: Apache-2.0 + +"""Wire-format choices use generated dispatch; Python construction uses fields.""" +import datetime +from decimal import Decimal +from enum import Enum +import json +import sys +import types +from typing import Annotated, ClassVar + +import pytest +from pydantic import ConfigDict, Field, TypeAdapter, ValidationError +from pydantic_core import PydanticSerializationError + +from rune.runtime.base_data_class import BaseDataClass +from rune.runtime.conditions import rune_condition +from rune.runtime.cow import rune_cow +from rune.runtime.func_proxy import rune_finalize_return +from rune.runtime.metadata import BaseReference, EnumWithMetaMixin, StrWithMeta +from rune.runtime.object_builder import ObjectBuilder +from rune.runtime.utils import rune_check_one_of + +NS = 'test_choice_serialization' + + +class Keyed(BaseDataClass): + _FQRTN: ClassVar[str] = f'{NS}.Keyed' + _ALLOWED_METADATA: ClassVar[set[str]] = {'@key', '@key:external'} + value: str + + +class A(BaseDataClass): + _FQRTN: ClassVar[str] = f'{NS}.A' + value: str + keyed: Annotated[Keyed | None, Keyed.serializer(), Keyed.validator()] = None + + +class B(BaseDataClass): + _FQRTN: ClassVar[str] = f'{NS}.B' + value: str + + +class C(BaseDataClass): + _FQRTN: ClassVar[str] = f'{NS}.C' + value: str + + +class D(BaseDataClass): + _FQRTN: ClassVar[str] = f'{NS}.D' + value: str + + +class Choice(BaseDataClass): + _RUNE_CHOICE: ClassVar[bool] = True + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {} + + @rune_condition + def exactly_one_option(self): + return rune_check_one_of(self, *type(self).model_fields, necessity=True) + + +_A, _B, _C, _D, _Keyed = A, B, C, D, Keyed + + +class ChoiceAB(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceAB' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {f'{NS}.A': 'A', f'{NS}.B': 'B'} + A: _A | None = None + B: _B | None = None + + +class ChoiceCD(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceCD' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {f'{NS}.C': 'C', f'{NS}.D': 'D'} + C: _C | None = None + D: _D | None = None + + +_ChoiceAB, _ChoiceCD = ChoiceAB, ChoiceCD + + +class ChoiceTree(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceTree' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + f'{NS}.A': 'ChoiceAB', f'{NS}.B': 'ChoiceAB', + f'{NS}.C': 'ChoiceCD', f'{NS}.D': 'ChoiceCD', + } + ChoiceAB: _ChoiceAB | None = None + ChoiceCD: _ChoiceCD | None = None + + +class ChoiceDeep(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceDeep' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + tag: 'tree' for tag in ChoiceTree._CHOICE_DISPATCH + } + tree: ChoiceTree | None = None + + +class ChoiceBasic(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceBasic' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + name: name for name in ('string', 'integer', 'boolean', 'number', 'date') + } + string: str | None = None + integer: int | None = None + boolean: bool | None = None + number: Decimal | None = None + date: datetime.date | None = None + + +class Currency(EnumWithMetaMixin, Enum): + USD = 'USD' + EUR = 'EUR' + + +class ChoiceBasicMeta(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceBasicMeta' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + 'string': 'string', f'{NS}.Currency': 'currency', + } + string: Annotated[StrWithMeta | None, StrWithMeta.serializer(), + StrWithMeta.validator(('@scheme', '@key:scoped'))] = None + currency: Annotated[Currency | None, Currency.serializer(), + Currency.validator(('@scheme',))] = None + + +class ChoiceLocated(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceLocated' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + f'{NS}.ChoiceAB': 'ChoiceAB', f'{NS}.A': 'ChoiceAB', + f'{NS}.B': 'ChoiceAB', 'string': 'string', + } + ChoiceAB: Annotated[_ChoiceAB | None, _ChoiceAB.serializer(), + _ChoiceAB.validator(('@key:scoped',))] = None + string: str | None = None + _KEY_REF_CONSTRAINTS: ClassVar[dict[str, set[str]]] = {'ChoiceAB': {'@key:scoped'}} + + +class ChoiceReference(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceReference' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {f'{NS}.Keyed': 'Keyed'} + Keyed: Annotated[_Keyed | BaseReference | None, _Keyed.serializer(), + _Keyed.validator(('@ref', '@ref:external'))] = None + _KEY_REF_CONSTRAINTS: ClassVar[dict[str, set[str]]] = {'Keyed': {'@ref', '@ref:external'}} + + +class ChoiceChoiceReference(Choice): + _FQRTN: ClassVar[str] = f'{NS}.ChoiceChoiceReference' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = { + f'{NS}.ChoiceAB': 'ChoiceAB', f'{NS}.A': 'ChoiceAB', f'{NS}.B': 'ChoiceAB', + } + ChoiceAB: Annotated[_ChoiceAB | BaseReference | None, _ChoiceAB.serializer(), + _ChoiceAB.validator(('@ref:scoped',))] = None + _KEY_REF_CONSTRAINTS: ClassVar[dict[str, set[str]]] = {'ChoiceAB': {'@ref:scoped'}} + + +class Root(BaseDataClass): + _FQRTN: ClassVar[str] = f'{NS}.Root' + data: ChoiceAB | None = None + hooked: Annotated[ChoiceAB | None, ChoiceAB.serializer(), ChoiceAB.validator()] = None + tree: ChoiceTree | None = None + deep: ChoiceDeep | None = None + basic: ChoiceBasic | None = None + basic_meta: ChoiceBasicMeta | None = None + entries: Annotated[list[ChoiceAB | BaseReference | None] | None, + ChoiceAB.serializer(), ChoiceAB.validator(('@ref:scoped',))] = None + located: Annotated[ChoiceAB | None, ChoiceAB.serializer(), + ChoiceAB.validator(('@key:scoped',))] = None + located_option: ChoiceLocated | None = None + referenced_option: Annotated[ChoiceReference | None, ChoiceReference.serializer(), + ChoiceReference.validator()] = None + referenced_choice_option: ChoiceChoiceReference | None = None + choice_ref: Annotated[ChoiceAB | BaseReference | None, ChoiceAB.serializer(), + ChoiceAB.validator(('@ref:scoped',))] = None + keyed: Annotated[Keyed | None, Keyed.serializer(), Keyed.validator()] = None + keyed_ref: Annotated[Keyed | BaseReference | None, Keyed.serializer(), + Keyed.validator(('@ref', '@ref:external'))] = None + _KEY_REF_CONSTRAINTS: ClassVar[dict[str, set[str]]] = { + 'entries': {'@ref:scoped'}, 'located': {'@key:scoped'}, + 'choice_ref': {'@ref:scoped'}, 'keyed_ref': {'@ref', '@ref:external'}, + } + + +@pytest.fixture(autouse=True) +def register_root_modules(monkeypatch): + for cls in (Root, ChoiceAB, ChoiceBasic, ChoiceDeep, ChoiceLocated, ChoiceBasicMeta): + module = types.ModuleType(cls._fqrtn()) + setattr(module, cls.__name__, cls) + monkeypatch.setitem(sys.modules, module.__name__, module) + + +def content(model): + return {key: value for key, value in json.loads(model.rune_serialize()).items() + if key not in ('@type', '@model', '@version')} + + +@pytest.mark.parametrize('name, leaf, branch', [ + ('A', A, ChoiceAB), ('B', B, ChoiceAB), ('C', C, ChoiceCD), ('D', D, ChoiceCD), +]) +def test_disjoint_nested_choices_round_trip_all_paths(name, leaf, branch): + nested = branch(**{name: leaf(value='foo')}) + tree = ChoiceTree(**{branch.__name__: nested}) + original = Root(tree=tree, deep=ChoiceDeep(tree=tree)) + expected = {'@type': f'{NS}.{name}', 'value': 'foo'} + assert content(original) == {'tree': expected, 'deep': expected} + restored = Root.rune_deserialize(original.rune_serialize()) + assert getattr(getattr(restored.tree, branch.__name__), name).value == 'foo' + assert content(restored) == content(original) + + +@pytest.mark.parametrize('field, value, wire_value', [ + ('string', 'foo', 'foo'), ('integer', 0, 0), ('boolean', False, False), + ('number', Decimal('1.234567890123456789'), '1.234567890123456789'), + ('date', datetime.date(2026, 11, 13), '2026-11-13'), +]) +def test_basic_options_preserve_values(field, value, wire_value): + original = Root(basic=ChoiceBasic(**{field: value})) + written = json.loads(original.rune_serialize()) + assert written['basic'] == {'@type': field, '@data': wire_value} + restored = Root.rune_deserialize(written) + assert getattr(restored.basic, field) == value + + +@pytest.mark.parametrize('field, tag, value, metadata', [ + ('string', 'string', 'foo', {'@scheme': 'urn:test', '@key:scoped': 'k1'}), + ('currency', f'{NS}.Currency', 'USD', {'@scheme': 'urn:currencies'}), +]) +def test_basic_and_enum_option_metadata_is_retained(field, tag, value, metadata): + expected = {'basic_meta': {'@type': tag, '@data': value, **metadata}} + restored = Root.rune_deserialize(expected) + option = getattr(restored.basic_meta, field) + assert all(option.get_meta(key) == val for key, val in metadata.items()) + assert content(restored) == expected + + +@pytest.mark.parametrize('field, value', [ + ('located', {'@key:scoped': 'outer', '@data': {'@type': f'{NS}.A', 'value': 'foo'}}), + ('located_option', {'@type': f'{NS}.ChoiceAB', '@key:scoped': 'inner', + '@data': {'@type': f'{NS}.B', 'value': 'foo'}}), +]) +def test_valid_choice_field_and_option_metadata_wrappers(field, value): + model = Root.rune_deserialize({field: value}) + assert content(model) == {field: value} + + +def test_references_select_the_option_and_preserve_target_identity(): + data = { + 'keyed': {'@key:external': 'k1', 'value': 'target'}, + 'referenced_option': {'@type': f'{NS}.Keyed', '@ref:external': 'k1'}, + 'located': {'@key:scoped': 'c1', '@data': {'@type': f'{NS}.A', 'value': 'foo'}}, + 'referenced_choice_option': {'@type': f'{NS}.ChoiceAB', '@ref:scoped': 'c1'}, + 'choice_ref': {'@ref:scoped': 'c1'}, + 'entries': [{'@ref:scoped': 'c1'}, {'@type': f'{NS}.B', 'value': 'inline'}], + } + model = Root.rune_deserialize(data) + assert model.referenced_option.Keyed is model.keyed + assert model.referenced_choice_option.ChoiceAB is model.located + assert model.choice_ref is model.located + assert model.entries[0] is model.located + assert content(model) == data + + +def test_key_inside_selected_leaf_is_registered(): + data = { + 'data': {'@type': f'{NS}.A', 'value': 'foo', + 'keyed': {'@key:external': 'k1', 'value': 'target'}}, + 'keyed_ref': {'@ref:external': 'k1'}, + } + model = Root.rune_deserialize(data) + assert model.keyed_ref is model.data.A.keyed + assert content(model) == data + + +@pytest.mark.parametrize('cls, initial', [ + (ChoiceAB, {'A': A(value='foo')}), + (ChoiceBasic, {'string': 'foo'}), + (ChoiceDeep, {'tree': ChoiceTree(ChoiceCD=ChoiceCD(D=D(value='foo')))}), +]) +def test_concrete_choice_roots_round_trip(cls, initial): + original = cls(**initial) + restored = cls.rune_deserialize(original.rune_serialize()) + assert type(restored) is cls + assert restored == original + + +@pytest.mark.parametrize('load', [ + lambda cls, data: cls(**data), + lambda cls, data: cls.model_validate(data), +]) +def test_python_construction_keeps_native_field_dictionaries(load): + model = load(Root, {'data': {'A': {'value': 'foo'}}}) + assert model.data.A.value == 'foo' + + +def test_missing_choice_dispatch_reports_regeneration_requirement(): + class OutdatedChoice(BaseDataClass): + _FQRTN: ClassVar[str] = f'{NS}.OutdatedChoice' + _RUNE_CHOICE: ClassVar[bool] = True + string: str | None = None + + with pytest.raises(RuntimeError, match='OutdatedChoice is missing _CHOICE_DISPATCH.*Regenerate the model'): + OutdatedChoice.model_validate_json('{"@type": "string", "@data": "foo"}') + + +@pytest.mark.parametrize('fields', [ + pytest.param({'string': 'foo'}, id='primitive'), + pytest.param({'currency': Currency.USD}, id='enum'), + pytest.param({'leaf': A(value='foo')}, id='data'), +]) +@pytest.mark.parametrize('dump', [ + lambda value: value.model_dump(), + lambda value: value.model_dump_json(), +]) +def test_missing_choice_dispatch_reports_regeneration_requirement_on_write(fields, dump): + class OutdatedChoice(BaseDataClass): + _FQRTN: ClassVar[str] = f'{NS}.OutdatedChoice' + _RUNE_CHOICE: ClassVar[bool] = True + string: str | None = None + currency: Currency | None = None + leaf: A | None = None + + with pytest.raises(PydanticSerializationError, + match='OutdatedChoice is missing _CHOICE_DISPATCH.*Regenerate the model'): + dump(OutdatedChoice(**fields)) + + +def test_python_construction_also_accepts_tagged_values(): + model = Root(hooked={'@type': f'{NS}.A', 'value': 'foo'}) + assert model.hooked.A.value == 'foo' + + +def test_builders_and_function_returns_materialize_choices(): + direct = ObjectBuilder(ChoiceAB) + direct.A.value = 'foo' + assert direct.to_model().A.value == 'foo' + parent = ObjectBuilder(Root) + parent.deep.tree.ChoiceCD.D.value = 'bar' + result = rune_finalize_return(parent) + assert isinstance(result, Root) + assert result.deep.tree.ChoiceCD.D.value == 'bar' + + +@pytest.mark.parametrize('load', [ + lambda data: Root.rune_deserialize(data, validate_model=False), + lambda data: Root.model_validate_json(json.dumps(data)), + lambda data: Root.model_validate_json(json.dumps(data), strict=True), + lambda data: TypeAdapter(Root).validate_json(json.dumps(data)), +]) +@pytest.mark.parametrize('value, error, message', [ + ({'A': {'value': 'foo'}}, ValidationError, "Missing required '@type'"), + ({}, ValidationError, "Missing required '@type'"), + ({'@type': f'{NS}.C', 'value': 'foo'}, ValidationError, 'not an option'), + ({'@type': f'{NS}.ChoiceAB', 'A': {'value': 'foo'}}, ValidationError, 'not an option'), + ({'@type': None}, TypeError, "Choice '@type' must be a string"), + ({'@type': 5}, TypeError, "Choice '@type' must be a string"), + ({'@type': []}, TypeError, "Choice '@type' must be a string"), + ({'@data': None}, TypeError, "Choice '@data' must contain an object"), + ({'@data': 'foo'}, TypeError, "Choice '@data' must contain an object"), + ({'@data': 5}, TypeError, "Choice '@data' must contain an object"), + ({'@data': []}, TypeError, "Choice '@data' must contain an object"), + ({'@data': {}}, ValidationError, "Missing required '@type'"), + ({'@data': {'@type': 5}}, TypeError, "Choice '@type' must be a string"), +]) +def test_wire_readers_reject_invalid_choice_inputs(load, value, error, message): + with pytest.raises(error, match=message): + load({'data': value}) + + +@pytest.mark.parametrize('value', [{'@type': 'string'}, {'@type': 'string', '@data': None}]) +def test_basic_wire_option_requires_non_null_data(value): + with pytest.raises(ValidationError): + Root.rune_deserialize({'basic': value}, validate_model=False) + + +def test_wrapped_basic_wire_option_requires_data(): + with pytest.raises(ValidationError, match='Missing required'): + Root.rune_deserialize({'basic_meta': {'@type': 'string'}}, validate_model=False) + + +@pytest.mark.parametrize('load', [ + lambda cls: cls.model_validate_json('{}'), + lambda cls: cls.model_validate_json('{}', strict=True), + lambda cls: TypeAdapter(cls).validate_json('{}'), +]) +def test_native_choice_default_factory_works_during_json_validation(load): + class FactoryRoot(BaseDataClass): + data: ChoiceAB = Field(default_factory=lambda: ChoiceAB(A=A(value='default'))) + + assert load(FactoryRoot).data.A.value == 'default' + + +@pytest.mark.parametrize('load', [ + lambda cls: cls.model_validate_json('{}'), + lambda cls: cls.model_validate_json('{}', strict=True), + lambda cls: TypeAdapter(cls).validate_json('{}'), +]) +def test_native_builder_default_factory_works_during_json_validation(load): + def make_choice(): + builder = ObjectBuilder(ChoiceAB) + builder.A.value = 'default' + return builder.to_model() + + class FactoryRoot(BaseDataClass): + data: ChoiceAB = Field(default_factory=make_choice) + + assert load(FactoryRoot).data.A.value == 'default' + + +def test_choice_option_cache_is_invalidated_by_model_rebuild(): + class Deferred(Choice): + model_config = ConfigDict(defer_build=True) + _FQRTN: ClassVar[str] = f'{NS}.Deferred' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {f'{NS}.A': 'value'} + value: 'Late | None' = None + + Deferred.model_rebuild(_types_namespace={'Late': A}) + assert Deferred(value=A(value='a')).model_dump(exclude_none=True)['@type'] == f'{NS}.A' + Deferred.model_fields['value'].annotation = B | None + Deferred._CHOICE_DISPATCH = {f'{NS}.B': 'value'} + Deferred.model_rebuild(force=True) + rebuilt = Deferred.model_validate_json(json.dumps({'@type': f'{NS}.B', 'value': 'b'})) + assert type(rebuilt.value) is B + assert rebuilt.model_dump(exclude_none=True)['@type'] == f'{NS}.B' + + +def test_failed_wire_read_does_not_change_python_construction_mode(): + with pytest.raises(ValidationError): + Root.rune_deserialize({'data': {'A': {'value': 'foo'}}}) + assert Root.model_validate({'data': {'A': {'value': 'foo'}}}).data.A.value == 'foo' + + +@pytest.mark.parametrize('options', [ + {'exclude': {'A'}}, {'include': set()}, {'exclude': {'A': True}}, +]) +def test_filtering_out_selected_option_does_not_raise(options): + assert ChoiceAB(A=A(value='foo')).model_dump(**options) == {} + + +def test_filtering_leaf_fields_keeps_discriminator(): + assert ChoiceAB(A=A(value='foo')).model_dump( + exclude={'A': {'value'}}, exclude_none=True) == {'@type': f'{NS}.A'} + + +def test_serialization_alias_uses_serialized_field_name(): + class Aliased(Choice): + _FQRTN: ClassVar[str] = f'{NS}.Aliased' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {f'{NS}.A': 'a'} + a: A | None = Field(None, serialization_alias='selected') + + assert Aliased(a=A(value='foo')).model_dump( + by_alias=True, exclude_none=True) == {'@type': f'{NS}.A', 'value': 'foo'} + + +def test_mangled_basic_option_uses_original_wire_name(): + class Mangled(Choice): + _FQRTN: ClassVar[str] = f'{NS}.Mangled' + _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {'class': 'rune_attr_class'} + rune_attr_class: str | None = None + + value = Mangled(rune_attr_class='foo') + assert value.model_dump() == {'@type': 'class', '@data': 'foo'} + assert Mangled.model_validate_json(value.model_dump_json()).rune_attr_class == 'foo' + + +def test_multiple_options_and_subtypes_are_rejected_on_write(): + with pytest.raises(PydanticSerializationError, match='Multiple choice options'): + ChoiceAB(A=A(value='a'), B=B(value='b')).model_dump() + + class SubA(A): + extra: str = 'subtype' + + choice = ChoiceAB() + choice.A = SubA(value='foo') + with pytest.raises(PydanticSerializationError, match='must be exactly'): + choice.model_dump() + + +def test_no_option_matches_java_empty_root_but_not_empty_nested_choice(): + assert Choice().model_dump() == {} + assert ChoiceAB().model_dump() == {} + with pytest.raises(PydanticSerializationError, match='has no selected option'): + ChoiceTree(ChoiceAB=ChoiceAB()).model_dump() + + +def test_choice_field_metadata_stays_in_its_field_wrapper(): + model = Root(located=ChoiceAB(A=A(value='foo'))) + model.located.set_meta(**{'@key:scoped': 'location'}) + written = json.loads(model.rune_serialize()) + assert written['located'] == { + '@key:scoped': 'location', '@data': {'@type': f'{NS}.A', 'value': 'foo'}, + } + root_written = json.loads(model.located.rune_serialize()) + assert not any(key.startswith('@key') for key in root_written) + assert model.located.get_meta('@key:scoped') == 'location' + + +def test_copy_on_write_choices_still_construct_without_mutating_source(): + original = ChoiceAB(A=A(value='original')) + proxy = rune_cow(original) + proxy.A.value = 'changed' + restored = ChoiceAB.model_validate(proxy) + assert restored.A.value == 'changed' + assert original.A.value == 'original' + + +def test_ordinary_optional_fields_are_not_treated_as_choices(): + class Ordinary(BaseDataClass): + a: A | None = None + b: B | None = None + + value = Ordinary(a=A(value='foo')) + assert value.model_dump(exclude_none=True) == {'a': {'value': 'foo'}} diff --git a/test/test_deferred_ref_deserialization.py b/test/test_deferred_ref_deserialization.py index ebc187d..dc7916b 100644 --- a/test/test_deferred_ref_deserialization.py +++ b/test/test_deferred_ref_deserialization.py @@ -9,19 +9,19 @@ Pydantic's model_rebuild reads model_fields["f"].annotation (the bare type, e.g. Optional[Party | BaseReference]) to build the core schema. The PlainValidator placed in __annotations__["f"] is ignored. The resulting -schema for the Party union arm is function-wrap[_deserialize_refs()], which +schema for the Party union arm is function-wrap[_deserialize_rune_model()], which calls handler(data) before checking whether data is a reference dict. When data is {'@ref:external': 'some-id'}, handler fails trying to construct Party because its required fields are absent. -The fix is to have _deserialize_refs check for reference dicts BEFORE calling +The fix is to have _deserialize_rune_model check for reference dicts BEFORE calling handler, consistent with ComplexTypeMetaDataMixin.deserialize. Target keys must also be retained when the deferred schema bypasses the field metadata validator, so references in sibling lists can find their targets. Note on placement: this test lives in rune-runtime even though the Phase 1/2/3 pattern originates in the rune-python-generator's bundle generator. The runtime -has no awareness of bundles; however, _deserialize_refs is a runtime method and +has no awareness of bundles; however, _deserialize_rune_model is a runtime method and the fix must be applied there. The test simulates the bundle generator's output in plain Python so that the runtime can own a regression guard for this behaviour without depending on the generator.