Skip to content

Commit 617230c

Browse files
author
samatrhea
committed
[Update] BranchDeserializer to make resilient to faulty data returned from server
1 parent 4d3247d commit 617230c

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

SySML2.NET.REST.Tests/RestClientTestFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ public async Task Verify_that_RestClient_can_query_projects_branches_tags_commit
8484
}
8585

8686
var commits = await this.restClient.RequestCommits(project.Id, null, null, this.cancellationTokenSource.Token);
87-
88-
Assert.That(commits, Is.Not.Empty);
89-
87+
9088
foreach (var commit in commits)
9189
{
9290
Assert.That(commit.OwningProject, Is.EqualTo(project.Id));

SysML2.NET.Serializer.Json/API/BranchDeserializer.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ internal static Branch DeSerialize(JsonElement jsonElement, SerializationModeKin
9393
}
9494
else
9595
{
96-
logger.LogDebug($"the timestamp Json property was not found in the Tag: {dtoInstance.Id}");
96+
logger.LogDebug($"the timestamp Json property was not found in the Branch: {dtoInstance.Id}");
9797
}
9898

9999
if (jsonElement.TryGetProperty("description", out JsonElement descriptionProperty))
@@ -111,18 +111,25 @@ internal static Branch DeSerialize(JsonElement jsonElement, SerializationModeKin
111111

112112
if (jsonElement.TryGetProperty("head", out JsonElement headProperty))
113113
{
114-
if (headProperty.TryGetProperty("@id", out JsonElement headIdProperty))
114+
if (headProperty.ValueKind == JsonValueKind.Null)
115+
{
116+
logger.LogWarning($"the head Json property was null which should not be allowed: Branch {dtoInstance.Id}");
117+
}
118+
else
115119
{
116-
var propertyValue = headIdProperty.GetString();
117-
if (propertyValue != null)
120+
if (headProperty.TryGetProperty("@id", out JsonElement headIdProperty))
118121
{
119-
dtoInstance.Head = Guid.Parse(propertyValue);
122+
var propertyValue = headIdProperty.GetString();
123+
if (propertyValue != null)
124+
{
125+
dtoInstance.Head = Guid.Parse(propertyValue);
126+
}
120127
}
121128
}
122129
}
123130
else
124131
{
125-
logger.LogDebug($"the referencedCommit Json property was not found in the Tag: {dtoInstance.Id}");
132+
logger.LogDebug($"the referencedCommit Json property was not found in the Branch: {dtoInstance.Id}");
126133
}
127134

128135
if (jsonElement.TryGetProperty("name", out JsonElement nameProperty))
@@ -163,18 +170,25 @@ internal static Branch DeSerialize(JsonElement jsonElement, SerializationModeKin
163170

164171
if (jsonElement.TryGetProperty("referencedCommit", out JsonElement referencedCommitProperty))
165172
{
166-
if (referencedCommitProperty.TryGetProperty("@id", out JsonElement referencedCommitIdProperty))
173+
if (referencedCommitProperty.ValueKind == JsonValueKind.Null)
167174
{
168-
var propertyValue = referencedCommitIdProperty.GetString();
169-
if (propertyValue != null)
175+
logger.LogWarning($"the referencedCommit Json property was null which should not be allowed: Branch {dtoInstance.Id}");
176+
}
177+
else
178+
{
179+
if (referencedCommitProperty.TryGetProperty("@id", out JsonElement referencedCommitIdProperty))
170180
{
171-
dtoInstance.ReferencedCommit = Guid.Parse(propertyValue);
181+
var propertyValue = referencedCommitIdProperty.GetString();
182+
if (propertyValue != null)
183+
{
184+
dtoInstance.ReferencedCommit = Guid.Parse(propertyValue);
185+
}
172186
}
173187
}
174188
}
175189
else
176190
{
177-
logger.LogDebug($"the referencedCommit Json property was not found in the Tag: {dtoInstance.Id}");
191+
logger.LogDebug($"the referencedCommit Json property was not found in the Branch: {dtoInstance.Id}");
178192
}
179193

180194
if (jsonElement.TryGetProperty("timestamp", out JsonElement timestampProperty))

0 commit comments

Comments
 (0)