Skip to content

Commit 95394b4

Browse files
Ensure string values within text_object string
Assisted-by: Codex
1 parent 869f601 commit 95394b4

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/pdfrest/models/_internal.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,22 @@ def _serialize_redactions(value: list[_PdfRedactionVariant]) -> str:
148148
return json.dumps(payload, separators=(",", ":"))
149149

150150

151+
def _serialize_text_object_value(value: Any) -> Any:
152+
if isinstance(value, str):
153+
return value
154+
return json.dumps(value, separators=(",", ":"))
155+
156+
151157
def _serialize_text_objects(value: list[BaseModel]) -> str:
152-
payload = [entry.model_dump(mode="json", exclude_none=True) for entry in value]
158+
payload = [
159+
{
160+
key: _serialize_text_object_value(field_value)
161+
for key, field_value in entry.model_dump(
162+
mode="json", exclude_none=True
163+
).items()
164+
}
165+
for entry in value
166+
]
153167
return json.dumps(payload, separators=(",", ":"))
154168

155169

tests/live/test_live_add_text_to_pdf.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,22 @@ def _serialize_text_object_for_extra_body(
4242
text_object: dict[str, object],
4343
) -> dict[str, object]:
4444
serialized = dict(text_object)
45+
# PdfAddTextObjectModel coercion: numeric placement/size fields are floats on
46+
# serialization even when callers provide integers.
47+
for key in ("max_width", "rotation", "text_size", "x", "y"):
48+
value = serialized.get(key)
49+
if isinstance(value, int):
50+
serialized[key] = float(value)
4551
rgb = serialized.get("text_color_rgb")
4652
if isinstance(rgb, (list, tuple)):
4753
serialized["text_color_rgb"] = ",".join(str(channel) for channel in rgb)
4854
cmyk = serialized.get("text_color_cmyk")
4955
if isinstance(cmyk, (list, tuple)):
5056
serialized["text_color_cmyk"] = ",".join(str(channel) for channel in cmyk)
57+
# Match add-text wire format where each non-string value is JSON-quoted.
58+
for key, value in list(serialized.items()):
59+
if not isinstance(value, str):
60+
serialized[key] = json.dumps(value, separators=(",", ":"))
5161
return serialized
5262

5363

tests/test_add_text_to_pdf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def _serialize_text_object_for_request(
3939
text_object: dict[str, object],
4040
) -> dict[str, object]:
4141
serialized = dict(text_object)
42+
# PdfAddTextObjectModel defines these fields as floats, so Pydantic serializes
43+
# integer inputs as 200.0/45.0/etc. Mirror that to keep wire assertions exact.
4244
for key in ("max_width", "rotation", "text_size", "x", "y"):
4345
value = serialized.get(key)
4446
if isinstance(value, int):
@@ -49,6 +51,10 @@ def _serialize_text_object_for_request(
4951
cmyk = serialized.get("text_color_cmyk")
5052
if isinstance(cmyk, (list, tuple)):
5153
serialized["text_color_cmyk"] = ",".join(str(channel) for channel in cmyk)
54+
# Add-text payloads now quote non-string values inside the text_objects JSON.
55+
for key, value in list(serialized.items()):
56+
if not isinstance(value, str):
57+
serialized[key] = json.dumps(value, separators=(",", ":"))
5258
return serialized
5359

5460

0 commit comments

Comments
 (0)