Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose JsonDocuments #1617

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Configure(EntityTypeBuilder<SupportTask> builder)
builder.HasOne<Person>().WithMany().HasForeignKey(p => p.PersonId).HasConstraintName("fk_support_tasks_person");
builder.HasIndex(t => t.OneLoginUserSubject);
builder.HasIndex(t => t.PersonId);
builder.Property<JsonDocument>("_data").HasColumnName("data").IsRequired();
builder.Property<JsonElement>("_data").HasColumnName("data").IsRequired();
builder.Ignore(t => t.Data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2145,8 +2145,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("timestamp with time zone")
.HasColumnName("updated_on");

b.Property<JsonDocument>("_data")
.IsRequired()
b.Property<JsonElement>("_data")
.HasColumnType("jsonb")
.HasColumnName("data");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SupportTask
internal static readonly JsonSerializerOptions SerializerOptions = new();
private static readonly char[] _validReferenceChars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789".ToCharArray();

private JsonDocument _data = null!;
private JsonElement _data;

public required string SupportTaskReference { get; set; }
public required DateTime CreatedOn { get; init; }
Expand All @@ -23,7 +23,7 @@ public class SupportTask
public required object Data
{
get => JsonSerializer.Deserialize(_data, GetDataType(), SerializerOptions)!;
set => _data = JsonSerializer.SerializeToDocument(value, GetDataType(), SerializerOptions);
set => _data = JsonSerializer.SerializeToElement(value, GetDataType(), SerializerOptions);
}

public static string GenerateSupportTaskReference()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public record SupportTask
{
[JsonInclude]
[JsonPropertyName("Data")]
private JsonDocument _data = null!;
private JsonElement _data;

public required string SupportTaskReference { get; init; }
public required SupportTaskType SupportTaskType { get; init; }
Expand All @@ -19,7 +19,7 @@ public record SupportTask
public object Data
{
get => JsonSerializer.Deserialize(_data, DataStore.Postgres.Models.SupportTask.GetDataType(SupportTaskType), DataStore.Postgres.Models.SupportTask.SerializerOptions)!;
init => _data = JsonSerializer.SerializeToDocument(value, DataStore.Postgres.Models.SupportTask.GetDataType(SupportTaskType), DataStore.Postgres.Models.SupportTask.SerializerOptions);
init => _data = JsonSerializer.SerializeToElement(value, DataStore.Postgres.Models.SupportTask.GetDataType(SupportTaskType), DataStore.Postgres.Models.SupportTask.SerializerOptions);
}

public static SupportTask FromModel(DataStore.Postgres.Models.SupportTask model) => new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ FROM matches m

return results switch
{
[MatchQueryResult r] => new MatchResult(r.person_id, r.trn, MapMatchedAttrs(r.matched_attrs)),
[MatchQueryResult r] => new MatchResult(r.person_id, r.trn, MapMatchedAttrs(r.GetMatchedAttributes())),
_ => null
};

static IReadOnlyCollection<KeyValuePair<OneLoginUserMatchedAttribute, string>> MapMatchedAttrs(JsonDocument doc) =>
doc.Deserialize<MatchedAttribute[]>()!
static IReadOnlyCollection<KeyValuePair<OneLoginUserMatchedAttribute, string>> MapMatchedAttrs(MatchedAttribute[] matchedAttributes) =>
matchedAttributes
.Select(a => new KeyValuePair<OneLoginUserMatchedAttribute, string>(
Enum.Parse<OneLoginUserMatchedAttribute>(a.attribute_type),
a.attribute_value))
Expand Down Expand Up @@ -199,7 +199,10 @@ FROM person_search_attributes a
NationalInsuranceNumberHelper.Normalize(value);

#pragma warning disable IDE1006 // Naming Styles
private record MatchQueryResult(Guid person_id, string trn, JsonDocument matched_attrs);
private record MatchQueryResult(Guid person_id, string trn, JsonElement matched_attrs)
{
public MatchedAttribute[] GetMatchedAttributes() => matched_attrs.Deserialize<MatchedAttribute[]>()!;
}

private record MatchedAttribute(string attribute_type, string attribute_value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Configure(WorkforceDataExportOptions options)

if (!string.IsNullOrEmpty(credentialsJson))
{
var credentialsJsonDoc = JsonDocument.Parse(credentialsJson);
using var credentialsJsonDoc = JsonDocument.Parse(credentialsJson);

if (credentialsJsonDoc.RootElement.TryGetProperty("private_key", out _))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record OneLoginUserInfo(string sub, string email, string vot, string sid,
public static OneLoginUserInfo Create(string sub, string email) =>
Create(sub, email, (string?)null);

public static OneLoginUserInfo Create(string sub, string email, JsonDocument? coreIdentityVc)
public static OneLoginUserInfo Create(string sub, string email, JsonElement? coreIdentityVc)
{
string? coretIdentityVcStr = coreIdentityVc is null ? null : JsonSerializer.Serialize(coreIdentityVc);
return Create(sub, email, coretIdentityVcStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public AuthenticationTicket CreateOneLoginAuthenticationTicket(
dateOfBirth ??= DateOnly.FromDateTime(Faker.Identification.DateOfBirth());

var vc = TestData.CreateOneLoginCoreIdentityVc(firstName, lastName, dateOfBirth.Value);
claims.Add(new Claim("vc", vc.RootElement.ToString(), "JSON"));
claims.Add(new Claim("vc", vc.ToString(), "JSON"));
}

var identity = new ClaimsIdentity(claims, authenticationType: "OneLogin", nameType: "sub", roleType: null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static async Task JsonResponseEquals(HttpResponseMessage response, object
ArgumentNullException.ThrowIfNull(response);
ArgumentNullException.ThrowIfNull(expected);

var jsonDocument = await JsonResponse(response, expectedStatusCode);
using var jsonDocument = await JsonResponse(response, expectedStatusCode);

JsonObjectEquals(expected, jsonDocument);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public Task<OneLoginUser> CreateOneLoginUser(

public string CreateOneLoginUserSubject() => Guid.NewGuid().ToString("N");

public JsonDocument CreateOneLoginCoreIdentityVc(string firstName, string lastName, DateOnly dateOfBirth) =>
JsonDocument.Parse(
public JsonElement CreateOneLoginCoreIdentityVc(string firstName, string lastName, DateOnly dateOfBirth) =>
JsonSerializer.SerializeToElement(
new JsonObject
{
["type"] = new JsonArray(
Expand Down
Loading