Skip to content

Commit e32b2e7

Browse files
authored
[FormRecognizer] Preview 1: fix modelId not being set in operation (bug fix) (#11477)
1 parent 80a6324 commit e32b2e7

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

sdk/formrecognizer/Azure.AI.FormRecognizer/src/FormRecognizerClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public virtual RecognizeCustomFormsOperation StartRecognizeCustomForms(string mo
242242
ContentType contentType = recognizeOptions.ContentType ?? DetectContentType(formFileStream, nameof(formFileStream));
243243

244244
ResponseWithHeaders<ServiceAnalyzeWithCustomModelHeaders> response = ServiceClient.RestClient.AnalyzeWithCustomModel(guid, contentType, formFileStream, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken);
245-
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, modelId, response.Headers.OperationLocation);
245+
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
246246
}
247247

248248
/// <summary>
@@ -266,7 +266,7 @@ public virtual RecognizeCustomFormsOperation StartRecognizeCustomFormsFromUri(st
266266

267267
SourcePath_internal sourcePath = new SourcePath_internal(formFileUri.ToString());
268268
ResponseWithHeaders<ServiceAnalyzeWithCustomModelHeaders> response = ServiceClient.RestClient.AnalyzeWithCustomModel(guid, includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken);
269-
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, modelId, response.Headers.OperationLocation);
269+
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
270270
}
271271

272272
/// <summary>
@@ -290,7 +290,7 @@ public virtual async Task<RecognizeCustomFormsOperation> StartRecognizeCustomFor
290290
ContentType contentType = recognizeOptions.ContentType ?? DetectContentType(formFileStream, nameof(formFileStream));
291291

292292
ResponseWithHeaders<ServiceAnalyzeWithCustomModelHeaders> response = await ServiceClient.RestClient.AnalyzeWithCustomModelAsync(guid, contentType, formFileStream, includeTextDetails: recognizeOptions.IncludeTextContent, cancellationToken).ConfigureAwait(false);
293-
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, modelId, response.Headers.OperationLocation);
293+
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
294294
}
295295

296296
/// <summary>
@@ -314,7 +314,7 @@ public virtual async Task<RecognizeCustomFormsOperation> StartRecognizeCustomFor
314314

315315
SourcePath_internal sourcePath = new SourcePath_internal(formFileUri.ToString());
316316
ResponseWithHeaders<ServiceAnalyzeWithCustomModelHeaders> response = await ServiceClient.RestClient.AnalyzeWithCustomModelAsync(guid, includeTextDetails: recognizeOptions.IncludeTextContent, sourcePath, cancellationToken).ConfigureAwait(false);
317-
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, modelId, response.Headers.OperationLocation);
317+
return new RecognizeCustomFormsOperation(ServiceClient, Diagnostics, response.Headers.OperationLocation);
318318
}
319319

320320
#endregion

sdk/formrecognizer/Azure.AI.FormRecognizer/src/RecognizeCustomFormsOperation.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class RecognizeCustomFormsOperation : Operation<IReadOnlyList<RecognizedF
3333
/// <summary>The id of the model to use for recognizing form values.</summary>
3434
private readonly string _modelId;
3535

36+
/// <summary>An ID representing the operation that can be used along with <see cref="_modelId"/> to poll for the status of the long-running operation.</summary>
37+
private readonly string _resultId;
38+
3639
/// <inheritdoc/>
3740
public override string Id { get; }
3841

@@ -60,17 +63,24 @@ public override ValueTask<Response<IReadOnlyList<RecognizedForm>>> WaitForComple
6063
/// </summary>
6164
/// <param name="operations"></param>
6265
/// <param name="diagnostics"></param>
63-
/// <param name="modelId"></param>
6466
/// <param name="operationLocation"></param>
65-
internal RecognizeCustomFormsOperation(ServiceClient operations, ClientDiagnostics diagnostics, string modelId, string operationLocation)
67+
internal RecognizeCustomFormsOperation(ServiceClient operations, ClientDiagnostics diagnostics, string operationLocation)
6668
{
6769
_serviceClient = operations;
6870
_diagnostics = diagnostics;
69-
_modelId = modelId;
7071

71-
// TODO: Add validation here
72+
// TODO: Use regex to parse ids.
73+
// https://github.com/Azure/azure-sdk-for-net/issues/11505
74+
75+
// TODO: Add validation here (should we store _resuldId and _modelId as GUIDs?)
7276
// https://github.com/Azure/azure-sdk-for-net/issues/10385
73-
Id = operationLocation.Split('/').Last();
77+
78+
string[] substrs = operationLocation.Split('/');
79+
80+
_resultId = substrs[substrs.Length - 1];
81+
_modelId = substrs[substrs.Length - 3];
82+
83+
Id = string.Join("/", substrs, substrs.Length - 3, 3);
7484
}
7585

7686
/// <summary>
@@ -80,9 +90,26 @@ internal RecognizeCustomFormsOperation(ServiceClient operations, ClientDiagnosti
8090
/// <param name="client">The client used to check for completion.</param>
8191
public RecognizeCustomFormsOperation(string operationId, FormRecognizerClient client)
8292
{
83-
Id = operationId;
8493
_serviceClient = client.ServiceClient;
8594
_diagnostics = client.Diagnostics;
95+
96+
// TODO: Use regex to parse ids.
97+
// https://github.com/Azure/azure-sdk-for-net/issues/11505
98+
99+
// TODO: Add validation here (should we store _resuldId and _modelId as GUIDs?)
100+
// https://github.com/Azure/azure-sdk-for-net/issues/10385
101+
102+
string[] substrs = operationId.Split('/');
103+
104+
if (substrs.Length < 3)
105+
{
106+
throw new ArgumentException($"Invalid {operationId}. It should be formatted as: '{{modelId}}/analyzeresults/{{resultId}}'.", operationId);
107+
}
108+
109+
_resultId = substrs.Last();
110+
_modelId = substrs.First();
111+
112+
Id = operationId;
86113
}
87114

88115
/// <summary>
@@ -105,8 +132,8 @@ private async ValueTask<Response> UpdateStatusAsync(bool async, CancellationToke
105132
if (!_hasCompleted)
106133
{
107134
Response<AnalyzeOperationResult_internal> update = async
108-
? await _serviceClient.GetAnalyzeFormResultAsync(new Guid(_modelId), new Guid(Id), cancellationToken).ConfigureAwait(false)
109-
: _serviceClient.GetAnalyzeFormResult(new Guid(_modelId), new Guid(Id), cancellationToken);
135+
? await _serviceClient.GetAnalyzeFormResultAsync(new Guid(_modelId), new Guid(_resultId), cancellationToken).ConfigureAwait(false)
136+
: _serviceClient.GetAnalyzeFormResult(new Guid(_modelId), new Guid(_resultId), cancellationToken);
110137

111138
// TODO: Add reasonable null checks.
112139

0 commit comments

Comments
 (0)