Skip to content

Commit

Permalink
Added default ctor to JwtHeader and decorated it with [JsonConstructo…
Browse files Browse the repository at this point in the history
…r] (#475)

* Bumped version to 10.0.3
* Updated JwtHeader.cs
* Updated CHANGELOG.md
* Updated JwtHeaderTests.cs
* Updated JwtBuilderEncodeTests.cs

---------

Co-authored-by: ajaume <[email protected]>
Co-authored-by: Alexander Batishchev <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2023
1 parent 9b9484f commit 906002e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

- TBD

# 10.0.3

- Added default ctor to JwtHeader and decorated it with `[JsonConstructor]`

# 10.0.2

- Disallowed Encode(payload) with AddClaim(s)
Expand Down
2 changes: 1 addition & 1 deletion JWT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C86A941F-F65
src\Directory.Build.targets = src\Directory.Build.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JWT.Tests.Net70", "tests\JWT.Tests.Net70\JWT.Tests.Net70.csproj", "{D7F24AC9-D178-4BAB-BF93-4BAD8028416D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JWT.Tests.Net70", "tests\JWT.Tests.Net70\JWT.Tests.Net70.csproj", "{D7F24AC9-D178-4BAB-BF93-4BAD8028416D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
8 changes: 7 additions & 1 deletion src/JWT/Builder/JwtHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace JWT.Builder
/// </summary>
public class JwtHeader
{
#if MODERN_DOTNET
[System.Text.Json.Serialization.JsonConstructor]
public JwtHeader()
{
}
#endif
[JsonProperty("typ")]
#if MODERN_DOTNET
[JsonPropertyName("typ")]
Expand Down Expand Up @@ -53,4 +59,4 @@ public class JwtHeader
#endif
public string X5t { get; set; }
}
}
}
9 changes: 6 additions & 3 deletions src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicenseExpression>CC0-1.0</PackageLicenseExpression>
<Version>10.0.2</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

<PropertyGroup>
<Version>10.0.3</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(DefineConstants);TRACE;DEBUG</DefineConstants>
<OutputPath>bin\Debug\</OutputPath>
Expand Down
7 changes: 0 additions & 7 deletions tests/JWT.Tests.Common/Builder/JwtBuilderEncodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,6 @@ public void Encode_Should_Throw_NotSupportedException_When_Using_EncodePayload_W
.Throw<NotSupportedException>("because using both Encode(payload) and AddClaims() is not supported");
}

/// <summary>
/// Copied from: https://stackoverflow.com/a/7077620/2890855
/// </summary>
/// <returns></returns>
private static bool IsRunningOnMono() =>
Type.GetType("Mono.Runtime") is not null;

[TestMethod]
public void Encode_Should_Return_Token_With_Custom_Extra_Headers_Full_Payload2()
{
Expand Down
30 changes: 30 additions & 0 deletions tests/JWT.Tests.Common/JwtHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FluentAssertions;
using JWT.Builder;
using JWT.Serializers;
using JWT.Tests.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JWT.Tests
{
[TestClass]
public class JwtHeaderTests
{
[TestMethod]
public void JwtHeader_Should_Be_Serializable()
{
const string token = TestData.TokenByAsymmetricAlgorithm;

var serializer = new DefaultJsonSerializerFactory().Create();
var urlEncoder = new JwtBase64UrlEncoder();
var decoder = new JwtDecoder(serializer, urlEncoder);

var expected = decoder.DecodeHeader<JwtHeader>(token);
expected.Should().NotBeNull();

var serializedHeader = serializer.Serialize(expected);
var actual = serializer.Deserialize<JwtHeader>(serializedHeader);

actual.Should().BeEquivalentTo(expected);
}
}
}

0 comments on commit 906002e

Please sign in to comment.