Return null, not throw, for stored JSON with no $type discriminator - #167
Merged
Merged
Conversation
…inator
System.Text.Json's polymorphic deserializer throws NotSupportedException for
well-formed JSON that lacks a $type property, such as {} or {"Token":"x"}.
Deserialize and DeserializeFromString caught only JsonException, so a
corrupt or foreign entry in a native store made CredentialCache.TryGet throw
instead of returning false. Both now treat that payload as unknown.
Fixes #164
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014jCUYfoRMwhybsUaSa1NFm
|
This was referenced Sep 28, 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.



Fixes #164
What was wrong
When well-formed JSON has no
$typeproperty (for example{}or{"Token":"x"}), System.Text.Json's polymorphic deserializer throwsNotSupportedException.CredentialSerialization.DeserializeandDeserializeFromStringcaught onlyJsonException. A corrupt, hand-edited or foreign entry in the Windows, macOS or Linux store therefore madeCredentialCache.TryGetthrow, even though theTrycontract promises it returnsfalse.Change
Both methods now also catch
NotSupportedExceptionand returnnull, which matches their documented behaviour ("returns null if the bytes do not represent a known credential"). The library targets net9.0 and net10.0 only, so the older System.Text.JsonInvalidOperationExceptionvariant mentioned in the triage doesn't apply.Tests
New file
UnknownPayloadTests:DeserializeandDeserializeFromStringreturnnullfor{},{"Token":"x"},{"$type":"Bogus"},[]and123.TryGetreturnsfalsefor a planted{}or{"Token":"x"}entry. The entry sits in aRawBlobCredentialStore, which reads raw bytes throughDeserializeAndScrubthe same way the native stores do.With
CredentialSerialization.csreverted, the 6 missing-discriminator cases fail. With the fix, the suite passes: 61 passed, and 5 native-store tests skipped as inconclusive with no keyring on the runner.🤖 Generated with Claude Code
https://claude.ai/code/session_014jCUYfoRMwhybsUaSa1NFm
Generated by Claude Code