Skip to content

Commit

Permalink
[formrecognizer] Fix dictionary methods on DocumentField model (Azure…
Browse files Browse the repository at this point in the history
…#23673)

* add fix for dict and list methods

* improve testing

* add invoice tests

* update changelog

* remove extra invoice test

* update tests to use AzureJSONEncoder

* move currency value in if

* pylint fix
  • Loading branch information
catalinaperalta authored Mar 26, 2022
1 parent 68dda72 commit 4e43eb0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
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 @@ -10,6 +10,7 @@
- Renamed `model_count` and `model_limit` on `AccountInfo` to `document_model_count` and `document_model_limit`.

### Bugs Fixed
- Fixed `to_dict()` and `from_dict()` methods on `DocumentField` to support converting lists, dictionaries, and CurrenyValue field types to and from a dictionary.

### Other Changes
- Renamed `sample_copy_model.py` and `sample_copy_model_async.py` to `sample_copy_model_to.py` and `sample_copy_model_to_async.py` under the `3.2-beta` samples folder. Updated the samples to use renamed copy model method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2598,9 +2598,18 @@ def to_dict(self):
:return: dict
:rtype: dict
"""
value = self.value
# CurrencyValue objects are interpreted as dict, therefore need to be processed first
# to call the proper to_dict() method.
if self.value_type == "currency":
value = self.value.to_dict()
elif isinstance(self.value, dict):
value = {k: v.to_dict() for k, v in self.value.items()}
elif isinstance(self.value, list):
value = [v.to_dict() for v in self.value]
return {
"value_type": self.value_type,
"value": self.value,
"value": value,
"content": self.content,
"bounding_regions": [f.to_dict() for f in self.bounding_regions]
if self.bounding_regions
Expand All @@ -2620,9 +2629,20 @@ def from_dict(cls, data):
:return: DocumentField
:rtype: DocumentField
"""

value = data.get("value", None)
# CurrencyValue objects are interpreted as dict, therefore need to be processed first
# to call the proper from_dict() method.
if data.get("value_type", None) == "currency":
value = CurrencyValue.from_dict(data.get("value")) #type: ignore
elif isinstance(data.get("value"), dict):
value = {k: DocumentField.from_dict(v) for k, v in data.get("value").items()} # type: ignore
elif isinstance(data.get("value"), list):
value = [DocumentField.from_dict(v) for v in data.get("value")] # type: ignore

return cls(
value_type=data.get("value_type", None),
value=data.get("value", None),
value=value,
content=data.get("content", None),
bounding_regions=[BoundingRegion.from_dict(v) for v in data.get("bounding_regions")] # type: ignore
if len(data.get("bounding_regions", [])) > 0
Expand Down Expand Up @@ -2880,7 +2900,7 @@ class DocumentPage(object):
:vartype width: float
:ivar height: The height of the image/PDF in pixels/inches, respectively.
:vartype height: float
:ivar unit: The unit used by the width, height, and boundingBox properties. For
:ivar unit: The unit used by the width, height, and bounding box properties. For
images, the unit is "pixel". For PDF, the unit is "inch". Possible values include: "pixel",
"inch".
:vartype unit: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
# ------------------------------------

import pytest
import json
import functools
from datetime import date, time
from devtools_testutils import recorded_by_proxy
from io import BytesIO
from azure.core.exceptions import ClientAuthenticationError, ServiceRequestError, HttpResponseError
from azure.core.credentials import AzureKeyCredential
from azure.core.serialization import AzureJSONEncoder
from azure.ai.formrecognizer._generated.v2022_01_30_preview.models import AnalyzeResultOperation
from azure.ai.formrecognizer import DocumentAnalysisClient, AnalyzeResult, FormContentType
from testcase import FormRecognizerTest
Expand Down Expand Up @@ -182,6 +184,9 @@ def test_invoice_jpg(self, client, **kwargs):
poller = client.begin_analyze_document("prebuilt-invoice", invoice)

result = poller.result()
d = result.to_dict()
json.dumps(d, cls=AzureJSONEncoder)
result = AnalyzeResult.from_dict(d)
assert len(result.documents) == 1
invoice = result.documents[0]

Expand Down Expand Up @@ -419,6 +424,8 @@ def test_receipt_multipage(self, client):
result = poller.result()

d = result.to_dict()
# this is simply checking that the dict is JSON serializable
json.dumps(d, cls=AzureJSONEncoder)
result = AnalyzeResult.from_dict(d)

assert len(result.documents) == 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
# ------------------------------------

import pytest
import json
import functools
from io import BytesIO
from devtools_testutils.aio import recorded_by_proxy_async
from datetime import date, time
from azure.core.exceptions import HttpResponseError
from azure.core.serialization import AzureJSONEncoder
from azure.ai.formrecognizer.aio import DocumentAnalysisClient
from azure.ai.formrecognizer import AnalyzeResult
from azure.ai.formrecognizer._generated.v2022_01_30_preview.models import AnalyzeResultOperation
Expand Down Expand Up @@ -210,6 +212,8 @@ async def test_receipt_multipage(self, client):
result = await poller.result()

d = result.to_dict()
# this is simply checking that the dict is JSON serializable
json.dumps(d, cls=AzureJSONEncoder)
result = AnalyzeResult.from_dict(d)

assert len(result.documents) == 2
Expand Down Expand Up @@ -491,6 +495,9 @@ async def test_invoice_jpg(self, client, **kwargs):
poller = await client.begin_analyze_document("prebuilt-invoice", invoice)

result = await poller.result()
d = result.to_dict()
json.dumps(d, cls=AzureJSONEncoder)
result = AnalyzeResult.from_dict(d)
assert len(result.documents) == 1
invoice = result.documents[0]

Expand Down

0 comments on commit 4e43eb0

Please sign in to comment.