Skip to content

Commit

Permalink
Fixed handling IEnumerable by DictionaryStringObjectJsonConverter (#439)
Browse files Browse the repository at this point in the history
* Fixed handling IEnumerable by DictionaryStringObjectJsonConverter
* Bumped version to 10.0.0-beta10

Co-authored-by: Alexander Batishchev <[email protected]>
  • Loading branch information
hartmark and abatishchev authored Sep 26, 2022
1 parent e59150f commit a2d7035
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net6.0;net35;net40;net462;</TargetFrameworks>
Expand All @@ -20,7 +20,7 @@
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicenseExpression>CC0-1.0</PackageLicenseExpression>
<Version>10.0.0-beta9</Version>
<Version>10.0.0-beta10</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if MODERN_DOTNET
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -104,16 +105,17 @@ private static void HandleValue(Utf8JsonWriter writer, string key, object object
writer.WriteEndObject();
break;
}
case object[] arr:
case IEnumerable enumerable:
{
writer.WriteStartArray();
foreach (var item in arr)
foreach (var item in enumerable)
{
HandleValue(writer, item);
}

writer.WriteEndArray();
break;
}
}
default:
{
var dic = objectValue.GetType()
Expand Down
2 changes: 1 addition & 1 deletion src/JWT/Serializers/DelegateJsonSerializerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace JWT.Serializers
namespace JWT.Serializers
{
internal sealed class DelegateJsonSerializerFactory : IJsonSerializerFactory
{
Expand Down
46 changes: 46 additions & 0 deletions tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using AutoFixture;
using FluentAssertions;
using JWT.Algorithms;
Expand Down Expand Up @@ -378,6 +381,49 @@ public void Encode_With_Secret_Should_Return_Valid_Token_Using_Json_Net()
.HaveCount(3, "because the token should consist of three parts");
}

#if NETSTANDARD2_0 || NET6_0
[TestMethod]
public void Encode_Test_Bug438()
{
var privateKey = ECDsa.Create();
var publicKey = ECDsa.Create();;

var algo = new ES256Algorithm(publicKey, privateKey);

var factory = new DelegateAlgorithmFactory(() => algo);

var now = DateTime.UtcNow;
var sessionId = Guid.NewGuid();

IEnumerable<string> enumerable = new List<string>
{
"string1",
"string2"
};

var array = new string[]
{
"one",
"two"
};

var builder = JwtBuilder.Create()
.WithAlgorithmFactory(factory)
.AddClaim("session_id", sessionId.ToString())
.AddClaim("enumerable", enumerable)
.AddClaim("array", array)
.Issuer("Security Guy")
.Audience("Strict access perimeter")
.IssuedAt(now)
.ExpirationTime(now.AddMinutes(30));

var token = builder.Encode();

token.Should()
.NotBeNullOrEmpty();
}
#endif

private sealed class CustomFactory : IAlgorithmFactory
{
public IJwtAlgorithm Create(JwtDecoderContext context) =>
Expand Down

0 comments on commit a2d7035

Please sign in to comment.