Skip to content

fix(components): fix Google Drive loader "Include Subfolders" Bad Request and subfolder traversal (#5063) - #6689

Open
ArslanRasheed60 wants to merge 2 commits into
FlowiseAI:mainfrom
ArslanRasheed60:bugfix/gdrive-loader-subfolder-pagesize
Open

fix(components): fix Google Drive loader "Include Subfolders" Bad Request and subfolder traversal (#5063)#6689
ArslanRasheed60 wants to merge 2 commits into
FlowiseAI:mainfrom
ArslanRasheed60:bugfix/gdrive-loader-subfolder-pagesize

Conversation

@ArslanRasheed60

Copy link
Copy Markdown

Description

Addresses #5063 — "Google Drive Loader: Include Subfolders ON = Bad Request".

Two related defects in the Google Drive document loader (packages/components/nodes/documentloaders/GoogleDrive/GoogleDrive.ts, getFilesFromFolder) are fixed here. They are split across two commits so each can be weighed independently.

Root cause

1. Recursive listing sends an invalid pageSize → HTTP 400 (the reported error).

When Include Subfolders is ON, getFilesFromFolder recurses into each subfolder passing the remaining budget as the child's maxFiles:

maxFiles - files.length

The child then builds its page request as:

url.searchParams.append('pageSize', Math.min(maxFiles - files.length, 1000).toString())

Once the budget is used up, maxFiles - files.length is 0 or negative, so pageSize becomes 0/negative. The Google Drive API rejects a pageSize outside [1, 1000] with HTTP 400 Bad Request, which surfaces as the reported:

Failed to list files: Bad Request

This only triggers when Include Subfolders is ON, because only recursion shrinks the budget below 1.

2. Subfolders are excluded by the fileTypes filter, so "Include Subfolders" silently does nothing.

The folder listing query applies the fileTypes filter:

query += ` and (${mimeTypeQuery})`

The default fileTypes does not include the folder mimeType (application/vnd.google-apps.folder), so subfolders are never returned by the query and the recursion has nothing to descend into. With the default configuration, "Include Subfolders" therefore loads nothing from subfolders even when the 400 does not fire.

The fix

Commit 1 — core (pageSize guard):

  • Stop issuing a request once the file budget is exhausted (remainingFiles <= 0 → break) instead of sending an invalid page request.
  • Clamp pageSize to a minimum of 1: Math.min(Math.max(remainingFiles, 1), 1000).
  • Skip subfolder recursion when no budget remains, so the child call is never asked for a non-positive pageSize.

Commit 2 — secondary (subfolder traversal):

  • When Include Subfolders is enabled, also request the folder mimeType so subfolders survive the fileTypes filter and can be discovered for recursion.
  • Separate folders out of the returned file list, so the fileTypes filter still applies to the actual loaded files and folders are never treated as loadable documents.

Testing

Added a co-located test suite GoogleDrive.test.ts that drives the real getFilesFromFolder with a mocked fetch that emulates the Drive API's mimeType filtering:

  • pageSize guard: a recursive listing whose budget is exhausted never issues a request with pageSize <= 0, and does not recurse once the budget is gone. On the pre-fix code this test fails with a recorded pageSize of -1.
  • Subfolder traversal: with the default fileTypes filter, subfolders are still traversed and their files collected, while folders themselves are excluded from the returned files. On the pre-fix code this test fails because the subfolder is filtered out and never visited.

Verified locally:

  • pnpm lint (root) — 0 errors.
  • packages/components full Jest suite — the new GoogleDrive.test.ts passes; all suites green (the only failures are the pre-existing Windows-only POSIX-path cases in validator.test.ts, unrelated to this change).
  • pnpm build (root) — passes.

Note for maintainers

This resolves the confirmed Include Subfolders → Bad Request defect (recursion budget → pageSize <= 0 → Drive 400) and the separate subfolder-skipped-under-default-fileTypes issue. I was not able to reproduce the reporter's exact minimal repro (2 files / default config) purely from source, so I've written this as Addresses #5063 rather than an unqualified "Closes". It may be worth having the reporter confirm their Max Files / File Types configuration so we can be certain their scenario is fully covered.

…er listing (FlowiseAI#5063)

When 'Include Subfolders' is enabled, getFilesFromFolder recurses with a
maxFiles budget of (maxFiles - files.length). Once the budget reaches zero,
the child call computed pageSize = Math.min(maxFiles - files.length, 1000),
which becomes 0 or negative. Google Drive rejects a pageSize outside [1, 1000]
with HTTP 400 (Bad Request), surfacing as 'Failed to list files: Bad Request'.

Stop issuing a request once the file budget is exhausted and clamp pageSize to
a minimum of 1, and skip subfolder recursion when no budget remains.
…lter is set (FlowiseAI#5063)

The default fileTypes filter excludes the folder mimeType
(application/vnd.google-apps.folder), so the folder listing query never
returned subfolders and 'Include Subfolders' silently loaded nothing whenever
fileTypes was set (which is the default configuration).

When Include Subfolders is enabled, also request the folder mimeType so
subfolders can be discovered for recursion, and separate folders out of the
returned file list so the fileTypes filter still applies to the loaded files
and folders are never treated as loadable documents.

Add co-located tests covering the recursive pageSize guard and subfolder
traversal under the default fileTypes filter.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@ArslanRasheed60

Copy link
Copy Markdown
Author

Following up on this — would appreciate a review when someone has a chance. Happy to make any changes needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant