From fcc465ec2e72b35e26082b880157d74f32cac05a Mon Sep 17 00:00:00 2001 From: stumpylog <797416+stumpylog@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:10:35 -0800 Subject: [PATCH] Fix: reject over-long text zone values instead of truncating A truncated URL is a different URL. Let field validation reject string and URL values that are too long. --- src/documents/tests/test_zone_ocr.py | 79 +++++++++++++++++++++------- src/documents/zone_ocr.py | 6 +-- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/documents/tests/test_zone_ocr.py b/src/documents/tests/test_zone_ocr.py index 3df51ab7d8..c79221efd4 100644 --- a/src/documents/tests/test_zone_ocr.py +++ b/src/documents/tests/test_zone_ocr.py @@ -2272,6 +2272,67 @@ class TestPrepareFieldUpdate: assert (update.value if update else None) == expected + @pytest.mark.parametrize( + ("data_type", "text", "expected"), + [ + pytest.param( + CustomField.FieldDataType.STRING, + "x" * 128, + "x" * 128, + id="string-at-the-limit", + ), + pytest.param( + CustomField.FieldDataType.STRING, + "x" * 129, + None, + id="string-over-the-limit", + ), + pytest.param( + CustomField.FieldDataType.URL, + "https://example.com/" + "a" * 150, + "https://example.com/" + "a" * 150, + id="url-longer-than-128", + ), + pytest.param( + CustomField.FieldDataType.URL, + "https://example.com/" + "a" * 200, + None, + id="url-over-the-limit", + ), + pytest.param( + CustomField.FieldDataType.URL, + "not a url", + None, + id="not-a-url", + ), + ], + ) + def test_text_values_are_kept_whole_or_rejected( + self, + data_type: str, + text: str, + expected: str | None, + ) -> None: + """ + GIVEN: + - A zone targeting a string or URL custom field + WHEN: + - A field update is prepared for text within or beyond the field's + limits + THEN: + - Valid text is kept whole and invalid or too long text produces no + update, never a truncated value + """ + field = CustomFieldFactory(data_type=data_type) + zone = OcrTemplateZoneFactory( + target=OcrTemplateZone.TargetType.CUSTOM_FIELD, + custom_field=field, + ) + + update = _prepare_field_update(zone, text) + + assert (update.value if update else None) == expected + @pytest.mark.parametrize( "target", [ @@ -2817,24 +2878,6 @@ class TestConvertValue: """ assert _convert_value(value, data_type, locale, ENG_DATES) == expected - def test_string_truncation(self) -> None: - """ - GIVEN: - - An extracted string longer than the string field limit - WHEN: - - The value is converted to a STRING field value - THEN: - - The result is truncated to 128 characters - """ - result = _convert_value( - "x" * 200, - CustomField.FieldDataType.STRING, - EN, - ENG_DATES, - ) - assert isinstance(result, str) - assert len(result) == 128 - @pytest.mark.parametrize( ("value", "locale", "expected"), [ diff --git a/src/documents/zone_ocr.py b/src/documents/zone_ocr.py index 5e18b36f94..fb132cc6a3 100644 --- a/src/documents/zone_ocr.py +++ b/src/documents/zone_ocr.py @@ -991,10 +991,10 @@ def _convert_value( if data_type in ( CustomField.FieldDataType.STRING, CustomField.FieldDataType.URL, + CustomField.FieldDataType.LONG_TEXT, ): - return value[:128] - - elif data_type == CustomField.FieldDataType.LONG_TEXT: + # Returned whole; _prepare_field_update validates length and URL + # format, so a value is rejected rather than truncated. return value elif data_type == CustomField.FieldDataType.INT: