Repository navigation
Refuse to save a subclass item that JSON would save as its base type [patch] - #113
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
…[patch] JsonFilePersistenceProvider<T> serializes items as the declared T, so a NavigationItem subclass in a Navigation<NavigationItem> lost its type and the properties it adds, and LoadStateAsync still reported success. SaveStateAsync now throws NotSupportedException naming the subclass when T has no polymorphism configured, before touching the saved file, so the last good save stays in place. The README shows how to configure polymorphism through the JSON options, and a test covers that round trip. Fixes #97 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW5pmswBjLMVcM1HgMb
|
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.



Fixes #97
What was wrong
JsonFilePersistenceProvider<T>.SaveStateAsyncserialized items as the declaredT. ANavigationItemsubclass in aNavigation<NavigationItem>was saved without its type or the properties it adds, andLoadStateAsyncstill returnedtrue. Nothing reported the loss.Change
This takes the issue's "at minimum" option, which leaves the polymorphism setup to the consumer.
SaveStateAsyncchecks each item. If an item's runtime type isn'tTand the JSON options configure no polymorphism forT(GetTypeInfo(typeof(T)).PolymorphismOptions is null), it throwsNotSupportedException. The message names the subclass and says how to fix it. The temp-file swap never starts, so the last good save stays in place.Tis skipped. That state can't be loaded back in any case, andLoadStateAsyncalready returns null for it (LoadStateAsync_InterfaceItemType_ReturnsNullInsteadOfThrowing), so its behaviour is unchanged.jsonOptions.Behaviour change: an app that saved subclass items without polymorphism configured now gets an exception from
SaveStateAsync. Until now, that data was dropped without any report.Tests
SaveStateAsync_SubclassItemWithoutPolymorphism_ThrowsNamingTheTypeAndKeepsTheLastSavefails onmain(no exception, data dropped) and passes with the change.SaveThenLoad_SubclassItemWithPolymorphismConfigured_RoundTripsItsTypeAndPropertieschecks the documented setup. A plain item and a subclass round-trip with their runtime types and the subclass'sFilePath.Full suite: 67 tests, 65 passed, 2 skipped (the unreadable-file tests are inconclusive when run as root).
Open PR #108 also edits
SaveStateAsync, in the swap at its end, while this change adds a check at its start. Whichever merges second may need a small rebase.Generated by Claude Code