From ed7460b490186abad028e58b939a0f876dadea92 Mon Sep 17 00:00:00 2001 From: Chris <66376200+crickman@users.noreply.github.com> Date: Sun, 20 Aug 2023 16:04:18 -0700 Subject: [PATCH] Fix Null Reference on Content-Saftey service (#222) ### Motivation and Context Reported by customer in https://github.com/microsoft/chat-copilot/issues/218 ### Description Use null-coalesce operator to return false when service null ### Contribution Checklist - [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 :smile: --- webapi/Controllers/DocumentImportController.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/webapi/Controllers/DocumentImportController.cs b/webapi/Controllers/DocumentImportController.cs index dd10a3a76..1f5cb3032 100644 --- a/webapi/Controllers/DocumentImportController.cs +++ b/webapi/Controllers/DocumentImportController.cs @@ -81,7 +81,7 @@ private enum SupportedFileType private const string ReceiveMessageClientCall = "ReceiveMessage"; private readonly IOcrEngine _ocrEngine; private readonly IAuthInfo _authInfo; - private readonly IContentSafetyService? _contentSafetyService = null; + private readonly IContentSafetyService? _contentSafetyService; /// /// Initializes a new instance of the class. @@ -119,7 +119,7 @@ public DocumentImportController( [ProducesResponseType(StatusCodes.Status200OK)] public bool ContentSafetyStatus() { - return this._contentSafetyService!.ContentSafetyStatus(this._logger); + return this._contentSafetyService?.ContentSafetyStatus(this._logger) ?? false; } /// @@ -298,7 +298,7 @@ private async Task ValidateDocumentImportFormAsync(DocumentImportForm documentIm { if (documentImportForm.UseContentSafety) { - if (!this._contentSafetyService!.ContentSafetyStatus(this._logger)) + if (this._contentSafetyService == null || !this._contentSafetyService.ContentSafetyStatus(this._logger)) { throw new ArgumentException("Unable to analyze image. Content Safety is currently disabled in the backend."); } @@ -307,8 +307,8 @@ private async Task ValidateDocumentImportFormAsync(DocumentImportForm documentIm try { // Call the content safety controller to analyze the image - var imageAnalysisResponse = await this._contentSafetyService!.ImageAnalysisAsync(formFile, default); - violations = this._contentSafetyService.ParseViolatedCategories(imageAnalysisResponse, this._contentSafetyService!.Options!.ViolationThreshold); + var imageAnalysisResponse = await this._contentSafetyService.ImageAnalysisAsync(formFile, default); + violations = this._contentSafetyService.ParseViolatedCategories(imageAnalysisResponse, this._contentSafetyService.Options.ViolationThreshold); } catch (Exception ex) when (!ex.IsCriticalException()) {