Skip to content

fix(documents): set the collection on found rows before decoding them - #1099

Merged
abnegate merged 1 commit into
mainfrom
fix/find-collection-before-decode
Dec 1, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix/find-collection-before-decode

Conversation

@abnegate

@abnegate abnegate commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Problem

find() decoded each row before setting $collection on it. A decode filter that reads $document->getCollection() got '' on every find(), but the right collection id on getDocument(), createDocument(), updateDocument() and upserts, which all set it first.

Appwrite's subQueryVariables filter reads it to choose function or site variables. On '' it falls back to both, so GET /v1/sites returned the variables of a function with the same $sequence, and the other way round (appwrite/appwrite#16395).

Fix

Set $collection on each found row before decode(), as the single-document paths already do. The value that ends up on the result is unchanged.

Tests

tests/unit/FilterCollectionTest.php records the collection a decode filter sees through getDocument() and find(), with and without a selection, on Memory and SQLite. It fails on main (the filter sees '' through find()) and passes with the fix. Locally, the other unit failures (EvictionProtocolTest, BaseAdapterStateTest, SwooleAbsentTest) happen with and without this change: they need Redis and Swoole, which the machine doesn't have.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Collection information is now available consistently when documents are read, including through decoding filters and queries that select specific fields. This improves the consistency of collection details across document retrieval and custom document types.

find() decoded each row before it set $collection, so a decode filter that
asked $document->getCollection() got an empty string on every list read but
the right id on getDocument(). Appwrite's subQueryVariables filter keys on it
to pick function or site variables, and fell back to both, so listing sites
returned the variables of a function with the same $sequence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@appwrite

appwrite Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Code review

Commit Status Tier Verdict Details
20dae22 Reviewed Reviewed 🟢 S · Ready to merge ✅ Approved View review

find() now assigns the collection ID to each non-empty row before decoding, allowing decode filters to inspect the correct collection. A new unit test covers collection visibility on reads using Memory and SQLite adapters.

Walkthrough (2)
File Change
src/Database/Trait/Documents.php Assigns the collection ID before decoding found rows.
tests/unit/FilterCollectionTest.php Adds Memory and SQLite coverage for collection visibility during decode and on find results.

Reviewed 20dae22

Tip

Comment @appwrite review to re-run, or mention @appwrite with a question.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

fetch now assigns collection metadata before decoding and custom document-type conversion. New tests check that decoding filters and query results see the collection across memory and in-memory SQLite adapters.

Changes: Collection metadata on reads

Layer / File(s) Summary
Assign and verify collection metadata
src/Database/Trait/Documents.php, tests/unit/FilterCollectionTest.php
fetch assigns the collection to non-empty nodes before decoding and custom document-type conversion. Tests check collection visibility in decoding filters, getDocument(), and find(), including selected-field results, across two adapters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: chiragagg5k, fogelito, appwrite

Merge Risk: Minimal · up to 20dae

This change makes decode filters see the correct collection during find(), which fixes variables from a function and a site with the same sequence being mixed together. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the collection is now set on found rows before they are decoded in find(). The raw summary confirms this for src/Database/Trait/Documents.php, where the collection ID …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the collection is now set on found rows before they are decoded in find(). The raw summary confirms this for src/Database/Trait/Documents.php, where the collection ID is assigned right after adapter casting and before decoding, and the objectives describe the same fix for decode filters that call getCollection(). The title is specific and follows the conventional-commit form, so it passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4c8d7de and 20dae22.

Files selected for processing: 2

  • src/Database/Trait/Documents.php
  • tests/unit/FilterCollectionTest.php

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

abnegate added a commit to appwrite/appwrite that referenced this pull request Dec 1, 2026
subQueryVariables chose function or site variables from
$document->getCollection(), and fell back to both when it was empty.
utopia-php/database decodes find() rows before it sets $collection, so on
every list read the filter returned the variables of whichever function and
site shared the document's $sequence. Dedicated tables give a project's first
function and first site the same $sequence, so GET /v1/sites showed the
function's variables, secrets included, and GET /v1/functions the site's.

An unknown collection now yields no variables. List responses carry an empty
vars until utopia-php/database sets $collection before decoding
(utopia-php/database#1099); getDocument() reads, which every build and
runtime path uses, are unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
abnegate added a commit to appwrite/appwrite that referenced this pull request Dec 1, 2026
With 10.3.6, find() decodes rows before setting $collection, so the
fail-closed variables filter left every site and function list response
without its own vars. Pin the fix branch (utopia-php/database#1099) until it
is released, and assert each list keeps exactly its own variables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@abnegate
abnegate merged commit c1e0003 into main Dec 1, 2026
31 checks passed
@abnegate
abnegate deleted the fix/find-collection-before-decode branch December 1, 2026 23:38
abnegate added a commit to appwrite/appwrite that referenced this pull request Dec 2, 2026
The Rust port has the same two defects as PHP: utopia-database find()
decoded rows before setting $collection, and appwrite-core's
subQueryVariables fell back to both function and site variables when the
collection was empty. Set the collection before decoding, as the PHP fix
(utopia-php/database#1099) does, which also keeps find() output key order
the same as PHP's for the parity suites, and return no variables for an
unknown collection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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