Skip to content

Make OpaqueChange fallback opt-in and EntityType nullable - #91

Merged
hahn-kev merged 2 commits into
mainfrom
claude/opaque-change-fallback-entitytype-a2097c
Jul 30, 2026
Merged

Make OpaqueChange fallback opt-in and EntityType nullable#91
hahn-kev merged 2 commits into
mainfrom
claude/opaque-change-fallback-entitytype-a2097c

Conversation

@hahn-kev-bot

@hahn-kev-bot hahn-kev-bot commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

per our discussion we can now opt out of this new behavior as it requires work on the fwlite side before we can adopt it and we don't want to block upgrading.

There's also a new HarmonyConfig.ConfigureExternalJsonOptions to setup an external options object with whatever it needs to handle changes and objects.


AI summary

Follow-up to #80 (OpaqueChange for unknown IChange $type). Two refinements:

1. Opaque fallback is now opt-in. New UnknownChangeHandling enum (Throw, Fallback) exposed as HarmonyConfig.UnknownChangeHandling. Default is Throw — an unknown $type now raises a JsonException during deserialization unless the caller sets UnknownChangeHandling.Fallback, which restores the #80 behavior of preserving the payload as an OpaqueChange.

⚠️ Behavior change: clients relying on #80's implicit opaque fallback must now explicitly set UnknownChangeHandling.Fallback.

2. OpaqueChange.EntityType no longer throws. It returns null, since an opaque change genuinely has no known entity type. IChange.EntityType is now Type? to advertise this; Change<T> continues to return typeof(T).

Wiring: the handling mode flows from HarmonyConfig into PeekThenConcreteChangeConverter, which throws or falls back accordingly. SnapshotWorker only encounters OpaqueChange in Fallback mode, so no other production code changed.

Test plan

  • ChangeConverterTests updated so its SampleOptions helper opts into Fallback for the existing opaque round-trip tests.
  • Added a test asserting an unknown $type throws JsonException when handling is Throw.
  • Added an assertion that OpaqueChange.EntityType is null.
  • dotnet test src/SIL.Harmony.Tests — all pass (the one failure, DataModelPerformanceTests.AddingChangePerformance, is a pre-existing debug-only Assert.Fail).

Summary by CodeRabbit

  • New Features
    • Added an UnknownChangeHandling setting to control what happens when an unrecognized change $type is encountered during deserialization (Throw by default, or Fallback to preserve unknown changes as opaque data for round-tripping).
    • Added support for applying Harmony JSON configuration to externally provided JsonSerializerOptions.
  • Bug Fixes
    • Accessing the entity type for unknown/opaque changes no longer throws an exception; it now returns no type information instead.

Unknown IChange $type handling is now configurable via
HarmonyConfig.UnknownChangeHandling, defaulting to Throw. Callers opt
into OpaqueChange fallback with UnknownChangeHandling.Fallback.

OpaqueChange.EntityType now returns null instead of throwing, and
IChange.EntityType is nullable to reflect that.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds configurable handling for unknown change $type values, defaulting to exceptions while supporting raw OpaqueChange fallback. The public change contract now permits a null entity type, serializer configuration is externally composable, and tests cover the behavior.

Changes

Unknown change handling

Layer / File(s) Summary
Handling policy and serializer wiring
src/SIL.Harmony/Config/UnknownChangeHandling.cs, src/SIL.Harmony/Config/HarmonyConfig.cs
Adds Throw and Fallback modes, configures the default policy, and centralizes JSON option customization.
Unknown discriminator conversion
src/SIL.Harmony/Changes/Change.cs, src/SIL.Harmony/Changes/OpaqueChange.cs, src/SIL.Harmony/Changes/PeekThenConcreteChangeConverter.cs
Throws JsonException for unknown discriminators unless fallback is enabled; fallback returns an opaque change whose EntityType is null.
Unknown type behavior and test execution
src/SIL.Harmony.Tests/ChangeConverterTests.cs, src/SIL.Harmony.Tests/DataModelPerformanceTests.cs
Configures handling per test, verifies throwing behavior, and skips the performance test in debug builds.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant JsonSerializer
  participant HarmonyConfig
  participant PeekThenConcreteChangeConverter
  participant OpaqueChange
  JsonSerializer->>HarmonyConfig: configure JSON options
  HarmonyConfig->>PeekThenConcreteChangeConverter: register handling policy
  JsonSerializer->>PeekThenConcreteChangeConverter: deserialize unknown $type
  alt Throw
    PeekThenConcreteChangeConverter-->>JsonSerializer: JsonException
  else Fallback
    PeekThenConcreteChangeConverter->>OpaqueChange: preserve raw JSON
    OpaqueChange-->>JsonSerializer: opaque IChange
  end
Loading

Possibly related PRs

Suggested reviewers: hahn-kev

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 9.09% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main changes: opt-in OpaqueChange fallback and making EntityType nullable.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/opaque-change-fallback-entitytype-a2097c

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/SIL.Harmony.Tests/ChangeConverterTests.cs`:
- Around line 12-17: Update SampleOptions to support creating
JsonSerializerOptions without explicitly setting
HarmonyConfig.UnknownChangeHandling, and use that unset-options path in the
default-policy test so it verifies the actual configured default. Keep passing
Fallback explicitly in tests that target fallback behavior, including the cases
around lines 53–63.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 07ed0b66-e788-4c06-8534-bd70800148bd

📥 Commits

Reviewing files that changed from the base of the PR and between c858cb4 and 408b4f7.

📒 Files selected for processing (6)
  • src/SIL.Harmony.Tests/ChangeConverterTests.cs
  • src/SIL.Harmony/Changes/Change.cs
  • src/SIL.Harmony/Changes/OpaqueChange.cs
  • src/SIL.Harmony/Changes/PeekThenConcreteChangeConverter.cs
  • src/SIL.Harmony/Config/HarmonyConfig.cs
  • src/SIL.Harmony/Config/UnknownChangeHandling.cs

Comment thread src/SIL.Harmony.Tests/ChangeConverterTests.cs
@hahn-kev
hahn-kev merged commit f9e1a97 into main Jul 30, 2026
7 checks passed
@hahn-kev
hahn-kev deleted the claude/opaque-change-fallback-entitytype-a2097c branch July 30, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants