Skip to content

Commit d5a0d46

Browse files
authored
Fix Null Reference on Content-Saftey service (#222)
### Motivation and Context <!-- Thank you for your contribution to the copilot-chat repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Reported by customer in #218 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Use null-coalesce operator to return false when service null ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [Contribution Guidelines](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 4a06267 commit d5a0d46

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

webapi/Controllers/DocumentImportController.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private enum SupportedFileType
8181
private const string ReceiveMessageClientCall = "ReceiveMessage";
8282
private readonly IOcrEngine _ocrEngine;
8383
private readonly IAuthInfo _authInfo;
84-
private readonly IContentSafetyService? _contentSafetyService = null;
84+
private readonly IContentSafetyService? _contentSafetyService;
8585

8686
/// <summary>
8787
/// Initializes a new instance of the <see cref="DocumentImportController"/> class.
@@ -119,7 +119,7 @@ public DocumentImportController(
119119
[ProducesResponseType(StatusCodes.Status200OK)]
120120
public bool ContentSafetyStatus()
121121
{
122-
return this._contentSafetyService!.ContentSafetyStatus(this._logger);
122+
return this._contentSafetyService?.ContentSafetyStatus(this._logger) ?? false;
123123
}
124124

125125
/// <summary>
@@ -298,7 +298,7 @@ private async Task ValidateDocumentImportFormAsync(DocumentImportForm documentIm
298298
{
299299
if (documentImportForm.UseContentSafety)
300300
{
301-
if (!this._contentSafetyService!.ContentSafetyStatus(this._logger))
301+
if (this._contentSafetyService == null || !this._contentSafetyService.ContentSafetyStatus(this._logger))
302302
{
303303
throw new ArgumentException("Unable to analyze image. Content Safety is currently disabled in the backend.");
304304
}
@@ -307,8 +307,8 @@ private async Task ValidateDocumentImportFormAsync(DocumentImportForm documentIm
307307
try
308308
{
309309
// Call the content safety controller to analyze the image
310-
var imageAnalysisResponse = await this._contentSafetyService!.ImageAnalysisAsync(formFile, default);
311-
violations = this._contentSafetyService.ParseViolatedCategories(imageAnalysisResponse, this._contentSafetyService!.Options!.ViolationThreshold);
310+
var imageAnalysisResponse = await this._contentSafetyService.ImageAnalysisAsync(formFile, default);
311+
violations = this._contentSafetyService.ParseViolatedCategories(imageAnalysisResponse, this._contentSafetyService.Options.ViolationThreshold);
312312
}
313313
catch (Exception ex) when (!ex.IsCriticalException())
314314
{

0 commit comments

Comments
 (0)