Skip to content

Support explicit depth-2 relations in the REST API via include - #32477

Open
BOHEUS wants to merge 1 commit into
twentyhq:mainfrom
BOHEUS:fix-rest-depth-2
Open

BOHEUS wants to merge 1 commit into
twentyhq:mainfrom
BOHEUS:fix-rest-depth-2

Conversation

@BOHEUS

@BOHEUS BOHEUS commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes twentyhq/core-team-issues#1674

Problem

REST relations stop at depth=1. The old depth-2 implementation pulled in every relation at both levels (75 relations for people on a seeded workspace), so it timed out and ignored field-level permissions. There was no way to ask for a specific relation two levels deep.

Changes

  • New include query param on the record endpoints, e.g. GET /rest/people?include=company.people,pointOfContactForOpportunities. Each path can be up to 2 relations deep and expands only what it names.
  • include combines with depth: with depth=1, every direct relation is expanded and the include paths add their nested relations. Included relations are returned even when they aren't listed in fields.
  • Permissions are checked at every level against unreadable fields and readable objects. Unknown and unreadable relations return the same 400, so the error doesn't reveal fields hidden by permissions.
  • Selection lives in CommonSelectFieldsBuilder.buildFromDepth (new includeTree option), next to requestedFields. The REST handlers only parse the param and pass it through.
  • The "is this relation readable" check is shared between depth and include (getReadableRelationTargetFlatObjectMetadata).
  • getIsFlatFieldAJoinColumn and getIsFlatFieldAJunctionRelationField now take Pick<FlatFieldMetadata, 'name' | 'settings'>. This fixes two type errors on main in buildRelationSelectionFromDepth, which passes a narrowed field type to them.
  • OpenAPI spec and developers/extend/api.mdx document include.

Performance

The existing runner guards still apply: a one-to-many nested in another one-to-many is rejected, and the query complexity limit is enforced. Unlike depth, an explicit include can expand timeline activities, since it's opt-in.

Testing

  • Unit tests for the parser and the include selection builder; the existing engine/api unit tests pass (1,007).
  • Integration tests: include in find-many (nested selection, combined with depth, unknown relation, too-deep path), and in the depth field-permissions suite (a restricted nested relation returns 400, an unrestricted one returns the nested records). Both suites pass locally.
  • Typecheck, oxlint and oxfmt are clean.

View guided diff

Add an `include` query param (JSON:API style, e.g.
`include=company.people,pointOfContactForOpportunities`) that selects
specific relations up to 2 levels deep, instead of fetching every
relation. It combines with `depth` and `fields`, is checked against object
and field permissions at every level, and rejects unknown or unreadable
relations with a 400.

Share the readable-relation check between depth and include.
Copilot AI balanced review requested due to automatic review settings December 5, 2026 13:31
@mintlify

mintlify Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 5f3f169.

Approve preview

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The change affects relation selection across REST record endpoints.

The existing rejection of included fields should be fixed before merging.

Findings

  1. P1 Included fields get rejected ▶

Summary

Adds explicit REST relation paths through include, with support for two relation levels.

  • REST callers can select named relation paths up to two levels deep.

Diagram

%%{init: {'theme': 'neutral', 'htmlLabels': false}}%%
flowchart LR
  A[REST request] --> B[Parse depth, fields, and include]
  B --> C[Load readable objects and fields]
  C --> D[Build field and relation selections]
  D --> E[Check nested relations and query complexity]
  E --> F[Read or write records]
  F --> G[Load selected related records]
  G --> H[REST response]
Loading

Reviews (2) · Last reviewed commit: "Support explicit depth-2 relations in th..." · Reviewed by Greptile

Comment on lines +178 to +179
...buildRelationSelectionFromInclude({
includeTree,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Included fields get rejected

GET /rest/companies?fields=id,people&include=people returns 400 instead of expanding people. REST defaults to depth=0, so validateRequestedFields rejects this one-to-many field before the new include selection runs. The same request succeeds when people is omitted from fields.

Let requested-field checks recognize relations expanded by includeTree, without requiring depth=1 and loading unrelated relations.

@twenty-ci-bot-public

Copy link
Copy Markdown

🤖 PR Review

Check Result
🔍 Build safety ⏭️ skipped — external-only
🧭 Triage ⏭️ skipped — external-only
📐 Quality ✅ passed — 5 nit(s)
🛡️ Security ✅ passed
🚦 Auto-approve 👀 needs review — Complexity is high
🔐 Permissions ⏭️ skipped: no endpoint permission change

🛡️ Security Review

✅ No high-severity vulnerabilities detected.


🚦 Auto-approve

🙋 Manual review recommended for the following reason(s):

  • Complexity is high

  • 🧠 Complexity: high

  • 📏 Size: +714 / -46 lines across 28 file(s)


View details

Automated pre-review — human approval still required.

@twenty-ci-bot-public

Copy link
Copy Markdown

🟡 Quality review · 5 findings

Safe to merge after one non-blocking fix: the new include path should apply the same timeline-activity exclusion that depth uses.

High-level — The additive REST include parameter, which reuses the shared readable-relation helper, fits the existing select-fields seam and needs no migration or rollout ordering, but its include builder misses depth's guard against expanding timelineActivity.
Low-level — 4 rule violations found — 1572 rule questions over 30 piece(s) of 27 file(s)

💬 5 inline comments on the diff.


Reviewed against the pr-review standard — high-level then low-level. Advisory; human review still required. Run details.


// Unknown and unreadable relations share one error so the response does
// not reveal fields hidden by permissions
if (!isDefined(relationTargetObjectMetadata)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Nit · High-level · engineering architecture — production cardinality / extend existing seams

The include builder is a second relation-selection path that skips isRelationTargetExcludedFromSelection, so include=timelineActivities (or company.timelineActivities) expands the timeline relation that depth deliberately leaves out

buildRelationSelectionFromDepth skips timelineActivity because expanding it for every record turns one page into thousands of rows. ?include=company.timelineActivities on a 200-record find-many reopens that load path. Put the exclusion check in the shared getReadableRelationTargetFlatObjectMetadata, or call it in the include builder too, so the two paths can't drift apart again.


const ALL_OBJECT_IDS = [PERSON_ID, COMPANY_ID, NOTE_ID];

const compute = (

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Nit · Low-level · More than one argument means one object argument

The compute helper takes two positional arguments (includeTree and an options object) instead of a single object argument, so call sites cannot read as named parameters.

})
: undefined;

// Unknown and unreadable relations share one error so the response does

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Nit · Low-level · A kept comment is one line

The added explanatory comment spans two // lines, but the reason (shared error so permissions aren't revealed) can be stated in one line.

Also on line 50.

// Unknown and unreadable relations share one error so the response does
// not reveal fields hidden by permissions
if (!isDefined(relationTargetObjectMetadata)) {
throw new CommonSelectFieldsException(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Nit · Low-level · Exceptions take a single object parameter

The CommonSelectFieldsException is constructed with a bare template string instead of a single object parameter carrying the message.

Also on lines 53–54.


const RELATION_FIELD_NAME_PATTERN = /^[a-zA-Z][a-zA-Z0-9]*$/;

const addRelationPathToIncludeTree = (

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Nit · Low-level · More than one argument means one object argument

addRelationPathToIncludeTree takes two positional parameters (the tree and the path array) instead of a single object parameter with named fields.

Also on lines 15–17.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement relations depth 2 for rest api

2 participants