Skip to content

Commit 3aecfb8

Browse files
cursoragentjherr
andcommitted
fix(core): a relation into a collection the reader can't read populates as null, like a missing target
Co-authored-by: jherr <jherr@pobox.com>
1 parent c4c810f commit 3aecfb8

7 files changed

Lines changed: 10 additions & 8 deletions

File tree

‎.changeset/populate-sidecar.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
- `get`, `list`, `client.get/list`, the `get`/`list` operations and `get_document` return `relations` unless called with `populate: false`.
1414
- Populated relations skip trashed documents: a relation to one is `null` (dropped from a `many` array), like a missing document.
15+
- A relation into a collection the reader can't read is `null` (or `[]`) instead of being left out of `relations`, so hidden and missing targets look the same.

‎docs/api/cms-core-server.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ The canonical reads. One bag each, the same at every layer: the store (`cmsAs(cm
177177

178178
A `locator` is the document's slug or its id, with leading slashes ignored. A document that is missing, or that `ctx` can't see, is `null` from `get` and absent from `list`. Documents are redacted for `ctx`, and filtering or sorting on a field it can't read is `FORBIDDEN`. Both functions validate the bag (`getInput`, `listInput` from `@tanstack/cms-core`) and throw `BAD_REQUEST` for an invalid one.
179179

180-
Both verbs populate relations by default. The related documents come back beside the document, under `relations`, keyed by relation field path (`author`, `seo.image`, `blocks.2.author`); `doc` keeps the ids. Population is one level deep: a related document's own relation fields stay ids, so cycles and self-references can't recurse. A related document is read and redacted as `ctx` reads it. A missing, trashed or hidden target is `null`, or dropped from a `many` array, and a relation into a collection `ctx` can't read is left out. `list` batches the reads: one storage read per target collection for the whole page, not one per row. Pass `populate: false` for ids only, or an array of paths to populate just those (and the relations under them). `related(result, path)` from `@tanstack/cms-core` reads one relation back, typed from `Register`: a registered relation field gives the target's document type, or an array for `many: true`.
180+
Both verbs populate relations by default. The related documents come back beside the document, under `relations`, keyed by relation field path (`author`, `seo.image`, `blocks.2.author`); `doc` keeps the ids. Population is one level deep: a related document's own relation fields stay ids, so cycles and self-references can't recurse. A related document is read and redacted as `ctx` reads it. A missing, trashed or hidden target is `null`, or dropped from a `many` array, whether the document or its whole collection is hidden, so a reader can't tell hidden from missing. `list` batches the reads: one storage read per target collection for the whole page, not one per row. Pass `populate: false` for ids only, or an array of paths to populate just those (and the relations under them). `related(result, path)` from `@tanstack/cms-core` reads one relation back, typed from `Register`: a registered relation field gives the target's document type, or an array for `many: true`.
181181

182182
Full-text search stays its own verb, `queryDocumentsAs` with `query`, and `search_documents` for agents. The older read verbs below keep their behaviour, populating only when asked.
183183

@@ -390,7 +390,7 @@ function populateRelationsAs(ctx: AccessContext, collectionName: string, doc: Do
390390
function populateRelations(collectionName: string, doc: Doc): Promise<Record<string, Doc | Array<Doc> | null>> // deprecated: reads as publicContext()
391391
```
392392

393-
Resolves every `relation` field of `doc` to the target document(s) `ctx` can read, keyed by field name. A missing, trashed or hidden target is `null` (dropped from a `many` array). A `publicRead` context sees published targets the way it sees published sources. Fields whose target collection `ctx` can't read are left out.
393+
Resolves every `relation` field of `doc` to the target document(s) `ctx` can read, keyed by field name. A missing, trashed or hidden target is `null` (dropped from a `many` array), including one in a collection `ctx` can't read. A `publicRead` context sees published targets the way it sees published sources.
394394

395395
### `listDocumentsAs(ctx, collection?, options?)`
396396

‎docs/deployment/upgrading.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ older verbs still populate only when asked.
350350
- **Populated relations skip trashed targets.** A relation to a trashed
351351
document is `null` (dropped from a `many` array), like a missing one; it
352352
used to come back with its `deletedAt` stamp.
353+
- **A relation into a collection the reader can't read is `null`** (or
354+
`[]`) instead of being left out of `relations`.
353355
- **`populate` takes a list of paths** as well as a boolean, on every verb
354356
that accepts it.
355357

‎packages/core/src/server/store/read.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,8 @@ const RELATION_BATCH = 500
603603
* costs one storage read per target collection, not one per row and field.
604604
* Depth 1: related documents come back as stored, their own relation fields
605605
* left as ids, so cycles can't recurse. A target that is missing, trashed,
606-
* or not visible to `ctx` is `null` (dropped from a `many` array); a
607-
* relation into a collection `ctx` can't read is left out. Every related
606+
* or not visible to `ctx` (its collection included) is `null`, dropped from
607+
* a `many` array, so a reader can't tell hidden from missing. Every related
608608
* document is redacted for `ctx`.
609609
*
610610
* @internal
@@ -656,7 +656,6 @@ export async function resolveRelationsAs(
656656
const out: Relations = {}
657657
for (const slot of slots) {
658658
const visible = found.get(slot.target)
659-
if (!visible) continue
660659
const docs = slot.ids.flatMap((id) => {
661660
const doc = visible?.get(id)
662661
return doc ? [doc] : []

‎packages/core/tests/access-context.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ describe('derivation', () => {
333333
cms,
334334
accessContext({ session: as('viewer') }),
335335
).populateRelations('post', { title: 'x', author: 'm-live' })
336-
expect(ruled).toEqual({})
336+
expect(ruled).toEqual({ author: null })
337337

338338
const admin = cmsAs(cms, accessContext({ session: as('admin') }))
339339
const [live] = (

‎packages/core/tests/field-access.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ describe('nested and document-aware field access', () => {
552552
articleDoc,
553553
)
554554
// secretCol is not readable by viewer
555-
expect(viewerRels.secretRef).toBeUndefined()
555+
expect(viewerRels.secretRef).toBeNull()
556556
// nested relations in group, array, and blocks are populated and redacted
557557
expect(viewerRels['meta.editor']).toMatchObject({
558558
name: 'Alice',

‎packages/core/tests/read-verbs.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ describe('get', () => {
309309
const b = await viewer.get({ collection: 'node', id: 'b' })
310310
// Bob is a draft author; memos are for editors only.
311311
expect(b?.relations?.['meta.owner']).toBeNull()
312-
expect(b?.relations).not.toHaveProperty('memo')
312+
expect(b?.relations?.memo).toBeNull()
313313
expect(b?.relations?.links).toEqual([
314314
expect.objectContaining({ title: 'A' }),
315315
])

0 commit comments

Comments
 (0)