Skip to content

Commit

Permalink
Fix Null Reference on Content-Saftey service (microsoft#222)
Browse files Browse the repository at this point in the history
### 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
microsoft#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 😄
  • Loading branch information
crickman authored Aug 20, 2023
1 parent cb48597 commit ed7460b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions webapi/Controllers/DocumentImportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Initializes a new instance of the <see cref="DocumentImportController"/> class.
Expand Down Expand Up @@ -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;
}

/// <summary>
Expand Down Expand Up @@ -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.");
}
Expand All @@ -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())
{
Expand Down

0 comments on commit ed7460b

Please sign in to comment.