diff --git a/packages/uipath-platform/pyproject.toml b/packages/uipath-platform/pyproject.toml index 78f8bc068..fdafe30cd 100644 --- a/packages/uipath-platform/pyproject.toml +++ b/packages/uipath-platform/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "uipath-platform" -version = "0.3.12" +version = "0.3.13" description = "HTTP client library for programmatic access to UiPath Platform" readme = { file = "README.md", content-type = "text/markdown" } requires-python = ">=3.11" diff --git a/packages/uipath-platform/src/uipath/platform/common/_span_utils.py b/packages/uipath-platform/src/uipath/platform/common/_span_utils.py index 6f1bb4849..af4240c6d 100644 --- a/packages/uipath-platform/src/uipath/platform/common/_span_utils.py +++ b/packages/uipath-platform/src/uipath/platform/common/_span_utils.py @@ -1,6 +1,7 @@ import inspect import json import logging +import mimetypes import os import uuid from dataclasses import dataclass, field @@ -14,7 +15,7 @@ from opentelemetry import trace from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor from opentelemetry.trace import StatusCode, format_span_id -from pydantic import BaseModel, ConfigDict, Field +from pydantic import BaseModel, ConfigDict, Field, model_validator from uipath.core.serialization import serialize_json from uipath.platform.constants import ( @@ -223,6 +224,15 @@ class SpanAttachment(BaseModel): default=AttachmentDirection.NONE, alias="direction" ) + @model_validator(mode="after") + def _default_blank_mime_type(self) -> "SpanAttachment": + # The traces API rejects the whole span when an attachment has no MIME type. + if not self.mime_type.strip(): + self.mime_type = ( + mimetypes.guess_type(self.file_name)[0] or "application/octet-stream" + ) + return self + @dataclass class UiPathSpan: diff --git a/packages/uipath-platform/tests/services/test_span_utils.py b/packages/uipath-platform/tests/services/test_span_utils.py index 170f4fced..59a6f64dd 100644 --- a/packages/uipath-platform/tests/services/test_span_utils.py +++ b/packages/uipath-platform/tests/services/test_span_utils.py @@ -22,6 +22,7 @@ from uipath.platform.common._span_utils import ( _SOURCE_BY_INT, ExecutionType, + SpanAttachment, SpanSource, SpanStatus, VerbosityLevel, @@ -1253,3 +1254,58 @@ def test_ended_span_keeps_real_end_time(self) -> None: assert uipath_span.end_time is not None assert uipath_span.to_dict()["EndTime"] == uipath_span.end_time + + +class TestSpanAttachmentMimeType: + @pytest.mark.parametrize( + ("file_name", "mime_type", "expected"), + [ + ("report.pdf", "", "application/pdf"), + ("client.ovpn", "", "application/octet-stream"), + ("client.ovpn", " ", "application/octet-stream"), + ("", "", "application/octet-stream"), + ("client.ovpn", "text/plain", "text/plain"), + ], + ) + def test_blank_mime_type_is_replaced(self, file_name, mime_type, expected): + attachment = SpanAttachment( + id="0f877427-f8ab-457e-1df3-08df4715cc29", + file_name=file_name, + mime_type=mime_type, + ) + + assert attachment.mime_type == expected + + def test_span_with_untyped_attachment_serializes_a_mime_type(self): + mock_span = Mock(spec=OTelSpan) + mock_span.get_span_context.return_value = SpanContext( + trace_id=0x123456789ABCDEF0123456789ABCDEF0, + span_id=0x0123456789ABCDEF, + is_remote=False, + ) + mock_span.name = "Agent run - Agent" + mock_span.parent = None + mock_span.status.status_code = StatusCode.OK + mock_span.attributes = { + "attachments": json.dumps( + [ + { + "id": "81584504-38b3-406b-1df4-08df4715cc29", + "fileName": "test-client-1.ovpn", + "mimeType": "", + "provider": 0, + "direction": 1, + } + ] + ) + } + mock_span.events = [] + mock_span.links = [] + mock_span.start_time = int(datetime.now().timestamp() * 1e9) + mock_span.end_time = mock_span.start_time + 1_000_000 + + payload = _SpanUtils.otel_span_to_uipath_span(mock_span).to_dict() + + assert [att["MimeType"] for att in payload["Attachments"]] == [ + "application/octet-stream" + ] diff --git a/packages/uipath-platform/uv.lock b/packages/uipath-platform/uv.lock index 479b3bdca..17d0ca82d 100644 --- a/packages/uipath-platform/uv.lock +++ b/packages/uipath-platform/uv.lock @@ -1082,7 +1082,7 @@ dev = [ [[package]] name = "uipath-platform" -version = "0.3.12" +version = "0.3.13" source = { editable = "." } dependencies = [ { name = "anyio" }, diff --git a/packages/uipath/uv.lock b/packages/uipath/uv.lock index 42e39b292..85cca848a 100644 --- a/packages/uipath/uv.lock +++ b/packages/uipath/uv.lock @@ -2883,7 +2883,7 @@ wheels = [ [[package]] name = "uipath-platform" -version = "0.3.12" +version = "0.3.13" source = { editable = "../uipath-platform" } dependencies = [ { name = "anyio" },