Skip to content

Commit 978fb46

Browse files
Refactor string interpolation to handle potential null values in PeopleMessageFormatter and Worker classes
1 parent 46e57e7 commit 978fb46

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

src/Examples/OpenApiNSwagClientExample/PeopleMessageFormatter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private static void WritePerson(DataInPersonResponse person, ICollection<Resourc
3838
{
3939
ICollection<TodoItemIdentifierInResponse> assignedTodoItems = person.Relationships.AssignedTodoItems?.Data ?? [];
4040

41-
builder.AppendLine($" Person {person.Id}: {person.Attributes.DisplayName} with {assignedTodoItems.Count} assigned todo-items:");
41+
builder.AppendLine($" Person {person.Id}: {person.Attributes.DisplayName ?? string.Empty} with {assignedTodoItems.Count} assigned todo-items:");
4242
WriteRelatedTodoItems(assignedTodoItems, includes, builder);
4343
}
4444

@@ -48,9 +48,9 @@ private static void WriteRelatedTodoItems(IEnumerable<TodoItemIdentifierInRespon
4848
foreach (TodoItemIdentifierInResponse todoItemIdentifier in todoItemIdentifiers)
4949
{
5050
DataInTodoItemResponse includedTodoItem = includes.OfType<DataInTodoItemResponse>().Single(include => include.Id == todoItemIdentifier.Id);
51-
ICollection<TagIdentifierInResponse> tags = includedTodoItem.Relationships.Tags?.Data ?? [];
51+
ICollection<TagIdentifierInResponse> tags = includedTodoItem.Relationships?.Tags?.Data ?? [];
5252

53-
builder.AppendLine($" TodoItem {includedTodoItem.Id}: {includedTodoItem.Attributes.Description} with {tags.Count} tags:");
53+
builder.AppendLine($" TodoItem {includedTodoItem.Id}: {includedTodoItem.Attributes.Description ?? string.Empty} with {tags.Count} tags:");
5454
WriteRelatedTags(tags, includes, builder);
5555
}
5656
}
@@ -60,7 +60,7 @@ private static void WriteRelatedTags(IEnumerable<TagIdentifierInResponse> tagIde
6060
foreach (TagIdentifierInResponse tagIdentifier in tagIdentifiers)
6161
{
6262
DataInTagResponse includedTag = includes.OfType<DataInTagResponse>().Single(include => include.Id == tagIdentifier.Id);
63-
builder.AppendLine($" Tag {includedTag.Id}: {includedTag.Attributes.Name}");
63+
builder.AppendLine($" Tag {includedTag.Id}: {includedTag.Attributes?.Name ?? string.Empty}");
6464
}
6565
}
6666
}

src/Examples/OpenApiNSwagClientExample/Worker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,6 @@ private async Task SendOperationsRequestAsync(CancellationToken cancellationToke
166166
ApiResponse<OperationsResponseDocument> operationsResponse = await _apiClient.PostOperationsAsync(operationsRequest, cancellationToken);
167167

168168
var newTodoItem = (DataInTodoItemResponse)operationsResponse.Result.Atomic_results.ElementAt(3).Data;
169-
Console.WriteLine($"Created todo-item with ID {newTodoItem.Id}: {newTodoItem.Attributes.Description}.");
169+
Console.WriteLine($"Created todo-item with ID {newTodoItem.Id}: {newTodoItem.Attributes?.Description ?? string.Empty}.");
170170
}
171171
}

0 commit comments

Comments
 (0)