Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FormRecognizer] Fix: client can now parse results with null fields #12233

Merged
merged 4 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/formrecognizer/Azure.AI.FormRecognizer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Custom form recognition without labels can now handle multipaged forms.
- `RecognizedForm.Pages` now only contains pages whose numbers are within `RecognizedForm.PageRange`.
- `FieldText.TextContent` cannot be `null` anymore, and it will be empty when no element is returned from the service.
- Custom form recognition with labels can now parse results from forms that do not contain all of the expected labels ([#11821](https://github.com/Azure/azure-sdk-for-net/issues/11821)).

## 1.0.0-preview.2 (05-06-2020)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected FormTrainingClient()
/// <summary>
/// Initializes a new instance of the <see cref="FormTrainingClient"/> class.
/// </summary>
/// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service. The URI is likely to be similar to <c>{protocol}://{resourcename}.cognitiveservices.azure.com</c>.</param>
/// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</param>
/// <param name="credential">A credential used to authenticate to an Azure Service.</param>
/// <remarks>
/// Both the <paramref name="endpoint"/> URI <c>string</c> and the <paramref name="credential"/> <c>string</c> key
Expand All @@ -48,7 +48,7 @@ protected FormTrainingClient()
/// <summary>
/// Initializes a new instance of the <see cref="FormTrainingClient"/> class.
/// </summary>
/// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service. The URI is likely to be similar to <c>{protocol}://{resourcename}.cognitiveservices.azure.com</c>.</param>
/// <param name="endpoint">The endpoint to use for connecting to the Form Recognizer Azure Cognitive Service.</param>
/// <param name="credential">A credential used to authenticate to an Azure Service.</param>
/// <param name="options">A set of options to apply when configuring the client.</param>
/// <remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ private static IReadOnlyDictionary<string, FormField> ConvertSupervisedFields(IR

foreach (var field in fields)
{
fieldDictionary[field.Key] = new FormField(field.Key, field.Value, readResults);
fieldDictionary[field.Key] = field.Value == null
? null
: new FormField(field.Key, field.Value, readResults);
}

return fieldDictionary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,43 @@ public async Task StartRecognizeCustomFormsWithLabelsCanParseMultipageForms(bool
}
}

[Test]
[TestCase(true)]
[TestCase(false)]
public async Task StartRecognizeCustomFormsWithLabelsCanParseDifferentTypeOfForm(bool useStream)
{
var client = CreateInstrumentedFormRecognizerClient();
RecognizeCustomFormsOperation operation;

// Use Form_<id>.<ext> files for training with labels.

await using var trainedModel = await CreateDisposableTrainedModelAsync(useTrainingLabels: true);

// Attempt to recognize a different type of form: Invoice_1.pdf. This form does not contain all the labels
// the newly trained model expects.

if (useStream)
{
using var stream = new FileStream(FormRecognizerTestEnvironment.RetrieveInvoicePath(1, ContentType.Pdf), FileMode.Open);
using (Recording.DisableRequestBodyRecording())
{
operation = await client.StartRecognizeCustomFormsAsync(trainedModel.ModelId, stream);
}
}
else
{
var uri = new Uri(FormRecognizerTestEnvironment.RetrieveInvoiceUri(1));
operation = await client.StartRecognizeCustomFormsFromUriAsync(trainedModel.ModelId, uri);
}

RecognizedFormCollection forms = await operation.WaitForCompletionAsync();
var fields = forms.Single().Fields;

// Verify that we got back at least one null field to make sure we hit the code path we want to test.

Assert.IsTrue(fields.Any(kvp => kvp.Value == null));
}

/// <summary>
/// Verifies that the <see cref="FormRecognizerClient" /> is able to connect to the Form
/// Recognizer cognitive service and perform analysis based on a custom labeled model.
Expand Down
Loading