diff --git a/src/Verify.Tests/Serialization/ClaimsTests.ClaimsPrincipalWithIdentity.verified.txt b/src/Verify.Tests/Serialization/ClaimsTests.ClaimsPrincipalWithIdentity.verified.txt new file mode 100644 index 000000000..4ec91e065 --- /dev/null +++ b/src/Verify.Tests/Serialization/ClaimsTests.ClaimsPrincipalWithIdentity.verified.txt @@ -0,0 +1,15 @@ +{ + Principal: { + Identities: [ + { + Claims: [ + { + TheClaimType: TheClaimValue + } + ], + AuthenticationType: TheAuthenticationType + } + ] + }, + Name: TheValue +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/ClaimsTests.EmptyClaimsPrincipal.verified.txt b/src/Verify.Tests/Serialization/ClaimsTests.EmptyClaimsPrincipal.verified.txt new file mode 100644 index 000000000..2bc334882 --- /dev/null +++ b/src/Verify.Tests/Serialization/ClaimsTests.EmptyClaimsPrincipal.verified.txt @@ -0,0 +1,4 @@ +{ + Principal: {}, + Name: TheValue +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/ClaimsTests.cs b/src/Verify.Tests/Serialization/ClaimsTests.cs new file mode 100644 index 000000000..cd68d7f8a --- /dev/null +++ b/src/Verify.Tests/Serialization/ClaimsTests.cs @@ -0,0 +1,25 @@ +public class ClaimsTests +{ + [Fact] + public Task EmptyClaimsPrincipal() => + // An empty principal still writes an object. Writing no token would leave the + // writer mid-property and corrupt every member written after it. + Verify( + new + { + Principal = new ClaimsPrincipal(), + Name = "TheValue" + }); + + [Fact] + public Task ClaimsPrincipalWithIdentity() => + Verify( + new + { + Principal = new ClaimsPrincipal( + new ClaimsIdentity( + [new Claim("TheClaimType", "TheClaimValue")], + "TheAuthenticationType")), + Name = "TheValue" + }); +} diff --git a/src/Verify/Serialization/Converters/ClaimsPrincipalConverter.cs b/src/Verify/Serialization/Converters/ClaimsPrincipalConverter.cs index fa5e9586e..8d3aafd33 100644 --- a/src/Verify/Serialization/Converters/ClaimsPrincipalConverter.cs +++ b/src/Verify/Serialization/Converters/ClaimsPrincipalConverter.cs @@ -5,11 +5,10 @@ class ClaimsPrincipalConverter : { public override void Write(VerifyJsonWriter writer, ClaimsPrincipal principal) { - if (!principal.Identities.Any()) - { - return; - } - + // The object is always written, even with no identities. Writing no token at all + // leaves the writer in Property state after the caller has written the member + // name, which corrupts every subsequent write. WriteMember drops the empty + // Identities collection, so an empty principal renders as {}. writer.WriteStartObject(); writer.WriteMember(principal, principal.Identities, "Identities"); writer.WriteEndObject();