Skip to content

Support Java flat serialization for Rune choices - #77

Merged
dschwartznyc merged 3 commits into
mainfrom
feat/java-flat-choice-serialization
Nov 13, 2026
Merged

dschwartznyc merged 3 commits into
mainfrom
feat/java-flat-choice-serialization

Conversation

@plamen-neykov

@plamen-neykov plamen-neykov commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Rune choices now serialize directly to their selected leaf using Java's
flat JSON representation. For example, ChoiceAB(A=A(value="foo")) writes
{"@type": "example.A", "value": "foo"}.

  • Route incoming discriminators through generated _CHOICE_DISPATCH maps,
    including deeply nested choices.
  • Support basic and enum options using @data.
  • Preserve valid metadata and references on choice-valued fields and options.
  • Preserve native Python construction, ObjectBuilder, and copy-on-write behavior.
  • Rename the internal model hooks to reflect their broader responsibilities.
  • Add 96 choice regression tests.

Compatibility

Choice models require regeneration with a generator that emits
_RUNE_CHOICE and _CHOICE_DISPATCH.

JSON readers require the flat format and a valid discriminator. Existing JSON
using nested option field names must be regenerated. Python constructors,
model_validate, and ObjectBuilder continue to accept native field dictionaries.

Ordinary types with a one-of condition retain their existing JSON structure.
The implementation assumes valid DSL-generated choices, which cannot declare
type-level metadata.

Validation

  • Runtime suite: 667 passed, 19 skipped, with the pre-existing Windows path
    failure in test_init_invalid_directory_or_module.
  • Generator choice tests: 20 passed.
  • Verified all eight generated leaf paths through three choice levels,
    with and without a Python namespace prefix.
  • Verified generated ObjectBuilder behavior and round-tripping of an actual
    Java metadata-wrapper payload.

Generator follow-up

Metadata-wrapped enum options require FQN discriminator keys. The current
generator still emits short names for the enum case.

Relationship to #75

This PR builds on @dschwartznyc's initial work on Java-compatible choice
serialization in #75. It retains and adapts the metadata-wrapper handling,
the distinction between option references and whole-choice references, and
the metaclass/context approach for distinguishing native Python construction
from JSON validation.

The implementation uses generated _CHOICE_DISPATCH tables for routing and
adds coverage for deeply nested choices, builders, serialization filters,
and namespace prefixes.

This PR supersedes #75. Once this PR is merged, #75 can be closed as superseded.

Closes #55

Read and write choices using generated discriminator dispatch maps.
Support deeply nested choices, basic and enum options, and field
metadata and reference wrappers. Preserve native Python construction
and ObjectBuilder behavior.

BREAKING CHANGE: JSON choice input requires the flat discriminated
format. Regenerate choice models with _RUNE_CHOICE and _CHOICE_DISPATCH.
@plamen-neykov plamen-neykov self-assigned this Nov 13, 2026
@plamen-neykov plamen-neykov added the enhancement New feature or request label Nov 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Test Results

694 tests  +103   674 ✅ +103   3s ⏱️ -1s
  1 suites ±  0    20 💤 ±  0 
  1 files   ±  0     0 ❌ ±  0 

Results for commit 0339820. ± Comparison against base commit 40abe43.

♻️ This comment has been updated with latest results.

Share the dispatch-table guard across deserialization and serialization.
Raise RuntimeError with model-regeneration guidance when _CHOICE_DISPATCH
is missing.

Add regression coverage for reading and for writing primitive, enum,
and data options.
@plamen-neykov
plamen-neykov marked this pull request as ready for review November 13, 2026 19:10
@plamen-neykov
plamen-neykov marked this pull request as draft November 13, 2026 20:32
@plamen-neykov
plamen-neykov marked this pull request as ready for review November 13, 2026 20:33

@dschwartznyc dschwartznyc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@dschwartznyc
dschwartznyc merged commit 8157f1c into main Nov 13, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python and Java serialize Rune choice types differently

2 participants