Skip to content

Commit 8db5b63

Browse files
Add JsonHelper to eliminate duplicated JSON serialization options
Co-authored-by: mdellison90-stack <230609064+mdellison90-stack@users.noreply.github.com>
1 parent 3bf1423 commit 8db5b63

7 files changed

Lines changed: 38 additions & 29 deletions

File tree

src/shared/Atlassian.Bitbucket.Tests/BitbucketTokenEndpointResponseJsonTest.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text.Json;
3+
using GitCredentialManager;
34
using Xunit;
45

56
namespace Atlassian.Bitbucket.Tests
@@ -17,11 +18,7 @@ public void BitbucketTokenEndpointResponseJson_Deserialize_Uses_Scopes()
1718

1819
var json = $"{{\"access_token\": \"{accessToken}\", \"token_type\": \"{tokenType}\", \"expires_in\": {expiresIn}, \"scopes\": \"{scopesString}\", \"scope\": \"{scopeString}\"}}";
1920

20-
var result = JsonSerializer.Deserialize<BitbucketTokenEndpointResponseJson>(json,
21-
new JsonSerializerOptions
22-
{
23-
PropertyNameCaseInsensitive = true
24-
});
21+
var result = JsonSerializer.Deserialize<BitbucketTokenEndpointResponseJson>(json, JsonHelper.CaseInsensitiveOptions);
2522

2623
Assert.Equal(accessToken, result.AccessToken);
2724
Assert.Equal(tokenType, result.TokenType);

src/shared/Atlassian.Bitbucket.Tests/Cloud/UserInfoTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Text.Json.Serialization;
44
using System.Threading.Tasks;
55
using Atlassian.Bitbucket.Cloud;
6+
using GitCredentialManager;
67
using Xunit;
78

89
namespace Atlassian.Bitbucket.Tests.Cloud
@@ -29,10 +30,7 @@ public void Deserialize_UserInfo()
2930

3031
var json = $"{{\"uuid\": \"{uuid}\", \"has_2fa_enabled\": null, \"username\": \"{userName}\", \"account_id\": \"{accountId}\"}}";
3132

32-
var result = JsonSerializer.Deserialize<UserInfo>(json, new JsonSerializerOptions()
33-
{
34-
PropertyNameCaseInsensitive = true,
35-
});
33+
var result = JsonSerializer.Deserialize<UserInfo>(json, JsonHelper.CaseInsensitiveOptions);
3634

3735
Assert.Equal(userName, result.UserName);
3836
}

src/shared/Atlassian.Bitbucket/Cloud/BitbucketRestApi.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ public async Task<RestApiResult<IUserInfo>> GetUserInformationAsync(string userN
4343

4444
if (response.IsSuccessStatusCode)
4545
{
46-
var obj = JsonSerializer.Deserialize<UserInfo>(json,
47-
new JsonSerializerOptions
48-
{
49-
PropertyNameCaseInsensitive = true,
50-
});
46+
var obj = JsonSerializer.Deserialize<UserInfo>(json, JsonHelper.CaseInsensitiveOptions);
5147

5248
return new RestApiResult<IUserInfo>(response.StatusCode, obj);
5349
}

src/shared/Atlassian.Bitbucket/DataCenter/BitbucketRestApi.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,7 @@ public async Task<List<AuthenticationMethod>> GetAuthenticationMethodsAsync()
107107

108108
if (response.IsSuccessStatusCode)
109109
{
110-
var loginOptions = JsonSerializer.Deserialize<LoginOptions>(json,
111-
new JsonSerializerOptions
112-
{
113-
PropertyNameCaseInsensitive = true,
114-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
115-
});
110+
var loginOptions = JsonSerializer.Deserialize<LoginOptions>(json, JsonHelper.CaseInsensitiveIgnoreNullOptions);
116111

117112
if (loginOptions.Results.Any(r => "LOGIN_FORM".Equals(r.Type)))
118113
{

src/shared/Core.Tests/TokenEndpointResponseJsonTest.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text.Json;
3+
using GitCredentialManager;
34
using GitCredentialManager.Authentication.OAuth.Json;
45
using Xunit;
56

@@ -17,11 +18,7 @@ public void TokenEndpointResponseJson_Deserialize_Uses_Scope()
1718
var scopeString = "a,b,c";
1819
var json = $"{{\"access_token\": \"{accessToken}\", \"token_type\": \"{tokenType}\", \"expires_in\": {expiresIn}, \"scopes\": \"{scopesString}\", \"scope\": \"{scopeString}\"}}";
1920

20-
var result = JsonSerializer.Deserialize<TokenEndpointResponseJson>(json,
21-
new JsonSerializerOptions
22-
{
23-
PropertyNameCaseInsensitive = true
24-
});
21+
var result = JsonSerializer.Deserialize<TokenEndpointResponseJson>(json, JsonHelper.CaseInsensitiveOptions);
2522

2623
Assert.Equal(accessToken, result.AccessToken);
2724
Assert.Equal(tokenType, result.TokenType);

src/shared/Core/Authentication/OAuth/OAuth2Client.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,7 @@ public async Task<OAuth2TokenResult> GetTokenByDeviceCodeAsync(OAuth2DeviceCodeR
321321
return result;
322322
}
323323

324-
var error = JsonSerializer.Deserialize<ErrorResponseJson>(json, new JsonSerializerOptions
325-
{
326-
PropertyNameCaseInsensitive = true
327-
});
324+
var error = JsonSerializer.Deserialize<ErrorResponseJson>(json, JsonHelper.CaseInsensitiveOptions);
328325

329326
switch (error.Error)
330327
{

src/shared/Core/JsonHelper.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace GitCredentialManager
5+
{
6+
/// <summary>
7+
/// Helper class providing common JSON serialization options and utilities.
8+
/// </summary>
9+
public static class JsonHelper
10+
{
11+
/// <summary>
12+
/// Gets JSON serializer options configured for case-insensitive property names.
13+
/// </summary>
14+
public static JsonSerializerOptions CaseInsensitiveOptions { get; } = new JsonSerializerOptions
15+
{
16+
PropertyNameCaseInsensitive = true
17+
};
18+
19+
/// <summary>
20+
/// Gets JSON serializer options configured for case-insensitive property names
21+
/// and ignoring null values when writing.
22+
/// </summary>
23+
public static JsonSerializerOptions CaseInsensitiveIgnoreNullOptions { get; } = new JsonSerializerOptions
24+
{
25+
PropertyNameCaseInsensitive = true,
26+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
27+
};
28+
}
29+
}

0 commit comments

Comments
 (0)