mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
Start Migration from Newtonsoft.Json to System.Text.Json (#1803)
* Start switch to System.Text.Json * Work on switching to System.Text.Json * Main work on STJ refactor * Fix build errors * Run formatting * Delete unused file * Use legacy for two factor providers * Run formatter * Add TokenProviderTests * Run formatting * Fix merge issues * Switch to use JsonSerializer * Address PR feedback * Fix formatting * Ran formatter * Switch to async * Ensure Enums are serialized as strings * Fix formatting * Enqueue single items as arrays * Remove CreateAsync method on AzureQueueService
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
using Bit.Core.Entities;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Test.AutoFixture.CipherFixtures;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Models
|
||||
@ -12,7 +12,7 @@ namespace Bit.Core.Test.Models
|
||||
[InlineOrganizationCipherAutoData]
|
||||
public void Clone_CreatesExactCopy(Cipher cipher)
|
||||
{
|
||||
Assert.Equal(JsonConvert.SerializeObject(cipher), JsonConvert.SerializeObject(cipher.Clone()));
|
||||
Assert.Equal(JsonSerializer.Serialize(cipher), JsonSerializer.Serialize(cipher.Clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
28
test/Core.Test/Models/Data/SendFileDataTests.cs
Normal file
28
test/Core.Test/Models/Data/SendFileDataTests.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.Text.Json;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Test.Common.Helpers;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Models.Data
|
||||
{
|
||||
public class SendFileDataTests
|
||||
{
|
||||
[Fact]
|
||||
public void Serialize_Success()
|
||||
{
|
||||
var sut = new SendFileData
|
||||
{
|
||||
Id = "test",
|
||||
Size = 100,
|
||||
FileName = "thing.pdf",
|
||||
Validated = true,
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(sut);
|
||||
var document = JsonDocument.Parse(json);
|
||||
var root = document.RootElement;
|
||||
AssertHelper.AssertJsonProperty(root, "Size", JsonValueKind.String);
|
||||
Assert.False(root.TryGetProperty("SizeString", out _));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,6 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using AutoFixture.Xunit2;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Utilities;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Models
|
||||
@ -33,21 +29,30 @@ namespace Bit.Core.Test.Models
|
||||
[Fact]
|
||||
public void Serialization_Success()
|
||||
{
|
||||
// minify expected json
|
||||
var expected = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(_exampleSerializedPermissions));
|
||||
|
||||
DefaultContractResolver contractResolver = new DefaultContractResolver
|
||||
var permissions = new Permissions
|
||||
{
|
||||
NamingStrategy = new CamelCaseNamingStrategy()
|
||||
AccessEventLogs = false,
|
||||
AccessImportExport = false,
|
||||
AccessReports = false,
|
||||
CreateNewCollections = true,
|
||||
EditAnyCollection = true,
|
||||
DeleteAnyCollection = true,
|
||||
EditAssignedCollections = false,
|
||||
DeleteAssignedCollections = false,
|
||||
ManageGroups = false,
|
||||
ManagePolicies = false,
|
||||
ManageSso = false,
|
||||
ManageUsers = false,
|
||||
ManageResetPassword = false,
|
||||
};
|
||||
|
||||
var actual = JsonConvert.SerializeObject(
|
||||
CoreHelpers.LoadClassFromJsonData<Permissions>(_exampleSerializedPermissions), new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = contractResolver,
|
||||
});
|
||||
// minify expected json
|
||||
var expected = JsonSerializer.Serialize(permissions, JsonHelpers.CamelCase);
|
||||
|
||||
var actual = JsonSerializer.Serialize(
|
||||
JsonHelpers.DeserializeOrNew<Permissions>(_exampleSerializedPermissions, JsonHelpers.CamelCase),
|
||||
JsonHelpers.CamelCase);
|
||||
|
||||
Console.WriteLine(actual);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,9 @@
|
||||
using Bit.Core.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models;
|
||||
using Bit.Test.Common.Helpers;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Models.Tables
|
||||
@ -39,5 +44,43 @@ namespace Bit.Core.Test.Models.Tables
|
||||
|
||||
Assert.Equal(expectedRemainingBytes, bytesRemaining);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetTwoFactorProviders()
|
||||
{
|
||||
var user = new User();
|
||||
user.SetTwoFactorProviders(new Dictionary<TwoFactorProviderType, TwoFactorProvider>
|
||||
{
|
||||
[TwoFactorProviderType.WebAuthn] = new TwoFactorProvider
|
||||
{
|
||||
Enabled = true,
|
||||
MetaData = new Dictionary<string, object>
|
||||
{
|
||||
["Item"] = "thing",
|
||||
},
|
||||
},
|
||||
[TwoFactorProviderType.Email] = new TwoFactorProvider
|
||||
{
|
||||
Enabled = false,
|
||||
MetaData = new Dictionary<string, object>
|
||||
{
|
||||
["Email"] = "test@email.com",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
using var jsonDocument = JsonDocument.Parse(user.TwoFactorProviders);
|
||||
var root = jsonDocument.RootElement;
|
||||
|
||||
var webAuthn = AssertHelper.AssertJsonProperty(root, "WebAuthn", JsonValueKind.Object);
|
||||
AssertHelper.AssertJsonProperty(webAuthn, "Enabled", JsonValueKind.True);
|
||||
var webMetaData = AssertHelper.AssertJsonProperty(webAuthn, "MetaData", JsonValueKind.Object);
|
||||
AssertHelper.AssertJsonProperty(webMetaData, "Item", JsonValueKind.String);
|
||||
|
||||
var email = AssertHelper.AssertJsonProperty(root, "Email", JsonValueKind.Object);
|
||||
AssertHelper.AssertJsonProperty(email, "Enabled", JsonValueKind.False);
|
||||
var emailMetaData = AssertHelper.AssertJsonProperty(email, "MetaData", JsonValueKind.Object);
|
||||
AssertHelper.AssertJsonProperty(emailMetaData, "Email", JsonValueKind.String);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user