Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: resolve Notion page listing and document import failures (#45053)

FFXN6af924cd9feb
diff --git a/api/controllers/console/datasets/data_source.py b/api/controllers/console/datasets/data_source.py
index db872c18833..9e09a34f94e 100644
--- a/api/controllers/console/datasets/data_source.py
+++ b/api/controllers/console/datasets/data_source.py
@@ -3,7 +3,7 @@ from typing import Never
from uuid import UUID
from flask_restx import Resource
-from pydantic import BaseModel, Field, field_serializer
+from pydantic import BaseModel, Field, field_serializer, field_validator
from configs import dify_config
from controllers.common.errors import InvalidArgumentError, NotFoundError
@@ -63,6 +63,11 @@ class DataSourceNotionListQuery(BaseModel):
dataset_id: str | None = Field(default=None, description="Dataset ID")
credential_id: str = Field(..., description="Credential ID", min_length=1)
+ @field_validator("dataset_id", mode="before")
+ @classmethod
+ def empty_dataset_id_to_none(cls, value: str | None) -> str | None:
+ return None if value == "" else value
+
class DataSourceNotionPreviewQuery(BaseModel):
credential_id: str = Field(..., description="Credential ID", min_length=1)
diff --git a/api/core/rag/extractor/notion_extractor.py b/api/core/rag/extractor/notion_extractor.py
index 8aa6da77616..3ba8b741b2b 100644
--- a/api/core/rag/extractor/notion_extractor.py
+++ b/api/core/rag/extractor/notion_extractor.py
@@ -358,8 +358,6 @@ class NotionExtractor(BaseExtractor):
else:
retrieve_page_url = RETRIEVE_PAGE_URL_TMPL.format(page_id=obj_id)
- query_dict: Mapping[str, Any] = MappingProxyType({})
-
res = httpx.request(
"GET",
retrieve_page_url,
@@ -368,7 +366,6 @@ class NotionExtractor(BaseExtractor):
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
},
- json=query_dict,
timeout=_REQUEST_TIMEOUT,
)
diff --git a/api/tests/unit_tests/controllers/console/datasets/test_data_source.py b/api/tests/unit_tests/controllers/console/datasets/test_data_source.py
index 927f68c674d..e887b5f8fa7 100644
--- a/api/tests/unit_tests/controllers/console/datasets/test_data_source.py
+++ b/api/tests/unit_tests/controllers/console/datasets/test_data_source.py
@@ -548,6 +548,17 @@ class TestDataSourceCases:
assert "page_type" not in payload[0]["pages"][0]
assert collaborators.notion_source.list_calls == [(TENANT_ID, ACCOUNT_ID, "credential-1")]
+ @staticmethod
+ def test_notion_list_accepts_empty_dataset_id_for_new_dataset(collaborators: ControllerCollaborators) -> None:
+ response = collaborators.client.get(
+ "/console/api/notion/pre-import/pages",
+ query_string={"credential_id": "credential-1", "dataset_id": ""},
+ )
+
+ assert response.status_code == 200
+ assert response.get_json()["notion_info"][0]["pages"][0]["is_bound"] is False
+ assert collaborators.notion_source.list_calls == [(TENANT_ID, ACCOUNT_ID, "credential-1")]
+
@staticmethod
@pytest.mark.parametrize("page_type", ["page", "database"])
def test_notion_preview_validates_page_type_and_delegates(
diff --git a/api/tests/unit_tests/core/rag/extractor/test_notion_extractor.py b/api/tests/unit_tests/core/rag/extractor/test_notion_extractor.py
index ef4e0baf130..a88afe56e42 100644
--- a/api/tests/unit_tests/core/rag/extractor/test_notion_extractor.py
+++ b/api/tests/unit_tests/core/rag/extractor/test_notion_extractor.py
@@ -139,6 +139,48 @@ class TestNotionExtractorCases:
with pytest.raises(ValueError, match="notion page type not supported"):
extractor._load_data_as_documents("obj", "unsupported")
+ @pytest.mark.parametrize(
+ ("page_type", "expected_path"),
+ [("page", "/v1/pages/obj"), ("database", "/v1/databases/obj")],
+ )
+ def test_last_edited_time_get_has_no_json_body(
+ self, monkeypatch: pytest.MonkeyPatch, page_type: str, expected_path: str
+ ) -> None:
+ requests: list[httpx.Request] = []
+
+ def respond(request: httpx.Request) -> httpx.Response:
+ requests.append(request)
+ return httpx.Response(200, json={"last_edited_time": "2024-01-01T00:00:00Z"})
+
+ transport = httpx.MockTransport(respond)
+
+ def send(
+ method: str,
+ url: str,
+ *,
+ headers: dict[str, str],
+ timeout: httpx.Timeout,
+ json: object | None = None,
+ ) -> httpx.Response:
+ with httpx.Client(transport=transport) as client:
+ return client.request(method, url, headers=headers, timeout=timeout, json=json)
+
+ monkeypatch.setattr(notion_extractor.httpx, "request", send)
+ extractor = notion_extractor.NotionExtractor(
+ notion_workspace_id="ws",
+ notion_obj_id="obj",
+ notion_page_type=page_type,
+ tenant_id="tenant",
+ notion_access_token="token",
+ notion_token_loader=lambda: "token",
+ )
+
+ assert extractor.get_notion_last_edited_time() == "2024-01-01T00:00:00Z"
+ assert len(requests) == 1
+ assert requests[0].method == "GET"
+ assert requests[0].url.path == expected_path
+ assert requests[0].content == b""
+
class TestNotionDatabase:
def test_get_notion_database_data_parses_property_types_and_pagination(self, mocker: MockerFixture) -> None:
extractor = notion_extractor.NotionExtractor(
diff --git a/web/app/components/base/notion-page-selector/__tests__/base.spec.tsx b/web/app/components/base/notion-page-selector/__tests__/base.spec.tsx
index 9216974e35e..6e23b3782b9 100644
--- a/web/app/components/base/notion-page-selector/__tests__/base.spec.tsx
+++ b/web/app/components/base/notion-page-selector/__tests__/base.spec.tsx
@@ -114,8 +114,8 @@ function render(ui: ReactElement<ComponentProps<typeof NotionPageSelector>>) {
consoleQuery.notion.preImport.pages.get.queryKey({
input: {
query: {
- dataset_id: ui.props.datasetId ?? '',
credential_id: ui.props.credentialList[0]?.id ?? '',
+ ...(ui.props.datasetId && { dataset_id: ui.props.datasetId }),
},
},
}),
@@ -140,21 +140,50 @@ describe('NotionPageSelector Base', () => {
expect(screen.getByTestId('notion-page-selector-loading')).toBeInTheDocument()
})
- it('should render connector and open settings when fetch fails', async () => {
+ it('shows the connector without requesting pages when no credential exists', () => {
seedPages = false
- fetchPages.mockRejectedValue(new Error('Notion unavailable'))
+
+ render(<NotionPageSelector credentialList={[]} onSelect={vi.fn()} />)
+
+ expect(
+ screen.getByRole('button', { name: 'datasetCreation.stepOne.connect' }),
+ ).toBeInTheDocument()
+ expect(fetchPages).not.toHaveBeenCalled()
+ })
+
+ it.each([undefined, ''])(
+ 'omits an absent dataset ID when loading pages for a new dataset',
+ async (datasetId) => {
+ seedPages = false
+
+ render(
+ <NotionPageSelector
+ credentialList={mockCredentialList}
+ onSelect={vi.fn()}
+ datasetId={datasetId}
+ />,
+ )
+
+ expect(await screen.findByText('Root 1')).toBeInTheDocument()
+ expect(fetchPages).toHaveBeenCalledWith({ query: { credential_id: 'c1' } })
+ },
+ )
+
+ it('shows a retryable error instead of a connector when page loading fails', async () => {
+ seedPages = false
+ fetchPages.mockRejectedValueOnce(new Error('Notion unavailable'))
const user = userEvent.setup()
render(<NotionPageSelector credentialList={mockCredentialList} onSelect={vi.fn()} />)
- const connectButton = await screen.findByRole('button', {
- name: 'datasetCreation.stepOne.connect',
- })
- await user.click(connectButton)
+ expect(await screen.findByRole('alert')).toHaveTextContent('common.api.actionFailed')
+ expect(
+ screen.queryByRole('button', { name: 'datasetCreation.stepOne.connect' }),
+ ).not.toBeInTheDocument()
+ await user.click(screen.getByRole('button', { name: 'common.operation.retry' }))
- await waitFor(() =>
- expect(onUrlUpdate.mock.lastCall?.[0].searchParams.get('settings')).toBe('data-source'),
- )
+ expect(await screen.findByText('Root 1')).toBeInTheDocument()
+ expect(screen.queryByRole('alert')).not.toBeInTheDocument()
})
it('should render page selector and allow selecting a page tree', async () => {
diff --git a/web/app/components/base/notion-page-selector/base.tsx b/web/app/components/base/notion-page-selector/base.tsx
index 444f0cb50f5..920027aba99 100644
--- a/web/app/components/base/notion-page-selector/base.tsx
+++ b/web/app/components/base/notion-page-selector/base.tsx
@@ -6,7 +6,8 @@ import type {
DataSourceNotionWorkspace,
NotionPage,
} from '@/models/common'
-import { useQuery, useQueryClient } from '@tanstack/react-query'
+import { Button } from '@langgenius/dify-ui/button'
+import { skipToken, useQuery, useQueryClient } from '@tanstack/react-query'
import { useQueryState } from 'nuqs'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
@@ -61,7 +62,7 @@ const NotionPageSelector = ({
canPreview,
previewPageId,
onPreview,
- datasetId = '',
+ datasetId,
credentialList,
onSelectCredential,
}: NotionPageSelectorProps) => {
@@ -71,10 +72,12 @@ const NotionPageSelector = ({
const queryClient = useQueryClient()
const invalidPreImportNotionPages = useCallback(
- ({ datasetId, credentialId }: { datasetId: string; credentialId: string }) =>
+ ({ datasetId, credentialId }: { datasetId?: string; credentialId: string }) =>
queryClient.invalidateQueries({
queryKey: consoleQuery.notion.preImport.pages.get.queryKey({
- input: { query: { dataset_id: datasetId, credential_id: credentialId } },
+ input: {
+ query: { credential_id: credentialId, ...(datasetId && { dataset_id: datasetId }) },
+ },
}),
}),
[queryClient],
@@ -129,9 +132,17 @@ const NotionPageSelector = ({
data: notionsPages,
isFetching: isFetchingNotionPages,
isError: isFetchingNotionPagesError,
+ refetch: refetchNotionPages,
} = useQuery(
consoleQuery.notion.preImport.pages.get.queryOptions({
- input: { query: { dataset_id: datasetId, credential_id: currentCredentialId } },
+ input: currentCredentialId
+ ? {
+ query: {
+ credential_id: currentCredentialId,
+ ...(datasetId && { dataset_id: datasetId }),
+ },
+ }
+ : skipToken,
retry: 0,
select: toNotionWorkspaces,
}),
@@ -205,7 +216,7 @@ const NotionPageSelector = ({
setSettingsDestination('data-source')
}, [setSettingsDestination])
- if (isFetchingNotionPagesError) {
+ if (!currentCredentialId) {
return <NotionConnector onSetting={handleConfigureNotion} />
}
@@ -230,7 +241,20 @@ const NotionPageSelector = ({
<SearchInput value={searchValue} onChange={handleSearchValueChange} />
</div>
<div className="overflow-hidden rounded-b-xl">
- {isFetchingNotionPages ? (
+ {isFetchingNotionPagesError ? (
+ <div className="flex h-74 flex-col items-center justify-center gap-3">
+ <p role="alert" className="text-text-secondary">
+ {t(($) => $['api.actionFailed'], { ns: 'common' })}
+ </p>
+ <Button
+ size="small"
+ loading={isFetchingNotionPages}
+ onClick={() => void refetchNotionPages()}
+ >
+ {t(($) => $['operation.retry'], { ns: 'common' })}
+ </Button>
+ </div>
+ ) : isFetchingNotionPages ? (
<div
className="flex h-74 items-center justify-center"
data-testid="notion-page-selector-loading"