Skip to content

[FormRecognizer] Parse operation IDs with regex #11505

Closed as not planned
Closed as not planned
@kinelski

Description

@kinelski

Summary

The implementation of the Operation constructors uses some magic numbers when parsing the operation ID.

Operations to address

  • CopyModelOperation
  • RecognizeContentOperation
  • RecognizeCustomFormsOperation
  • RecognizeReceiptsOperation
  • RecognizeIdDocumentsOperation
  • TrainingOperation
  • AnalyzeDocumentOperation
  • ClassifyDocumentOperation

Internal constructor

string[] substrs = operationLocation.Split('/');
_resultId = substrs[substrs.Length - 1];
_modelId = substrs[substrs.Length - 3];
Id = string.Join("/", substrs, substrs.Length - 3, 3);

The internal constructor takes a parameter called operationLocation, which is returned from the service and is expected to look like:

{endpoint}/formrecognizer/{version}/custom/models/{modelId}/analyzeresults/{resultId}

Public constructor

string[] substrs = operationId.Split('/');
if (substrs.Length < 3)
{
throw new ArgumentException($"Invalid {operationId}. It should be formatted as: '{{modelId}}/analyzeresults/{{resultId}}'.", operationId);
}
_resultId = substrs.Last();
_modelId = substrs.First();
Id = operationId;

The public constructor, on the other hand, takes a parameter called operationId, which is given by the developer and is expected to be a substring of the aforementioned operationLocation:

{modelId}/analyzeresults/{resultId}

Goals

  • Stop using magic numbers and correctly parse the arguments using regex, handling format errors.
  • Make sure we support parsing model IDs with characters _, ~, and .. These characters are allowed when naming a custom model.
  • Include new tests to verify proper behavior.
  • Make sure existing tests still pass reliably.

The following file can be used as a reference:

private static Regex _wordRegex = new Regex(@"/readResults/(?<pageIndex>\d*)/lines/(?<lineIndex>\d*)/words/(?<wordIndex>\d*)$", RegexOptions.Compiled, TimeSpan.FromSeconds(2));
private static Regex _lineRegex = new Regex(@"/readResults/(?<pageIndex>\d*)/lines/(?<lineIndex>\d*)$", RegexOptions.Compiled, TimeSpan.FromSeconds(2));

Related: #10385

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions