Repository navigation
Fix match java choice serialization - - #75
Closed
dschwartznyc wants to merge 2 commits into
Closed
dschwartznyc wants to merge 2 commits into
dschwartznyc wants to merge 2 commits into
Conversation
This was referenced Nov 12, 2026
This was referenced Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Read and write choice types in Java's flattened
@typeformatCloses #55. This resolves the last open item of #42 (bug 2).
Python and Java wrote Rune
choicetypes in different JSON forms, so neither could read the other's choice data. Python now uses Java's form, whichrune-commonand CDM's expected output already use."choiceData": {"A": {"fieldA": "foo"}}"choiceData": {"@type": "example.demo.A", "fieldA": "foo"}"choiceBasic": {"string": "x"}"choiceBasic": {"@type": "string", "@data": "x"}{"@key:scoped": "c1", "@data": {"@type": ..., ...}}Changes
Writing (
BaseDataClass._serialize_refs,ComplexTypeMetaDataMixin.serialise)@typenames the selected option, and the option's fields are written directly on the choice.{"@type": "<option>", "@data": <value>}.@typeonly when the option has metadata or is a reference.@data.ValueErrorif more than one option is set, or if an option holds a subtype of its declared class (see the limitation below).Reading (
BaseDataClass._deserialize_refs,ComplexTypeMetaDataMixin.deserialize)@typeselects the option, including an option of a nested choice.@type, or a choice without@type, is rejected.@typebelongs to the selected option, not to the attribute.Building in Python is unchanged.
ChoiceBasic(string='x')still takes option names as fields. A metaclass (_RuneModelMeta) records direct construction, so the validator can tell it apart from the JSON{"string": "x"}. Defining__init__instead would have made pydantic build nested models through it, so JSON would also have looked like direct construction.Tests
test/test_choice_serialization.py: 24 tests of reading, writing and round trips, using generated-style classes for data, basic, nested, list and metadata cases, references into choices, bundled classes and invalid input.serializationor CDM packages.main. Both remaining failures (extension/extended-type-polymorphic*.json) need subtypes kept on assignment (Assigning an object to an attribute should store a copy that keeps its actual type #66).fix-circular-import-errorPython tests: all 18 choice-format tests pass. That branch generates_RUNE_CHOICEand the matching classes, and depends on this PR.Known limitation
An option holding a subtype of its declared class is rejected on write, rather than written with the subtype's
@type. Keeping the subtype depends on #66.test_write_subtype_of_optionwas removed from this PR, to be added with the #66 fix.Compatibility
This changes the JSON format for choice types: data written by earlier Python runtimes in the nested form can't be read back. The new form is the one Java and CDM use.