Skip to content

Commit 0f706c6

Browse files
authored
fix: datasource owner/project missing parsing (#1700)
Eric Summers pointed out that when the User Visibility setting is set to "Limited," TSC fails to parse because it can't retrieve any owner information. This bug is due to an `UnboundLocalError` where the `owner` and `project` variables were not assigned in cases where the owner and project elements were not included in the XML response. This PR also includes a test for the parsing where the owner and project elements are missing and properly set to `None` on the DatasourceItem. Co-authored-by: Jordan Woods <13803242+jorwoods@users.noreply.github.com>
1 parent 0fe4e2a commit 0f706c6

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

tableauserverclient/models/datasource_item.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,15 @@ def _parse_element(datasource_xml: ET.Element, ns: dict) -> tuple:
535535

536536
project_id = None
537537
project_name = None
538+
project = None
538539
project_elem = datasource_xml.find(".//t:project", namespaces=ns)
539540
if project_elem is not None:
540541
project = ProjectItem.from_xml(project_elem, ns)
541542
project_id = project_elem.get("id", None)
542543
project_name = project_elem.get("name", None)
543544

544545
owner_id = None
546+
owner = None
545547
owner_elem = datasource_xml.find(".//t:owner", namespaces=ns)
546548
if owner_elem is not None:
547549
owner = UserItem.from_xml(owner_elem, ns)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
3+
<pagination pageNumber="1" pageSize="100" totalAvailable="2" />
4+
<datasources>
5+
<datasource id="e76a1461-3b1d-4588-bf1b-17551a879ad9" name="SampleDS" size="4096" contentUrl="SampleDS" description="SampleDsDescription" type="dataengine" createdAt="2016-08-11T21:22:40Z" updatedAt="2016-08-11T21:34:17Z" encryptExtracts="false" hasExtracts="true" useRemoteQueryAgent="false" webpageUrl="https://web.com">
6+
<tags />
7+
</datasource>
8+
<datasource id="9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" name="Sample datasource" description="description Sample" size="10240" contentUrl="Sampledatasource" type="dataengine" createdAt="2016-08-04T21:31:55Z" updatedAt="2016-08-04T21:31:55Z" encryptExtracts="true" hasExtracts="false" useRemoteQueryAgent="true" webpageUrl="https://page.com">
9+
<tags>
10+
<tag label="world" />
11+
<tag label="indicators" />
12+
<tag label="sample" />
13+
</tags>
14+
</datasource>
15+
</datasources>
16+
</tsResponse>

test/test_datasource.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
GET_EMPTY_XML = TEST_ASSET_DIR / "datasource_get_empty.xml"
2525
GET_BY_ID_XML = TEST_ASSET_DIR / "datasource_get_by_id.xml"
2626
GET_XML_ALL_FIELDS = TEST_ASSET_DIR / "datasource_get_all_fields.xml"
27+
GET_NO_OWNER = TEST_ASSET_DIR / "datasource_get_no_owner.xml"
2728
POPULATE_CONNECTIONS_XML = TEST_ASSET_DIR / "datasource_populate_connections.xml"
2829
POPULATE_PERMISSIONS_XML = TEST_ASSET_DIR / "datasource_populate_permissions.xml"
2930
PUBLISH_XML = TEST_ASSET_DIR / "datasource_publish.xml"
@@ -850,3 +851,13 @@ def test_get_datasource_all_fields(server) -> None:
850851
assert datasources[0].owner.last_login == parse_datetime("2025-02-04T06:39:20Z")
851852
assert datasources[0].owner.name == "bob@example.com"
852853
assert datasources[0].owner.site_role == "SiteAdministratorCreator"
854+
855+
856+
def test_get_datasource_no_owner(server: TSC.Server) -> None:
857+
with requests_mock.mock() as m:
858+
m.get(server.datasources.baseurl, text=GET_NO_OWNER.read_text())
859+
datasources, _ = server.datasources.get()
860+
861+
datasource = datasources[0]
862+
assert datasource.owner is None
863+
assert datasource.project is None

0 commit comments

Comments
 (0)