Skip to content

Fix match java choice serialization - - #75

Closed
dschwartznyc wants to merge 2 commits into
mainfrom
fix-match-java-choice-serialization
Closed

dschwartznyc wants to merge 2 commits into
mainfrom
fix-match-java-choice-serialization

Conversation

@dschwartznyc

Copy link
Copy Markdown
Contributor

Read and write choice types in Java's flattened @type format

Closes #55. This resolves the last open item of #42 (bug 2).

Python and Java wrote Rune choice types in different JSON forms, so neither could read the other's choice data. Python now uses Java's form, which rune-common and CDM's expected output already use.

Before (Python) After (matches Java)
Data type option "choiceData": {"A": {"fieldA": "foo"}} "choiceData": {"@type": "example.demo.A", "fieldA": "foo"}
Basic type option "choiceBasic": {"string": "x"} "choiceBasic": {"@type": "string", "@data": "x"}
Choice attribute with metadata rejected on read {"@key:scoped": "c1", "@data": {"@type": ..., ...}}

Changes

Writing (BaseDataClass._serialize_refs, ComplexTypeMetaDataMixin.serialise)

  • @type names the selected option, and the option's fields are written directly on the choice.
  • Basic type and enum options are written as {"@type": "<option>", "@data": <value>}.
  • A nested choice is already flat, so it's written as it is. It gets an @type only when the option has metadata or is a reference.
  • Metadata on a choice attribute sits beside the flattened choice, under @data.
  • Writing fails with a ValueError if 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)

  • @type selects the option, including an option of a nested choice.
  • An unknown @type, or a choice without @type, is rejected.
  • The old nested form is no longer accepted when reading JSON.
  • A reference beside @type belongs to the selected option, not to the attribute.
  • Keys inside choice data are registered, so references to them resolve.

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.
  • Full suite: 606 passed, 20 skipped. The skipped tests need generated serialization or CDM packages.
  • The Python and Java serialize Rune choice types differently #55 test module: 11 of 11 pass.
  • rune-python-generator's serialization tests, which round-trip rune-common's samples: 50 of 52 pass, against 31 of 52 on 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).
  • rune-python-generator's fix-circular-import-error Python tests: all 18 choice-format tests pass. That branch generates _RUNE_CHOICE and 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_option was 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.

@github-actions

Copy link
Copy Markdown

Test Results

626 tests  +49   606 ✅ +49   3s ⏱️ ±0s
  1 suites ± 0    20 💤 ± 0 
  1 files   ± 0     0 ❌ ± 0 

Results for commit f645c41. ± Comparison against base commit 272e179.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

superseded This PR/Issue has been superseded by another one.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python and Java serialize Rune choice types differently

3 participants