diff --git a/src/Microsoft.IdentityModel.Tokens/Json/JsonSerializerPrimitives.cs b/src/Microsoft.IdentityModel.Tokens/Json/JsonSerializerPrimitives.cs index 56f415a2be..8adea1d2b5 100644 --- a/src/Microsoft.IdentityModel.Tokens/Json/JsonSerializerPrimitives.cs +++ b/src/Microsoft.IdentityModel.Tokens/Json/JsonSerializerPrimitives.cs @@ -941,6 +941,8 @@ public static void WriteObject(ref Utf8JsonWriter writer, string key, object obj writer.WriteNumber(key, d); else if (obj is float f) writer.WriteNumber(key, f); + else if (obj is Guid g) + writer.WriteString(key, g); else throw LogHelper.LogExceptionMessage( new ArgumentException( diff --git a/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonSerializerPrimitivesTests.cs b/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonSerializerPrimitivesTests.cs index d08348b3e8..d27ab79791 100644 --- a/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonSerializerPrimitivesTests.cs +++ b/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonSerializerPrimitivesTests.cs @@ -20,7 +20,7 @@ public class JsonSerializerPrimitivesTests [Fact] public void CheckMaxDepthReading() { - var document = JsonDocument.Parse(@"{""key"":" + new string('[', 62) + @"""value""" + new string(']', 62) + "}"); + var document = JsonDocument.Parse(@"{""key"":" + new string('[', 62) + @"""value""" + new string(']', 62) + "}"); Dictionary value; JsonSerializerPrimitives.TryCreateTypeFromJsonElement(document.RootElement, out value); @@ -76,7 +76,7 @@ public void CheckNumberOfProperties() json.Append('{'); - foreach(var i in Enumerable.Range(0, 100)) + foreach (var i in Enumerable.Range(0, 100)) { json.Append($@"""key-{i}"":""value-{i}"""); if (i != 99) @@ -135,7 +135,7 @@ public void CheckMaximumDepthWriting(JsonSerializerTheoryData theoryData) IdentityComparer.AreEqual(json, theoryData.Json, context); theoryData.ExpectedException.ProcessNoException(context); } - catch (Exception ex ) + catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } @@ -248,7 +248,7 @@ public static TheoryData CheckMaximumDepthWritingTheor Json = json, PropertyName = "key", Object = result, - ExpectedException = new ExpectedException(typeExpected:typeof(InvalidOperationException)) + ExpectedException = new ExpectedException(typeExpected: typeof(InvalidOperationException)) }); (json, result) = CreateJsonSerializerTheoryData(50); @@ -259,7 +259,7 @@ public static TheoryData CheckMaximumDepthWritingTheor var mergedObjects = new Dictionary { ["key1"] = new Dictionary { ["key"] = result }, - ["key2"] = new Dictionary { ["key"] = result2 }, + ["key2"] = new Dictionary { ["key"] = result2 }, }; theoryData.Add(new JsonSerializerTheoryData($"MultipleObjects") @@ -338,7 +338,7 @@ public void Deserialize(JsonSerializerTheoryData theoryData) jsonIdentityModel = JsonConvert.DeserializeObject(theoryData.Json); theoryData.IdentityModelSerializerExpectedException.ProcessNoException(serializationContext); } - catch (Exception ex ) + catch (Exception ex) { theoryData.IdentityModelSerializerExpectedException.ProcessException(ex, serializationContext); } @@ -586,6 +586,11 @@ public static TheoryData SerializeTheoryData JsonTestClass = CreateJsonTestClass("String") }); + theoryData.Add(new JsonSerializerTheoryData("Guid") + { + JsonTestClass = CreateJsonTestClass("Guid") + }); + return theoryData; } } @@ -618,6 +623,9 @@ private static JsonTestClass CreateJsonTestClass(string propertiesToSet) if (propertiesToSet == "*" || propertiesToSet.Contains("String")) jsonTestClass.String = "string"; + if (propertiesToSet == "*" || propertiesToSet.Contains("Guid")) + jsonTestClass.Guid = Guid.NewGuid(); + return jsonTestClass; } } diff --git a/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonTestClass.cs b/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonTestClass.cs index 2bfa4cb636..340e7eabd0 100644 --- a/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonTestClass.cs +++ b/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonTestClass.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; using System.Collections.Generic; using Microsoft.IdentityModel.Logging; @@ -39,6 +40,8 @@ public IList ListString public string String { get; set; } + public Guid? Guid { get; set; } + public bool ShouldSerializeAdditionalData() { return AdditionalData.Count > 0; @@ -71,5 +74,10 @@ public bool ShouldSerializeString() { return String != null; } + + public bool ShouldSerializeGuid() + { + return Guid.HasValue; + } } } diff --git a/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonTestClassSerializer.cs b/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonTestClassSerializer.cs index b6c8164b72..68d90f1d22 100644 --- a/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonTestClassSerializer.cs +++ b/test/Microsoft.IdentityModel.Tokens.Tests/Json/JsonTestClassSerializer.cs @@ -216,6 +216,9 @@ public static void Serialize(JsonTestClass jsonTestClass, Utf8JsonWriter writer, if (!string.IsNullOrEmpty(jsonTestClass.String)) writer.WriteString("String", jsonTestClass.String); + if (jsonTestClass.Guid.HasValue) + writer.WriteString("Guid", jsonTestClass.Guid.Value); + if (jsonTestClass.AdditionalData != null && jsonTestClass.AdditionalData.Count > 0) { foreach (var item in jsonTestClass.AdditionalData) diff --git a/test/System.IdentityModel.Tokens.Jwt.Tests/JwtPayloadTest.cs b/test/System.IdentityModel.Tokens.Jwt.Tests/JwtPayloadTest.cs index 9798607dc5..9d343e5322 100644 --- a/test/System.IdentityModel.Tokens.Jwt.Tests/JwtPayloadTest.cs +++ b/test/System.IdentityModel.Tokens.Jwt.Tests/JwtPayloadTest.cs @@ -521,5 +521,15 @@ public void GetStandardClaimNull() var issuer = jwtPayload.Iss; Assert.True(issuer == null); } + + [Fact] + public void TestGuidClaim() + { + JwtPayload jwtPayload = new JwtPayload(); + Guid guid = Guid.NewGuid(); + string expected = $"{{\"appid\":\"{guid}\"}}"; + jwtPayload.Add("appid", guid); + Assert.Equal(expected, jwtPayload.SerializeToJson()); + } } }