Skip to content

Commit

Permalink
Add back support for Guid values in JwtPayload... (#2440)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinH-MS authored Jan 26, 2024
1 parent 3be3173 commit ecd1912
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> value;
JsonSerializerPrimitives.TryCreateTypeFromJsonElement(document.RootElement, out value);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -248,7 +248,7 @@ public static TheoryData<JsonSerializerTheoryData> CheckMaximumDepthWritingTheor
Json = json,
PropertyName = "key",
Object = result,
ExpectedException = new ExpectedException(typeExpected:typeof(InvalidOperationException))
ExpectedException = new ExpectedException(typeExpected: typeof(InvalidOperationException))
});

(json, result) = CreateJsonSerializerTheoryData(50);
Expand All @@ -259,7 +259,7 @@ public static TheoryData<JsonSerializerTheoryData> CheckMaximumDepthWritingTheor
var mergedObjects = new Dictionary<string, object>
{
["key1"] = new Dictionary<string, object> { ["key"] = result },
["key2"] = new Dictionary<string, object> { ["key"] = result2 },
["key2"] = new Dictionary<string, object> { ["key"] = result2 },
};

theoryData.Add(new JsonSerializerTheoryData($"MultipleObjects")
Expand Down Expand Up @@ -338,7 +338,7 @@ public void Deserialize(JsonSerializerTheoryData theoryData)
jsonIdentityModel = JsonConvert.DeserializeObject<JsonTestClass>(theoryData.Json);
theoryData.IdentityModelSerializerExpectedException.ProcessNoException(serializationContext);
}
catch (Exception ex )
catch (Exception ex)
{
theoryData.IdentityModelSerializerExpectedException.ProcessException(ex, serializationContext);
}
Expand Down Expand Up @@ -586,6 +586,11 @@ public static TheoryData<JsonSerializerTheoryData> SerializeTheoryData
JsonTestClass = CreateJsonTestClass("String")
});

theoryData.Add(new JsonSerializerTheoryData("Guid")
{
JsonTestClass = CreateJsonTestClass("Guid")
});

return theoryData;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -39,6 +40,8 @@ public IList<string> ListString

public string String { get; set; }

public Guid? Guid { get; set; }

public bool ShouldSerializeAdditionalData()
{
return AdditionalData.Count > 0;
Expand Down Expand Up @@ -71,5 +74,10 @@ public bool ShouldSerializeString()
{
return String != null;
}

public bool ShouldSerializeGuid()
{
return Guid.HasValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/System.IdentityModel.Tokens.Jwt.Tests/JwtPayloadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

0 comments on commit ecd1912

Please sign in to comment.