Skip to content

Commit c255d5c

Browse files
author
Mohamed Shaban
authored
add guid validation (#22535)
1 parent c44b851 commit c255d5c

15 files changed

+4372
-3
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
6+
namespace Azure.AI.Translation.Document
7+
{
8+
internal static class ClientCommon
9+
{
10+
/// <summary>
11+
/// Used as part of argument validation. Attempts to create a <see cref="Guid"/> from a <c>string</c> and
12+
/// throws an <see cref="ArgumentException"/> in case of failure.
13+
/// </summary>
14+
/// <param name="possibleGuid">The identifier to be parsed into a <see cref="Guid"/>.</param>
15+
/// <param name="paramName">The original parameter name of the <paramref name="possibleGuid"/>. Used to create exceptions in case of failure.</param>
16+
/// <returns>The <see cref="Guid"/> instance created from the <paramref name="possibleGuid"/>.</returns>
17+
/// <exception cref="ArgumentException">Thrown when parsing fails.</exception>
18+
public static Guid ValidateModelId(string possibleGuid, string paramName)
19+
{
20+
Guid guid;
21+
22+
try
23+
{
24+
guid = new Guid(possibleGuid);
25+
}
26+
catch (Exception ex) when (ex is FormatException || ex is OverflowException)
27+
{
28+
throw new ArgumentException($"The {paramName} must be a valid GUID.", paramName, ex);
29+
}
30+
31+
return guid;
32+
}
33+
}
34+
}

sdk/translation/Azure.AI.Translation.Document/src/DocumentTranslationOperation.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ protected DocumentTranslationOperation()
145145
/// <param name="client">The client used to check for completion.</param>
146146
public DocumentTranslationOperation(string translationId, DocumentTranslationClient client)
147147
{
148-
Id = translationId;
148+
var parsedTranslationId = ClientCommon.ValidateModelId(translationId, nameof(translationId));
149+
Id = parsedTranslationId.ToString();
149150
_serviceClient = client._serviceRestClient;
150151
_diagnostics = client._clientDiagnostics;
151152
}
@@ -326,7 +327,8 @@ public virtual Response<DocumentStatus> GetDocumentStatus(string documentId, Can
326327

327328
try
328329
{
329-
return _serviceClient.GetDocumentStatus(new Guid(Id), new Guid(documentId), cancellationToken);
330+
var parsedDocumentId = ClientCommon.ValidateModelId(documentId, nameof(documentId));
331+
return _serviceClient.GetDocumentStatus(new Guid(Id), parsedDocumentId, cancellationToken);
330332
}
331333
catch (Exception e)
332334
{
@@ -347,7 +349,8 @@ public virtual async Task<Response<DocumentStatus>> GetDocumentStatusAsync(strin
347349

348350
try
349351
{
350-
return await _serviceClient.GetDocumentStatusAsync(new Guid(Id), new Guid(documentId), cancellationToken).ConfigureAwait(false);
352+
var parsedDocumentId = ClientCommon.ValidateModelId(documentId, nameof(documentId));
353+
return await _serviceClient.GetDocumentStatusAsync(new Guid(Id), parsedDocumentId, cancellationToken).ConfigureAwait(false);
351354
}
352355
catch (Exception e)
353356
{

sdk/translation/Azure.AI.Translation.Document/tests/SessionRecords/TranslationOperationLiveTests/DocumentTranslationOperationWithInvalidGuidTest(%%,System.ArgumentException).json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/translation/Azure.AI.Translation.Document/tests/SessionRecords/TranslationOperationLiveTests/DocumentTranslationOperationWithInvalidGuidTest(%%,System.ArgumentException)Async.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/translation/Azure.AI.Translation.Document/tests/SessionRecords/TranslationOperationLiveTests/DocumentTranslationOperationWithInvalidGuidTest(%Foo Bar%,System.ArgumentException).json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/translation/Azure.AI.Translation.Document/tests/SessionRecords/TranslationOperationLiveTests/DocumentTranslationOperationWithInvalidGuidTest(%Foo Bar%,System.ArgumentException)Async.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/translation/Azure.AI.Translation.Document/tests/SessionRecords/TranslationOperationLiveTests/DocumentTranslationOperationWithInvalidGuidTest(null,System.ArgumentNullException).json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/translation/Azure.AI.Translation.Document/tests/SessionRecords/TranslationOperationLiveTests/DocumentTranslationOperationWithInvalidGuidTest(null,System.ArgumentNullException)Async.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)