Merge pull request #139 from roydonsequeira/fix/sharepoint-children-paging

fix(sharepoint): follow @odata.nextLink when listing folders
This commit is contained in:
Tim Baek authored and GitHub committed 2026-10-28 07:52:03 +09:00
commit e60d106167
2 files changed
+69 -4

No files matched your search

+9 -3
View File
@@ -149,10 +149,16 @@ class SharePointConnector(BaseConnector):
def _walk_folder(self, folder_path: str, prefix: str, entries: list[ManifestEntry]) -> None:
url = f"/drives/{self._drive_id}/root/children" if folder_path == "/" else f"/drives/{self._drive_id}/root:/{quote(folder_path)}:/children"
resp = self._http.get(url)
resp.raise_for_status()
items: list[dict[str, Any]] = []
while url:
resp = self._http.get(url)
resp.raise_for_status()
data = resp.json()
items.extend(data.get("value", []))
# Graph pages large folders; @odata.nextLink is the absolute URL of the next page.
url = data.get("@odata.nextLink")
for item in resp.json().get("value", []):
for item in items:
if "folder" in item:
sub = f"{prefix}/{item['name']}" if prefix else item["name"]
child_path = f"{folder_path}/{item['name']}" if folder_path != "/" else item["name"]
+60 -1
View File
@@ -1,5 +1,7 @@
from __future__ import annotations
from typing import Any
import httpx
import pytest
import respx
@@ -18,12 +20,16 @@ def _sharepoint_env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("SHAREPOINT_CLIENT_SECRET", "secret")
def _mock_graph(router: respx.MockRouter) -> None:
def _mock_site(router: respx.MockRouter) -> None:
router.post("https://login.microsoftonline.com/tenant/oauth2/v2.0/token").respond(
json={"access_token": "token"}
)
router.get(f"{GRAPH}/sites/contoso.sharepoint.com:/sites/Team").respond(json={"id": "site-id"})
router.get(f"{GRAPH}/sites/site-id/drives").respond(json={"value": [{"id": "drive-id", "name": "Docs"}]})
def _mock_graph(router: respx.MockRouter) -> None:
_mock_site(router)
router.get(f"{GRAPH}/drives/drive-id/root/children").respond(
json={
"value": [
@@ -140,3 +146,56 @@ def test_checksum_is_the_unquoted_etag(respx_mock: respx.MockRouter) -> None:
@respx.mock(assert_all_called=False)
def test_checksum_is_empty_without_etag_or_ctag(respx_mock: respx.MockRouter) -> None:
assert _manifest_for(respx_mock, {})[0].checksum == ""
def _file(name: str) -> dict[str, Any]:
return {"name": name, "file": {}, "size": 1}
def _folder(name: str) -> dict[str, Any]:
return {"name": name, "folder": {}}
def _children(router: respx.MockRouter, path: str, *pages: list[dict[str, Any]]) -> respx.Route:
"""Serve a children listing split into pages chained by @odata.nextLink, as Graph does."""
responses = []
for number, items in enumerate(pages, start=1):
body: dict[str, Any] = {"value": items}
if number < len(pages):
body["@odata.nextLink"] = f"{GRAPH}{path}?$skiptoken={number}"
responses.append(httpx.Response(200, json=body))
return router.get(f"{GRAPH}{path}").mock(side_effect=responses)
@respx.mock
def test_build_manifest_follows_next_link_through_every_page(respx_mock: respx.MockRouter) -> None:
_mock_site(respx_mock)
root = _children(
respx_mock,
"/drives/drive-id/root/children",
[_file("a.pdf"), _folder("Reports")],
[], # A page may be empty and still link to the next one.
[_file("z.pdf"), _folder("Archive")],
)
_children(respx_mock, "/drives/drive-id/root:/Reports:/children", [_file("q1.xlsx")])
_children(
respx_mock,
"/drives/drive-id/root:/Archive:/children",
[_file("2024.xlsx")],
[_file("2025.xlsx")],
)
manifest = _resolve_connector(SOURCE).build_manifest()
assert [entry.display_path for entry in manifest] == [
"Archive/2024.xlsx",
"Archive/2025.xlsx",
"Reports/q1.xlsx",
"a.pdf",
"z.pdf",
]
assert [str(call.request.url) for call in root.calls] == [
f"{GRAPH}/drives/drive-id/root/children",
f"{GRAPH}/drives/drive-id/root/children?$skiptoken=1",
f"{GRAPH}/drives/drive-id/root/children?$skiptoken=2",
]