diff --git a/AGENTS.md b/AGENTS.md index b4e59695a8..eec83c4b17 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -71,7 +71,7 @@ PyPI has had no product change since `0.25.0` — nothing is broken. - **Kotlin** — `codegen-kotlin` (KotlinPoet on JVM): entity + Exposed table + Spring controller + payload + relations + filter allowlist + validator + stored-proc + output-parser generators. `integration-tests-kotlin` runs the persistence-conformance corpus through Exposed against Testcontainers Postgres. **Cross-port conformance corpora** (every port runs the shared corpus): -- Metamodel: `fixtures/conformance/` (490 fixtures; 38 shared corpora in total — per-corpus counts + the corpus x port matrix live in `docs/CONFORMANCE.md`). TS / C# / Java / Python all green. +- Metamodel: `fixtures/conformance/` (510 fixtures; 38 shared corpora in total — per-corpus counts + the corpus x port matrix live in `docs/CONFORMANCE.md`). TS / C# / Java / Python all green. - Render: `fixtures/render-conformance/`. TS / C# / Java / Kotlin / Python byte-identical. - Persistence: `fixtures/persistence-conformance/`. **Query** scenarios run on every port (TS / C# / Java / Kotlin / Python), each provisioning its test DB by executing the committed, TS-produced `canonical/schema.postgres.sql` (Postgres only — Derby dropped for the cross-port query corpus, ADR-0015; the FR-044 `report-*` scenarios also run on SQLite, MySQL and D1 where a port has the engine, see [docs/CONFORMANCE.md](docs/CONFORMANCE.md)). The **migration** scenarios are exercised by **TS only** (TS owns schema migrations). **The corpus now gates WRITES, not just reads (SP-H):** an `op: roundtrip` scenario type INSERTs through each port's runtime/ORM write codec (NOT raw SQL), reads the row back, and asserts the wire-normalized value. The `AllTypes` entity (`roundtrip-all-types.yaml`) carries one field of **every** persistable `field.*` subtype — string/int/long/double/float/decimal/boolean/date/time/timestamp(+tz)/currency/enum/uuid/object — plus an **array-of-VO** `field.object @isArray @storage:jsonb` column (`labels`, written as 2-element / empty-`[]` / single-element arrays across the three rows) — so every subtype write+read (incl. the array-of-value-object jsonb codec) round-trips through every port against Testcontainers PG. (`field.byte`/`field.short`/`field.class` were cut as non-functional registration-only stubs — the matrix tracks only genuinely-supported subtypes; see `fixtures/registry-conformance/README.md` → "Per-subtype write-round-trip matrix".) - API-contract: `fixtures/api-contract-conformance/`. TS / C# / Java / Kotlin / Python all green — each port runs **two lanes**: a hand-rolled reference server AND its **generated** API artifact booted over HTTP (the deployed controller/routes; TS+C# full-stack vs Testcontainers PG, Java/Kotlin/Python generated controller + in-memory repo behind the consumer seam). The generated fan-out found 10 real deployment bugs golden snapshots missed. Three sub-corpora run the **generated lane only, on all five ports** — `write-through/`, `projection/` (F22: a view-only `object.projection` serves GET list + GET by id and answers every write verb with `405 {"error": "method_not_allowed"}`) and `report/` (FR-044: a view-backed `object.report` serves GET list, answers `POST` with that same `405`, and mounts no `/{id}` route). That is deliberate, not a gap: what is under test is whether a port's GENERATOR emits those routes, and a hand-rolled reference server would answer every scenario by construction. The `m2m/` sub-corpus also gates **TPH x M:N together** (base-declared, subtype-declared, abstract-mid-declared, a non-subtype source onto a subtype TARGET, and the cross-subtype source id answering `200 []`) — the two corpora were originally built disjoint (`tph/` had no relationships, `m2m/` no discriminators), which is precisely how that defect class survived. diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aa9f96685..49bde5fe32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,8 +63,9 @@ _npm `1.3.0` (full lockstep across all 14 `@metaobjectsdev/*` publish candidates **`metamodelVersion` moves to `1.3`.** This release adds vocabulary (`field.duration`, `validator.exactlyOne`, `@constraintName`, `@condition`, `@maxLength` on `field.enum`, `@sequenceStart`, `@sequenceName`, `@flattenedColumnPrefix`, the `pascal` column rule, -`@collectionName`, and the `case` and `now` expression nodes) and nothing that loaded before -stops loading. The registry gains two +`@collectionName`, the `case` and `now` expression nodes, `@segment` on `origin.first` / +`origin.aggregate`, and the `isDistinctFrom` / `isNotDistinctFrom` filter ops) and nothing that +loaded before stops loading. The registry gains two fields on a type or attr entry, carried into the registry manifest: `implementedIn` (cross-port vocabulary only the named ports implement so far, e.g. the `view.*` controls, `["typescript"]`) and `ports` (vocabulary only the named ports ever act on, e.g. `@objectAdapter`, `["java", "kotlin"]`: @@ -147,9 +148,13 @@ Each item below is described in full under Added, Changed or Fixed; this list is `[csharp-only]`) and its JSON carries `ports`. `@objectAdapter` is the one there is. - **`@collectionName`, and the `naming.collections` pluralizer.** An `object.entity`, `object.projection` or `object.report` may name its collection in its own casing - (`"@collectionName": "CaseHistory"`); it need not be a grammatical plural. The generated C# - context uses it as the DbSet property name in place of the derived plural, and the routes, the - reverse finders and the api reference read the property through it. Absent, the project's + (`"@collectionName": "CaseHistory"`); it need not be a grammatical plural. In C# and TypeScript it + names the REST collection segment, snake_cased with an acronym-aware split (`FAQs` serves + `/faqs`, shared by `fixtures/naming-conformance/collection-segments.json`), and the reverse + finders; in C# also the DbSet property (in place of the derived plural, which the api reference + reads through it), and in TypeScript the list function, the list hook and the Drizzle table + variable. Two objects whose collection names meet are refused by name. A project that declares + neither the attr nor `naming.collections` keeps every generated name and URL. Absent, the project's pluralizer derives the name: `"naming": { "collections": "standard" }` (the default) or `"none"` in `.metaobjects/config.json`, or one the application registers (`registerPluralizer` in TypeScript, `NamingStrategies.RegisterPluralizer` in C#, `NamingStrategies.registerPluralizer` in @@ -173,9 +178,16 @@ Each item below is described in full under Added, Changed or Fixed; this list is `operand`), and a ref in it may name another of the projection's own fields (a passthrough, an `origin.first` or another `origin.computed`, never itself or an aggregate) before a base field (`origin-computed-case-over-joined`, `error-origin-computed-case-mixed-types`). A - simple `case` compares a literal `when` with any `operand`, so an enum or long operand maps - its codes to labels. A `case`'s results and a `coalesce`'s arguments unify a numeric literal - with any numeric field and a `now` with a date or timestamp (`origin-computed-literal-unifies`); + simple `case` compares a literal `when` with its `operand`, so an enum or long operand maps + its codes to labels. A literal compared with a field, as a `when` against a field operand or + on either side of a comparison, must be of that field's kind and, for a `field.enum`, one of + its `@values`, the rule a validator `@condition` already follows (`ERR_INVALID_ORIGIN`, + `error-origin-computed-literal-not-enum-member`, `error-origin-computed-literal-wrong-kind`). + A `case`'s results and a `coalesce`'s arguments unify a whole-number + literal with any numeric field, a fractional literal only with a `field.double`, `field.float` + or `field.decimal` (refused beside a long, int or currency field, + `error-origin-computed-literal-fractional`), and a `now` with a date or timestamp + (`origin-computed-literal-unifies`); - the loaders refuse what a view cannot lower: a `now` whose value is not an ISO-8601 duration or whose `startOf` is not day, week or month (`error-origin-computed-now-bad-duration`); a searched `case` whose `when` is not boolean (`error-origin-computed-case-non-boolean-when`); @@ -189,6 +201,40 @@ Each item below is described in full under Added, Changed or Fixed; this list is `error-origin-computed-node-mixed-keys`); - a validator `@condition` still refuses `case` and `now` (`ERR_BAD_ATTR_VALUE`, `error-validator-condition-now`): it becomes a database CHECK, which can hold neither. +- **Segments and correlated filters on origins.** An `origin.first` / `origin.aggregate` may scope + its related rows by a `@segment` declared on that entity (`ERR_INVALID_ORIGIN` otherwise), and + its `@filter` may compare a related column with a base-row field (`{ "eq": { "field": + "statusId" } }`, or the null-safe `isDistinctFrom` / `isNotDistinctFrom`, which take only such an + operand; `ERR_BAD_ATTR_FILTER` for an unknown base field or another op). Every port's loader + validates them (`origin-segment-scoped`, `origin-correlated-filter-and-segment`, + `error-origin-segment-unresolved`, `error-origin-correlated-operand-unknown`). +- **Filter paths, flattened members and inherited filters in projections.** A projection's + `@filter` key may name a field of its base entity the view does not output, or a path of to-one + hops from the base. A projection that extends another is checked against the effective (ANDed) + filter of its chain. An origin's `@from` / `@of` may name a member of a flattened value object. + Every port's loader validates them, gated by eleven new shared fixtures. A report dimension's + required flag now treats an unenforced reference, or a hop onto a TPH subtype, as possibly + missing. +- **A view the lowering cannot express is refused, not shortened.** The TypeScript view extractor + skipped any origin it could not lower, so a projection loaded clean and `meta migrate` created a + view missing a column its generated type declares. Each such shape now fails, naming the + projection and the field: an origin `@segment`, a base-row operand in an origin `@filter`, a + computed node the lowering does not support, and an unknown origin or aggregate. A projection + that extends another projection now fails too, naming the projection: the lowering reads only + the fields and `@filter` declared on the projection itself, so the view it created dropped the + inherited columns and the inherited row gate. +- **A reference over a flattened member reads the member's column.** A reference whose `@fields` + names a member of a flattened value object (`"address.countryId"`) now resolves to that member's + real column in the foreign key `meta migrate` emits, a view's join, a passthrough `@from` onto + the member, and the Drizzle key the reverse finder and `relations()` block read. Two references + onto one target now each read the join their own `@via` walked, rather than the first join to + that entity. +- **One wording for a to-many passthrough, in every port.** The refusal of a passthrough across a + reverse `@via` hop names the hop as the path wrote it and the reference it walks + (`@via hop "consents" is to-many (reverse hop over identity.reference "patient")`), and Java's and + Python's wording of the refusal now matches the other ports'. The fixtures + `error-origin-passthrough-reverse-hop` (new) and `error-origin-passthrough-to-many` pin the + message. - **An int-backed enum can be an array.** `@intValueMap` together with `isArray: true` (the map on the field, or inherited from a shared enum) now loads in every port instead of diff --git a/docs/CONFORMANCE.md b/docs/CONFORMANCE.md index 5bfc57f808..010d55867f 100644 --- a/docs/CONFORMANCE.md +++ b/docs/CONFORMANCE.md @@ -26,7 +26,7 @@ regenerate with `ls -d fixtures//*/ | wc -l` for directory-shaped corpor | Corpus | Fixtures | TS | Java | Kotlin | C# | Python | |---|---|---|---|---|---|---| -| [`fixtures/conformance/`](../fixtures/conformance/) (metamodel) | 490 | ✓ | ✓ | inherits via `metadata-ktx` | ✓ | ✓ | +| [`fixtures/conformance/`](../fixtures/conformance/) (metamodel) | 510 | ✓ | ✓ | inherits via `metadata-ktx` | ✓ | ✓ | | [`fixtures/yaml-conformance/`](../fixtures/yaml-conformance/) | 16 | 16 / 16 | 15 / 16 (1 ledgered: `yaml-quoted-leading-zero` — Java pipeline strips quotes off `"007"`) | inherits via Java | 15 / 16 (1 ledgered: `error-yaml-coerced-hex-in-string` — YamlDotNet doesn't coerce `0xFF`) | 16 / 16 | | [`fixtures/verify-conformance/`](../fixtures/verify-conformance/) | 45 | ✓ | ✓ | inherits via Java | ✓ | ✓ | | [`fixtures/verify-strict-conformance/`](../fixtures/verify-strict-conformance/) | 1 | ✓ | — | — | — | ✓ | @@ -148,9 +148,12 @@ canonical (`fixtures/registry-conformance/expected-registry.json`). - `view.image`, and its `@accept`, `@aspectRatio`, `@maxBytes`, `@maxEdge`, `@store` - *Implemented in Java only* (`implementedIn: ["java"]`): `object.* @object` (ADR-0001), the native class an object binds to. Java's runtime acts on it; Java's own behaviour is unchanged. -- *Implemented in C# only* (`implementedIn: ["csharp"]`): `object.* @collectionName`, the - collection name the generated DbSet takes. Every port registers it and reads the `naming.collections` - key that names the default pluralizer; C# codegen does not apply that pluralizer yet. +- *Implemented in C# and TypeScript* (`implementedIn: ["csharp", "typescript"]`): `object.* + @collectionName`, an object's collection name: the REST collection segment and reverse-finder + names in both, the DbSet in C#, the list function, list hook and Drizzle table variable in + TypeScript. Both apply the `naming.collections` pluralizer to an object that declares none + (`fixtures/naming-conformance/collection-segments.json`); every port registers and type-checks + the attr and the key. - *Loads strictly in every port.* Each port registers the `metaobjects-ui-web` provider (`view.textarea @rows`, the `view.image` attrs) and `@object` / `@objectAdapter` on `object.*`, loads all three fixtures strictly, and emits `implementedIn` in its manifest, which @@ -354,7 +357,7 @@ unit-test runners (`bun test`, `dotnet test`, `pytest`, `mvn test`) pull Docker. ## Fixture-to-doc mapping -### `fixtures/conformance/` — metamodel loader + canonical serializer (490) +### `fixtures/conformance/` — metamodel loader + canonical serializer (510) | Fixture prefix | Feature doc | |---|---| @@ -716,7 +719,7 @@ JSON and verify payload feeds it unchanged. ## Orphaned fixtures (tested but not yet documented) -The fixtures in the nine corpora mapped above (metamodel 490 + yaml 16 + verify 45 +The fixtures in the nine corpora mapped above (metamodel 510 + yaml 16 + verify 45 + render 18 + persistence 62 + api-contract 123 + source-resolution 25 + scope 10 + dependency 47) each map to a feature doc, and so do the two requirement corpora, `shared-model-conformance`, the `cube-model` corpus and `explorer-conformance`, whose case lists live in their own READMEs. None are orphaned today. The remaining diff --git a/docs/features/migrations/physical-naming.md b/docs/features/migrations/physical-naming.md index 82606abf68..888a3b69fb 100644 --- a/docs/features/migrations/physical-naming.md +++ b/docs/features/migrations/physical-naming.md @@ -27,7 +27,7 @@ every runtime `ObjectManager` and `verify` read it, so they always produce the s |---|---|---|---| | `columns` | `snake_case`, `literal`, `kebab-case`, `pascal`, or a name your application registered | a field with no `@column` | `snake_case` | | `tables` | `snake_case`, `literal`, or a name your application registered | a source with no `@table`/`@view`/`@proc`/`@function`: its `name`, else the object's name made plural | `snake_case` | -| `collections` | `standard`, `none`, or a name your application registered | an object with no `@collectionName`: the collection name the generated C# DbSet takes and a backwards `@via` hop is named by | `standard` | +| `collections` | `standard`, `none`, or a name your application registered | an object with no `@collectionName`: its collection name (see below) and the name a backwards `@via` hop is named by | `standard` | An unknown key fails when the file is read, and so does a name no registered strategy answers. Index and constraint names are unchanged. @@ -73,6 +73,27 @@ the port's reset when it is done (`resetNamingStrategiesForTesting()`, `NamingStrategies.ResetForTesting()`, `NamingStrategies.resetForTesting()`, `reset_naming_strategies_for_testing()`), which leaves only the built-ins. +### Collection names + +An object's collection name is its own `@collectionName`, else the `collections` pluralizer over +its name. C# and TypeScript generators read it (the other ports load and type-check both, and +act on neither yet, #549): + +| Artifact | Port | `Faq` with `@collectionName: "FAQs"` | `Order` under `collections: "none"` | +|---|---|---|---| +| REST collection segment | C#, TypeScript | `/faqs` | `/order` | +| Reverse finder | C#, TypeScript | `FindFAQsByOwner` / `findFAQsByOwner` | `FindOrderByFaq` / `findOrderByFaq` | +| DbSet property | C# | `FAQs` | `Order` | +| List function, list hook, Drizzle table variable | TypeScript | `listFAQs`, `useFAQs`, `fAQs` | `listOrder`, `useOrderList`, `order` | + +The REST segment is the collection name snake_cased, with a trailing plural `s` after an acronym +kept on it (`FAQs` serves `/faqs`, `TaxIDs` serves `/tax_ids`). With neither `@collectionName` nor +a `collections` setting, every name is what it has always been: the segment is the snake_cased +name made plural, as every port serves it. A list hook whose name would equal the detail hook's +takes a `List` suffix. The cases both ports run are in +`fixtures/naming-conformance/collection-segments.json`. Changing either one changes URLs and +generated names an application calls, so treat it as an API change. + A name nothing registered fails when the file is read, naming the setting, the name, the port, and what is registered: diff --git a/docs/features/source-kinds.md b/docs/features/source-kinds.md index 2b2377b09b..be42e4381b 100644 --- a/docs/features/source-kinds.md +++ b/docs/features/source-kinds.md @@ -96,7 +96,10 @@ emitter, two hosts. A derived field must be **providable** — a read-capable source must be able to carry it. A derived field on a table-only entity (no read source) is a load error -(`ERR_DERIVED_FIELD_NO_READ_SOURCE`). +(`ERR_DERIVED_FIELD_NO_READ_SOURCE`), with one exception: an `origin.first` or `origin.computed` +that a projection reaches through its `@filter` or a passthrough `@from` is inlined there, so it +needs no read source. The entity's table then gets no column for it: generated entity, DbContext, +Kotlin, Python `ObjectManager` and Java OMDB mappings leave such a field out of table reads and writes. ### Entity read-view vs projection — which to reach for @@ -204,6 +207,14 @@ never itself or an aggregate), else a base field. An `origin.first` or `origin.a `@filter`, like a projection's, may hold a relative date (`{ "now": "-P7D" }`, see [reporting.md](reporting.md#relative-dates-in-filters)). Every port's loader validates these. +**Segments and correlated filters.** An `origin.first` or `origin.aggregate` may scope its +related rows by a `@segment` declared on that entity (ANDed with its `@filter`), and its +`@filter` may compare a related column with a base-row field +(`{ "statusId": { "eq": { "field": "statusId" } } }`); the null-safe `isDistinctFrom` / +`isNotDistinctFrom` take only such an operand. Every port's loader validates them +(`ERR_INVALID_ORIGIN` for a `@segment` the entity does not declare, `ERR_BAD_ATTR_FILTER` for +an unknown base field or another op). + **Projection row-scope `@filter` (#207).** Distinct from the *per-aggregate* `@filter` above, an **object-level `@filter` on an `object.projection`** scopes the whole view's rows — it lowers to the view's outer `WHERE`. This is the metadata-managed way to express a diff --git a/examples/advanced-modeling/codegen/templates/queries-file.ts b/examples/advanced-modeling/codegen/templates/queries-file.ts index d7362d6181..22264e2430 100644 --- a/examples/advanced-modeling/codegen/templates/queries-file.ts +++ b/examples/advanced-modeling/codegen/templates/queries-file.ts @@ -22,7 +22,7 @@ import { schemaInputType, } from "./queries.js"; import { getPkInfo, getPkFields } from "@metaobjectsdev/codegen-ts/engine"; -import { pluralize, findByIdFnName, listFnName, createFnName, insertPreservingFnName, updateFnName, singleRowVarName } from "@metaobjectsdev/codegen-ts/engine"; +import { findByIdFnName, listFnName, createFnName, insertPreservingFnName, updateFnName, singleRowVarName } from "@metaobjectsdev/codegen-ts/engine"; import { GENERATED_HEADER, GENERATED_EDIT_NOTE, sidecarLine } from "@metaobjectsdev/codegen-ts/engine"; import { isTphDiscriminatorBase, tphConcreteSubtypes } from "./tph-discriminator.js"; import { isProjection, isWriteThrough } from "@metaobjectsdev/codegen-ts/engine"; @@ -186,7 +186,7 @@ import { ${viewVar}, type ${entityName} } from ${JSON.stringify(entityFileName)} } const reads = code` -${findById}export async function ${listFnName(entityName)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { +${findById}export async function ${listFnName(obj)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { let q = db.select().from(${viewVar}).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); @@ -346,7 +346,7 @@ export async function find${baseName}ById(db: Db, ${pkField}: ${pkType}): Promis return row ? ${parseSym}(row) : null; } -export async function list${pluralize(baseName)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${baseTypeSym}[]> { +export async function ${listFnName(base)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${baseTypeSym}[]> { let q = db.select().from(${tableSym}).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); @@ -377,7 +377,7 @@ export async function list${pluralize(baseName)}(db: Db, opts?: { limit?: number const subPartialSym = hasInsertRefinements(sub) ? imp(`${sub.name}InsertBaseSchema@${subFileSpec}`) : subInsertSym; subtypeSections.push(code` -export async function list${pluralize(sub.name)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${subTypeSym}[]> { +export async function ${listFnName(sub)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${subTypeSym}[]> { let q = db.select().from(${tableSym}).where(${eqSym}(${tableSym}.${discField}, ${valueLit})).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); diff --git a/examples/advanced-modeling/codegen/templates/queries.ts b/examples/advanced-modeling/codegen/templates/queries.ts index 95f427fd6e..7dda46bc5a 100644 --- a/examples/advanced-modeling/codegen/templates/queries.ts +++ b/examples/advanced-modeling/codegen/templates/queries.ts @@ -12,6 +12,7 @@ import { import type { RenderContext } from "@metaobjectsdev/codegen-ts/engine"; import { getPkInfo, pkInfoOf, subTypeToTsType } from "@metaobjectsdev/codegen-ts/engine"; import { supportsReturning } from "@metaobjectsdev/codegen-ts/engine"; +import { flatKeyOfPath } from "@metaobjectsdev/codegen-ts/engine"; import { findByIdFnName, listFnName, @@ -49,7 +50,7 @@ export function renderListFn(entity: MetaObject, ctx: RenderContext, readVar?: s const entityName = entity.name; // Pluralize the PascalCase entity name, preserving capitalization // (e.g., "Category" -> "Categories", not "Categorys"). - const fnName = listFnName(entityName); + const fnName = listFnName(entity); return code` export async function ${fnName}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { @@ -231,9 +232,11 @@ interface ReverseFk { export function reverseFksFor(entity: MetaObject): ReverseFk[] { const out: ReverseFk[] = []; for (const ref of entity.referenceIdentities()) { - const fkField = ref.fields[0]; + const fkPath = ref.fields[0]; const targetRef = ref.targetEntity; - if (!fkField || !targetRef) continue; + if (!fkPath || !targetRef) continue; + // A reference over a flattened value-object member names its flat key on the table. + const fkField = flatKeyOfPath(entity, fkPath); const bound = refTarget(ref, IDENTITY_REFERENCE_ATTR_REFERENCES); const target = isMetaObject(bound) ? bound : undefined; out.push({ fkField, targetEntity: target?.name ?? stripPackage(targetRef), target }); @@ -255,8 +258,8 @@ export function renderReverseFinderFns(entity: MetaObject, fk: ReverseFk, ctx: R const ownField = entity.findField(fk.fkField); const valueType = subTypeToTsType(targetPk?.fieldSubType ?? ownField?.subType ?? "long"); - const singleName = reverseFinderFnName(entityName, fk.fkField); - const batchName = reverseFinderInFnName(entityName, fk.fkField); + const singleName = reverseFinderFnName(entity, fk.fkField); + const batchName = reverseFinderInFnName(entity, fk.fkField); const eqSym = imp("eq@drizzle-orm"); const inArraySym = imp("inArray@drizzle-orm"); diff --git a/examples/showcase/codegen/templates/queries-file.ts b/examples/showcase/codegen/templates/queries-file.ts index d7362d6181..22264e2430 100644 --- a/examples/showcase/codegen/templates/queries-file.ts +++ b/examples/showcase/codegen/templates/queries-file.ts @@ -22,7 +22,7 @@ import { schemaInputType, } from "./queries.js"; import { getPkInfo, getPkFields } from "@metaobjectsdev/codegen-ts/engine"; -import { pluralize, findByIdFnName, listFnName, createFnName, insertPreservingFnName, updateFnName, singleRowVarName } from "@metaobjectsdev/codegen-ts/engine"; +import { findByIdFnName, listFnName, createFnName, insertPreservingFnName, updateFnName, singleRowVarName } from "@metaobjectsdev/codegen-ts/engine"; import { GENERATED_HEADER, GENERATED_EDIT_NOTE, sidecarLine } from "@metaobjectsdev/codegen-ts/engine"; import { isTphDiscriminatorBase, tphConcreteSubtypes } from "./tph-discriminator.js"; import { isProjection, isWriteThrough } from "@metaobjectsdev/codegen-ts/engine"; @@ -186,7 +186,7 @@ import { ${viewVar}, type ${entityName} } from ${JSON.stringify(entityFileName)} } const reads = code` -${findById}export async function ${listFnName(entityName)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { +${findById}export async function ${listFnName(obj)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { let q = db.select().from(${viewVar}).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); @@ -346,7 +346,7 @@ export async function find${baseName}ById(db: Db, ${pkField}: ${pkType}): Promis return row ? ${parseSym}(row) : null; } -export async function list${pluralize(baseName)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${baseTypeSym}[]> { +export async function ${listFnName(base)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${baseTypeSym}[]> { let q = db.select().from(${tableSym}).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); @@ -377,7 +377,7 @@ export async function list${pluralize(baseName)}(db: Db, opts?: { limit?: number const subPartialSym = hasInsertRefinements(sub) ? imp(`${sub.name}InsertBaseSchema@${subFileSpec}`) : subInsertSym; subtypeSections.push(code` -export async function list${pluralize(sub.name)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${subTypeSym}[]> { +export async function ${listFnName(sub)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${subTypeSym}[]> { let q = db.select().from(${tableSym}).where(${eqSym}(${tableSym}.${discField}, ${valueLit})).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); diff --git a/examples/showcase/codegen/templates/queries.ts b/examples/showcase/codegen/templates/queries.ts index 95f427fd6e..7dda46bc5a 100644 --- a/examples/showcase/codegen/templates/queries.ts +++ b/examples/showcase/codegen/templates/queries.ts @@ -12,6 +12,7 @@ import { import type { RenderContext } from "@metaobjectsdev/codegen-ts/engine"; import { getPkInfo, pkInfoOf, subTypeToTsType } from "@metaobjectsdev/codegen-ts/engine"; import { supportsReturning } from "@metaobjectsdev/codegen-ts/engine"; +import { flatKeyOfPath } from "@metaobjectsdev/codegen-ts/engine"; import { findByIdFnName, listFnName, @@ -49,7 +50,7 @@ export function renderListFn(entity: MetaObject, ctx: RenderContext, readVar?: s const entityName = entity.name; // Pluralize the PascalCase entity name, preserving capitalization // (e.g., "Category" -> "Categories", not "Categorys"). - const fnName = listFnName(entityName); + const fnName = listFnName(entity); return code` export async function ${fnName}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { @@ -231,9 +232,11 @@ interface ReverseFk { export function reverseFksFor(entity: MetaObject): ReverseFk[] { const out: ReverseFk[] = []; for (const ref of entity.referenceIdentities()) { - const fkField = ref.fields[0]; + const fkPath = ref.fields[0]; const targetRef = ref.targetEntity; - if (!fkField || !targetRef) continue; + if (!fkPath || !targetRef) continue; + // A reference over a flattened value-object member names its flat key on the table. + const fkField = flatKeyOfPath(entity, fkPath); const bound = refTarget(ref, IDENTITY_REFERENCE_ATTR_REFERENCES); const target = isMetaObject(bound) ? bound : undefined; out.push({ fkField, targetEntity: target?.name ?? stripPackage(targetRef), target }); @@ -255,8 +258,8 @@ export function renderReverseFinderFns(entity: MetaObject, fk: ReverseFk, ctx: R const ownField = entity.findField(fk.fkField); const valueType = subTypeToTsType(targetPk?.fieldSubType ?? ownField?.subType ?? "long"); - const singleName = reverseFinderFnName(entityName, fk.fkField); - const batchName = reverseFinderInFnName(entityName, fk.fkField); + const singleName = reverseFinderFnName(entity, fk.fkField); + const batchName = reverseFinderInFnName(entity, fk.fkField); const eqSym = imp("eq@drizzle-orm"); const inArraySym = imp("inArray@drizzle-orm"); diff --git a/examples/showcase/site-payload.json b/examples/showcase/site-payload.json index c0da6ed6d5..c716780b2b 100644 --- a/examples/showcase/site-payload.json +++ b/examples/showcase/site-payload.json @@ -7,7 +7,7 @@ "metamodel": "1.3" }, "counts": { - "fixtures": 490, + "fixtures": 510, "corpora": 38, "baseTypes": 17 }, diff --git a/fixtures/conformance/entity-derived-filter-gate/expected.json b/fixtures/conformance/entity-derived-filter-gate/expected.json new file mode 100644 index 0000000000..11fd957b9e --- /dev/null +++ b/fixtures/conformance/entity-derived-filter-gate/expected.json @@ -0,0 +1,328 @@ +{ + "metadata.root": { + "package": "acme", + "children": [ + { + "object.entity": { + "name": "Member", + "children": [ + { + "source.rdb": { + "@table": "members" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.boolean": { + "name": "hasActiveAgreement", + "children": [ + { + "origin.first": { + "@of": "acme::Agreement.agreementIsOnFile", + "@orderBy": [ + "id:desc" + ], + "@segment": "agreementDecisive", + "@via": "acme::Member.agreements" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.entity": { + "name": "Agreement", + "children": [ + { + "source.rdb": { + "@table": "agreements" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "memberId", + "@required": true + } + }, + { + "field.int": { + "name": "agreementTypeId" + } + }, + { + "field.boolean": { + "name": "agreementIsOnFile" + } + }, + { + "field.boolean": { + "name": "memberAgreement" + } + }, + { + "field.date": { + "name": "dateReceived" + } + }, + { + "field.date": { + "name": "effectiveEndDate" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "memberId", + "@fields": [ + "memberId" + ], + "@references": "acme::Member" + } + }, + { + "segment.filter": { + "name": "agreementDecisive", + "@filter": { + "agreementTypeId": { + "eq": 1 + }, + "or": [ + { + "agreementIsOnFile": { + "eq": false + } + }, + { + "and": [ + { + "agreementIsOnFile": { + "eq": true + } + }, + { + "or": [ + { + "memberAgreement": { + "isNull": true + } + }, + { + "memberAgreement": { + "eq": true + } + } + ] + }, + { + "dateReceived": { + "lte": { + "now": "P0D" + } + } + }, + { + "or": [ + { + "effectiveEndDate": { + "isNull": true + } + }, + { + "effectiveEndDate": { + "gte": { + "now": "P0D" + } + } + } + ] + } + ] + } + ] + } + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "memberId", + "@required": true + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "memberId", + "@fields": [ + "memberId" + ], + "@references": "acme::Member" + } + } + ] + } + }, + { + "object.entity": { + "name": "Authorization", + "children": [ + { + "source.rdb": { + "@table": "authorizations" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.string": { + "name": "code" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": [ + "caseId" + ], + "@references": "acme::Case" + } + } + ] + } + }, + { + "object.projection": { + "name": "AuthorizationExtract", + "@filter": { + "caseId.memberId.hasActiveAgreement": { + "eq": true + } + }, + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_authorization_extract" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::Authorization.id" + } + }, + { + "field.string": { + "name": "code", + "extends": "acme::Authorization.code" + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::Authorization.id" + } + } + ] + } + }, + { + "object.projection": { + "name": "MemberExtract", + "@filter": { + "hasActiveAgreement": { + "eq": true + } + }, + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_member_extract" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::Member.id" + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::Member.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/entity-derived-filter-gate/input/meta.agreement.json b/fixtures/conformance/entity-derived-filter-gate/input/meta.agreement.json new file mode 100644 index 0000000000..4025cd21ab --- /dev/null +++ b/fixtures/conformance/entity-derived-filter-gate/input/meta.agreement.json @@ -0,0 +1,298 @@ +{ + "metadata.root": { + "package": "acme", + "children": [ + { + "object.entity": { + "name": "Member", + "children": [ + { + "source.rdb": { + "@table": "members" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.boolean": { + "name": "hasActiveAgreement", + "children": [ + { + "origin.first": { + "@via": "acme::Member.agreements", + "@of": "acme::Agreement.agreementIsOnFile", + "@orderBy": [ + "id:desc" + ], + "@segment": "agreementDecisive" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.entity": { + "name": "Agreement", + "children": [ + { + "source.rdb": { + "@table": "agreements" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "memberId", + "@required": true + } + }, + { + "field.int": { + "name": "agreementTypeId" + } + }, + { + "field.boolean": { + "name": "agreementIsOnFile" + } + }, + { + "field.boolean": { + "name": "memberAgreement" + } + }, + { + "field.date": { + "name": "dateReceived" + } + }, + { + "field.date": { + "name": "effectiveEndDate" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "memberId", + "@fields": "memberId", + "@references": "acme::Member" + } + }, + { + "segment.filter": { + "name": "agreementDecisive", + "@filter": { + "agreementTypeId": 1, + "or": [ + { + "agreementIsOnFile": false + }, + { + "and": [ + { + "agreementIsOnFile": true + }, + { + "or": [ + { + "memberAgreement": null + }, + { + "memberAgreement": true + } + ] + }, + { + "dateReceived": { + "lte": { + "now": "P0D" + } + } + }, + { + "or": [ + { + "effectiveEndDate": null + }, + { + "effectiveEndDate": { + "gte": { + "now": "P0D" + } + } + } + ] + } + ] + } + ] + } + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "memberId", + "@required": true + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "memberId", + "@fields": "memberId", + "@references": "acme::Member" + } + } + ] + } + }, + { + "object.entity": { + "name": "Authorization", + "children": [ + { + "source.rdb": { + "@table": "authorizations" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.string": { + "name": "code" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": "caseId", + "@references": "acme::Case" + } + } + ] + } + }, + { + "object.projection": { + "name": "AuthorizationExtract", + "@filter": { + "caseId.memberId.hasActiveAgreement": true + }, + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_authorization_extract" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::Authorization.id" + } + }, + { + "field.string": { + "name": "code", + "extends": "acme::Authorization.code" + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::Authorization.id" + } + } + ] + } + }, + { + "object.projection": { + "name": "MemberExtract", + "@filter": { + "hasActiveAgreement": true + }, + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_member_extract" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::Member.id" + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::Member.id" + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/fixtures/conformance/entity-derived-filter-gate/providers.json b/fixtures/conformance/entity-derived-filter-gate/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/entity-derived-filter-gate/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/error-origin-computed-literal-not-enum-member/expected-errors.json b/fixtures/conformance/error-origin-computed-literal-not-enum-member/expected-errors.json new file mode 100644 index 0000000000..fe6ab3facc --- /dev/null +++ b/fixtures/conformance/error-origin-computed-literal-not-enum-member/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_INVALID_ORIGIN", + "source": { + "format": "json", + "files": [ + "meta.views.json" + ], + "jsonPath": "$['metadata.root'].children[1]['object.projection'].children[2]['field.string'].children[0]['origin.computed']" + } + } + ], + "warnings": [] +} diff --git a/fixtures/conformance/error-origin-computed-literal-not-enum-member/input/meta.views.json b/fixtures/conformance/error-origin-computed-literal-not-enum-member/input/meta.views.json new file mode 100644 index 0000000000..2d1b3cc85a --- /dev/null +++ b/fixtures/conformance/error-origin-computed-literal-not-enum-member/input/meta.views.json @@ -0,0 +1,98 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.enum": { + "name": "status", + "@values": [ + "PAID", + "REFUNDED" + ] + } + }, + { + "field.int": { + "name": "priority" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.projection": { + "name": "CaseLabels", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_case_labels" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.string": { + "name": "label", + "children": [ + { + "origin.computed": { + "@expr": { + "case": [ + { + "when": { + "value": "PAYD" + }, + "then": { + "value": "Paid" + } + } + ], + "operand": { + "field": "status" + }, + "else": { + "value": "Other" + } + } + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/error-origin-computed-literal-not-enum-member/providers.json b/fixtures/conformance/error-origin-computed-literal-not-enum-member/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/error-origin-computed-literal-not-enum-member/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/error-origin-computed-literal-wrong-kind/expected-errors.json b/fixtures/conformance/error-origin-computed-literal-wrong-kind/expected-errors.json new file mode 100644 index 0000000000..37bdbf6548 --- /dev/null +++ b/fixtures/conformance/error-origin-computed-literal-wrong-kind/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_INVALID_ORIGIN", + "source": { + "format": "json", + "files": [ + "meta.views.json" + ], + "jsonPath": "$['metadata.root'].children[1]['object.projection'].children[2]['field.boolean'].children[0]['origin.computed']" + } + } + ], + "warnings": [] +} diff --git a/fixtures/conformance/error-origin-computed-literal-wrong-kind/input/meta.views.json b/fixtures/conformance/error-origin-computed-literal-wrong-kind/input/meta.views.json new file mode 100644 index 0000000000..1139db8841 --- /dev/null +++ b/fixtures/conformance/error-origin-computed-literal-wrong-kind/input/meta.views.json @@ -0,0 +1,89 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.enum": { + "name": "status", + "@values": [ + "PAID", + "REFUNDED" + ] + } + }, + { + "field.int": { + "name": "priority" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.projection": { + "name": "CaseLabels", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_case_labels" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.boolean": { + "name": "urgent", + "children": [ + { + "origin.computed": { + "@expr": { + "op": "eq", + "left": { + "field": "priority" + }, + "right": { + "value": "high" + } + } + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/error-origin-computed-literal-wrong-kind/providers.json b/fixtures/conformance/error-origin-computed-literal-wrong-kind/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/error-origin-computed-literal-wrong-kind/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/error-origin-correlated-operand-unknown/expected-errors.json b/fixtures/conformance/error-origin-correlated-operand-unknown/expected-errors.json new file mode 100644 index 0000000000..46d838d180 --- /dev/null +++ b/fixtures/conformance/error-origin-correlated-operand-unknown/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_BAD_ATTR_FILTER", + "source": { + "format": "json", + "files": [ + "meta.views.json" + ], + "jsonPath": "$['metadata.root'].children[3]['object.projection'].children[2]['field.timestamp'].children[0]['origin.aggregate']" + } + } + ], + "warnings": [] +} diff --git a/fixtures/conformance/error-origin-correlated-operand-unknown/input/meta.views.json b/fixtures/conformance/error-origin-correlated-operand-unknown/input/meta.views.json new file mode 100644 index 0000000000..a0b9f911a2 --- /dev/null +++ b/fixtures/conformance/error-origin-correlated-operand-unknown/input/meta.views.json @@ -0,0 +1,196 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.entity": { + "name": "Status", + "children": [ + { + "source.rdb": { + "@table": "statuses" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.int": { + "name": "priorType" + } + }, + { + "field.int": { + "name": "cumulativeStatus" + } + }, + { + "field.int": { + "name": "status" + } + }, + { + "field.date": { + "name": "effectiveFrom" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "statusId", + "@fields": "statusId", + "@references": "acme::views::Status" + } + } + ] + } + }, + { + "object.entity": { + "name": "CaseHistory", + "@collectionName": "CaseHistory", + "children": [ + { + "source.rdb": { + "@table": "case_history" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.timestamp": { + "name": "createdDate" + } + }, + { + "field.boolean": { + "name": "active" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": "caseId", + "@references": "acme::views::Case" + } + }, + { + "segment.filter": { + "name": "live", + "@filter": { + "active": true + } + } + } + ] + } + }, + { + "object.projection": { + "name": "BadCorrelation", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_badcorrelation" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.timestamp": { + "name": "x", + "children": [ + { + "origin.aggregate": { + "@agg": "min", + "@of": "acme::views::CaseHistory.createdDate", + "@via": "acme::views::Case.caseHistory", + "@filter": { + "statusId": { + "eq": { + "field": "nope" + } + } + } + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/fixtures/conformance/error-origin-correlated-operand-unknown/providers.json b/fixtures/conformance/error-origin-correlated-operand-unknown/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/error-origin-correlated-operand-unknown/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/error-origin-passthrough-reverse-hop/expected-errors.json b/fixtures/conformance/error-origin-passthrough-reverse-hop/expected-errors.json new file mode 100644 index 0000000000..d8f5e231e0 --- /dev/null +++ b/fixtures/conformance/error-origin-passthrough-reverse-hop/expected-errors.json @@ -0,0 +1,18 @@ +{ + "errors": [ + { + "code": "ERR_ORIGIN_CARDINALITY", + "messageIncludes": [ + "origin.passthrough on PatientConsentRow.onFile: @via hop \"consents\" is to-many (reverse hop over identity.reference \"patient\") — a row-multiplying passthrough — you meant aggregate (ADR-0029)." + ], + "source": { + "format": "json", + "files": [ + "meta.care.json" + ], + "jsonPath": "$['metadata.root'].children[2]['object.projection'].children[2]['field.boolean'].children[0]['origin.passthrough']" + } + } + ], + "warnings": [] +} diff --git a/fixtures/conformance/error-origin-passthrough-reverse-hop/input/meta.care.json b/fixtures/conformance/error-origin-passthrough-reverse-hop/input/meta.care.json new file mode 100644 index 0000000000..fbc7fe7a4b --- /dev/null +++ b/fixtures/conformance/error-origin-passthrough-reverse-hop/input/meta.care.json @@ -0,0 +1,41 @@ +{ + "metadata.root": { + "package": "acme::care", + "children": [ + { + "object.entity": { + "name": "Patient", + "children": [ + { "source.rdb": { "@table": "patients" } }, + { "field.long": { "name": "id" } }, + { "identity.primary": { "name": "id", "@fields": "id" } } + ] + } + }, + { + "object.entity": { + "name": "Consent", + "children": [ + { "source.rdb": { "@table": "consents" } }, + { "field.long": { "name": "id" } }, + { "field.long": { "name": "patientId" } }, + { "field.boolean": { "name": "onFile" } }, + { "identity.primary": { "name": "id", "@fields": "id" } }, + { "identity.reference": { "name": "patient", "@fields": "patientId", "@references": "acme::care::Patient" } } + ] + } + }, + { + "object.projection": { + "name": "PatientConsentRow", + "children": [ + { "source.rdb": { "@kind": "view", "@view": "v_patient_consent_row" } }, + { "field.long": { "name": "id", "extends": "acme::care::Patient.id" } }, + { "field.boolean": { "name": "onFile", "children": [ { "origin.passthrough": { "@from": "acme::care::Consent.onFile", "@via": "acme::care::Patient.consents" } } ] } }, + { "identity.primary": { "name": "id", "extends": "acme::care::Patient.id" } } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/error-origin-passthrough-reverse-hop/providers.json b/fixtures/conformance/error-origin-passthrough-reverse-hop/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/error-origin-passthrough-reverse-hop/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/error-origin-passthrough-to-many/expected-errors.json b/fixtures/conformance/error-origin-passthrough-to-many/expected-errors.json index 7f7f2c3c72..faffe9dc86 100644 --- a/fixtures/conformance/error-origin-passthrough-to-many/expected-errors.json +++ b/fixtures/conformance/error-origin-passthrough-to-many/expected-errors.json @@ -2,6 +2,9 @@ "errors": [ { "code": "ERR_ORIGIN_CARDINALITY", + "messageIncludes": [ + "origin.passthrough on UserSummary.orderLabel: @via hop \"orders\" is to-many (@cardinality \"many\") — a row-multiplying passthrough — you meant aggregate (ADR-0029)." + ], "source": { "format": "json", "files": [ diff --git a/fixtures/conformance/error-origin-segment-flattened-of/expected-errors.json b/fixtures/conformance/error-origin-segment-flattened-of/expected-errors.json new file mode 100644 index 0000000000..241596252f --- /dev/null +++ b/fixtures/conformance/error-origin-segment-flattened-of/expected-errors.json @@ -0,0 +1,14 @@ +{ + "errors": [ + { + "code": "ERR_INVALID_ORIGIN", + "source": { + "format": "json", + "files": [ + "meta.views.json" + ], + "jsonPath": "$['metadata.root'].children[3]['object.projection'].children[2]['field.string'].children[0]['origin.first']" + } + } + ] +} diff --git a/fixtures/conformance/error-origin-segment-flattened-of/input/meta.views.json b/fixtures/conformance/error-origin-segment-flattened-of/input/meta.views.json new file mode 100644 index 0000000000..a3faa7b82f --- /dev/null +++ b/fixtures/conformance/error-origin-segment-flattened-of/input/meta.views.json @@ -0,0 +1,141 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.value": { + "name": "Closure", + "children": [ + { + "field.string": { + "name": "reason" + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.entity": { + "name": "CaseHistory", + "@collectionName": "CaseHistory", + "children": [ + { + "source.rdb": { + "@table": "case_history" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.object": { + "name": "closure", + "@objectRef": "acme::views::Closure", + "@storage": "flattened" + } + }, + { + "field.boolean": { + "name": "active" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": "caseId", + "@references": "acme::views::Case" + } + }, + { + "segment.filter": { + "name": "live", + "@filter": { + "active": true + } + } + } + ] + } + }, + { + "object.projection": { + "name": "LastClosure", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_last_closure" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.string": { + "name": "reason", + "children": [ + { + "origin.first": { + "@of": "acme::views::CaseHistory.closure.reason", + "@via": "acme::views::Case.caseHistory", + "@orderBy": [ + "id" + ], + "@segment": "dead" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/error-origin-segment-flattened-of/providers.json b/fixtures/conformance/error-origin-segment-flattened-of/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/error-origin-segment-flattened-of/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/error-origin-segment-unresolved/expected-errors.json b/fixtures/conformance/error-origin-segment-unresolved/expected-errors.json new file mode 100644 index 0000000000..c2be014ee2 --- /dev/null +++ b/fixtures/conformance/error-origin-segment-unresolved/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_INVALID_ORIGIN", + "source": { + "format": "json", + "files": [ + "meta.views.json" + ], + "jsonPath": "$['metadata.root'].children[3]['object.projection'].children[2]['field.long'].children[0]['origin.aggregate']" + } + } + ], + "warnings": [] +} diff --git a/fixtures/conformance/error-origin-segment-unresolved/input/meta.views.json b/fixtures/conformance/error-origin-segment-unresolved/input/meta.views.json new file mode 100644 index 0000000000..dd80eac514 --- /dev/null +++ b/fixtures/conformance/error-origin-segment-unresolved/input/meta.views.json @@ -0,0 +1,190 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.entity": { + "name": "Status", + "children": [ + { + "source.rdb": { + "@table": "statuses" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.int": { + "name": "priorType" + } + }, + { + "field.int": { + "name": "cumulativeStatus" + } + }, + { + "field.int": { + "name": "status" + } + }, + { + "field.date": { + "name": "effectiveFrom" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "statusId", + "@fields": "statusId", + "@references": "acme::views::Status" + } + } + ] + } + }, + { + "object.entity": { + "name": "CaseHistory", + "@collectionName": "CaseHistory", + "children": [ + { + "source.rdb": { + "@table": "case_history" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.timestamp": { + "name": "createdDate" + } + }, + { + "field.boolean": { + "name": "active" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": "caseId", + "@references": "acme::views::Case" + } + }, + { + "segment.filter": { + "name": "live", + "@filter": { + "active": true + } + } + } + ] + } + }, + { + "object.projection": { + "name": "NoSegment", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_nosegment" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.long": { + "name": "live", + "children": [ + { + "origin.aggregate": { + "@agg": "count", + "@of": "acme::views::CaseHistory.id", + "@via": "acme::views::Case.caseHistory", + "@segment": "dead" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/fixtures/conformance/error-origin-segment-unresolved/providers.json b/fixtures/conformance/error-origin-segment-unresolved/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/error-origin-segment-unresolved/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/error-projection-extends-filter-sql-body/expected-errors.json b/fixtures/conformance/error-projection-extends-filter-sql-body/expected-errors.json new file mode 100644 index 0000000000..81ba0622c1 --- /dev/null +++ b/fixtures/conformance/error-projection-extends-filter-sql-body/expected-errors.json @@ -0,0 +1 @@ +[{ "code": "ERR_ORIGIN_UNDER_SQL_BODY" }] diff --git a/fixtures/conformance/error-projection-extends-filter-sql-body/input/meta.plans.json b/fixtures/conformance/error-projection-extends-filter-sql-body/input/meta.plans.json new file mode 100644 index 0000000000..673b59dbf9 --- /dev/null +++ b/fixtures/conformance/error-projection-extends-filter-sql-body/input/meta.plans.json @@ -0,0 +1,38 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Plan", + "children": [ + { "source.rdb": { "@table": "plans" } }, + { "field.long": { "name": "id" } }, + { "field.boolean": { "name": "consented" } }, + { "identity.primary": { "name": "id", "@fields": "id" } } + ] + } + }, + { + "object.projection": { + "name": "GatedPlans", + "abstract": true, + "@filter": { "consented": true }, + "children": [ + { "field.long": { "name": "planId", "extends": "Plan.id" } }, + { "identity.primary": { "name": "id", "extends": "Plan.id" } } + ] + } + }, + { + "object.projection": { + "name": "HandWrittenPlans", + "extends": "GatedPlans", + "children": [ + { "source.rdb": { "@kind": "view", "@view": "v_hand_written_plans", "@sql": "SELECT id AS plan_id FROM plans" } } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/error-projection-extends-filter-sql-body/providers.json b/fixtures/conformance/error-projection-extends-filter-sql-body/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/error-projection-extends-filter-sql-body/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/error-projection-filter-path-to-many/expected-errors.json b/fixtures/conformance/error-projection-filter-path-to-many/expected-errors.json new file mode 100644 index 0000000000..105cfd7427 --- /dev/null +++ b/fixtures/conformance/error-projection-filter-path-to-many/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_BAD_ATTR_FILTER", + "source": { + "format": "json", + "files": [ + "meta.tasks.json" + ], + "jsonPath": "$['metadata.root'].children[2]['object.projection']" + } + } + ], + "warnings": [] +} diff --git a/fixtures/conformance/error-projection-filter-path-to-many/input/meta.tasks.json b/fixtures/conformance/error-projection-filter-path-to-many/input/meta.tasks.json new file mode 100644 index 0000000000..1d95da3239 --- /dev/null +++ b/fixtures/conformance/error-projection-filter-path-to-many/input/meta.tasks.json @@ -0,0 +1,28 @@ +{ + "metadata.root": { + "package": "acme::tasks", + "children": [ + { "object.entity": { "name": "Case", "children": [ + { "source.rdb": { "@table": "cases" } }, + { "field.long": { "name": "id" } }, + { "field.string": { "name": "status" } }, + { "identity.primary": { "name": "id", "@fields": "id" } }, + { "relationship.composition": { "name": "tasks", "@objectRef": "acme::tasks::Task", "@cardinality": "many" } } + ] } }, + { "object.entity": { "name": "Task", "children": [ + { "source.rdb": { "@table": "tasks" } }, + { "field.long": { "name": "id" } }, + { "field.string": { "name": "title" } }, + { "field.long": { "name": "caseId" } }, + { "identity.primary": { "name": "id", "@fields": "id" } }, + { "identity.reference": { "name": "caseId", "@fields": "caseId", "@references": "acme::tasks::Case" } } + ] } }, + { "object.projection": { "name": "OpenTasks", "@filter": { "caseId.tasks.title": "x" }, "children": [ + { "source.rdb": { "@kind": "view", "@view": "v_open_tasks" } }, + { "field.long": { "name": "id", "extends": "acme::tasks::Task.id" } }, + { "field.string": { "name": "title", "extends": "acme::tasks::Task.title" } }, + { "identity.primary": { "name": "id", "extends": "acme::tasks::Task.id" } } + ] } } + ] + } +} diff --git a/fixtures/conformance/error-projection-filter-path-to-many/providers.json b/fixtures/conformance/error-projection-filter-path-to-many/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/error-projection-filter-path-to-many/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/origin-correlated-filter-and-segment/expected.json b/fixtures/conformance/origin-correlated-filter-and-segment/expected.json new file mode 100644 index 0000000000..cb50ec0209 --- /dev/null +++ b/fixtures/conformance/origin-correlated-filter-and-segment/expected.json @@ -0,0 +1,233 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.entity": { + "name": "Status", + "children": [ + { + "source.rdb": { + "@table": "statuses" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.int": { + "name": "priorType" + } + }, + { + "field.int": { + "name": "cumulativeStatus" + } + }, + { + "field.int": { + "name": "status" + } + }, + { + "field.date": { + "name": "effectiveFrom" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "statusId", + "@fields": [ + "statusId" + ], + "@references": "acme::views::Status" + } + } + ] + } + }, + { + "object.entity": { + "name": "CaseHistory", + "@collectionName": "CaseHistory", + "children": [ + { + "source.rdb": { + "@table": "case_history" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.timestamp": { + "name": "createdDate" + } + }, + { + "field.boolean": { + "name": "active" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": [ + "caseId" + ], + "@references": "acme::views::Case" + } + }, + { + "segment.filter": { + "name": "live", + "@filter": { + "active": { + "eq": true + } + } + } + } + ] + } + }, + { + "object.projection": { + "name": "CasePlacement", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_caseplacement" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.timestamp": { + "name": "statusPlaced", + "children": [ + { + "origin.aggregate": { + "@agg": "min", + "@filter": { + "statusId": { + "eq": { + "field": "statusId" + } + } + }, + "@of": "acme::views::CaseHistory.createdDate", + "@segment": "live", + "@via": "acme::views::Case.caseHistory" + } + } + ] + } + }, + { + "field.long": { + "name": "otherStatus", + "children": [ + { + "origin.first": { + "@filter": { + "statusId": { + "isDistinctFrom": { + "field": "statusId" + } + } + }, + "@of": "acme::views::CaseHistory.statusId", + "@orderBy": [ + "statusId" + ], + "@segment": "live", + "@via": "acme::views::Case.caseHistory" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/origin-correlated-filter-and-segment/input/meta.views.json b/fixtures/conformance/origin-correlated-filter-and-segment/input/meta.views.json new file mode 100644 index 0000000000..dfbf1198a4 --- /dev/null +++ b/fixtures/conformance/origin-correlated-filter-and-segment/input/meta.views.json @@ -0,0 +1,221 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.entity": { + "name": "Status", + "children": [ + { + "source.rdb": { + "@table": "statuses" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.int": { + "name": "priorType" + } + }, + { + "field.int": { + "name": "cumulativeStatus" + } + }, + { + "field.int": { + "name": "status" + } + }, + { + "field.date": { + "name": "effectiveFrom" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "statusId", + "@fields": "statusId", + "@references": "acme::views::Status" + } + } + ] + } + }, + { + "object.entity": { + "name": "CaseHistory", + "@collectionName": "CaseHistory", + "children": [ + { + "source.rdb": { + "@table": "case_history" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.timestamp": { + "name": "createdDate" + } + }, + { + "field.boolean": { + "name": "active" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": "caseId", + "@references": "acme::views::Case" + } + }, + { + "segment.filter": { + "name": "live", + "@filter": { + "active": true + } + } + } + ] + } + }, + { + "object.projection": { + "name": "CasePlacement", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_caseplacement" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.timestamp": { + "name": "statusPlaced", + "children": [ + { + "origin.aggregate": { + "@agg": "min", + "@of": "acme::views::CaseHistory.createdDate", + "@via": "acme::views::Case.caseHistory", + "@segment": "live", + "@filter": { + "statusId": { + "eq": { + "field": "statusId" + } + } + } + } + } + ] + } + }, + { + "field.long": { + "name": "otherStatus", + "children": [ + { + "origin.first": { + "@of": "acme::views::CaseHistory.statusId", + "@via": "acme::views::Case.caseHistory", + "@orderBy": [ + "statusId" + ], + "@segment": "live", + "@filter": { + "statusId": { + "isDistinctFrom": { + "field": "statusId" + } + } + } + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/fixtures/conformance/origin-correlated-filter-and-segment/providers.json b/fixtures/conformance/origin-correlated-filter-and-segment/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/origin-correlated-filter-and-segment/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/origin-passthrough-flattened-member/expected.json b/fixtures/conformance/origin-passthrough-flattened-member/expected.json new file mode 100644 index 0000000000..025803deca --- /dev/null +++ b/fixtures/conformance/origin-passthrough-flattened-member/expected.json @@ -0,0 +1,121 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.value": { + "name": "Address", + "children": [ + { + "field.string": { + "name": "city" + } + } + ] + } + }, + { + "object.value": { + "name": "ReferenceIds", + "children": [ + { + "field.string": { + "name": "externalReferenceId" + } + } + ] + } + }, + { + "object.entity": { + "name": "Plan", + "children": [ + { + "source.rdb": { + "@table": "plans" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.object": { + "name": "address", + "@objectRef": "acme::plans::Address", + "@storage": "flattened" + } + }, + { + "field.object": { + "name": "referenceIds", + "@flattenedColumnPrefix": "", + "@objectRef": "acme::plans::ReferenceIds", + "@storage": "flattened" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.projection": { + "name": "PlanExtract", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_plan_extract" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + }, + { + "field.string": { + "name": "city", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.address.city" + } + } + ] + } + }, + { + "field.string": { + "name": "externalReferenceId", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.referenceIds.externalReferenceId" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + } + ] + } + } + ] + } +} + diff --git a/fixtures/conformance/origin-passthrough-flattened-member/input/meta.plans.json b/fixtures/conformance/origin-passthrough-flattened-member/input/meta.plans.json new file mode 100644 index 0000000000..c3c7bbe993 --- /dev/null +++ b/fixtures/conformance/origin-passthrough-flattened-member/input/meta.plans.json @@ -0,0 +1,118 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.value": { + "name": "Address", + "children": [ + { + "field.string": { + "name": "city" + } + } + ] + } + }, + { + "object.value": { + "name": "ReferenceIds", + "children": [ + { + "field.string": { + "name": "externalReferenceId" + } + } + ] + } + }, + { + "object.entity": { + "name": "Plan", + "children": [ + { + "source.rdb": { + "@table": "plans" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.object": { + "name": "address", + "@objectRef": "acme::plans::Address", + "@storage": "flattened" + } + }, + { + "field.object": { + "name": "referenceIds", + "@objectRef": "acme::plans::ReferenceIds", + "@storage": "flattened", + "@flattenedColumnPrefix": "" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.projection": { + "name": "PlanExtract", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_plan_extract" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + }, + { + "field.string": { + "name": "city", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.address.city" + } + } + ] + } + }, + { + "field.string": { + "name": "externalReferenceId", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.referenceIds.externalReferenceId" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/fixtures/conformance/origin-passthrough-flattened-member/providers.json b/fixtures/conformance/origin-passthrough-flattened-member/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/origin-passthrough-flattened-member/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/origin-passthrough-two-references-one-target/expected.json b/fixtures/conformance/origin-passthrough-two-references-one-target/expected.json new file mode 100644 index 0000000000..49b81aca23 --- /dev/null +++ b/fixtures/conformance/origin-passthrough-two-references-one-target/expected.json @@ -0,0 +1,143 @@ +{ + "metadata.root": { + "package": "acme", + "children": [ + { + "object.entity": { + "name": "User", + "children": [ + { + "source.rdb": { + "@table": "users" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name", + "@required": true + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.entity": { + "name": "Account", + "children": [ + { + "source.rdb": { + "@table": "accounts" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "ownerId", + "@required": true + } + }, + { + "field.long": { + "name": "approverId" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "owner", + "@fields": [ + "ownerId" + ], + "@references": "acme::User" + } + }, + { + "identity.reference": { + "name": "approver", + "@fields": [ + "approverId" + ], + "@references": "acme::User" + } + } + ] + } + }, + { + "object.projection": { + "name": "AccountView", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_accounts" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::Account.id" + } + }, + { + "field.string": { + "name": "ownerName", + "children": [ + { + "origin.passthrough": { + "@from": "acme::User.name", + "@via": "acme::Account.owner" + } + } + ] + } + }, + { + "field.string": { + "name": "approverName", + "children": [ + { + "origin.passthrough": { + "@from": "acme::User.name", + "@via": "acme::Account.approver" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::Account.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/origin-passthrough-two-references-one-target/input/meta.json b/fixtures/conformance/origin-passthrough-two-references-one-target/input/meta.json new file mode 100644 index 0000000000..e9db3e0008 --- /dev/null +++ b/fixtures/conformance/origin-passthrough-two-references-one-target/input/meta.json @@ -0,0 +1,135 @@ +{ + "metadata.root": { + "package": "acme", + "children": [ + { + "object.entity": { + "name": "User", + "children": [ + { + "source.rdb": { + "@table": "users" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name", + "@required": true + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.entity": { + "name": "Account", + "children": [ + { + "source.rdb": { + "@table": "accounts" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "ownerId", + "@required": true + } + }, + { + "field.long": { + "name": "approverId" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "owner", + "@fields": "ownerId", + "@references": "User" + } + }, + { + "identity.reference": { + "name": "approver", + "@fields": "approverId", + "@references": "User" + } + } + ] + } + }, + { + "object.projection": { + "name": "AccountView", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_accounts" + } + }, + { + "field.long": { + "name": "id", + "extends": "Account.id" + } + }, + { + "field.string": { + "name": "ownerName", + "children": [ + { + "origin.passthrough": { + "@from": "User.name", + "@via": "Account.owner" + } + } + ] + } + }, + { + "field.string": { + "name": "approverName", + "children": [ + { + "origin.passthrough": { + "@from": "User.name", + "@via": "Account.approver" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "Account.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/origin-passthrough-two-references-one-target/providers.json b/fixtures/conformance/origin-passthrough-two-references-one-target/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/origin-passthrough-two-references-one-target/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/origin-segment-scoped/expected.json b/fixtures/conformance/origin-segment-scoped/expected.json new file mode 100644 index 0000000000..1671e0ce0c --- /dev/null +++ b/fixtures/conformance/origin-segment-scoped/expected.json @@ -0,0 +1,224 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.entity": { + "name": "Status", + "children": [ + { + "source.rdb": { + "@table": "statuses" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.int": { + "name": "priorType" + } + }, + { + "field.int": { + "name": "cumulativeStatus" + } + }, + { + "field.int": { + "name": "status" + } + }, + { + "field.date": { + "name": "effectiveFrom" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "statusId", + "@fields": [ + "statusId" + ], + "@references": "acme::views::Status" + } + } + ] + } + }, + { + "object.entity": { + "name": "CaseHistory", + "@collectionName": "CaseHistory", + "children": [ + { + "source.rdb": { + "@table": "case_history" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.timestamp": { + "name": "createdDate" + } + }, + { + "field.boolean": { + "name": "active" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": [ + "caseId" + ], + "@references": "acme::views::Case" + } + }, + { + "segment.filter": { + "name": "live", + "@filter": { + "active": { + "eq": true + } + } + } + } + ] + } + }, + { + "object.projection": { + "name": "CasePlacement", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_caseplacement" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.timestamp": { + "name": "statusPlaced", + "children": [ + { + "origin.aggregate": { + "@agg": "min", + "@of": "acme::views::CaseHistory.createdDate", + "@segment": "live", + "@via": "acme::views::Case.caseHistory" + } + } + ] + } + }, + { + "field.long": { + "name": "otherStatus", + "children": [ + { + "origin.first": { + "@filter": { + "statusId": { + "ne": 3 + } + }, + "@of": "acme::views::CaseHistory.statusId", + "@orderBy": [ + "statusId" + ], + "@segment": "live", + "@via": "acme::views::Case.caseHistory" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/origin-segment-scoped/input/meta.views.json b/fixtures/conformance/origin-segment-scoped/input/meta.views.json new file mode 100644 index 0000000000..8144e90f1e --- /dev/null +++ b/fixtures/conformance/origin-segment-scoped/input/meta.views.json @@ -0,0 +1,212 @@ +{ + "metadata.root": { + "package": "acme::views", + "children": [ + { + "object.entity": { + "name": "Status", + "children": [ + { + "source.rdb": { + "@table": "statuses" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.int": { + "name": "priorType" + } + }, + { + "field.int": { + "name": "cumulativeStatus" + } + }, + { + "field.int": { + "name": "status" + } + }, + { + "field.date": { + "name": "effectiveFrom" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "statusId", + "@fields": "statusId", + "@references": "acme::views::Status" + } + } + ] + } + }, + { + "object.entity": { + "name": "CaseHistory", + "@collectionName": "CaseHistory", + "children": [ + { + "source.rdb": { + "@table": "case_history" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "caseId", + "@required": true + } + }, + { + "field.long": { + "name": "statusId" + } + }, + { + "field.timestamp": { + "name": "createdDate" + } + }, + { + "field.boolean": { + "name": "active" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": "caseId", + "@references": "acme::views::Case" + } + }, + { + "segment.filter": { + "name": "live", + "@filter": { + "active": true + } + } + } + ] + } + }, + { + "object.projection": { + "name": "CasePlacement", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_caseplacement" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::views::Case.id" + } + }, + { + "field.timestamp": { + "name": "statusPlaced", + "children": [ + { + "origin.aggregate": { + "@agg": "min", + "@of": "acme::views::CaseHistory.createdDate", + "@via": "acme::views::Case.caseHistory", + "@segment": "live" + } + } + ] + } + }, + { + "field.long": { + "name": "otherStatus", + "children": [ + { + "origin.first": { + "@of": "acme::views::CaseHistory.statusId", + "@via": "acme::views::Case.caseHistory", + "@orderBy": [ + "statusId" + ], + "@segment": "live", + "@filter": { + "statusId": { + "ne": 3 + } + } + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::views::Case.id" + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/fixtures/conformance/origin-segment-scoped/providers.json b/fixtures/conformance/origin-segment-scoped/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/origin-segment-scoped/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/projection-extends-abstract-projection/expected.json b/fixtures/conformance/projection-extends-abstract-projection/expected.json new file mode 100644 index 0000000000..46f686c9f4 --- /dev/null +++ b/fixtures/conformance/projection-extends-abstract-projection/expected.json @@ -0,0 +1,154 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Plan", + "children": [ + { + "source.rdb": { + "@table": "plans" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "field.string": { + "name": "code" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.entity": { + "name": "Week", + "children": [ + { + "source.rdb": { + "@table": "weeks" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "planId" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "plan", + "@fields": [ + "planId" + ], + "@references": "acme::plans::Plan" + } + } + ] + } + }, + { + "object.projection": { + "name": "PlanExtract", + "abstract": true, + "children": [ + { + "field.long": { + "name": "planId", + "extends": "acme::plans::Plan.id" + } + }, + { + "field.string": { + "name": "label", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.name" + } + } + ] + } + }, + { + "field.int": { + "name": "weekCount", + "children": [ + { + "origin.aggregate": { + "@agg": "count", + "@of": "acme::plans::Week.id", + "@via": "acme::plans::Plan.weeks" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + } + ] + } + }, + { + "object.projection": { + "name": "DataAggregationPlans", + "extends": "acme::plans::PlanExtract", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_da_plans" + } + }, + { + "field.string": { + "name": "code", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.code" + } + } + ] + } + } + ] + } + } + ] + } +} + diff --git a/fixtures/conformance/projection-extends-abstract-projection/input/meta.plans.json b/fixtures/conformance/projection-extends-abstract-projection/input/meta.plans.json new file mode 100644 index 0000000000..26caa4c09c --- /dev/null +++ b/fixtures/conformance/projection-extends-abstract-projection/input/meta.plans.json @@ -0,0 +1,147 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Plan", + "children": [ + { + "source.rdb": { + "@table": "plans" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "field.string": { + "name": "code" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.entity": { + "name": "Week", + "children": [ + { + "source.rdb": { + "@table": "weeks" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.long": { + "name": "planId" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + }, + { + "identity.reference": { + "name": "plan", + "@fields": "planId", + "@references": "acme::plans::Plan" + } + } + ] + } + }, + { + "object.projection": { + "name": "PlanExtract", + "abstract": true, + "children": [ + { + "field.long": { + "name": "planId", + "extends": "acme::plans::Plan.id" + } + }, + { + "field.string": { + "name": "label", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.name" + } + } + ] + } + }, + { + "field.int": { + "name": "weekCount", + "children": [ + { + "origin.aggregate": { + "@agg": "count", + "@of": "acme::plans::Week.id", + "@via": "acme::plans::Plan.weeks" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + } + ] + } + }, + { + "object.projection": { + "name": "DataAggregationPlans", + "extends": "acme::plans::PlanExtract", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_da_plans" + } + }, + { + "field.string": { + "name": "code", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.code" + } + } + ] + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/fixtures/conformance/projection-extends-abstract-projection/providers.json b/fixtures/conformance/projection-extends-abstract-projection/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/projection-extends-abstract-projection/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/projection-extends-filtered-projection/expected.json b/fixtures/conformance/projection-extends-filtered-projection/expected.json new file mode 100644 index 0000000000..340e890c8f --- /dev/null +++ b/fixtures/conformance/projection-extends-filtered-projection/expected.json @@ -0,0 +1,133 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Plan", + "children": [ + { + "source.rdb": { + "@table": "plans" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "field.string": { + "name": "code" + } + }, + { + "field.boolean": { + "name": "consented" + } + }, + { + "field.boolean": { + "name": "deleted" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.projection": { + "name": "GatedPlans", + "abstract": true, + "@filter": { + "consented": { + "eq": true + } + }, + "children": [ + { + "field.long": { + "name": "planId", + "extends": "acme::plans::Plan.id" + } + }, + { + "field.string": { + "name": "label", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.name" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + } + ] + } + }, + { + "object.projection": { + "name": "LivePlans", + "extends": "acme::plans::GatedPlans", + "abstract": true, + "@filter": { + "deleted": { + "eq": false + } + } + } + }, + { + "object.projection": { + "name": "ExportPlans", + "extends": "acme::plans::LivePlans", + "@filter": { + "label": { + "isNull": false + } + }, + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_export_plans" + } + }, + { + "field.string": { + "name": "code", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.code" + } + } + ] + } + } + ] + } + } + ] + } +} + diff --git a/fixtures/conformance/projection-extends-filtered-projection/input/meta.plans.json b/fixtures/conformance/projection-extends-filtered-projection/input/meta.plans.json new file mode 100644 index 0000000000..3e2b1d8326 --- /dev/null +++ b/fixtures/conformance/projection-extends-filtered-projection/input/meta.plans.json @@ -0,0 +1,52 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Plan", + "children": [ + { "source.rdb": { "@table": "plans" } }, + { "field.long": { "name": "id" } }, + { "field.string": { "name": "name" } }, + { "field.string": { "name": "code" } }, + { "field.boolean": { "name": "consented" } }, + { "field.boolean": { "name": "deleted" } }, + { "identity.primary": { "name": "id", "@fields": "id" } } + ] + } + }, + { + "object.projection": { + "name": "GatedPlans", + "abstract": true, + "@filter": { "consented": true }, + "children": [ + { "field.long": { "name": "planId", "extends": "Plan.id" } }, + { "field.string": { "name": "label", "children": [ { "origin.passthrough": { "@from": "Plan.name" } } ] } }, + { "identity.primary": { "name": "id", "extends": "Plan.id" } } + ] + } + }, + { + "object.projection": { + "name": "LivePlans", + "abstract": true, + "extends": "GatedPlans", + "@filter": { "deleted": false } + } + }, + { + "object.projection": { + "name": "ExportPlans", + "extends": "LivePlans", + "@filter": { "label": { "isNull": false } }, + "children": [ + { "source.rdb": { "@kind": "view", "@view": "v_export_plans" } }, + { "field.string": { "name": "code", "children": [ { "origin.passthrough": { "@from": "Plan.code" } } ] } } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/projection-extends-filtered-projection/providers.json b/fixtures/conformance/projection-extends-filtered-projection/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/projection-extends-filtered-projection/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/projection-extends-projection-own-identity/expected.json b/fixtures/conformance/projection-extends-projection-own-identity/expected.json new file mode 100644 index 0000000000..16a450ef04 --- /dev/null +++ b/fixtures/conformance/projection-extends-projection-own-identity/expected.json @@ -0,0 +1,123 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Plan", + "children": [ + { + "source.rdb": { + "@table": "plans" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "field.string": { + "name": "code" + } + }, + { + "field.boolean": { + "name": "consented" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.projection": { + "name": "GatedPlans", + "abstract": true, + "@filter": { + "consented": { + "eq": true + } + }, + "children": [ + { + "field.long": { + "name": "planId", + "extends": "acme::plans::Plan.id" + } + }, + { + "field.string": { + "name": "label", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.name" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + } + ] + } + }, + { + "object.projection": { + "name": "ExportPlans", + "extends": "acme::plans::GatedPlans", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_export_plans" + } + }, + { + "field.long": { + "name": "exportId", + "extends": "acme::plans::Plan.id" + } + }, + { + "field.string": { + "name": "code", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Plan.code" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + } + ] + } + } + ] + } +} + diff --git a/fixtures/conformance/projection-extends-projection-own-identity/input/meta.plans.json b/fixtures/conformance/projection-extends-projection-own-identity/input/meta.plans.json new file mode 100644 index 0000000000..48ae1570c9 --- /dev/null +++ b/fixtures/conformance/projection-extends-projection-own-identity/input/meta.plans.json @@ -0,0 +1,44 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Plan", + "children": [ + { "source.rdb": { "@table": "plans" } }, + { "field.long": { "name": "id" } }, + { "field.string": { "name": "name" } }, + { "field.string": { "name": "code" } }, + { "field.boolean": { "name": "consented" } }, + { "identity.primary": { "name": "id", "@fields": "id" } } + ] + } + }, + { + "object.projection": { + "name": "GatedPlans", + "abstract": true, + "@filter": { "consented": true }, + "children": [ + { "field.long": { "name": "planId", "extends": "Plan.id" } }, + { "field.string": { "name": "label", "children": [ { "origin.passthrough": { "@from": "Plan.name" } } ] } }, + { "identity.primary": { "name": "id", "extends": "Plan.id" } } + ] + } + }, + { + "object.projection": { + "name": "ExportPlans", + "extends": "GatedPlans", + "children": [ + { "source.rdb": { "@kind": "view", "@view": "v_export_plans" } }, + { "field.long": { "name": "exportId", "extends": "Plan.id" } }, + { "field.string": { "name": "code", "children": [ { "origin.passthrough": { "@from": "Plan.code" } } ] } }, + { "identity.primary": { "name": "id", "extends": "Plan.id" } } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/projection-extends-projection-own-identity/providers.json b/fixtures/conformance/projection-extends-projection-own-identity/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/projection-extends-projection-own-identity/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/projection-filter-joined-path/expected.json b/fixtures/conformance/projection-filter-joined-path/expected.json new file mode 100644 index 0000000000..7122594bb5 --- /dev/null +++ b/fixtures/conformance/projection-filter-joined-path/expected.json @@ -0,0 +1,124 @@ +{ + "metadata.root": { + "package": "acme::tasks", + "children": [ + { + "object.entity": { + "name": "Case", + "children": [ + { + "source.rdb": { + "@table": "cases" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "status" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "relationship.composition": { + "name": "tasks", + "@cardinality": "many", + "@objectRef": "acme::tasks::Task" + } + } + ] + } + }, + { + "object.entity": { + "name": "Task", + "children": [ + { + "source.rdb": { + "@table": "tasks" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "title" + } + }, + { + "field.long": { + "name": "caseId" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "caseId", + "@fields": [ + "caseId" + ], + "@references": "acme::tasks::Case" + } + } + ] + } + }, + { + "object.projection": { + "name": "OpenTasks", + "@filter": { + "caseId.status": { + "eq": "open" + } + }, + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_open_tasks" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::tasks::Task.id" + } + }, + { + "field.string": { + "name": "title", + "extends": "acme::tasks::Task.title" + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::tasks::Task.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/projection-filter-joined-path/input/meta.tasks.json b/fixtures/conformance/projection-filter-joined-path/input/meta.tasks.json new file mode 100644 index 0000000000..e6bf337e67 --- /dev/null +++ b/fixtures/conformance/projection-filter-joined-path/input/meta.tasks.json @@ -0,0 +1,28 @@ +{ + "metadata.root": { + "package": "acme::tasks", + "children": [ + { "object.entity": { "name": "Case", "children": [ + { "source.rdb": { "@table": "cases" } }, + { "field.long": { "name": "id" } }, + { "field.string": { "name": "status" } }, + { "identity.primary": { "name": "id", "@fields": "id" } }, + { "relationship.composition": { "name": "tasks", "@objectRef": "acme::tasks::Task", "@cardinality": "many" } } + ] } }, + { "object.entity": { "name": "Task", "children": [ + { "source.rdb": { "@table": "tasks" } }, + { "field.long": { "name": "id" } }, + { "field.string": { "name": "title" } }, + { "field.long": { "name": "caseId" } }, + { "identity.primary": { "name": "id", "@fields": "id" } }, + { "identity.reference": { "name": "caseId", "@fields": "caseId", "@references": "acme::tasks::Case" } } + ] } }, + { "object.projection": { "name": "OpenTasks", "@filter": { "caseId.status": "open" }, "children": [ + { "source.rdb": { "@kind": "view", "@view": "v_open_tasks" } }, + { "field.long": { "name": "id", "extends": "acme::tasks::Task.id" } }, + { "field.string": { "name": "title", "extends": "acme::tasks::Task.title" } }, + { "identity.primary": { "name": "id", "extends": "acme::tasks::Task.id" } } + ] } } + ] + } +} diff --git a/fixtures/conformance/projection-filter-joined-path/providers.json b/fixtures/conformance/projection-filter-joined-path/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/projection-filter-joined-path/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/projection-filter-unprojected-base-field/expected.json b/fixtures/conformance/projection-filter-unprojected-base-field/expected.json new file mode 100644 index 0000000000..7bef802c1d --- /dev/null +++ b/fixtures/conformance/projection-filter-unprojected-base-field/expected.json @@ -0,0 +1,79 @@ +{ + "metadata.root": { + "package": "acme::tasks", + "children": [ + { + "object.entity": { + "name": "Task", + "children": [ + { + "source.rdb": { + "@table": "tasks" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "title" + } + }, + { + "field.boolean": { + "name": "deleted" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.projection": { + "name": "LiveTasks", + "@filter": { + "deleted": { + "eq": false + } + }, + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_live_tasks" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::tasks::Task.id" + } + }, + { + "field.string": { + "name": "title", + "extends": "acme::tasks::Task.title" + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::tasks::Task.id" + } + } + ] + } + } + ] + } +} + diff --git a/fixtures/conformance/projection-filter-unprojected-base-field/input/meta.tasks.json b/fixtures/conformance/projection-filter-unprojected-base-field/input/meta.tasks.json new file mode 100644 index 0000000000..2005b82235 --- /dev/null +++ b/fixtures/conformance/projection-filter-unprojected-base-field/input/meta.tasks.json @@ -0,0 +1,74 @@ +{ + "metadata.root": { + "package": "acme::tasks", + "children": [ + { + "object.entity": { + "name": "Task", + "children": [ + { + "source.rdb": { + "@table": "tasks" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "title" + } + }, + { + "field.boolean": { + "name": "deleted" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": "id" + } + } + ] + } + }, + { + "object.projection": { + "name": "LiveTasks", + "@filter": { + "deleted": false + }, + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_live_tasks" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::tasks::Task.id" + } + }, + { + "field.string": { + "name": "title", + "extends": "acme::tasks::Task.title" + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::tasks::Task.id" + } + } + ] + } + } + ] + } +} \ No newline at end of file diff --git a/fixtures/conformance/projection-filter-unprojected-base-field/providers.json b/fixtures/conformance/projection-filter-unprojected-base-field/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/projection-filter-unprojected-base-field/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/reference-flattened-member/expected.json b/fixtures/conformance/reference-flattened-member/expected.json new file mode 100644 index 0000000000..9f30d88eb9 --- /dev/null +++ b/fixtures/conformance/reference-flattened-member/expected.json @@ -0,0 +1,133 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Country", + "children": [ + { + "source.rdb": { + "@table": "countries" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "name" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.value": { + "name": "Address", + "children": [ + { + "field.string": { + "name": "city" + } + }, + { + "field.long": { + "name": "countryId" + } + } + ] + } + }, + { + "object.entity": { + "name": "Plan", + "children": [ + { + "source.rdb": { + "@table": "plans" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.object": { + "name": "address", + "@objectRef": "acme::plans::Address", + "@storage": "flattened" + } + }, + { + "identity.primary": { + "name": "id", + "@fields": [ + "id" + ] + } + }, + { + "identity.reference": { + "name": "countryId", + "@fields": [ + "address.countryId" + ], + "@references": "acme::plans::Country" + } + } + ] + } + }, + { + "object.projection": { + "name": "PlanExtract", + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_plan_extract" + } + }, + { + "field.long": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + }, + { + "field.string": { + "name": "countryName", + "children": [ + { + "origin.passthrough": { + "@from": "acme::plans::Country.name", + "@via": "acme::plans::Plan.countryId" + } + } + ] + } + }, + { + "identity.primary": { + "name": "id", + "extends": "acme::plans::Plan.id" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/reference-flattened-member/input/meta.plans.json b/fixtures/conformance/reference-flattened-member/input/meta.plans.json new file mode 100644 index 0000000000..e95a9cace2 --- /dev/null +++ b/fixtures/conformance/reference-flattened-member/input/meta.plans.json @@ -0,0 +1,57 @@ +{ + "metadata.root": { + "package": "acme::plans", + "children": [ + { + "object.entity": { + "name": "Country", + "children": [ + { "source.rdb": { "@table": "countries" } }, + { "field.long": { "name": "id" } }, + { "field.string": { "name": "name" } }, + { "identity.primary": { "name": "id", "@fields": "id" } } + ] + } + }, + { + "object.value": { + "name": "Address", + "children": [ + { "field.string": { "name": "city" } }, + { "field.long": { "name": "countryId" } } + ] + } + }, + { + "object.entity": { + "name": "Plan", + "children": [ + { "source.rdb": { "@table": "plans" } }, + { "field.long": { "name": "id" } }, + { "field.object": { "name": "address", "@objectRef": "acme::plans::Address", "@storage": "flattened" } }, + { "identity.primary": { "name": "id", "@fields": "id" } }, + { "identity.reference": { "name": "countryId", "@fields": "address.countryId", "@references": "acme::plans::Country" } } + ] + } + }, + { + "object.projection": { + "name": "PlanExtract", + "children": [ + { "source.rdb": { "@kind": "view", "@view": "v_plan_extract" } }, + { "field.long": { "name": "id", "extends": "acme::plans::Plan.id" } }, + { + "field.string": { + "name": "countryName", + "children": [ + { "origin.passthrough": { "@from": "acme::plans::Country.name", "@via": "acme::plans::Plan.countryId" } } + ] + } + }, + { "identity.primary": { "name": "id", "extends": "acme::plans::Plan.id" } } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/reference-flattened-member/providers.json b/fixtures/conformance/reference-flattened-member/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/reference-flattened-member/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/conformance/report-dimension-required-by-model/expected-report-required.json b/fixtures/conformance/report-dimension-required-by-model/expected-report-required.json new file mode 100644 index 0000000000..8d7f71c961 --- /dev/null +++ b/fixtures/conformance/report-dimension-required-by-model/expected-report-required.json @@ -0,0 +1,20 @@ +{ + "_comment": "The required of each SnapRealms field, derived from the model, never from the join type the view lowering picks. A joined dimension is required only when its @of field is required and every hop is a belongs-to reference over a required, enforced foreign key onto a target that hides no rows (not a TPH subtype). Read by TypeScript (metadata/test/report-required-conformance.test.ts) C# (ReportShapeTests), Java (ReportShapeTest) and Python (test_report_shape.py).", + "report": "acme::reports::SnapRealms", + "required": { + "sessionKey": true, + "sessionRealmId": true, + "realmName": true, + "looseRealmName": false, + "maybeRealmName": false, + "optionalFirstRealmName": false, + "realmMotto": false, + "orgName": false, + "relSessionRealmId": true, + "relRealmName": true, + "mixedRealmName": true, + "relMaybeRealmName": false, + "stampLabel": false, + "snaps": true + } +} diff --git a/fixtures/conformance/report-dimension-required-by-model/expected.json b/fixtures/conformance/report-dimension-required-by-model/expected.json new file mode 100644 index 0000000000..5aee5c4767 --- /dev/null +++ b/fixtures/conformance/report-dimension-required-by-model/expected.json @@ -0,0 +1,437 @@ +{ + "metadata.root": { + "package": "acme::reports", + "children": [ + { + "object.entity": { + "name": "Realm", + "children": [ + { + "source.rdb": { + "@table": "realms" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + }, + { + "field.string": { + "name": "name", + "@required": true + } + }, + { + "field.string": { + "name": "motto" + } + } + ] + } + }, + { + "object.entity": { + "name": "Session", + "children": [ + { + "source.rdb": { + "@table": "sessions" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + }, + { + "field.long": { + "name": "realmId", + "@required": true + } + }, + { + "field.long": { + "name": "maybeRealmId" + } + }, + { + "identity.reference": { + "name": "fkRealm", + "@fields": [ + "realmId" + ], + "@references": "acme::reports::Realm" + } + }, + { + "identity.reference": { + "name": "fkMaybeRealm", + "@fields": [ + "maybeRealmId" + ], + "@references": "acme::reports::Realm" + } + }, + { + "relationship.association": { + "name": "realm", + "@cardinality": "one", + "@objectRef": "acme::reports::Realm", + "@sourceRefField": "realmId" + } + }, + { + "relationship.association": { + "name": "maybeRealm", + "@cardinality": "one", + "@objectRef": "acme::reports::Realm", + "@sourceRefField": "maybeRealmId" + } + } + ] + } + }, + { + "object.entity": { + "name": "Snap", + "children": [ + { + "source.rdb": { + "@table": "snaps" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + }, + { + "field.long": { + "name": "sessionId", + "@required": true + } + }, + { + "field.long": { + "name": "unenforcedSessionId", + "@required": true + } + }, + { + "field.long": { + "name": "maybeSessionId" + } + }, + { + "field.long": { + "name": "orgId", + "@required": true + } + }, + { + "identity.reference": { + "name": "fkSession", + "@fields": [ + "sessionId" + ], + "@references": "acme::reports::Session" + } + }, + { + "identity.reference": { + "name": "fkMaybeSession", + "@fields": [ + "maybeSessionId" + ], + "@references": "acme::reports::Session" + } + }, + { + "identity.reference": { + "name": "fkOrg", + "@fields": [ + "orgId" + ], + "@references": "acme::reports::Org" + } + }, + { + "identity.reference": { + "name": "fkLoose", + "@enforce": false, + "@fields": [ + "unenforcedSessionId" + ], + "@references": "acme::reports::Session" + } + }, + { + "relationship.association": { + "name": "session", + "@cardinality": "one", + "@objectRef": "acme::reports::Session" + } + }, + { + "relationship.association": { + "name": "stamp", + "@cardinality": "one", + "@objectRef": "acme::reports::Stamp" + } + }, + { + "measure.aggregate": { + "name": "snaps", + "@agg": "count", + "@of": [ + "acme::reports::Snap.id" + ] + } + }, + { + "dimension.attribute": { + "name": "sessionKey", + "@of": "acme::reports::Snap.sessionId" + } + }, + { + "dimension.attribute": { + "name": "sessionRealmId", + "@of": "acme::reports::Session.realmId", + "@via": "acme::reports::Snap.fkSession" + } + }, + { + "dimension.attribute": { + "name": "realmName", + "@of": "acme::reports::Realm.name", + "@via": "acme::reports::Snap.fkSession.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "looseRealmName", + "@of": "acme::reports::Realm.name", + "@via": "acme::reports::Snap.fkLoose.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "maybeRealmName", + "@of": "acme::reports::Realm.name", + "@via": "acme::reports::Snap.fkSession.fkMaybeRealm" + } + }, + { + "dimension.attribute": { + "name": "optionalFirstRealmName", + "@of": "acme::reports::Realm.name", + "@via": "acme::reports::Snap.fkMaybeSession.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "realmMotto", + "@of": "acme::reports::Realm.motto", + "@via": "acme::reports::Snap.fkSession.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "orgName", + "@of": "acme::reports::Org.orgName", + "@via": "acme::reports::Snap.fkOrg" + } + }, + { + "dimension.attribute": { + "name": "relSessionRealmId", + "@of": "acme::reports::Session.realmId", + "@via": "acme::reports::Snap.session" + } + }, + { + "dimension.attribute": { + "name": "relRealmName", + "@of": "acme::reports::Realm.name", + "@via": "acme::reports::Snap.session.realm" + } + }, + { + "dimension.attribute": { + "name": "mixedRealmName", + "@of": "acme::reports::Realm.name", + "@via": "acme::reports::Snap.session.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "relMaybeRealmName", + "@of": "acme::reports::Realm.name", + "@via": "acme::reports::Snap.session.maybeRealm" + } + }, + { + "dimension.attribute": { + "name": "stampLabel", + "@of": "acme::reports::Stamp.label", + "@via": "acme::reports::Snap.stamp" + } + } + ] + } + }, + { + "object.entity": { + "name": "Stamp", + "children": [ + { + "source.rdb": { + "@table": "stamps" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + }, + { + "field.long": { + "name": "snapId", + "@required": true + } + }, + { + "field.string": { + "name": "label", + "@required": true + } + }, + { + "identity.reference": { + "name": "fkSnap", + "@fields": [ + "snapId" + ], + "@references": "acme::reports::Snap" + } + } + ] + } + }, + { + "object.entity": { + "name": "Party", + "@discriminator": "kind", + "children": [ + { + "source.rdb": { + "@table": "parties" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "kind", + "@required": true + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.entity": { + "name": "Org", + "extends": "acme::reports::Party", + "@discriminatorValue": "ORG", + "children": [ + { + "field.string": { + "name": "orgName", + "@required": true + } + } + ] + } + }, + { + "object.report": { + "name": "SnapRealms", + "@dimensions": [ + "sessionKey", + "sessionRealmId", + "realmName", + "looseRealmName", + "maybeRealmName", + "optionalFirstRealmName", + "realmMotto", + "orgName", + "relSessionRealmId", + "relRealmName", + "mixedRealmName", + "relMaybeRealmName", + "stampLabel" + ], + "@from": "acme::reports::Snap", + "@measures": [ + "snaps" + ], + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_snap_realms" + } + } + ] + } + } + ] + } +} + diff --git a/fixtures/conformance/report-dimension-required-by-model/input/meta.reports.json b/fixtures/conformance/report-dimension-required-by-model/input/meta.reports.json new file mode 100644 index 0000000000..e7f54c1082 --- /dev/null +++ b/fixtures/conformance/report-dimension-required-by-model/input/meta.reports.json @@ -0,0 +1,434 @@ +{ + "metadata.root": { + "package": "acme::reports", + "children": [ + { + "object.entity": { + "name": "Realm", + "children": [ + { + "source.rdb": { + "@table": "realms" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + }, + { + "field.string": { + "name": "name", + "@required": true + } + }, + { + "field.string": { + "name": "motto" + } + } + ] + } + }, + { + "object.entity": { + "name": "Session", + "children": [ + { + "source.rdb": { + "@table": "sessions" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + }, + { + "field.long": { + "name": "realmId", + "@required": true + } + }, + { + "field.long": { + "name": "maybeRealmId" + } + }, + { + "identity.reference": { + "name": "fkRealm", + "@fields": [ + "realmId" + ], + "@references": "Realm" + } + }, + { + "identity.reference": { + "name": "fkMaybeRealm", + "@fields": [ + "maybeRealmId" + ], + "@references": "Realm" + } + }, + { + "relationship.association": { + "name": "realm", + "@objectRef": "Realm", + "@cardinality": "one", + "@sourceRefField": "realmId" + } + }, + { + "relationship.association": { + "name": "maybeRealm", + "@objectRef": "Realm", + "@cardinality": "one", + "@sourceRefField": "maybeRealmId" + } + } + ] + } + }, + { + "object.entity": { + "name": "Snap", + "children": [ + { + "source.rdb": { + "@table": "snaps" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + }, + { + "field.long": { + "name": "sessionId", + "@required": true + } + }, + { + "field.long": { + "name": "unenforcedSessionId", + "@required": true + } + }, + { + "field.long": { + "name": "maybeSessionId" + } + }, + { + "field.long": { + "name": "orgId", + "@required": true + } + }, + { + "identity.reference": { + "name": "fkSession", + "@fields": [ + "sessionId" + ], + "@references": "Session" + } + }, + { + "identity.reference": { + "name": "fkMaybeSession", + "@fields": [ + "maybeSessionId" + ], + "@references": "Session" + } + }, + { + "identity.reference": { + "name": "fkOrg", + "@fields": [ + "orgId" + ], + "@references": "Org" + } + }, + { + "identity.reference": { + "name": "fkLoose", + "@fields": [ + "unenforcedSessionId" + ], + "@references": "Session", + "@enforce": false + } + }, + { + "relationship.association": { + "name": "session", + "@objectRef": "Session", + "@cardinality": "one" + } + }, + { + "relationship.association": { + "name": "stamp", + "@objectRef": "Stamp", + "@cardinality": "one" + } + }, + { + "measure.aggregate": { + "name": "snaps", + "@agg": "count", + "@of": "Snap.id" + } + }, + { + "dimension.attribute": { + "name": "sessionKey", + "@of": "Snap.sessionId" + } + }, + { + "dimension.attribute": { + "name": "sessionRealmId", + "@of": "Session.realmId", + "@via": "Snap.fkSession" + } + }, + { + "dimension.attribute": { + "name": "realmName", + "@of": "Realm.name", + "@via": "Snap.fkSession.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "looseRealmName", + "@of": "Realm.name", + "@via": "Snap.fkLoose.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "maybeRealmName", + "@of": "Realm.name", + "@via": "Snap.fkSession.fkMaybeRealm" + } + }, + { + "dimension.attribute": { + "name": "optionalFirstRealmName", + "@of": "Realm.name", + "@via": "Snap.fkMaybeSession.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "realmMotto", + "@of": "Realm.motto", + "@via": "Snap.fkSession.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "orgName", + "@of": "Org.orgName", + "@via": "Snap.fkOrg" + } + }, + { + "dimension.attribute": { + "name": "relSessionRealmId", + "@of": "Session.realmId", + "@via": "Snap.session" + } + }, + { + "dimension.attribute": { + "name": "relRealmName", + "@of": "Realm.name", + "@via": "Snap.session.realm" + } + }, + { + "dimension.attribute": { + "name": "mixedRealmName", + "@of": "Realm.name", + "@via": "Snap.session.fkRealm" + } + }, + { + "dimension.attribute": { + "name": "relMaybeRealmName", + "@of": "Realm.name", + "@via": "Snap.session.maybeRealm" + } + }, + { + "dimension.attribute": { + "name": "stampLabel", + "@of": "Stamp.label", + "@via": "Snap.stamp" + } + } + ] + } + }, + { + "object.entity": { + "name": "Stamp", + "children": [ + { + "source.rdb": { + "@table": "stamps" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + }, + { + "field.long": { + "name": "snapId", + "@required": true + } + }, + { + "field.string": { + "name": "label", + "@required": true + } + }, + { + "identity.reference": { + "name": "fkSnap", + "@fields": [ + "snapId" + ], + "@references": "Snap" + } + } + ] + } + }, + { + "object.entity": { + "name": "Party", + "@discriminator": "kind", + "children": [ + { + "source.rdb": { + "@table": "parties" + } + }, + { + "field.long": { + "name": "id" + } + }, + { + "field.string": { + "name": "kind", + "@required": true + } + }, + { + "identity.primary": { + "name": "pk", + "@fields": [ + "id" + ] + } + } + ] + } + }, + { + "object.entity": { + "name": "Org", + "extends": "Party", + "@discriminatorValue": "ORG", + "children": [ + { + "field.string": { + "name": "orgName", + "@required": true + } + } + ] + } + }, + { + "object.report": { + "name": "SnapRealms", + "@from": "Snap", + "@dimensions": [ + "sessionKey", + "sessionRealmId", + "realmName", + "looseRealmName", + "maybeRealmName", + "optionalFirstRealmName", + "realmMotto", + "orgName", + "relSessionRealmId", + "relRealmName", + "mixedRealmName", + "relMaybeRealmName", + "stampLabel" + ], + "@measures": [ + "snaps" + ], + "children": [ + { + "source.rdb": { + "@kind": "view", + "@view": "v_snap_realms" + } + } + ] + } + } + ] + } +} diff --git a/fixtures/conformance/report-dimension-required-by-model/providers.json b/fixtures/conformance/report-dimension-required-by-model/providers.json new file mode 100644 index 0000000000..f9bb435a37 --- /dev/null +++ b/fixtures/conformance/report-dimension-required-by-model/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types","metaobjects-db"] diff --git a/fixtures/metamodel-docs/expected/providers.md b/fixtures/metamodel-docs/expected/providers.md index 54ff5312e6..881fe9cce8 100644 --- a/fixtures/metamodel-docs/expected/providers.md +++ b/fixtures/metamodel-docs/expected/providers.md @@ -33,9 +33,9 @@ Core metaobjects metamodel types and subtypes. - `object.entity`: `@collectionName`, `@discriminator`, `@discriminatorValue` - `object.projection`: `@collectionName`, `@filter` - `object.report`: `@collectionName`, `@dimensions`, `@filter`, `@from`, `@measures`, `@segment`, `@spine` -- `origin.aggregate`: `@agg`, `@distinct`, `@filter`, `@of`, `@orderBy`, `@via` +- `origin.aggregate`: `@agg`, `@distinct`, `@filter`, `@of`, `@orderBy`, `@segment`, `@via` - `origin.computed`: `@expr` -- `origin.first`: `@filter`, `@of`, `@orderBy`, `@via` +- `origin.first`: `@filter`, `@of`, `@orderBy`, `@segment`, `@via` - `origin.passthrough`: `@convert`, `@from`, `@via` - `relationship.aggregation`: `@cardinality`, `@objectRef`, `@onDelete`, `@onUpdate`, `@sourceRefField`, `@symmetric`, `@through` - `relationship.association`: `@cardinality`, `@objectRef`, `@onDelete`, `@onUpdate`, `@sourceRefField`, `@symmetric`, `@through` diff --git a/fixtures/metamodel-docs/expected/types/object.md b/fixtures/metamodel-docs/expected/types/object.md index 1ceb902a36..36b03043a3 100644 --- a/fixtures/metamodel-docs/expected/types/object.md +++ b/fixtures/metamodel-docs/expected/types/object.md @@ -46,7 +46,7 @@ An object that owns its data: own identity, writable sources, and lifecycle. The | Attribute | Type | Required | Default | Allowed values | Provider | Description | | --- | --- | --- | --- | --- | --- | --- | -| `@collectionName` | string | no | | | metaobjects-core-types | The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). | +| `@collectionName` | string | no | | | metaobjects-core-types | The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). | | `@discriminator` | string | no | | | metaobjects-core-types | FR-014: names the field on this entity (resolvable via extends:) that holds the subtype-discriminator value. Subtypes of this entity declare @discriminatorValue to bind their rows to a discriminator value. The discriminator field itself is an ordinary field declaration (typically field.enum or field.int / field.string). | | `@discriminatorValue` | string | no | | | metaobjects-core-types | FR-014: on a subtype of an entity with @discriminator — the value that identifies rows of this subtype in the shared discriminator field. Wire form is always a string; the underlying field's subtype (enum / int / string) controls codegen + storage coercion. Required on every concrete subtype of a discriminated entity. | | `@object` | string | no | | | metaobjects-core-types | The native class an object binds to (ADR-0001), in place of its default runtime class: on the JVM, a fully-qualified class name. Cross-port vocabulary only Java implements so far; every port loads and type-checks it, and the others do not act on it yet. | @@ -80,8 +80,8 @@ A derived read-only representation of entities. Its fields are extends-bound / o | Attribute | Type | Required | Default | Allowed values | Provider | Description | | --- | --- | --- | --- | --- | --- | --- | -| `@collectionName` | string | no | | | metaobjects-core-types | The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). | -| `@filter` | filter | no | | | metaobjects-core-types | Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. Resolves against the projection's own declared fields; an aggregate-derived field is not addressable (fail-closed). | +| `@collectionName` | string | no | | | metaobjects-core-types | The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). | +| `@filter` | filter | no | | | metaobjects-core-types | Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. A key names one of the projection's own fields, else a field of its base entity the view need not output, else a PATH of to-one hops (identity.reference or @cardinality one relationship names) from the base then a field of the entity reached ('orderId.customerId.isActive'), joined but not output. A reached entity field may itself be derived (entity-derived: an origin.computed or origin.first declared on the entity, inlined where the filter reaches it). An aggregate-derived field is not addressable (fail-closed); a to-many hop is refused. A value may be a relative date ({ now: "", startOf? }) on a date or timestamp field. | | `@object` | string | no | | | metaobjects-core-types | The native class an object binds to (ADR-0001), in place of its default runtime class: on the JVM, a fully-qualified class name. Cross-port vocabulary only Java implements so far; every port loads and type-checks it, and the others do not act on it yet. | | `@objectAdapter` | string | no | | | metaobjects-core-types | JVM-only (ADR-0005): the fully-qualified name of an ObjectAdapter that takes over value access for this object's instances. Every port loads and type-checks it; only Java and Kotlin act on it. | @@ -106,7 +106,7 @@ A declared report (FR-044): a fixed combination of dimensions and measures of ON | Attribute | Type | Required | Default | Allowed values | Provider | Description | | --- | --- | --- | --- | --- | --- | --- | -| `@collectionName` | string | no | | | metaobjects-core-types | The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). | +| `@collectionName` | string | no | | | metaobjects-core-types | The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). | | `@dimensions` | string[] | no | | | metaobjects-core-types | Dimension names of @from; a time dimension is written 'name:grain'. Absent means one global row. | | `@filter` | filter | no | | | metaobjects-core-types | Optional row scope over @from's fields; may use relative-date values. | | `@from` | string | yes | | | metaobjects-core-types | The object.entity whose rows the report aggregates. | diff --git a/fixtures/metamodel-docs/expected/types/origin.md b/fixtures/metamodel-docs/expected/types/origin.md index 4a2ce7953b..f2e4c364fc 100644 --- a/fixtures/metamodel-docs/expected/types/origin.md +++ b/fixtures/metamodel-docs/expected/types/origin.md @@ -26,9 +26,10 @@ A value reduced from the related row-set reached along a relationship path (@via | --- | --- | --- | --- | --- | --- | --- | | `@agg` | string | yes | | `count`, `sum`, `avg`, `min`, `max`, `any`, `all`, `collect` | metaobjects-core-types | The reducing function applied over the related row-set: count/sum/avg/min/max (numeric/ordinal reduces over @of); any/all (predicate quantifiers over @filter — @of forbidden; empty set → any=false, all=true); collect (array rollup — of the @of column, or of the carrying field.object's declared @objectRef value object when @of is omitted; the field must be isArray). | | `@distinct` | boolean | no | | | metaobjects-core-types | Set (collect-only) to dedupe collected values (set semantics). Not supported on a whole-object collect (@of omitted): it is a guaranteed no-op whenever the value object carries the primary key, and a silent no-op is worse than a refusal. | -| `@filter` | filter | no | | | metaobjects-core-types | Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: "", startOf? }) on a date or timestamp field. | +| `@filter` | filter | no | | | metaobjects-core-types | Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: "", startOf? }) on a date or timestamp field, or { field: "" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom. | | `@of` | string | no | | | metaobjects-core-types | Dotted Entity.field reference identifying the column being aggregated (e.g. 'Week.durationMinutes'). Required for count/sum/avg/min/max; OPTIONAL for collect, where absent means a whole-object rollup of the field's declared @objectRef value object; forbidden for any/all (which quantify over rows via @filter, not a column). | | `@orderBy` | string[] | no | | | metaobjects-core-types | Ordering keys as 'field[:asc\|desc]' (default asc) over the related entity's fields; nulls sort last. On @agg:collect sets element order (non-distinct only); on origin.first (required) selects the row. Semantic — carries no SQL syntax. | +| `@segment` | string | no | | | metaobjects-core-types | Optional name of a segment.filter declared on the related entity (the @of entity, else the @via terminal). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates. | | `@via` | string | no | | | metaobjects-core-types | Dotted relationship path from the base entity to the aggregated rows (e.g. 'Program.weeks' or 'Program.weeks.workouts'). May be omitted only when exactly one single-hop relationship leads from the base entity to the @of entity (FR-024, ADR-0029). | **Allowed children** @@ -81,9 +82,10 @@ The single related row selected by @orderBy along @via, projecting its @of colum | Attribute | Type | Required | Default | Allowed values | Provider | Description | | --- | --- | --- | --- | --- | --- | --- | -| `@filter` | filter | no | | | metaobjects-core-types | Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: "", startOf? }) on a date or timestamp field. | +| `@filter` | filter | no | | | metaobjects-core-types | Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: "", startOf? }) on a date or timestamp field, or { field: "" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom. | | `@of` | string | yes | | | metaobjects-core-types | Dotted Entity.field reference identifying the column projected from the selected row (e.g. 'ChildA.label'). Type-preserving: the field's declared subType must equal this column's subType (#185 doctrine). | | `@orderBy` | string[] | yes | | | metaobjects-core-types | Ordering keys as 'field[:asc\|desc]' (default asc) over the related entity's fields; nulls sort last. Selects the single row (the first after ordering). The related PK ascending is appended as the deterministic tie-breaker. Semantic — carries no SQL syntax. | +| `@segment` | string | no | | | metaobjects-core-types | Optional name of a segment.filter declared on the related entity (the @of entity). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates. | | `@via` | string | no | | | metaobjects-core-types | Dotted relationship path from the base entity to the related rows (e.g. 'Parent.childAs'). May be omitted only when exactly one single-hop relationship leads from the base entity to the @of entity (FR-024, ADR-0029). | **Allowed children** diff --git a/fixtures/naming-conformance/README.md b/fixtures/naming-conformance/README.md index 97382e9bde..2542c818be 100644 --- a/fixtures/naming-conformance/README.md +++ b/fixtures/naming-conformance/README.md @@ -114,6 +114,17 @@ pair is REFUSED when the port puts both in one scope, naming the colliding name. Physical names are not this corpus's concern: `(schema, name)` uniqueness belongs to the schema layer (`meta migrate`'s `ERR_DUPLICATE_SQL_NAME`). +## Collection segments + +[`collection-segments.json`](./collection-segments.json) is the REST collection segment an +object is served under, given its name, the `@collectionName` it declares and the project's +`naming.collections` pluralizer. With neither, the segment is the rule above (snake_case, then +the API pluralizer), unchanged. A declared name, or a configured pluralizer's output, is +snake_cased with an acronym-aware split (`FAQs` -> `faqs`). TypeScript +(`metadata/test/naming.test.ts`) and C# (`MetaObjects.Codegen.Tests/CollectionSegmentTests.cs`) +run it: they are the two ports whose generators read `@collectionName` and `naming.collections` +(the other ports are tracked in #549). + ## Per-port runner Each port's existing naming unit-test file reads this JSON and asserts both diff --git a/fixtures/naming-conformance/collection-segments.json b/fixtures/naming-conformance/collection-segments.json new file mode 100644 index 0000000000..f1a9656053 --- /dev/null +++ b/fixtures/naming-conformance/collection-segments.json @@ -0,0 +1,28 @@ +{ + "_comment": "Cross-port REST collection segments. For each case, `segment` is the REST collection segment the object is served under, given its name, the @collectionName it declares (`collectionName`, absent when none) and the project's `naming.collections` (`pluralizer`, absent when none). With neither, the segment is the rule every port has always served: snake_case the name, then pluralize. A declared name, or a configured pluralizer's output over the name, is snake_cased with an acronym-aware split: a trailing plural `s`/`es` after a capital is a suffix, not a word (`FAQs` -> `faqs`, not `fa_qs`). Run by TypeScript (metadata/test/naming.test.ts) and C# (MetaObjects.Codegen.Tests/CollectionSegmentTests.cs), the two ports whose generators read @collectionName and naming.collections (#549).", + "cases": [ + { "name": "Author", "segment": "authors" }, + { "name": "PostCategory", "segment": "post_categories" }, + { "name": "Stats", "segment": "stats" }, + { "name": "Analysis", "segment": "analysises", "note": "the cross-port rule has no -sis case; C#'s DbSet keeps its own (Analyses)" }, + { "name": "FAQ", "segment": "faqs" }, + { "name": "PDF", "segment": "pdfs" }, + { "name": "TaxID", "segment": "tax_ids" }, + { "name": "SKU", "segment": "skus" }, + { "name": "SSN", "segment": "ssns" }, + { "name": "PA", "segment": "pas" }, + { "name": "URLHost", "segment": "url_hosts" }, + { "name": "CaseHistory", "collectionName": "CaseHistory", "segment": "case_history" }, + { "name": "Faq", "collectionName": "FAQs", "segment": "faqs" }, + { "name": "TaxId", "collectionName": "TaxIDs", "segment": "tax_ids" }, + { "name": "Box", "collectionName": "PDFBoxes", "segment": "pdf_boxes" }, + { "name": "Queue", "collectionName": "queues", "segment": "queues" }, + { "name": "FAQ", "pluralizer": "standard", "segment": "faqs" }, + { "name": "TaxID", "pluralizer": "standard", "segment": "tax_ids" }, + { "name": "PostCategory", "pluralizer": "standard", "segment": "post_categories" }, + { "name": "Analysis", "pluralizer": "standard", "segment": "analysises" }, + { "name": "FAQ", "pluralizer": "none", "segment": "faq" }, + { "name": "TaxID", "pluralizer": "none", "segment": "tax_id" }, + { "name": "Faq", "collectionName": "FAQs", "pluralizer": "none", "segment": "faqs", "note": "a declared name beats the configured pluralizer" } + ] +} diff --git a/fixtures/registry-conformance/expected-registry.json b/fixtures/registry-conformance/expected-registry.json index 4f1d8dd42c..1e85bfee65 100644 --- a/fixtures/registry-conformance/expected-registry.json +++ b/fixtures/registry-conformance/expected-registry.json @@ -3526,9 +3526,10 @@ "isArray": false, "required": false, "implementedIn": [ - "csharp" + "csharp", + "typescript" ], - "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." + "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." }, { "name": "discriminator", @@ -3659,16 +3660,17 @@ "isArray": false, "required": false, "implementedIn": [ - "csharp" + "csharp", + "typescript" ], - "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." + "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." }, { "name": "filter", "valueType": "filter", "isArray": false, "required": false, - "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. Resolves against the projection's own declared fields; an aggregate-derived field is not addressable (fail-closed)." + "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. A key names one of the projection's own fields, else a field of its base entity the view need not output, else a PATH of to-one hops (identity.reference or @cardinality one relationship names) from the base then a field of the entity reached ('orderId.customerId.isActive'), joined but not output. A reached entity field may itself be derived (entity-derived: an origin.computed or origin.first declared on the entity, inlined where the filter reaches it). An aggregate-derived field is not addressable (fail-closed); a to-many hop is refused. A value may be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." }, { "name": "object", @@ -3749,9 +3751,10 @@ "isArray": false, "required": false, "implementedIn": [ - "csharp" + "csharp", + "typescript" ], - "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." + "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." }, { "name": "dimensions", @@ -3991,7 +3994,7 @@ "valueType": "filter", "isArray": false, "required": false, - "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." + "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." }, { "name": "of", @@ -4007,6 +4010,13 @@ "required": false, "description": "Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. On @agg:collect sets element order (non-distinct only); on origin.first (required) selects the row. Semantic — carries no SQL syntax." }, + { + "name": "segment", + "valueType": "string", + "isArray": false, + "required": false, + "description": "Optional name of a segment.filter declared on the related entity (the @of entity, else the @via terminal). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." + }, { "name": "via", "valueType": "string", @@ -4052,7 +4062,7 @@ "valueType": "filter", "isArray": false, "required": false, - "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." + "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." }, { "name": "of", @@ -4068,6 +4078,13 @@ "required": true, "description": "Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. Selects the single row (the first after ordering). The related PK ascending is appended as the deterministic tie-breaker. Semantic — carries no SQL syntax." }, + { + "name": "segment", + "valueType": "string", + "isArray": false, + "required": false, + "description": "Optional name of a segment.filter declared on the related entity (the @of entity). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." + }, { "name": "via", "valueType": "string", diff --git a/server/csharp/MetaObjects.Codegen.Tests/CollectionSegmentTests.cs b/server/csharp/MetaObjects.Codegen.Tests/CollectionSegmentTests.cs new file mode 100644 index 0000000000..d14ae78ae1 --- /dev/null +++ b/server/csharp/MetaObjects.Codegen.Tests/CollectionSegmentTests.cs @@ -0,0 +1,67 @@ +// The REST collection segment and the DbSet name an object takes from its collection name: its own +// @collectionName, else the project's naming.collections pluralizer, else the rule every port has +// always served. fixtures/naming-conformance/collection-segments.json is shared with TypeScript. + +using System.IO; +using System.Text.Json; +using MetaObjects.Codegen; +using MetaObjects.Loader; +using MetaObjects.Meta; +using Xunit; + +namespace MetaObjects.Codegen.Tests; + +public class CollectionSegmentTests +{ + public sealed record SegmentCase(string Name, string? CollectionName, string? Pluralizer, string Segment) + { + public override string ToString() => $"{Name} (declared {CollectionName ?? "-"}, pluralizer {Pluralizer ?? "-"}) -> {Segment}"; + } + + public static TheoryData SegmentCases() + { + var path = Path.Combine(CorpusPaths.RepoRoot(), "fixtures", "naming-conformance", "collection-segments.json"); + using var doc = JsonDocument.Parse(File.ReadAllText(path)); + var data = new TheoryData(); + foreach (var c in doc.RootElement.GetProperty("cases").EnumerateArray()) + { + data.Add(new SegmentCase( + c.GetProperty("name").GetString()!, + c.TryGetProperty("collectionName", out var d) ? d.GetString() : null, + c.TryGetProperty("pluralizer", out var p) ? p.GetString() : null, + c.GetProperty("segment").GetString()!)); + } + return data; + } + + private static MetaObject Entity(string name, string? collectionName, string? pluralizer) + { + var declared = collectionName is null ? "" : $@"""@collectionName"": ""{collectionName}"", "; + var json = $@"{{ ""metadata.root"": {{ ""package"": ""acme"", ""children"": [ + {{ ""object.entity"": {{ ""name"": ""{name}"", {declared}""children"": [ + {{ ""source.rdb"": {{ ""@table"": ""t"" }} }}, + {{ ""field.long"": {{ ""name"": ""id"" }} }}, + {{ ""identity.primary"": {{ ""@fields"": ""id"" }} }} + ] }} }} + ] }} }}"; + var r = new MetaDataLoader().Load([new InMemoryStringSource(json, id: "segment.json")]); + Assert.Empty(r.Errors); + if (pluralizer is not null) r.Root.PhysicalNaming = PhysicalNaming.Default with { Collections = pluralizer }; + return r.Root.FindObject(name)!; + } + + [Theory] + [MemberData(nameof(SegmentCases))] + public void RoutePath_matches_the_shared_corpus(SegmentCase c) => + Assert.Equal(c.Segment, CSharpNaming.RoutePath(Entity(c.Name, c.CollectionName, c.Pluralizer))); + + [Fact] + public void DbSetName_takes_the_configured_pluralizer_and_a_declared_name_wins() + { + Assert.Equal("Analyses", CSharpNaming.DbSetName(Entity("Analysis", null, null))); + Assert.Equal("Order", CSharpNaming.DbSetName(Entity("Order", null, "none"))); + Assert.Equal("Orders", CSharpNaming.DbSetName(Entity("Order", null, "standard"))); + Assert.Equal("FAQs", CSharpNaming.DbSetName(Entity("Faq", "FAQs", "none"))); + Assert.Equal("FindFAQsByOwner", CSharpNaming.ReverseFinderName(Entity("Faq", "FAQs", null), "ownerId")); + } +} diff --git a/server/csharp/MetaObjects.Codegen.Tests/DerivedFieldWithoutReadSourceTests.cs b/server/csharp/MetaObjects.Codegen.Tests/DerivedFieldWithoutReadSourceTests.cs new file mode 100644 index 0000000000..cb10ab8d54 --- /dev/null +++ b/server/csharp/MetaObjects.Codegen.Tests/DerivedFieldWithoutReadSourceTests.cs @@ -0,0 +1,49 @@ +// An entity may carry a derived (origin.*) field that only a projection reads, with no read +// source of its own: the shared fixture entity-derived-filter-gate (Member.hasActiveAgreement, +// an origin.first a projection's @filter reaches). Its table, the one `meta migrate` creates, +// has no column for that field. So the generated EF entity must declare no property for it and +// the DbContext must map none, or every EF query on Member names a column the table lacks. + +using System.IO; +using System.Linq; +using MetaObjects.Codegen; +using MetaObjects.Codegen.Generators; +using MetaObjects.Loader; +using MetaObjects.Meta; +using Xunit; + +namespace MetaObjects.Codegen.Tests; + +public class DerivedFieldWithoutReadSourceTests +{ + private static GenContext Context() + { + var dir = Path.Combine(CorpusPaths.RepoRoot(), "fixtures", "conformance", "entity-derived-filter-gate", "input"); + var load = MetaDataLoader.FromDirectory(dir); + Assert.Empty(load.Errors); + return new GenContext + { + Entities = load.Root.Objects(), + Root = load.Root, + Config = new GenConfig { OutDir = "/tmp", Namespace = "Acme.Gen" }, + }; + } + + [Fact] + public void The_entity_declares_no_property_for_the_derived_field() + { + var member = Assert.Single(new EntityGenerator().Generate(Context()), f => f.Path == "Member.g.cs").Content; + Assert.Contains("public long Id", member); + Assert.DoesNotContain("HasActiveAgreement", member); + Assert.DoesNotContain("has_active_agreement", member); + } + + [Fact] + public void The_DbContext_maps_no_column_for_the_derived_field() + { + var dbContext = Assert.Single(new DbContextGenerator().Generate(Context())).Content; + Assert.Contains("Members", dbContext); + Assert.DoesNotContain("HasActiveAgreement", dbContext); + Assert.DoesNotContain("has_active_agreement", dbContext); + } +} diff --git a/server/csharp/MetaObjects.Codegen/CSharpNaming.cs b/server/csharp/MetaObjects.Codegen/CSharpNaming.cs index ae102e5420..3d96b26390 100644 --- a/server/csharp/MetaObjects.Codegen/CSharpNaming.cs +++ b/server/csharp/MetaObjects.Codegen/CSharpNaming.cs @@ -350,10 +350,14 @@ public static string PropertyName(MetaField field, GenConfig? config) => public static string RoutesClassName(MetaObject entity) => Pascal(entity.Name) + "Routes"; /// - /// The REST collection URL segment for an entity: the ENTITY NAME + /// The REST collection URL segment for an entity: its declared @collectionName, or the + /// configured naming.collections pluralizer's output, snake_cased + /// (); with neither, the ENTITY NAME /// ToSnakeCased and then Pluralized (Author → /// authors, PostCategory → post_categories). Never the - /// physical @table. One rule, identical in all five ports. + /// physical @table. The unconfigured rule is identical in all five ports; the + /// collection-name rule is shared with TypeScript + /// (fixtures/naming-conformance/collection-segments.json). /// /// It was Pluralize(name).ToLowerInvariant(), which served /// PostCategory at /postcategories — the capitals carried the @@ -362,11 +366,14 @@ public static string PropertyName(MetaField field, GenConfig? config) => /// See fixtures/api-contract-conformance/m2m/ for the gating scenario. /// /// - public static string RoutePath(MetaObject entity) => PluralizeCrossPort(ToSnakeCase(entity.Name)); + public static string RoutePath(MetaObject entity) => + CollectionNames.Segment(entity, name => PluralizeCrossPort(ToSnakeCase(name))); /// /// The DbSet property name for an object: its @collectionName when it declares one, else - /// Pluralize(Pascal(name)). Every site that names the property (the context, the + /// the configured pluralizer (naming.collections) over Pascal(name), else + /// Pluralize(Pascal(name)) (which keeps C#'s -sis rule, so an unconfigured project's + /// DbSets are unchanged). Every site that names the property (the context, the /// routes, the reverse finders, the api reference) reads it here, so they always agree. /// public static string DbSetName(MetaObject entity) @@ -374,7 +381,9 @@ public static string DbSetName(MetaObject entity) // ADR-0039: own — @collectionName names THIS object's property; an abstract base's value // inherited by every subtype would hand them all one name. if (entity.OwnAttr(OBJECT_ATTR_COLLECTION_NAME) is not string { Length: > 0 } declared) - return Pluralize(Pascal(entity.Name)); + return CollectionNames.ConfiguredPluralizer(entity) is { } configured + ? NamingStrategies.Pluralizer(configured)(Pascal(entity.Name)) + : Pluralize(Pascal(entity.Name)); // Refused here, the one place every generator reads it from, rather than as a compile // error in the generated context (or a property that hides DbContext.Database). if (!System.Text.RegularExpressions.Regex.IsMatch(declared, "^[A-Za-z_][A-Za-z0-9_]*$") || ReservedDbSetNames.Contains(declared)) @@ -444,7 +453,7 @@ public static string ReverseFinderFkSegment(string fkFieldName) /// ADR-0038 — the single-value reverse finder name: Find<EPlural>By<FkField>. public static string ReverseFinderName(MetaObject entity, string fkFieldName) => - $"Find{Pluralize(Pascal(entity.Name))}By{ReverseFinderFkSegment(fkFieldName)}"; + $"Find{DbSetName(entity)}By{ReverseFinderFkSegment(fkFieldName)}"; /// ADR-0038 — the batched (anti-N+1) reverse finder name: Find<EPlural>By<FkField>In. public static string ReverseFinderInName(MetaObject entity, string fkFieldName) => diff --git a/server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs b/server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs index 7be5950fc7..1ef86a1577 100644 --- a/server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs +++ b/server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs @@ -192,13 +192,12 @@ public virtual IEnumerable Generate(GenContext ctx) foreach (var e in objects.Where(o => o.IsEntity() && !o.IsReadOnlyProjection())) { var owner = CSharpNaming.Pascal(e.Name); - // #214 — for a write-through entity these config lines target the WRITE (table) - // class, which omits the derived (origin.*) fields; so must the config, or it - // would reference properties the write class does not declare (a compile error). - // The derived fields' EF mapping lives on the read model (registered below). - IEnumerable configFields = e.IsWriteThrough() - ? e.Fields().Where(f => !f.IsDerived()) - : e.Fields(); + // #214 — these config lines target the table-mapped class, which omits the derived + // (origin.*) fields; so must the config, or it would reference properties the class + // does not declare (a compile error). A write-through entity's derived fields are + // mapped on its read model (registered below). An entity's primary source is always + // writable (the loader refuses a read-only primary), so this class always maps a table. + IEnumerable configFields = e.Fields().Where(f => !f.IsDerived()); // #214 [0] — the per-field EF TYPE converters (owned-VO / enum / primitive // collection / decimal precision / timestamp column type / field.uri / field.inet / // @dbColumnType) over the write entity's derived-EXCLUDED field set. The SAME helper @@ -1445,17 +1444,17 @@ protected virtual IEnumerable ReferenceConfig( } var props = new List(fkFields.Count); - var isWriteThrough = entity.IsWriteThrough(); foreach (var name in fkFields) { - // #214 — a write-through entity's WRITE class omits the derived (origin.*) - // fields, so naming one here would reference a property the class does not - // declare (a compile error in the generated file). + // #214 — an entity's table-mapped class omits the derived (origin.*) fields, so + // naming one here would reference a property the class does not declare (a + // compile error in the generated file). An entity's primary source is always + // writable (the loader refuses a read-only primary), so its class maps a table. if (entity.FindField(name) is not { } field) { return []; } - if (isWriteThrough && field.IsDerived()) + if (field.IsDerived()) { return []; } diff --git a/server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs b/server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs index 6d42a1c8c2..c6a59fcd7f 100644 --- a/server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs +++ b/server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs @@ -277,8 +277,10 @@ protected virtual EmittedFile EmitMappedClass(MetaObject entity, GenContext ctx) // Read-only projections map to a view (no [Table]); tables / a write-through // WRITE entity carry [Table]. isView: entity.IsReadOnlyProjection(), - // #214 — the write entity of a write-through omits the derived (view-only) fields. - excludeDerived: entity.IsWriteThrough(), + // #214 — an entity's table-mapped class omits the derived fields: its table has no column for them. + // An entity's primary source is always writable (the loader refuses a read-only primary), + // so every entity class here maps a table. + excludeDerived: entity.IsEntity(), // A plain read-only projection keeps its warn-and-skip on object-typed columns // (collection materialization is a separate concern); the write entity is not a // view so this flag is inert there. diff --git a/server/csharp/MetaObjects.Conformance.Tests/AtLeastOneMaxDefaultLoadTests.cs b/server/csharp/MetaObjects.Conformance.Tests/AtLeastOneMaxDefaultLoadTests.cs new file mode 100644 index 0000000000..9c7c30d72b --- /dev/null +++ b/server/csharp/MetaObjects.Conformance.Tests/AtLeastOneMaxDefaultLoadTests.cs @@ -0,0 +1,43 @@ +// validator.atLeastOne takes no @max. The shared fixture error-validator-at-least-one-max-unknown +// runs under the strict loader, where the attribute is unregistered; this runs the same input +// through the default (non-strict) loader too, which would otherwise keep @max and ignore it. +// Oracle: that fixture's expected-errors.json, exactly one ERR_UNKNOWN_ATTR at the validator, +// under either loader. + +using System.IO; +using System.Linq; +using System.Text.Json; +using MetaObjects; +using MetaObjects.Loader; +using MetaObjects.Meta; +using MetaObjects.Source; +using Xunit; + +namespace MetaObjects.Conformance.Tests; + +public sealed class AtLeastOneMaxDefaultLoadTests +{ + private static readonly string Fixture = + Path.Combine(CorpusRoot.Path, "error-validator-at-least-one-max-unknown"); + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void AtLeastOne_with_max_is_refused_once(bool strict) + { + using var doc = JsonDocument.Parse(File.ReadAllText(Path.Combine(Fixture, "expected-errors.json"))); + var expected = doc.RootElement.GetProperty("errors").EnumerateArray() + .Select(e => (e.GetProperty("code").GetString(), e.GetProperty("source").GetProperty("jsonPath").GetString())) + .ToList(); + + var sources = Directory.GetFiles(Path.Combine(Fixture, "input"), "*.json") + .OrderBy(f => f, StringComparer.Ordinal) + .Select(f => (IMetaDataSource)new FileSource(f)).ToList(); + var result = new MetaDataLoader(Provider.ComposeRegistry(CoreTypes.LibraryProviders), strict: strict).Load(sources); + + var actual = result.Errors + .Select(e => ((string?)e.Code.ToString(), (e.Envelope as JsonSource)?.JsonPath)) + .ToList(); + Assert.Equal(expected, actual); + } +} diff --git a/server/csharp/MetaObjects.Conformance.Tests/CollectionNameVocabularyTests.cs b/server/csharp/MetaObjects.Conformance.Tests/CollectionNameVocabularyTests.cs index fcaccfd0d0..66a2363624 100644 --- a/server/csharp/MetaObjects.Conformance.Tests/CollectionNameVocabularyTests.cs +++ b/server/csharp/MetaObjects.Conformance.Tests/CollectionNameVocabularyTests.cs @@ -1,5 +1,5 @@ -// @collectionName is shared vocabulary: an object's collection name (the EF Core DbSet property). -// It is in the shared registry marked implementedIn ["csharp"], so every port loads and +// @collectionName is shared vocabulary: an object's collection name (the EF Core DbSet property, the REST segment). +// It is in the shared registry marked implementedIn ["csharp", "typescript"], so every port loads and // type-checks it. It loads under the strict // loader on object.entity / projection / report. @@ -46,7 +46,7 @@ public void It_is_shared_vocabulary_marked_by_implementing_port() Assert.Equal(RegistryManifest.ExclusionReason.Included, RegistryManifest.ClassifyPerTypeAttr("collectionName")); var registry = Provider.ComposeRegistry(CoreTypes.LibraryProviders); var attr = registry.Find("object", "entity")!.Attributes.Single(a => a.Name == "collectionName"); - Assert.Equal(new[] { "csharp" }, attr.ImplementedIn); + Assert.Equal(new[] { "csharp", "typescript" }, attr.ImplementedIn); } [Fact] public void The_standard_pluralizer_matches_the_shared_naming_conformance_corpus() diff --git a/server/csharp/MetaObjects.Conformance.Tests/ReportShapeTests.cs b/server/csharp/MetaObjects.Conformance.Tests/ReportShapeTests.cs index c4d9cc4529..2655ddd4b0 100644 --- a/server/csharp/MetaObjects.Conformance.Tests/ReportShapeTests.cs +++ b/server/csharp/MetaObjects.Conformance.Tests/ReportShapeTests.cs @@ -50,6 +50,22 @@ private static MetaRoot LoadFixture(string name) return result.Root; } + // A joined dimension's required comes from the model, never from the join type the view + // lowering picks — a required, enforced belongs-to path onto a target that hides no rows. The shared + // table is the fixture's expected-report-required.json, which TypeScript reads too. + [Fact] + public void A_dimensions_required_is_the_shared_tables() + { + const string name = "report-dimension-required-by-model"; + var root = LoadFixture(name); + using var doc = System.Text.Json.JsonDocument.Parse( + File.ReadAllText(Path.Combine(CorpusRoot.Path, name, "expected-report-required.json"))); + var reportKey = doc.RootElement.GetProperty("report").GetString()!; + var expected = doc.RootElement.GetProperty("required").EnumerateObject().ToDictionary(p => p.Name, p => p.Value.GetBoolean()); + var shape = ReportShapes.Of(root.Objects().Single(o => o.ResolutionKey() == reportKey), root); + Assert.Equal(expected, shape.Fields.ToDictionary(f => f.Name, f => f.Required)); + } + [Fact] public void Derived_shapes_byte_match_the_committed_artifact() { @@ -649,9 +665,9 @@ public void A_via_dimension_is_required_when_every_hop_is_a_required_belongs_to( var plain = RequiredOf(root, "Plain"); Assert.True(plain["realmName"]); // two required hops over a required field Assert.True(plain["sessionRealmId"]); // one required hop - Assert.True(plain["looseRealmName"]); // @enforce: false does not change the join type + Assert.False(plain["looseRealmName"]); // an @enforce: false hop can name a missing row Assert.False(plain["realmMotto"]); // the target field is optional - Assert.False(plain["maybeRealmName"]); // a hop over a nullable foreign key is a LEFT join + Assert.False(plain["maybeRealmName"]); // a hop over a nullable foreign key can have no match var spined = RequiredOf(root, "Spined"); Assert.False(spined["realmName"]); // under @spine every join beyond the spine is LEFT diff --git a/server/csharp/MetaObjects/Core/Reporting/ReportShape.cs b/server/csharp/MetaObjects/Core/Reporting/ReportShape.cs index 3b48bc8a1a..e4f0999018 100644 --- a/server/csharp/MetaObjects/Core/Reporting/ReportShape.cs +++ b/server/csharp/MetaObjects/Core/Reporting/ReportShape.cs @@ -233,14 +233,15 @@ private static bool IsPrimaryKeyField(MetaObject entity, MetaField field) } /// - /// True when every hop of , walked from , is a - /// belongs-to hop over a required foreign key: the hops the view joins INNER, so every row - /// of the report has a match. This is the join-type rule of the view lowering (TypeScript - /// walkViaPath, #209): the reference is held by the entity the hop leaves, its first - /// FK field is required, and every earlier hop is INNER too. @enforce: false does not - /// change it. A hop that does not resolve, an ambiguous reference or a has-many hop is not INNER. + /// True when every hop of , walked from , + /// is certain to match: a belongs-to hop (the reference is held by the entity the hop leaves) + /// whose first FK field is required, whose reference is enforced (not @enforce: false, + /// which can name a row that does not exist), onto a target that hides no rows (not a TPH + /// subtype, whose join carries the discriminator). That is exactly when a LEFT OUTER join + /// returns what INNER would, whichever join type the view lowering picks; this reads the model, + /// never the join type. Ported from report-shape.ts viaAlwaysMatches. /// - private static bool ViaJoinsInner(IReadOnlyList hops, MetaObject from, MetaRoot root) + private static bool ViaAlwaysMatches(IReadOnlyList hops, MetaObject from, MetaRoot root) { var current = from; foreach (string hopName in hops) @@ -257,20 +258,36 @@ private static bool ViaJoinsInner(IReadOnlyList hops, MetaObject from, M // sits on the child, matches none of the current entity's own references. var reference = refNode ?? RelationshipReferences.ResolveRelationshipReference(current, hopName, target, rel!.SourceRefField); - if (reference is null) return false; + if (reference is null || !reference.Enforce) return false; string? fkName = RelationshipReferences.RefFkField(reference); var fk = fkName is null ? null : current.FindField(fkName); if (fk is null || !fk.IsRequired) return false; + if (HidesRows(target)) return false; current = target; } return true; } + /// + /// True when a join onto carries a row scope of its own: a TPH subtype, or + /// an abstract level beneath a TPH base, both scoped by the discriminator. The model has no + /// soft-delete or query-filter vocabulary, so this is the only row-hiding scope. + /// + private static bool HidesRows(MetaObject obj) + { + // ADR-0039: own — the discriminator is declared on the level that owns it. + if (obj.OwnAttr(OBJECT_ATTR_DISCRIMINATOR) is not null) return false; + var seen = new HashSet(ReferenceEqualityComparer.Instance) { obj }; + for (var s = obj.SuperData; s is not null && seen.Add(s); s = s.SuperData) + if (s.OwnAttr(OBJECT_ATTR_DISCRIMINATOR) is not null) return true; + return false; + } + /// /// Table C (required of a dimension). Without @spine: only a dimension over an /// @of field that is effectively required (@required or a validator.required child) /// and, when it has a @via, only when every hop is a required belongs-to reference, the - /// hops the view joins INNER (). With + /// hops certain to match (). With /// @spine: only a dimension whose @via hops equal the spine's (a column of /// the spine entity itself, whose rows are the report's rows) over an @of field that /// is required (same rule) or a primary-key column of the entity @of names. A dimension @@ -289,7 +306,7 @@ private static bool DimensionRequired( if (!ofRequired) return false; var viaHops = ReportingViaHops(via, ReportingMemberOwner(dim, from), from, root) ?? throw Unresolved(reportName, $"dimension '{dim.Name}' @via '{via}'"); - return ViaJoinsInner(viaHops, from, root); + return ViaAlwaysMatches(viaHops, from, root); } if (via is null) return false; // a column of @from: null in a spine row with no facts var hops = ReportingViaHops(via, ReportingMemberOwner(dim, from), from, root) diff --git a/server/csharp/MetaObjects/Loader/MetaDataLoader.cs b/server/csharp/MetaObjects/Loader/MetaDataLoader.cs index 2d8d311a12..92d48a76ea 100644 --- a/server/csharp/MetaObjects/Loader/MetaDataLoader.cs +++ b/server/csharp/MetaObjects/Loader/MetaDataLoader.cs @@ -525,6 +525,7 @@ public LoadResult Load(IReadOnlyList sources) // Pass 7b (#207): projection view-level @filter — a dangling or // aggregate-derived field-ref fails closed with ERR_BAD_ATTR_FILTER. errors.AddRange(ValidationPasses.ValidateProjectionFilter(root)); + errors.AddRange(ValidationPasses.ValidateOriginSegmentsAndCorrelation(root)); // Pass 7c (FR-044): reporting vocabulary cross-node rules (dimensions, measures, // segments, reports, relative-date filter values). @@ -538,7 +539,7 @@ public LoadResult Load(IReadOnlyList sources) errors.AddRange(ValidationPasses.ValidateFieldMap(root)); errors.AddRange(ValidationPasses.ValidateEnumMaxLength(root)); errors.AddRange(ValidationPasses.ValidateIdentitySequence(root)); - errors.AddRange(ValidationPasses.ValidateCrossFieldValidators(root)); + errors.AddRange(ValidationPasses.ValidateCrossFieldValidators(root, _strict)); // Pass 9: template @payloadRef / @requiredSlots resolution errors.AddRange(ValidationPasses.ValidateTemplatePayloadRefs(root)); diff --git a/server/csharp/MetaObjects/Loader/ValidationPasses.cs b/server/csharp/MetaObjects/Loader/ValidationPasses.cs index 57214fa874..7d720f0d58 100644 --- a/server/csharp/MetaObjects/Loader/ValidationPasses.cs +++ b/server/csharp/MetaObjects/Loader/ValidationPasses.cs @@ -835,6 +835,25 @@ public static IReadOnlyList ValidateOriginPaths(MetaData root) ErrorCode.ERR_INVALID_ORIGIN, Envelope: src)); continue; } + // A literal compared with a field must be able to equal it: of its kind, and for + // an enum one of its @values (the rule a validator's @condition follows). + // Resolved as the inference resolved the ref: a projection field first, else a base field. + var literalProblem = LiteralComparisonProblem(expr, name => + { + if (!ReferenceEquals(obj, computedBase) && name != field.Name + && obj.Children().FirstOrDefault(f => f.Type == TYPE_FIELD && f.Name == name) is MetaField own) + { + return ownOriginOf(name)?.SubType == ORIGIN_SUBTYPE_AGGREGATE ? null : own; + } + return computedBase!.Children().FirstOrDefault(f => f.Type == TYPE_FIELD && f.Name == name) as MetaField; + }); + if (literalProblem is not null) + { + errors.Add(new MetaError( + $"origin.computed on {obj.Name}.{field.Name}: @expr {literalProblem}", + ErrorCode.ERR_INVALID_ORIGIN, Envelope: src)); + continue; + } if (inferred.Type is not null && inferred.Type != field.SubType) errors.Add(new MetaError( $"origin.computed on {obj.Name}.{field.Name}: @expr infers field.{inferred.Type} but the field is declared field.{field.SubType} — a computed column's type is derived from its expression and must match (no @convert escape).", @@ -1121,7 +1140,8 @@ private static bool IsReferenceHop(MetaData hop) return null; } - var sourceField = FindField(sourceObj, targetFieldName); + // A member of a flattened value object (`Plan.address.city`) is a column of the owning table. + var sourceField = FindField(sourceObj, targetFieldName) ?? FlattenedMembers.Resolve(sourceObj, targetFieldName)?.Member; if (sourceField is null) { // FR5d — entity resolved, field on it did not. target = full ref. @@ -1162,7 +1182,7 @@ private static bool IsReferenceHop(MetaData hop) /// entity they terminate at. Returned as a pair (rather than re-walking for the terminal) /// because a second walk means a second copy of the ADR-0042 package-resolution rule — /// mirrors the TS WalkedViaPath. - private readonly record struct WalkedViaPath(List Hops, MetaData Terminal, IReadOnlySet Reverse); + private readonly record struct WalkedViaPath(List Hops, MetaData Terminal, IReadOnlyDictionary Reverse); private static WalkedViaPath? ValidateViaPath( string viaAttr, @@ -1209,7 +1229,7 @@ private static bool IsReferenceHop(MetaData hop) // entity name; each successful relationship hop appends a segment. var validSegments = new List { entityName }; var hops = new List(); - var reverse = new HashSet(ReferenceEqualityComparer.Instance); + var reverse = new Dictionary(ReferenceEqualityComparer.Instance); foreach (var relName in relSegments) { @@ -1228,7 +1248,7 @@ private static bool IsReferenceHop(MetaData hop) { validSegments.Add(relName); hops.Add(reversed[0].Reference); - reverse.Add(reversed[0].Reference); + reverse[reversed[0].Reference] = relName; currentObj = reversed[0].Holder; continue; } @@ -1299,8 +1319,8 @@ private static bool IsReferenceHop(MetaData hop) /// A hop's effective @cardinality, or null when not declared. A reference hop /// (a forward FK) is inherently to-one — a child names the parent it points at. - private static string? HopCardinality(MetaData rel, IReadOnlySet? reverse = null) - => reverse is not null && reverse.Contains(rel) + private static string? HopCardinality(MetaData rel, IReadOnlyDictionary? reverse = null) + => reverse is not null && reverse.ContainsKey(rel) ? CARDINALITY_MANY // A reference walked backwards reaches every row that references the walked-from one : IsReferenceHop(rel) ? CARDINALITY_ONE @@ -1363,6 +1383,10 @@ private static bool IsReferenceHop(MetaData hop) } } if (targets.Count == 1) return targets.First(); + // A projection that extends another projection, and anchors nothing of its own, has + // the base entity its super has. + if (targets.Count == 0 && obj.SuperData is { Type: TYPE_OBJECT, SubType: OBJECT_SUBTYPE_PROJECTION } superProjection) + return DeriveBaseEntity(superProjection, root, fieldName, originSource, errors); if (targets.Count > 1) { string names = string.Join(", ", targets.Select(t => $"\"{t.Name}\"")); @@ -1431,15 +1455,15 @@ private static bool IsBaseRelationTarget(MetaData target, MetaData baseEntity, M /// EVERY hop. A hop is to-many only when it DECLARES @cardinality "many". private static void CheckPassthroughCardinality( IReadOnlyList hops, MetaData obj, string fieldName, ErrorSource originSource, List errors, - IReadOnlySet? reverse = null) + IReadOnlyDictionary? reverse = null) { foreach (var rel in hops) { if (HopCardinality(rel, reverse) == CARDINALITY_MANY) { errors.Add(new MetaError( - $"origin.passthrough on {obj.Name}.{fieldName}: @via hop \"{rel.Name}\" is to-many " + - (reverse?.Contains(rel) == true + $"origin.passthrough on {obj.Name}.{fieldName}: @via hop \"{(reverse is not null && reverse.TryGetValue(rel, out var hop) ? hop : rel.Name)}\" is to-many " + + (reverse?.ContainsKey(rel) == true ? $"(reverse hop over identity.reference \"{rel.Name}\")" : $"(@cardinality \"{CARDINALITY_MANY}\")") + " — a row-multiplying passthrough — you meant aggregate (ADR-0029).", @@ -1455,7 +1479,7 @@ private static void CheckPassthroughCardinality( /// (every hop declares @cardinality "one"). private static void CheckAggregateCardinality( IReadOnlyList hops, MetaData obj, string fieldName, ErrorSource originSource, List errors, - IReadOnlySet? reverse = null) + IReadOnlyDictionary? reverse = null) { if (hops.Count == 0) return; bool provablyToOne = hops.All(rel => HopCardinality(rel, reverse) == CARDINALITY_ONE); @@ -1637,6 +1661,141 @@ private static bool ExtendsChainReaches(MetaData node, MetaData target) return false; } + + // ========================================================================= + // The entity fields projections REACH without outputting them (a + // @filter key naming a base field or a to-one path, a passthrough @from). Ported from + // validation-passes.ts projectionReachedEntityFields. + // ========================================================================= + + private static HashSet ProjectionReachedEntityFields(MetaData root) + { + var reached = new HashSet(ReferenceEqualityComparer.Instance); + foreach (var obj in root.OwnChildren().Where(c => c.Type == TYPE_OBJECT && c.SubType == OBJECT_SUBTYPE_PROJECTION)) + { + var silent = new List(); + var baseEntity = DeriveBaseEntity(obj, root, "@filter", obj.Source, silent); + if (baseEntity is MetaObject baseObject && !ReferenceEquals(baseEntity, obj) + // The effective filter — an extender reaches what its supers' filters name. + && ProjectionFilters.Effective(obj) is IReadOnlyDictionary filter) + { + foreach (var key in FilterKeys(filter)) + { + var path = FilterPaths.Resolve(baseObject, key, out var why); + if (path is not null) reached.Add(path.Field); + else if (why is null && baseObject.Children().FirstOrDefault(c => c.Type == TYPE_FIELD && c.Name == key) is { } f) + reached.Add(f); + } + } + foreach (var field in obj.Children().Where(c => c.Type == TYPE_FIELD)) + foreach (var origin in field.OwnChildren().Where(c => c.Type == TYPE_ORIGIN && c.SubType == ORIGIN_SUBTYPE_PASSTHROUGH)) + if (origin.OwnAttr(ORIGIN_PASSTHROUGH_ATTR_FROM) is string from + && ValidateFromPath(from, root, obj, field.Name, silent, "origin.passthrough.@from", origin.Source) is { } target) + reached.Add(target.Field); + } + return reached; + } + + // ========================================================================= + // Origin segments and correlated filters, on every origin.first / origin.aggregate: + // @segment names a segment of the related entity (ERR_INVALID_ORIGIN); an @filter operand + // { field: "" } names a base-row field with a column-comparing op, and the null-safe + // ops take only such an operand (ERR_BAD_ATTR_FILTER). Ported from validation-passes.ts + // validateOriginSegmentsAndCorrelation. + // ========================================================================= + + private static readonly string[] CorrelatedFilterOps = + ["eq", "ne", "gt", "gte", "lt", "lte", FILTER_OP_IS_DISTINCT_FROM, FILTER_OP_IS_NOT_DISTINCT_FROM]; + + public static IReadOnlyList ValidateOriginSegmentsAndCorrelation(MetaData root) + { + var errors = new List(); + foreach (var obj in root.OwnChildren().Where(c => c.Type == TYPE_OBJECT + && (c.SubType == OBJECT_SUBTYPE_PROJECTION || c.SubType == OBJECT_SUBTYPE_ENTITY))) + { + foreach (var field in obj.OwnChildren().Where(c => c.Type == TYPE_FIELD)) + { + foreach (var origin in field.OwnChildren().Where(c => c.Type == TYPE_ORIGIN + && (c.SubType == ORIGIN_SUBTYPE_FIRST || c.SubType == ORIGIN_SUBTYPE_AGGREGATE))) + { + var where = $"origin.{origin.SubType} on {obj.Name}.{field.Name}"; + if (origin.OwnAttr(ORIGIN_ATTR_SEGMENT) is string segment + && OriginRelatedEntity(origin, obj, root) is { } related + && !related.Children().Any(c => c.Type == TYPE_SEGMENT && c.Name == segment)) + { + errors.Add(new MetaError( + $"{where}: @segment \"{segment}\" is not a segment declared on \"{related.Name}\", the entity whose rows it scopes.", + ErrorCode.ERR_INVALID_ORIGIN, Envelope: origin.Source)); + } + if (origin.OwnAttr(ORIGIN_FIRST_ATTR_FILTER) is not IReadOnlyDictionary filter) continue; + var silent = new List(); + if (DeriveBaseEntity(obj, root, field.Name, origin.Source, silent) is not { } baseEntity) continue; + CheckCorrelatedOperands(filter, baseEntity, where, origin.Source, errors); + } + } + } + return errors.AsReadOnly(); + } + + private static MetaData? OriginRelatedEntity(MetaData origin, MetaData obj, MetaData root) + { + if (origin.OwnAttr(ORIGIN_FIRST_ATTR_OF) is string of && of.Length > 0) + return ValidateFromPath(of, root, obj, origin.Parent?.Name ?? "", [], "", origin.Source)?.Entity; + if (origin.OwnAttr(ORIGIN_AGGREGATE_ATTR_VIA) is not string via) return null; + var pkg = NamingRefs.EffectivePackage(obj); + var segments = via.Split('.'); + var current = NamingRefs.ResolveObjectRef(root, segments[0], pkg); + foreach (var hop in segments.Skip(1)) + { + if (current is null) return null; + var rel = current.Children().FirstOrDefault(c => (c.Type == TYPE_RELATIONSHIP || c.Type == TYPE_IDENTITY) && c.Name == hop); + current = rel is not null + ? NamingRefs.RefTarget(rel, rel.Type == TYPE_RELATIONSHIP ? RELATIONSHIP_ATTR_OBJECT_REF : IDENTITY_REFERENCE_ATTR_REFERENCES) + : current is MetaObject target && root is MetaRoot r + ? ReverseReferenceHops.Find(target, hop, r.Objects()).FirstOrDefault()?.Holder + : null; + } + return current; + } + + private static void CheckCorrelatedOperands( + IReadOnlyDictionary filter, MetaData baseEntity, string where, ErrorSource source, List errors) + { + foreach (var (key, clause) in filter) + { + if (key == FILTER_COMPOSE_AND || key == FILTER_COMPOSE_OR) + { + if (clause is IReadOnlyList subs) + foreach (var sub in subs) + if (sub is IReadOnlyDictionary subFilter) + CheckCorrelatedOperands(subFilter, baseEntity, where, source, errors); + continue; + } + if (clause is not IReadOnlyDictionary ops) continue; + foreach (var (op, value) in ops) + { + var nullSafe = op == FILTER_OP_IS_DISTINCT_FROM || op == FILTER_OP_IS_NOT_DISTINCT_FROM; + if (value is not IReadOnlyDictionary operand || !operand.ContainsKey(FILTER_OPERAND_FIELD)) + { + if (nullSafe) + errors.Add(new MetaError( + $"{where}: @filter on \"{key}\" uses \"{op}\", which compares with a base-row field: write {{ {op}: {{ {FILTER_OPERAND_FIELD}: \"\" }} }}.", + ErrorCode.ERR_BAD_ATTR_FILTER, Envelope: source)); + continue; + } + var name = operand[FILTER_OPERAND_FIELD] as string; + if (!CorrelatedFilterOps.Contains(op)) + errors.Add(new MetaError( + $"{where}: @filter on \"{key}\" compares with a base-row field using \"{op}\"; a correlated operand takes {string.Join(", ", CorrelatedFilterOps)}.", + ErrorCode.ERR_BAD_ATTR_FILTER, Envelope: source)); + else if (name is null || !baseEntity.Children().Any(c => c.Type == TYPE_FIELD && c.Name == name)) + errors.Add(new MetaError( + $"{where}: @filter on \"{key}\" names base-row field \"{name}\", which \"{baseEntity.Name}\" does not declare.", + ErrorCode.ERR_BAD_ATTR_FILTER, Envelope: source)); + } + } + } + // ========================================================================= // FR-024 B6 — derived-field providability (spec §7 population doctrine). // An object.entity field carrying any origin.* is derived (read-only): it @@ -1648,6 +1807,7 @@ private static bool ExtendsChainReaches(MetaData node, MetaData target) public static IReadOnlyList ValidateDerivedFieldProvidability(MetaData root) { var errors = new List(); + var reached = ProjectionReachedEntityFields(root); foreach (var obj in root.OwnChildren() .Where(c => c.Type == TYPE_OBJECT && c.SubType == OBJECT_SUBTYPE_ENTITY)) { @@ -1658,11 +1818,17 @@ public static IReadOnlyList ValidateDerivedFieldProvidability(MetaDat if (hasReadCapableSource) continue; foreach (var field in obj.OwnChildren().Where(c => c.Type == TYPE_FIELD)) { - if (!field.OwnChildren().Any(c => c.Type == TYPE_ORIGIN)) continue; + var origin = field.OwnChildren().FirstOrDefault(c => c.Type == TYPE_ORIGIN); + if (origin is null) continue; + // A derived field a projection reaches (its @filter or a passthrough + // @from) is lowered inline there, so it needs no read source. Only the inlinable origins. + if ((origin.SubType == ORIGIN_SUBTYPE_FIRST || origin.SubType == ORIGIN_SUBTYPE_COMPUTED) && reached.Contains(field)) + continue; errors.Add(new MetaError( $"derived field \"{obj.Name}.{field.Name}\" carries an origin.* but entity \"{obj.Name}\" declares no " + "read-capable source — derived fields do not exist on the writable table. Declare a read-only source " + - "(e.g. source.rdb @kind \"view\" @role \"replica\") to provide it, or move the field to an object.projection (FR-024 §7).", + "(e.g. source.rdb @kind \"view\" @role \"replica\") to provide it, move the field to an object.projection (FR-024 §7), " + + "or reach it from a projection's @filter or @from (an origin.first or origin.computed is then inlined there).", ErrorCode.ERR_DERIVED_FIELD_NO_READ_SOURCE, Envelope: field.Source)); } @@ -1860,8 +2026,9 @@ private static void CheckFilterClauses( // hardening, NOT gated cross-port — intentionally omitted here (the // compose recursion skips non-array/non-object shapes silently). // - // Own-attrs / own-children only: the @filter is declared locally on the - // projection, and origin.* never inherits (ADR-0029 / ADR-0039). + // Effective, not own: the @filter is every level of the extends chain + // ANDed (ProjectionFilters.Effective), and the fields are the projection's resolved + // ones; each field's origin.* is read own (it never inherits, ADR-0029 / ADR-0039). // // Ported from typescript/packages/metadata/src/loader/validation-passes.ts // validateProjectionFilter + checkProjectionFilterRefs. @@ -1874,21 +2041,23 @@ public static IReadOnlyList ValidateProjectionFilter(MetaData root) foreach (var obj in root.OwnChildren() .Where(c => c.Type == TYPE_OBJECT && c.SubType == OBJECT_SUBTYPE_PROJECTION)) { - // ADR-0039: own — the @filter is declared locally on this projection. + // The EFFECTIVE @filter — every level of an extends chain, supers first, ANDed — + // is what the view applies, so it is what is checked. // Non-object shapes are rejected by the attr-schema check (FilterAttr). - if (obj.OwnAttr(OBJECT_PROJECTION_ATTR_FILTER) + if (ProjectionFilters.Effective(obj) is not IReadOnlyDictionary filter) { continue; } - // Classify the projection's OWN fields (the declared set IS the exposure — + // Classify the projection's EFFECTIVE fields (the declared set IS the exposure — // FR-024 / ADR-0028): a field is aggregate-derived when it carries an OWN - // origin child whose subType is OTHER than passthrough/computed. origin.* - // never inherits (ADR-0029), so the origin read is own. + // origin child whose subType is OTHER than passthrough/computed. ADR-0039: + // resolving — a field inherited from a super projection is a column of this view + // and carries its own origin child, read own below. var declared = new HashSet(StringComparer.Ordinal); var aggregateDerived = new HashSet(StringComparer.Ordinal); - foreach (var f in obj.OwnChildren().Where(c => c.Type == TYPE_FIELD)) + foreach (var f in obj.Children().Where(c => c.Type == TYPE_FIELD)) { declared.Add(f.Name); var origin = f.OwnChildren().FirstOrDefault(c => c.Type == TYPE_ORIGIN); @@ -1900,12 +2069,68 @@ public static IReadOnlyList ValidateProjectionFilter(MetaData root) } } + // A field of the BASE entity the view does not output is filterable too (a WHERE + // reads the base row); a projection field of the same name wins. + if (DeriveBaseEntity(obj, root, "@filter", obj.Source, []) is { } baseEntity && !ReferenceEquals(baseEntity, obj)) + { + foreach (var f in baseEntity.Children().Where(c => c.Type == TYPE_FIELD)) + { + if (!declared.Add(f.Name)) continue; + if (EntityFieldAggregates(f)) aggregateDerived.Add(f.Name); + } + // A key that is a PATH of to-one hops from the base, + // then a field of the entity reached (`caseId.status`). Not output; the view joins it. + if (baseEntity is MetaObject baseObject) + { + foreach (var key in FilterKeys(filter)) + { + if (declared.Contains(key)) continue; + var path = FilterPaths.Resolve(baseObject, key, out var why); + if (path is null && why is null) continue; + declared.Add(key); + if (path is null) + { + errors.Add(new MetaError( + $"projection \"{obj.Name}\" @filter path \"{key}\" does not resolve: {why}.", + ErrorCode.ERR_BAD_ATTR_FILTER, Envelope: obj.Source)); + continue; + } + if (EntityFieldAggregates(path.Field)) aggregateDerived.Add(key); + } + } + } + CheckProjectionFilterRefs(filter, declared, aggregateDerived, obj.Name, obj.Source, errors); } return errors.AsReadOnly(); } + /// + /// An entity field a projection @filter reaches (on the base, or through a path) cannot be filtered + /// when it is an aggregate (a WHERE runs before grouping); its column, an origin.computed or an + /// origin.first (a correlated scalar subquery) can (entity-derived). origin.* never inherits. + /// + private static bool EntityFieldAggregates(MetaData f) => + f.OwnChildren().FirstOrDefault(c => c.Type == TYPE_ORIGIN) is { SubType: ORIGIN_SUBTYPE_AGGREGATE }; + + /// Every field key of a filter, through and/or. + private static IEnumerable FilterKeys(IReadOnlyDictionary filter) + { + foreach (var (key, clause) in filter) + { + if (key == FILTER_COMPOSE_OR || key == FILTER_COMPOSE_AND) + { + if (clause is IReadOnlyList subs) + foreach (var sub in subs) + if (sub is IReadOnlyDictionary subFilter) + foreach (var k in FilterKeys(subFilter)) yield return k; + continue; + } + yield return key; + } + } + private static void CheckProjectionFilterRefs( IReadOnlyDictionary filter, HashSet declared, @@ -1938,8 +2163,7 @@ private static void CheckProjectionFilterRefs( { errors.Add(new MetaError( $"projection \"{projectionName}\" @filter references \"{key}\", which is not a declared " + - "field of the projection. A view-level @filter may only reference the projection's own " + - "declared fields.", + "field of the projection or of its base entity. A view-level @filter may only reference those fields.", ErrorCode.ERR_BAD_ATTR_FILTER, Envelope: source)); continue; @@ -2208,7 +2432,9 @@ private static void ValidateAttrSchemaNode( VALIDATOR_SUBTYPE_PRESENT_IFF, VALIDATOR_SUBTYPE_AT_LEAST_ONE, VALIDATOR_SUBTYPE_EXACTLY_ONE, }; - public static IReadOnlyList ValidateCrossFieldValidators(MetaData root) + /// A strict load already refuses an atLeastOne @max as an unregistered attr + /// while parsing, so the check here runs only on the default load, keeping one error per node. + public static IReadOnlyList ValidateCrossFieldValidators(MetaData root, bool strict = false) { var errors = new List(); foreach (var obj in root.Children().OfType()) @@ -2216,6 +2442,20 @@ public static IReadOnlyList ValidateCrossFieldValidators(MetaData roo var fieldNames = obj.Fields().Select(f => f.Name).ToHashSet(StringComparer.Ordinal); foreach (var v in obj.OwnChildren().Where(c => c.Type == TYPE_VALIDATOR && CrossFieldValidatorSubtypes.Contains(c.SubType))) { + // validator.atLeastOne takes no @max: "exactly one of these fields" is + // validator.exactlyOne. A strict load already refuses the unregistered attr; this + // refuses it under the default load too, where it would be kept and ignored. + // (A strict load has already reported it while parsing.) + // ADR-0039: own — checked on the validator that declares it. + if (!strict && v.SubType == VALIDATOR_SUBTYPE_AT_LEAST_ONE && v.OwnAttr(VALIDATOR_ATTR_MAX) is not null) + { + errors.Add(new MetaError( + $"validator.atLeastOne \"{obj.Name}.{v.Name}\" carries @max, which atLeastOne does not take. " + + "For \"exactly one of these fields\", declare validator.exactlyOne instead; for \"at least one\", remove @max.", + ErrorCode.ERR_UNKNOWN_ATTR, + Envelope: v.Source)); + continue; + } if (v.SubType == VALIDATOR_SUBTYPE_EXACTLY_ONE) { var fields = v.Attr(VALIDATOR_ATTR_FIELDS) switch @@ -2315,66 +2555,77 @@ public static IReadOnlyList ValidateCrossFieldValidators(MetaData roo { return $"must be a boolean expression (a comparison, a null test, or and / or / not of them), not a field.{inferred.Type ?? "undefined"}."; } - string? problem = null; - void Visit(object? node) + foreach (var n in ExpressionGrammar.Nodes(condition)) { - if (problem is not null || node is not IReadOnlyDictionary n) return; - if (n.TryGetValue("left", out var left) && left is not null && n.TryGetValue("right", out var right) && right is not null) + if (n.TryGetValue("left", out var left) && left is not null && n.TryGetValue("right", out var right) && right is not null + && new[] { left, right }.Any(side => side is IReadOnlyDictionary lit && lit.TryGetValue("value", out var v) && v is null)) { - foreach (var (a, b) in new[] { (left, right), (right, left) }) + return "compares with null, which is never true: use isNull or isNotNull."; + } + } + return LiteralComparisonProblem(condition, name => fields.TryGetValue(name, out var f) ? f : null); + } + + /// + /// The first literal in that cannot equal the field it is compared + /// with, or null. A literal is compared with a field by a comparison (either side) or as a + /// simple case's when against a {field} operand. The rule, the same in every port + /// and shared by a validator's @condition and an origin.computed @expr: the literal is of the + /// field's kind (a boolean for field.boolean, a number for a numeric field, a string otherwise), + /// one of its @values for field.enum, and an ISO-8601 duration for field.duration. A null + /// literal, and a literal facing anything but a field ref, are not checked here. Mirrors TS + /// literalComparisonProblem. + /// + internal static string? LiteralComparisonProblem(object? expr, Func fieldOf) + { + string? Check(object? a, object? b) + { + if (a is not IReadOnlyDictionary lit || !lit.TryGetValue("value", out var value) || value is null) return null; + if (b is not IReadOnlyDictionary refNode + || !refNode.TryGetValue("field", out var refName) || refName is not string name + || fieldOf(name) is not { } field) + { + return null; + } + var kind = field.SubType == FIELD_SUBTYPE_BOOLEAN ? "boolean" + : ConditionNumberSubtypes.Contains(field.SubType) ? "number" : "string"; + var actual = value switch + { + bool => "boolean", + long or int or double or decimal => "number", + string => "string", + _ => null, + }; + if (actual != kind) + return $"compares \"{field.Name}\" (field.{field.SubType}) with {JsonStringify(value)}; it needs a {kind}."; + var members = field.SubType == FIELD_SUBTYPE_ENUM ? field.EffectiveEnumValues : null; + if (members is not null && !members.Contains(Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture), StringComparer.Ordinal)) + return $"compares \"{field.Name}\" with {JsonStringify(value)}, which is not one of its @values ({string.Join(", ", members)})."; + if (field.SubType == FIELD_SUBTYPE_DURATION && !DurationLiteral.IsMatch((string)value)) + return $"compares \"{field.Name}\" with {JsonStringify(value)}, which is not {DurationLiteralForm}."; + return null; + } + foreach (var n in ExpressionGrammar.Nodes(expr)) + { + if (n.TryGetValue("left", out var left) && left is not null && n.TryGetValue("right", out var right) && right is not null + && (Check(left, right) ?? Check(right, left)) is { } problem) + { + return problem; + } + if (n.TryGetValue(ExpressionGrammar.EXPR_CASE_OPERAND, out var operand) && operand is not null + && n.TryGetValue(ExpressionGrammar.EXPR_CASE, out var arms) && arms is IEnumerable armList) + { + foreach (var arm in armList) { - if (a is not IReadOnlyDictionary lit || !lit.TryGetValue("value", out var value)) continue; - if (value is null) - { - problem = "compares with null, which is never true: use isNull or isNotNull."; - return; - } - if (b is not IReadOnlyDictionary refNode - || !refNode.TryGetValue("field", out var refName) || refName is not string name - || !fields.TryGetValue(name, out var field)) - { - continue; - } - var kind = field.SubType == FIELD_SUBTYPE_BOOLEAN ? "boolean" - : ConditionNumberSubtypes.Contains(field.SubType) ? "number" : "string"; - var actual = value switch - { - bool => "boolean", - long or int or double => "number", - string => "string", - _ => null, - }; - if (actual != kind) - { - problem = $"compares \"{field.Name}\" (field.{field.SubType}) with {JsonStringify(value)}; it needs a {kind}."; - return; - } - var members = field.SubType == FIELD_SUBTYPE_ENUM ? field.EffectiveEnumValues : null; - if (members is not null && !members.Contains(Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture), StringComparer.Ordinal)) - { - problem = $"compares \"{field.Name}\" with {JsonStringify(value)}, which is not one of its @values ({string.Join(", ", members)})."; - return; - } - if (field.SubType == FIELD_SUBTYPE_DURATION && !DurationLiteral.IsMatch((string)value)) + if (arm is IReadOnlyDictionary a && a.TryGetValue(ExpressionGrammar.EXPR_CASE_WHEN, out var when) + && Check(when, operand) is { } armProblem) { - problem = $"compares \"{field.Name}\" with {JsonStringify(value)}, which is not {DurationLiteralForm}."; - return; + return armProblem; } } } - n.TryGetValue("left", out var l); - Visit(l); - n.TryGetValue("right", out var r); - Visit(r); - n.TryGetValue("arg", out var g); - Visit(g); - if (n.TryGetValue("args", out var args) && args is IEnumerable list) - { - foreach (var x in list) Visit(x); - } } - Visit(condition); - return problem; + return null; } // --------------------------------------------------------------------------- @@ -2895,9 +3146,9 @@ public static SourceEscapeValidationResult ValidateSourceEscapes(MetaData root) // WHERE), so wrapping it is deferred cleverness (design doc D5) — reject. if (hasSqlHost && obj.SubType == OBJECT_SUBTYPE_PROJECTION) { - // ADR-0039: own — the @filter is declared locally on this projection - // (mirrors ValidateProjectionFilter). - var filter = obj.OwnAttr(OBJECT_PROJECTION_ATTR_FILTER); + // The effective @filter (mirrors ValidateProjectionFilter) — an inherited row + // scope cannot be applied to an author-owned body either, so it is refused. + var filter = ProjectionFilters.Effective(obj); if (filter is not null) { errors.Add(new MetaError( diff --git a/server/csharp/MetaObjects/Meta/CollectionNames.cs b/server/csharp/MetaObjects/Meta/CollectionNames.cs index 843914d43b..6ab5928f55 100644 --- a/server/csharp/MetaObjects/Meta/CollectionNames.cs +++ b/server/csharp/MetaObjects/Meta/CollectionNames.cs @@ -26,6 +26,33 @@ public static string Of(MetaObject obj) => // ADR-0039: own — a collection name is the declaring object's alone (see Of). obj.OwnAttr(OBJECT_ATTR_COLLECTION_NAME) is string { Length: > 0 } own ? own : null; + /// + /// The REST collection segment is served under + /// (fixtures/naming-conformance/collection-segments.json, shared with TypeScript): its + /// declared @collectionName, else the configured pluralizer's output over its name, each + /// snake_cased by ; with neither, + /// over the name, so an unconfigured project's segments never move. + /// + public static string Segment(MetaObject obj, Func unconfigured) + { + if (Declared(obj) is { } declared) return SnakeCollectionName(declared); + if (ConfiguredPluralizer(obj) is { } configured) return SnakeCollectionName(NamingStrategies.Pluralizer(configured)(obj.Name)); + return unconfigured(obj.Name); + } + + /// + /// snake_case a collection name with an acronym-aware split: a trailing plural s / es + /// after a capital is a suffix of the acronym, not a word of its own (FAQs → faqs, + /// TaxIDs → tax_ids, PDFBoxes → pdf_boxes). + /// + public static string SnakeCollectionName(string s) + { + var m = System.Text.RegularExpressions.Regex.Match(s, "^(.*[A-Z])(e?s)$"); + return m.Success + ? Persistence.Source.SourceNaming.ToSnakeCase(m.Groups[1].Value) + m.Groups[2].Value + : Persistence.Source.SourceNaming.ToSnakeCase(s); + } + /// /// The standard pluralizer, the rule every port shares (fixtures/naming-conformance): /// an already-plural name stays, else the suffix rule. diff --git a/server/csharp/MetaObjects/Meta/EffectiveProjectionFilter.cs b/server/csharp/MetaObjects/Meta/EffectiveProjectionFilter.cs new file mode 100644 index 0000000000..c14f000fe5 --- /dev/null +++ b/server/csharp/MetaObjects/Meta/EffectiveProjectionFilter.cs @@ -0,0 +1,41 @@ +// A projection's EFFECTIVE row scope. `@filter` composes down an `extends` chain +// rather than overriding: a projection that extends a filtered projection keeps the base's row +// scope (a consent gate on an abstract base must survive in every extender). Every level's own +// `@filter`, root-most first, ANDed. Ported from +// typescript/packages/metadata/src/core/object/effective-projection-filter.ts. + +using static MetaObjects.Core.Object.ObjectConstants; +using static MetaObjects.Core.Query.QueryConstants; + +namespace MetaObjects.Meta; + +public static class ProjectionFilters +{ + /// + /// The @filter a projection's view applies: its own and every super's, root-most + /// first. One declaration is returned as declared; two or more are ANDed in one flat + /// { and: [...] }. Null when no level declares one. + /// + public static object? Effective(MetaData projection) + { + var levels = new List(); + var seen = new HashSet(ReferenceEqualityComparer.Instance); + for (var node = projection; node is not null && seen.Add(node); node = node.SuperData) + { + // ADR-0039: own — each level contributes the @filter declared on it; the resolving + // read would return only the nearest one, which is the override this replaces. + if (node.OwnAttr(OBJECT_PROJECTION_ATTR_FILTER) is { } own) levels.Insert(0, own); + } + if (levels.Count <= 1) return levels.Count == 0 ? null : levels[0]; + var members = new List(); + foreach (var level in levels) members.AddRange(AndMembers(level)); + return new Dictionary { [FILTER_COMPOSE_AND] = members }; + } + + /// A level that is itself a bare { and: [...] } contributes its members. + private static IEnumerable AndMembers(object? filter) => + filter is IReadOnlyDictionary { Count: 1 } map + && map.TryGetValue(FILTER_COMPOSE_AND, out var subs) && subs is IReadOnlyList list + ? list + : [filter]; +} diff --git a/server/csharp/MetaObjects/Meta/FilterPath.cs b/server/csharp/MetaObjects/Meta/FilterPath.cs new file mode 100644 index 0000000000..f482d4f68b --- /dev/null +++ b/server/csharp/MetaObjects/Meta/FilterPath.cs @@ -0,0 +1,59 @@ +// A projection @filter key may be a PATH: to-one hops from +// the base entity, then a field of the entity they reach (`caseId.status`). A hop is an +// identity.reference or a relationship with @cardinality one; a to-many hop is refused. Ported +// from typescript/packages/metadata/src/core/relationship/filter-path.ts. + +using static MetaObjects.Core.Identity.IdentityConstants; +using static MetaObjects.Core.Relationship.RelationshipConstants; +using static MetaObjects.Shared.BaseTypes; + +namespace MetaObjects.Meta; + +/// A resolved filter path: the hop names walked, the entity reached, and its field. +public sealed record FilterPath(IReadOnlyList Hops, MetaObject Entity, MetaData Field); + +public static class FilterPaths +{ + /// + /// Resolve a dotted filter key against . Null when the key has no + /// dot; otherwise the path, or the reason it does not resolve in . + /// + public static FilterPath? Resolve(MetaObject baseEntity, string key, out string? error) + { + error = null; + var segments = key.Split('.'); + if (segments.Length < 2) return null; + var current = baseEntity; + foreach (var hop in segments[..^1]) + { + var reference = current.Children().FirstOrDefault(c => + c.Type == TYPE_IDENTITY && c.SubType == IDENTITY_SUBTYPE_REFERENCE && c.Name == hop); + MetaObject? next; + if (reference is not null) + { + next = NamingRefs.RefTarget(reference, IDENTITY_REFERENCE_ATTR_REFERENCES) as MetaObject; + if (next is null) { error = $"reference \"{current.Name}.{hop}\" does not resolve"; return null; } + } + else if (current.Children().FirstOrDefault(c => c.Type == TYPE_RELATIONSHIP && c.Name == hop) is { } rel) + { + if (rel.Attr(RELATIONSHIP_ATTR_CARDINALITY) as string != CARDINALITY_ONE) + { + error = $"\"{current.Name}.{hop}\" is to-many; a filter path may only walk to-one hops"; + return null; + } + next = NamingRefs.RefTarget(rel, RELATIONSHIP_ATTR_OBJECT_REF) as MetaObject; + if (next is null) { error = $"relationship \"{current.Name}.{hop}\" does not resolve"; return null; } + } + else + { + error = $"\"{current.Name}\" declares no identity.reference or to-one relationship named \"{hop}\""; + return null; + } + current = next; + } + var fieldName = segments[^1]; + var field = current.Children().FirstOrDefault(c => c.Type == TYPE_FIELD && c.Name == fieldName); + if (field is null) { error = $"\"{current.Name}\" declares no field \"{fieldName}\""; return null; } + return new FilterPath(segments[..^1], current, field); + } +} diff --git a/server/csharp/MetaObjects/Meta/FlattenedMember.cs b/server/csharp/MetaObjects/Meta/FlattenedMember.cs new file mode 100644 index 0000000000..9821185cd4 --- /dev/null +++ b/server/csharp/MetaObjects/Meta/FlattenedMember.cs @@ -0,0 +1,33 @@ +// A path into a member of a FLATTENED value object (`address.city`): the +// member's column lives on the owning table under the embedding field's prefix. Ported from +// typescript/packages/metadata/src/core/field/flattened-member.ts. + +using static MetaObjects.Core.Field.FieldConstants; +using static MetaObjects.Shared.BaseTypes; + +namespace MetaObjects.Meta; + +/// A flattened member path, resolved: the embedding field on the entity and the member on its value object. +public sealed record FlattenedMember(MetaData Embedding, MetaData Member); + +public static class FlattenedMembers +{ + /// + /// Resolve <field>.<member> against : a field.object with + /// @storage: flattened, and a field its value object declares. Null for anything else. One level. + /// + public static FlattenedMember? Resolve(MetaData entity, string path) + { + var dot = path.IndexOf('.'); + if (dot < 1 || dot == path.Length - 1 || path.IndexOf('.', dot + 1) >= 0) return null; + var head = path[..dot]; + var tail = path[(dot + 1)..]; + var embedding = entity.Children().FirstOrDefault(c => c.Type == TYPE_FIELD && c.Name == head); + if (embedding is null || embedding.SubType != FIELD_SUBTYPE_OBJECT) return null; + // ADR-0039: resolving — @storage / @objectRef may be inherited via extends. + if (embedding.Attr(FIELD_ATTR_STORAGE) as string != STORAGE_FLATTENED) return null; + if (NamingRefs.RefTarget(embedding, FIELD_ATTR_OBJECT_REF) is not { } vo) return null; + var member = vo.Children().FirstOrDefault(c => c.Type == TYPE_FIELD && c.Name == tail); + return member is null ? null : new FlattenedMember(embedding, member); + } +} diff --git a/server/csharp/MetaObjects/Persistence/Origin/OriginConstants.cs b/server/csharp/MetaObjects/Persistence/Origin/OriginConstants.cs index 42bb13f82a..f3b0824a70 100644 --- a/server/csharp/MetaObjects/Persistence/Origin/OriginConstants.cs +++ b/server/csharp/MetaObjects/Persistence/Origin/OriginConstants.cs @@ -114,4 +114,12 @@ public static class OriginConstants public static readonly string[] ORIGIN_AGG_VALUES = [AGGREGATE_FN_COUNT, AGGREGATE_FN_SUM, AGGREGATE_FN_AVG, AGGREGATE_FN_MIN, AGGREGATE_FN_MAX, AGG_ANY, AGG_ALL, AGG_COLLECT]; + + /// A segment.filter of the related entity scoping the related rows. + public const string ORIGIN_ATTR_SEGMENT = "segment"; + /// The null-safe comparisons with a base-row field operand. + public const string FILTER_OP_IS_DISTINCT_FROM = "isDistinctFrom"; + public const string FILTER_OP_IS_NOT_DISTINCT_FROM = "isNotDistinctFrom"; + /// The key of a correlated operand: { field: "<base field>" }. + public const string FILTER_OPERAND_FIELD = "field"; } diff --git a/server/csharp/MetaObjects/Persistence/Origin/OriginSchema.cs b/server/csharp/MetaObjects/Persistence/Origin/OriginSchema.cs index 5bff8120ca..31e430d675 100644 --- a/server/csharp/MetaObjects/Persistence/Origin/OriginSchema.cs +++ b/server/csharp/MetaObjects/Persistence/Origin/OriginSchema.cs @@ -74,6 +74,13 @@ public static class OriginSchema Required: false, IsArray: true, Description: "Ordering keys as 'field[:asc|desc]' over the related entity's fields; nulls sort last. On @agg:collect sets element order (non-distinct only)."), + + // Origin @segment. + new AttrSchema( + Name: OriginConstants.ORIGIN_ATTR_SEGMENT, + ValueType: AttrConstants.ATTR_SUBTYPE_STRING, + Required: false, + Description: "Optional name of a segment.filter declared on the related entity; its predicate scopes the related rows, ANDed with @filter."), ]; // #195 — computed: a row-level value via a structured @expr tree (attr.expression). @@ -113,6 +120,13 @@ public static class OriginSchema ValueType: AttrConstants.ATTR_SUBTYPE_FILTER, Required: false, Description: "Optional structured predicate scoping which related rows are eligible for selection."), + + // Origin @segment. + new AttrSchema( + Name: OriginConstants.ORIGIN_ATTR_SEGMENT, + ValueType: AttrConstants.ATTR_SUBTYPE_STRING, + Required: false, + Description: "Optional name of a segment.filter declared on the related entity; its predicate scopes the related rows, ANDed with @filter."), ]; /// diff --git a/server/csharp/MetaObjects/SpecMetamodel/object.json b/server/csharp/MetaObjects/SpecMetamodel/object.json index 8c9f73ae9e..c633eaa653 100644 --- a/server/csharp/MetaObjects/SpecMetamodel/object.json +++ b/server/csharp/MetaObjects/SpecMetamodel/object.json @@ -31,7 +31,7 @@ { "type": "segment", "subType": "*", "name": "*", "min": 0, "max": null }, { "type": "attr", "subType": "string", "name": "discriminator", "min": 0, "max": 1, "description": "FR-014: names the field on this entity (resolvable via extends:) that holds the subtype-discriminator value. Subtypes of this entity declare @discriminatorValue to bind their rows to a discriminator value. The discriminator field itself is an ordinary field declaration (typically field.enum or field.int / field.string)." }, { "type": "attr", "subType": "string", "name": "discriminatorValue", "min": 0, "max": 1, "description": "FR-014: on a subtype of an entity with @discriminator — the value that identifies rows of this subtype in the shared discriminator field. Wire form is always a string; the underlying field's subtype (enum / int / string) controls codegen + storage coercion. Required on every concrete subtype of a discriminated entity." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] }, { @@ -53,8 +53,8 @@ "whenToUse": "You need a derived, read-only view over entities — a report, summary, or joined/aggregated read model. Declare it (with origin.* children) instead of hand-writing the SELECT/joins; it stays read-only and regenerates.", "rules": "object.projection is a derived read-only representation: every field is extends-bound, origin-derived, or self-declared-under-external-assembly, and all are read-only at the subtype level. Identity is optional and, when present, MUST extend an entity identity. Sources are restricted to read-only @kinds. The declared field set IS the exposure — an inclusive list, fail-closed. A projection NEVER declares relationships (derivation is expressed via @via, not a relationship child) and NEVER co-locates templates — hence its child set omits both relationship and template. The assembly origins (origin.aggregate / origin.computed / origin.first) live HERE, not on object.value; a SOURCELESS projection is the legal shape for a wire-assembled read model, and a template-level @payloadRef/@responseRef may target one (#210). See ADR-0028 (object taxonomy, projection).", "children": [ - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. Resolves against the projection's own declared fields; an aggregate-derived field is not addressable (fail-closed)." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. A key names one of the projection's own fields, else a field of its base entity the view need not output, else a PATH of to-one hops (identity.reference or @cardinality one relationship names) from the base then a field of the entity reached ('orderId.customerId.isActive'), joined but not output. A reached entity field may itself be derived (entity-derived: an origin.computed or origin.first declared on the entity, inlined where the filter reaches it). An aggregate-derived field is not addressable (fail-closed); a to-many hop is refused. A value may be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." }, + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] }, { @@ -70,7 +70,7 @@ { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional segment of @from scoping the rows." }, { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row scope over @from's fields; may use relative-date values." }, { "type": "attr", "subType": "string", "name": "spine", "min": 0, "max": 1, "description": "Optional to-one path from @from to the entity whose rows supply the report's rows (e.g. 'Purchase.program'), written like a dimension's @via. With it the report has one row per distinct dimension tuple among THAT entity's rows, including the ones no row of @from refers to: a count there is 0 and any other measure is null unless it declares @default. Every listed dimension must be reached through this path. @segment and @filter still scope the rows of @from and never remove a row. A row of @from whose reference is null or matches nothing is in no row of the report." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] } ] diff --git a/server/csharp/MetaObjects/SpecMetamodel/origin.json b/server/csharp/MetaObjects/SpecMetamodel/origin.json index 00f949a3e3..d5b5b85415 100644 --- a/server/csharp/MetaObjects/SpecMetamodel/origin.json +++ b/server/csharp/MetaObjects/SpecMetamodel/origin.json @@ -27,7 +27,8 @@ { "type": "attr", "subType": "string", "name": "agg", "min": 1, "max": 1, "allowedValues": ["count", "sum", "avg", "min", "max", "any", "all", "collect"], "description": "The reducing function applied over the related row-set: count/sum/avg/min/max (numeric/ordinal reduces over @of); any/all (predicate quantifiers over @filter — @of forbidden; empty set → any=false, all=true); collect (array rollup — of the @of column, or of the carrying field.object's declared @objectRef value object when @of is omitted; the field must be isArray)." }, { "type": "attr", "subType": "string", "name": "of", "min": 0, "max": 1, "description": "Dotted Entity.field reference identifying the column being aggregated (e.g. 'Week.durationMinutes'). Required for count/sum/avg/min/max; OPTIONAL for collect, where absent means a whole-object rollup of the field's declared @objectRef value object; forbidden for any/all (which quantify over rows via @filter, not a column)." }, { "type": "attr", "subType": "string", "name": "via", "min": 0, "max": 1, "description": "Dotted relationship path from the base entity to the aggregated rows (e.g. 'Program.weeks' or 'Program.weeks.workouts'). May be omitted only when exactly one single-hop relationship leads from the base entity to the @of entity (FR-024, ADR-0029)." }, - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." }, + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." }, + { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional name of a segment.filter declared on the related entity (the @of entity, else the @via terminal). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." }, { "type": "attr", "subType": "boolean", "name": "distinct", "min": 0, "max": 1, "description": "Set (collect-only) to dedupe collected values (set semantics). Not supported on a whole-object collect (@of omitted): it is a guaranteed no-op whenever the value object carries the primary key, and a silent no-op is worse than a refusal." }, { "type": "attr", "subType": "string", "name": "orderBy", "isArray": true, "min": 0, "max": 1, "description": "Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. On @agg:collect sets element order (non-distinct only); on origin.first (required) selects the row. Semantic — carries no SQL syntax." } ] @@ -51,7 +52,8 @@ { "type": "attr", "subType": "string", "name": "of", "min": 1, "max": 1, "description": "Dotted Entity.field reference identifying the column projected from the selected row (e.g. 'ChildA.label'). Type-preserving: the field's declared subType must equal this column's subType (#185 doctrine)." }, { "type": "attr", "subType": "string", "name": "via", "min": 0, "max": 1, "description": "Dotted relationship path from the base entity to the related rows (e.g. 'Parent.childAs'). May be omitted only when exactly one single-hop relationship leads from the base entity to the @of entity (FR-024, ADR-0029)." }, { "type": "attr", "subType": "string", "name": "orderBy", "isArray": true, "min": 1, "max": 1, "description": "Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. Selects the single row (the first after ordering). The related PK ascending is appended as the deterministic tie-breaker. Semantic — carries no SQL syntax." }, - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." } + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." }, + { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional name of a segment.filter declared on the related entity (the @of entity). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." } ] } ] diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt index 23ed3b53dc..810cf28e5b 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt @@ -263,13 +263,15 @@ open class KotlinEntityGenerator : MultiFileDirectGeneratorBase() { // view-computed value fails jackson-module-kotlin deserialization → HTTP 400). Its validation // annotations are suppressed too (no @field:NotNull to reject the omitted value). Mirrors the // Java port dropping @NotNull for derived-on-write-through. A projection is read-only - // (isWriteThrough=false), so its derived fields keep their origin-aware non-null read type. - val writeThrough = obj.isWriteThrough + // (not an entity), so its derived fields keep their origin-aware non-null read type. An + // entity with no read view never reads a derived field back at all, so it defaults too: + // its primary source is always writable (the loader refuses a read-only primary). + val entityHost = obj.subType == MetaObject.SUBTYPE_ENTITY val ctorBuilder = FunSpec.constructorBuilder() for (field in obj.metaFields) { val baseType = resolvePropertyType(field, obj, loader) - val derivedReadOnly = writeThrough && KotlinGenUtil.isDerivedField(field) + val derivedReadOnly = entityHost && KotlinGenUtil.isDerivedField(field) // #195: a field derived by origin.aggregate @agg:any|all|collect is COALESCE-guaranteed // non-null in the synthesized read-model view (any→false, all→true, collect→[]), so its // read type is non-null even when the field is NOT @required — matching the Exposed view diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt index d6c60e0b58..87dc01cb63 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt @@ -252,7 +252,12 @@ open class KotlinExposedTableGenerator : MultiFileDirectGeneratorBaseTable` and the read `View` — so the caller passes an explicit // object name (default: `tableObjectName(shortName)`, byte-identical for a vanilla entity). objectNameOverride: String? = null, - // #214: on the write table, DERIVED fields (`origin.*` children) are excluded — they are - // not stored on the writable table, only computed by the read view. Vanilla entities carry - // no derived fields, so the default `false` keeps their output byte-identical. + // #214: on an entity's table, DERIVED fields (`origin.*` children) are excluded — they are + // not stored on the writable table, only computed by a read view (when there is one). excludeDerivedFields: Boolean = false, ): Boolean { val (pkg, shortName) = PackageMapping.splitFqn(entity.name) @@ -1085,7 +1089,7 @@ open class KotlinExposedTableGenerator : MultiFileDirectGeneratorBase): Boolean = field.isDerived + /** + * The fields [obj] reads from its own relation. A derived field on an entity that has no + * read view (not [MetaObject.isWriteThrough]) has no column anywhere: its table never stores + * it, so it is left out of every column read and write. An entity's primary source is always + * writable (the loader refuses a read-only primary), so an entity that is not write-through + * reads a table. + */ + fun columnFields(obj: MetaObject): List> = obj.metaFields.filterNot { + isDerivedField(it) && obj.subType == MetaObject.SUBTYPE_ENTITY && !obj.isWriteThrough + } + /** * FR-037 R1 — a field's EFFECTIVE `@mutability` mode: who may write it, and when. * Absent => `readWrite`. THE accessor every Kotlin consumer should use, so the diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRepositoryGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRepositoryGenerator.kt index 1c7918f78c..ac90591394 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRepositoryGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinRepositoryGenerator.kt @@ -148,8 +148,8 @@ open class KotlinRepositoryGenerator : MultiFileDirectGeneratorBase( // incl. derived; the WRITE set (insert / update / patch / @autoSet) EXCLUDES derived // fields — they have no column on the write table (computed by the view). Vanilla entity = // no derived fields, so the two sets are equal → byte-identical output. - val readScalarFields = entity.metaFields.filter { it !is ObjectField && it !is MapField } - val scalarFields = readScalarFields.filterNot { writeThrough && KotlinGenUtil.isDerivedField(it) } + val readScalarFields = KotlinGenUtil.columnFields(entity).filter { it !is ObjectField && it !is MapField } + val scalarFields = readScalarFields.filterNot { KotlinGenUtil.isDerivedField(it) } val hasUuidColumn = readScalarFields.any { columnElementType(it) == "UUID" } val uuidPk = primary?.isUuid == true val incrementPk = primary?.isIncrement == true diff --git a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinSpringControllerGenerator.kt b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinSpringControllerGenerator.kt index cc014650d1..312382c169 100644 --- a/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinSpringControllerGenerator.kt +++ b/server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinSpringControllerGenerator.kt @@ -281,7 +281,7 @@ open class KotlinSpringControllerGenerator : MultiFileDirectGeneratorBase = entity.metaFields + val scalarFields: List = KotlinGenUtil.columnFields(entity) .filterNot { isNonScalarSurface(it) } .map { ScalarFieldSpec(it.name, it.subType, columnElementType(it), intBackedEnumType(it, entity)) } @@ -520,7 +520,7 @@ open class KotlinSpringControllerGenerator : MultiFileDirectGeneratorBase) and the Exposed Column codec // encodes it to jsonb. A dict-of-VO MapField is written the same way (Map); a @@ -765,9 +765,9 @@ open class KotlinSpringControllerGenerator : MultiFileDirectGeneratorBase`) are skipped — the data class defaults them. if ((field is MapField && !isVoMapColumn(field)) || (field is ObjectField && !isJsonbObjectColumn(field))) continue diff --git a/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/KotlinDerivedFieldWithoutReadSourceTest.kt b/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/KotlinDerivedFieldWithoutReadSourceTest.kt new file mode 100644 index 0000000000..7b2805ef0f --- /dev/null +++ b/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/KotlinDerivedFieldWithoutReadSourceTest.kt @@ -0,0 +1,66 @@ +package com.metaobjects.generator.kotlin + +import com.metaobjects.metadata.ktx.loadDirectory +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths +import kotlin.io.path.isRegularFile +import kotlin.io.path.readText +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** + * An entity may carry a derived (`origin.*`) field that only a projection reads, with no read + * view of its own: the shared fixture `entity-derived-filter-gate` (`Member.hasActiveAgreement`, + * an `origin.first` a projection's `@filter` reaches). Its table, the one `meta migrate` creates, + * has no column for that field. So the Exposed `MemberTable` must declare none, and no generated + * read or write may name one; the data class keeps the property, nullable and defaulted, since no + * row ever fills it. + */ +class KotlinDerivedFieldWithoutReadSourceTest { + + private fun fixtureInput(): Path { + var cur: Path? = Paths.get("").toAbsolutePath() + while (cur != null) { + val candidate = cur.resolve("fixtures/conformance/entity-derived-filter-gate/input") + if (Files.isDirectory(candidate)) return candidate + cur = cur.parent + } + error("fixtures/conformance/entity-derived-filter-gate not found from ${Paths.get("").toAbsolutePath()}") + } + + @Test fun `the derived field has no column and no generated read or write names one`() { + val outDir = Files.createTempDirectory("kderived-no-read-source-") + try { + val loader = loadDirectory("entity-derived-filter-gate", fixtureInput()) + for (gen in listOf( + KotlinEntityGenerator(), KotlinExposedTableGenerator(), + KotlinRepositoryGenerator(), KotlinSpringControllerGenerator(), + )) { + gen.setArgs(mapOf("outputDir" to outDir.toString())) + gen.execute(loader) + } + val files = Files.walk(outDir).filter { it.isRegularFile() }.toList() + fun source(name: String): String = + files.single { it.fileName.toString() == name }.readText() + + val table = source("MemberTable.kt") + assertTrue("object MemberTable : Table(\"members\")" in table, "saw:\n$table") + assertFalse("hasActiveAgreement" in table || "has_active_agreement" in table, + "MemberTable must declare no column for the derived field; saw:\n$table") + + val member = source("Member.kt") + assertTrue("val hasActiveAgreement: Boolean? = null" in member, + "the data class keeps the derived property, nullable and defaulted; saw:\n$member") + + for (f in files.filter { it.fileName.toString().startsWith("Member") && it.fileName.toString() != "Member.kt" }) { + val src = f.readText() + assertFalse("MemberTable.hasActiveAgreement" in src || "[hasActiveAgreement]" in src, + "${f.fileName} names a column the members table lacks; saw:\n$src") + } + } finally { + outDir.toFile().deleteRecursively() + } + } +} diff --git a/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/PortScopedAttrKtTest.kt b/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/PortScopedAttrKtTest.kt index 9c5254ccfd..d77a601eb7 100644 --- a/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/PortScopedAttrKtTest.kt +++ b/server/java/codegen-kotlin/src/test/kotlin/com/metaobjects/generator/kotlin/PortScopedAttrKtTest.kt @@ -9,8 +9,8 @@ import kotlin.test.assertEquals import kotlin.test.assertTrue /** - * The Kotlin half of vocabulary another port implements: `@collectionName` (`implementedIn: - * ["csharp"]`) loads under the strict loader the Kotlin generators read; a wrong value type, the + * The Kotlin half of vocabulary other ports implement: `@collectionName` (`implementedIn: + * ["csharp", "typescript"]`) loads under the strict loader the Kotlin generators read; a wrong value type, the * never-released `@dbSetName` and an attribute the registry does not carry still fail. */ class PortScopedAttrKtTest { diff --git a/server/java/metadata/src/main/java/com/metaobjects/field/FlattenedMember.java b/server/java/metadata/src/main/java/com/metaobjects/field/FlattenedMember.java new file mode 100644 index 0000000000..499ab75e2b --- /dev/null +++ b/server/java/metadata/src/main/java/com/metaobjects/field/FlattenedMember.java @@ -0,0 +1,51 @@ +package com.metaobjects.field; + +import com.metaobjects.MetaData; +import com.metaobjects.database.ColumnNaming; +import com.metaobjects.loader.References; +import com.metaobjects.object.MetaObject; + +/** + * A path into a member of a FLATTENED value object ({@code address.city}): the member's column + * lives on the owning table, under the embedding field's prefix + * ({@link ColumnNaming#resolveFlattened}). One resolver for the loader and every lowering. + * Mirrors TS {@code flattened-member.ts} / C# {@code FlattenedMember}. + * + * @param embedding the embedding {@code field.object} on the entity + * @param member the member field its value object declares + */ +public record FlattenedMember(MetaField embedding, MetaField member) { + + /** + * Resolve {@code .} against {@code entity}: {@code } a scalar + * {@code field.object} with {@code @storage: flattened}, {@code } a field its + * {@code @objectRef} value object declares (effective). {@code null} for anything else — a + * plain field name, a jsonb object, an array, an unknown member. One level only. + */ + public static FlattenedMember resolve(MetaObject entity, String path) { + if (path == null) return null; + int dot = path.indexOf('.'); + if (dot < 1 || dot == path.length() - 1 || path.indexOf('.', dot + 1) != -1) return null; + String head = path.substring(0, dot); + String tail = path.substring(dot + 1); + // ADR-0039: resolving — the embedding field may be inherited via extends. + MetaField embedding = field(entity, head); + if (embedding == null || !ObjectField.SUBTYPE_OBJECT.equals(embedding.getSubType())) return null; + // ADR-0039: resolving — @storage / @objectRef may be inherited via extends. + if (!embedding.hasMetaAttr(MetaField.ATTR_STORAGE, true) + || !ObjectField.STORAGE_FLATTENED.equals(embedding.getMetaAttr(MetaField.ATTR_STORAGE, true).getValueAsString())) { + return null; + } + MetaObject vo = References.refTarget(embedding, MetaField.ATTR_OBJECT_REF); + if (vo == null) return null; + MetaField member = field(vo, tail); + return member == null ? null : new FlattenedMember(embedding, member); + } + + private static MetaField field(MetaObject obj, String name) { + for (MetaData c : obj.getChildren(MetaData.class, true)) { + if (c instanceof MetaField && name.equals(c.getShortName())) return (MetaField) c; + } + return null; + } +} diff --git a/server/java/metadata/src/main/java/com/metaobjects/loader/ValidationPhase.java b/server/java/metadata/src/main/java/com/metaobjects/loader/ValidationPhase.java index 05fbbb388d..6342303203 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/loader/ValidationPhase.java +++ b/server/java/metadata/src/main/java/com/metaobjects/loader/ValidationPhase.java @@ -266,7 +266,8 @@ public static void run(MetaRoot root, MetaDataLoader loader) { pass(collected, () -> validateObjectFieldStorage(root)); validateEnumMaxLength(root, collected); validateIdentitySequence(root, collected); - validateCrossFieldValidators(root, collected); + validateCrossFieldValidators(root, collected, + loader != null && loader.getLoaderOptions() != null && loader.getLoaderOptions().isStrict()); pass(collected, () -> validateFieldMap(root)); pass(collected, () -> validateIdentityFieldsAndGeneration(root)); // FR-024 B3/B4a — subtype rules (identity-name, value purity, projection licensing). @@ -276,6 +277,9 @@ public static void run(MetaRoot root, MetaDataLoader loader) { pass(collected, () -> validateDataGridLayouts(root)); // #207 — projection row-scope @filter field-ref validation (fail-closed). pass(collected, () -> validateProjectionFilter(root)); + // Origin @segment names a segment of the related entity; a correlated @filter operand + // names a base-row field. + pass(collected, () -> validateOriginSegmentsAndCorrelation(root)); // FR-044 — the reporting vocabulary's cross-node rules (D1-D4, M1-M6, S1, R1-R7, // F1/F2). Collects every finding; a broken rule yields exactly one error. The pass // already drops its own repeats (ReportingValidation's sink, keyed by node + message), @@ -1635,9 +1639,9 @@ private static void validateObjectSourceEscapes(MetaObject obj, MetaDataLoader l // a TOOL-SYNTHESIZED body; with @sql the author owns the body (and its // WHERE), so wrapping it is deferred cleverness (design doc D5) — reject. if (hasSqlHost && MetaObject.SUBTYPE_PROJECTION.equals(obj.getSubType())) { - // ADR-0039: own — the @filter is declared locally on this projection - // (mirrors validateProjectionFilter). - if (obj.hasMetaAttr(MetaObject.ATTR_FILTER, false)) { + // The effective @filter (mirrors validateProjectionFilter) — an inherited row scope + // cannot be applied to an author-owned body either, so it is refused, never dropped. + if (com.metaobjects.object.EffectiveProjectionFilter.of(obj) != null) { throw new MetaDataException( ErrorMessageConstants.ERR_ORIGIN_UNDER_SQL_BODY + ": projection \"" + obj.getName() @@ -2364,6 +2368,15 @@ private static List junctionReferenceFkFields(MetaObject junction) { // every violation collected. Mirrors TS validateCrossFieldValidators. // ========================================================================= static void validateCrossFieldValidators(MetaRoot root, List out) { + validateCrossFieldValidators(root, out, false); + } + + /** + * @param strict a strict load already refuses an atLeastOne {@code @max} as an unregistered + * attr while parsing, so the check here runs only on the default load, + * keeping one error per node + */ + static void validateCrossFieldValidators(MetaRoot root, List out, boolean strict) { java.util.Set crossField = java.util.Set.of( com.metaobjects.validator.ComparisonValidator.SUBTYPE_COMPARISON, com.metaobjects.validator.RequiredWhenValidator.SUBTYPE_REQUIRED_WHEN, @@ -2377,6 +2390,19 @@ static void validateCrossFieldValidators(MetaRoot root, List : obj.getChildren(com.metaobjects.validator.MetaValidator.class, false)) { if (!crossField.contains(v.getSubType())) continue; String label = v.getSubType() + " \"" + obj.getShortName() + "." + v.getShortName() + "\""; + // validator.atLeastOne takes no @max: "exactly one of these fields" is + // validator.exactlyOne. A strict load already refuses the unregistered attr; this + // refuses it under the default load too, where it would be kept and ignored. + // ADR-0039: own — checked on the validator that declares it. + if (!strict && com.metaobjects.validator.AtLeastOneValidator.SUBTYPE_AT_LEAST_ONE.equals(v.getSubType()) + && v.hasMetaAttr(com.metaobjects.validator.MetaValidator.ATTR_MAX, false)) { + out.add(new MetaDataException( + "ERR_UNKNOWN_ATTR: validator." + label + + " carries @max, which atLeastOne does not take. For \"exactly one of these fields\", " + + "declare validator.exactlyOne instead; for \"at least one\", remove @max.", + ErrorCode.ERR_UNKNOWN_ATTR, v.getSource())); + continue; + } if (com.metaobjects.validator.ExactlyOneValidator.SUBTYPE_EXACTLY_ONE.equals(v.getSubType())) { Object raw = v.hasMetaAttr(com.metaobjects.validator.ExactlyOneValidator.ATTR_FIELDS) ? v.getMetaAttr(com.metaobjects.validator.ExactlyOneValidator.ATTR_FIELDS).getValue() : null; @@ -2470,61 +2496,80 @@ private static String conditionProblem(Object condition, java.util.Map> fields) { - if (!(node instanceof java.util.Map n)) return null; - if (n.get("left") != null && n.get("right") != null) { - Object[][] sides = { { n.get("left"), n.get("right") }, { n.get("right"), n.get("left") } }; - for (Object[] side : sides) { - if (!(side[0] instanceof java.util.Map lit) || !lit.containsKey("value")) continue; - Object value = lit.get("value"); - if (value == null) { + for (java.util.Map n : exprNodes(condition, new ArrayList<>())) { + if (n.get("left") == null || n.get("right") == null) continue; + for (Object side : List.of(n.get("left"), n.get("right"))) { + if (side instanceof java.util.Map lit && lit.containsKey("value") && lit.get("value") == null) { return "compares with null, which is never true: use isNull or isNotNull."; } - MetaField field = side[1] instanceof java.util.Map ref && ref.get("field") instanceof String f - ? fields.get(f) : null; - if (field == null) continue; - String subType = field.getSubType(); - String kind = BooleanField.SUBTYPE_BOOLEAN.equals(subType) ? "boolean" - : CONDITION_NUMBER_SUBTYPES.contains(subType) ? "number" : "string"; - boolean matches = switch (kind) { - case "boolean" -> value instanceof Boolean; - case "number" -> value instanceof Number; - default -> value instanceof String; - }; - if (!matches) { - return "compares \"" + field.getShortName() + "\" (field." + subType + ") with " - + conditionJsonLiteral(value) + "; it needs a " + kind + "."; - } - if (EnumField.SUBTYPE_ENUM.equals(subType) && field.hasMetaAttr(EnumField.ATTR_VALUES) - && field.getMetaAttr(EnumField.ATTR_VALUES).getValue() instanceof java.util.Collection members) { - List names = new java.util.ArrayList<>(); - for (Object m : members) names.add(String.valueOf(m)); - if (!names.contains(String.valueOf(value))) { - return "compares \"" + field.getShortName() + "\" with " + conditionJsonLiteral(value) - + ", which is not one of its @values (" + String.join(", ", names) + ")."; - } - } - if (DurationField.SUBTYPE_DURATION.equals(subType) && !DurationWire.ACCEPTED.matcher((String) value).matches()) { - return "compares \"" + field.getShortName() + "\" with " + conditionJsonLiteral(value) - + ", which is not " + DURATION_LITERAL_FORM + "."; + } + } + return literalComparisonProblem(condition, fields::get); + } + + /** + * The first literal in {@code expr} that cannot equal the field it is compared with, or + * {@code null}. A literal is compared with a field by a comparison (either side) or as a simple + * case's {@code when} against a {@code {field}} operand. The rule, the same in every port and + * shared by a validator's @condition and an origin.computed @expr: the literal is of the + * field's kind (a boolean for field.boolean, a number for a numeric field, a string otherwise), + * one of its @values for field.enum, and an ISO-8601 duration for field.duration. A null + * literal, and a literal facing anything but a field ref, are not checked here. Mirrors TS + * literalComparisonProblem. + */ + static String literalComparisonProblem(Object expr, java.util.function.Function> fieldOf) { + for (java.util.Map n : exprNodes(expr, new ArrayList<>())) { + if (n.get("left") != null && n.get("right") != null) { + String p = literalProblem(n.get("left"), n.get("right"), fieldOf); + if (p == null) p = literalProblem(n.get("right"), n.get("left"), fieldOf); + if (p != null) return p; + } + if (n.get(ExpressionAttribute.EXPR_CASE_OPERAND) != null + && n.get(ExpressionAttribute.EXPR_CASE) instanceof java.util.Collection arms) { + for (Object arm : arms) { + if (!(arm instanceof java.util.Map am)) continue; + String p = literalProblem(am.get(ExpressionAttribute.EXPR_CASE_WHEN), + n.get(ExpressionAttribute.EXPR_CASE_OPERAND), fieldOf); + if (p != null) return p; } } } - for (String k : List.of("left", "right", "arg")) { - String p = conditionLiteralProblem(n.get(k), fields); - if (p != null) return p; + return null; + } + + /** Why literal {@code a} cannot equal field ref {@code b}, or {@code null}. */ + private static String literalProblem(Object a, Object b, java.util.function.Function> fieldOf) { + if (!(a instanceof java.util.Map lit) || !lit.containsKey("value")) return null; + Object value = lit.get("value"); + if (value == null) return null; + MetaField field = b instanceof java.util.Map ref && ref.get("field") instanceof String f + ? fieldOf.apply(f) : null; + if (field == null) return null; + String subType = field.getSubType(); + String kind = BooleanField.SUBTYPE_BOOLEAN.equals(subType) ? "boolean" + : CONDITION_NUMBER_SUBTYPES.contains(subType) ? "number" : "string"; + boolean matches = switch (kind) { + case "boolean" -> value instanceof Boolean; + case "number" -> value instanceof Number; + default -> value instanceof String; + }; + if (!matches) { + return "compares \"" + field.getShortName() + "\" (field." + subType + ") with " + + conditionJsonLiteral(value) + "; it needs a " + kind + "."; } - if (n.get("args") instanceof java.util.Collection args) { - for (Object a : args) { - String p = conditionLiteralProblem(a, fields); - if (p != null) return p; + if (EnumField.SUBTYPE_ENUM.equals(subType) && field.hasMetaAttr(EnumField.ATTR_VALUES) + && field.getMetaAttr(EnumField.ATTR_VALUES).getValue() instanceof java.util.Collection members) { + List names = new java.util.ArrayList<>(); + for (Object m : members) names.add(String.valueOf(m)); + if (!names.contains(String.valueOf(value))) { + return "compares \"" + field.getShortName() + "\" with " + conditionJsonLiteral(value) + + ", which is not one of its @values (" + String.join(", ", names) + ")."; } } + if (DurationField.SUBTYPE_DURATION.equals(subType) && !DurationWire.ACCEPTED.matcher((String) value).matches()) { + return "compares \"" + field.getShortName() + "\" with " + conditionJsonLiteral(value) + + ", which is not " + DURATION_LITERAL_FORM + "."; + } return null; } @@ -3303,6 +3348,31 @@ private static void validateOriginNode(MetaRoot root, MetaObject obj, + ": " + inferred.errors.get(0), ErrorCode.ERR_INVALID_ORIGIN, src); } + // A literal compared with a field must be able to equal it: of its kind, and for an + // enum one of its @values (the rule a validator's @condition follows). Resolved as the + // inference resolved the ref: a projection field first, else a base field. + String literalProblem = literalComparisonProblem(expr, name -> { + if (obj != baseEntity && !name.equals(selfName)) { + for (MetaField own : obj.getChildren(MetaField.class, true)) { + if (!name.equals(own.getShortName())) continue; + MetaOrigin ownOrigin = ownOriginOf(own); + if (ownOrigin != null + && AggregateOrigin.SUBTYPE_AGGREGATE.equals(ownOrigin.getSubType())) return null; + return own; + } + } + for (MetaData c : baseEntity.getChildren(MetaData.class, true)) { + if (c instanceof MetaField f && nameMatches(c, name)) return f; + } + return null; + }); + if (literalProblem != null) { + throw new MetaDataException( + ErrorMessageConstants.ERR_INVALID_ORIGIN + + ": origin.computed on " + obj.getName() + "." + field.getName() + + ": @expr " + literalProblem, + ErrorCode.ERR_INVALID_ORIGIN, src); + } if (inferred.type != null && !inferred.type.equals(field.getSubType())) { throw new MetaDataException( "ERR_COMPUTED_TYPE_MISMATCH" @@ -3880,18 +3950,21 @@ static void validateProjectionFilter(MetaRoot root) { if (!(rootChild instanceof MetaObject)) continue; MetaObject obj = (MetaObject) rootChild; if (!MetaObject.SUBTYPE_PROJECTION.equals(obj.getSubType())) continue; - // ADR-0039: own — the @filter is declared locally on this projection. - if (!obj.hasMetaAttr(MetaObject.ATTR_FILTER, false)) continue; - Object raw = obj.getMetaAttr(MetaObject.ATTR_FILTER, false).getValue(); + // The EFFECTIVE @filter — every level of an extends chain, supers first, ANDed — is + // what the view applies, so it is what is checked (an inherited gate is checked + // against the extender's fields too). + Object raw = com.metaobjects.object.EffectiveProjectionFilter.of(obj); // A non-object shape is rejected by the attr schema check (FilterAttribute). if (!(raw instanceof java.util.Map)) continue; - // Classify the projection's OWN fields (the declared set IS the exposure — + // Classify the projection's EFFECTIVE fields (the declared set IS the exposure — // FR-024/ADR-0028): aggregate-derived-ness by the field's OWN origin child. - // origin.* never inherits (ADR-0029), so both reads are own (category 4). + // ADR-0039: resolving — a field inherited from a super projection is a column of + // this view and carries its own origin child, read own below (origin.* never + // inherits, ADR-0029). java.util.Set declared = new java.util.HashSet<>(); java.util.Set aggregateDerived = new java.util.HashSet<>(); - for (MetaField f : obj.getChildren(MetaField.class, false)) { + for (MetaField f : obj.getChildren(MetaField.class, true)) { String name = f.getShortName(); declared.add(name); for (MetaData originChild : f.getChildren(MetaData.class, false)) { @@ -3905,10 +3978,208 @@ static void validateProjectionFilter(MetaRoot root) { } } } + // A field of the BASE entity the view does not output is filterable too (a WHERE + // reads the base row); a projection field of the same name wins. No base, no + // fallback: the origin passes report why one cannot be derived. + MetaObject base = deriveBaseEntityOrNull(obj, root, "@filter", obj.getSource()); + if (base != null && base != obj) { + for (MetaField f : base.getChildren(MetaField.class, true)) { + String name = f.getShortName(); + if (declared.add(name) && entityFieldAggregates(f)) aggregateDerived.add(name); + } + // A key that is a PATH of to-one hops from the base, then a field of the entity + // reached (`caseId.status`). Not output; the view joins what it walks. + for (String key : filterKeys(raw)) { + if (declared.contains(key)) continue; + com.metaobjects.relationship.FilterPath.Result path = + com.metaobjects.relationship.FilterPath.resolve(base, key); + if (path == null) continue; + if (path.error() != null) { + throw new MetaDataException( + ErrorMessageConstants.ERR_BAD_ATTR_FILTER + + ": projection '" + obj.getShortName() + "' @filter path '" + key + + "' does not resolve: " + path.error() + ".", + ErrorCode.ERR_BAD_ATTR_FILTER, obj.getSource()); + } + declared.add(key); + if (entityFieldAggregates(path.path().field())) aggregateDerived.add(key); + } + } checkProjectionFilterRefs(obj, (java.util.Map) raw, declared, aggregateDerived); } } + /** + * Origin segments and correlated filters, on every origin.first / origin.aggregate: + *
    + *
  • {@code @segment} names a segment.filter declared on the related entity (the + * {@code @of} entity, else the {@code @via} terminal) — ERR_INVALID_ORIGIN otherwise;
  • + *
  • an {@code @filter} operand {@code { field: "" }} names a field of the base row + * (the projection's base entity, or the entity hosting the field), with an op that + * compares two columns; the null-safe ops take only such an operand — + * ERR_BAD_ATTR_FILTER otherwise.
  • + *
+ * Mirrors TS {@code validateOriginSegmentsAndCorrelation}. + */ + static void validateOriginSegmentsAndCorrelation(MetaRoot root) { + for (MetaObject obj : rootObjects(root)) { + if (!MetaObject.SUBTYPE_PROJECTION.equals(obj.getSubType()) + && !MetaObject.SUBTYPE_ENTITY.equals(obj.getSubType())) continue; + for (MetaField field : obj.getChildren(MetaField.class, false)) { + for (MetaData oc : field.getChildren(MetaData.class, false)) { + if (!(oc instanceof MetaOrigin)) continue; + if (!FirstOrigin.SUBTYPE_FIRST.equals(oc.getSubType()) + && !AggregateOrigin.SUBTYPE_AGGREGATE.equals(oc.getSubType())) continue; + MetaOrigin origin = (MetaOrigin) oc; + String where = "origin." + origin.getSubType() + " on " + obj.getName() + "." + field.getShortName(); + if (origin.hasMetaAttr(MetaOrigin.ATTR_SEGMENT, false)) { + String segment = origin.getMetaAttr(MetaOrigin.ATTR_SEGMENT, false).getValueAsString(); + MetaObject related = originRelatedEntity(origin, obj, root); + if (related != null && !declaresSegment(related, segment)) { + throw new MetaDataException( + ErrorMessageConstants.ERR_INVALID_ORIGIN + ": " + where + ": @segment \"" + segment + + "\" is not a segment declared on \"" + related.getName() + + "\", the entity whose rows it scopes.", + ErrorCode.ERR_INVALID_ORIGIN, origin.getSource()); + } + } + if (!origin.hasMetaAttr(MetaOrigin.ATTR_FILTER, false)) continue; + Object filter = origin.getMetaAttr(MetaOrigin.ATTR_FILTER, false).getValue(); + MetaObject base = deriveBaseEntityOrNull(obj, root, field.getName(), origin.getSource()); + if (base == null) continue; + checkCorrelatedOperands(filter, base, where, origin.getSource()); + } + } + } + } + + private static boolean declaresSegment(MetaObject entity, String segment) { + // ADR-0039: resolving — a segment may be inherited through extends. + for (MetaData c : entity.getChildren(MetaData.class, true)) { + if (com.metaobjects.reporting.ReportingConstants.TYPE_SEGMENT.equals(c.getType()) + && segment.equals(c.getShortName())) return true; + } + return false; + } + + /** The entity whose rows an origin.first / origin.aggregate spans: its {@code @of} entity, + * else its {@code @via} terminal. {@code null} when neither resolves (reported elsewhere). */ + private static MetaObject originRelatedEntity(MetaOrigin origin, MetaObject obj, MetaRoot root) { + String of = origin.hasMetaAttr(MetaOrigin.ATTR_OF, false) + ? origin.getMetaAttr(MetaOrigin.ATTR_OF, false).getValueAsString() : null; + if (of != null && !of.isEmpty()) { + try { + return validateFromOrOfPath(of, root, obj, origin.getParent().getName(), "", origin.getSource()).entity; + } catch (MetaDataException e) { + return null; + } + } + String referrerPkg = obj.getPackage() == null ? "" : obj.getPackage(); + String via = origin.hasMetaAttr(MetaOrigin.ATTR_VIA, false) + ? origin.getMetaAttr(MetaOrigin.ATTR_VIA, false).getValueAsString() : null; + if (via == null) return null; + String[] parts = via.split("\\."); + MetaObject current = resolveRootObject(root, parts[0], referrerPkg); + for (int i = 1; i < parts.length && current != null; i++) { + MetaRelationship rel = findRelationship(current, parts[i]); + MetaData hop = rel != null ? rel : findReference(current, parts[i]); + if (hop != null) { + current = References.refTarget(hop, + isReferenceHop(hop) ? MetaIdentity.ATTR_REFERENCES : MetaRelationship.ATTR_OBJECT_REF); + } else { + java.util.List reversed = + ReverseReferenceHops.find(current, parts[i], rootObjects(root)); + current = reversed.isEmpty() ? null : reversed.get(0).holder(); + } + } + return current; + } + + private static void checkCorrelatedOperands(Object filter, MetaObject base, String where, + com.metaobjects.source.ErrorSource source) { + if (!(filter instanceof java.util.Map m)) return; + for (java.util.Map.Entry e : m.entrySet()) { + String key = String.valueOf(e.getKey()); + if ("and".equals(key) || "or".equals(key)) { + if (e.getValue() instanceof Iterable subs) { + for (Object sub : subs) checkCorrelatedOperands(sub, base, where, source); + } + continue; + } + if (!(e.getValue() instanceof java.util.Map clause)) continue; + for (java.util.Map.Entry opEntry : clause.entrySet()) { + String op = String.valueOf(opEntry.getKey()); + Object value = opEntry.getValue(); + boolean isOperand = value instanceof java.util.Map vm && vm.containsKey(MetaOrigin.FILTER_OPERAND_FIELD); + boolean nullSafe = MetaOrigin.FILTER_OP_IS_DISTINCT_FROM.equals(op) + || MetaOrigin.FILTER_OP_IS_NOT_DISTINCT_FROM.equals(op); + if (!isOperand) { + if (nullSafe) { + throw new MetaDataException( + ErrorMessageConstants.ERR_BAD_ATTR_FILTER + ": " + where + ": @filter on \"" + key + + "\" uses \"" + op + "\", which compares with a base-row field: write { " + op + ": { " + + MetaOrigin.FILTER_OPERAND_FIELD + ": \"\" } }.", + ErrorCode.ERR_BAD_ATTR_FILTER, source); + } + continue; + } + Object name = ((java.util.Map) value).get(MetaOrigin.FILTER_OPERAND_FIELD); + if (!MetaOrigin.CORRELATED_FILTER_OPS.contains(op)) { + throw new MetaDataException( + ErrorMessageConstants.ERR_BAD_ATTR_FILTER + ": " + where + ": @filter on \"" + key + + "\" compares with a base-row field using \"" + op + "\"; a correlated operand takes " + + String.join(", ", MetaOrigin.CORRELATED_FILTER_OPS) + ".", + ErrorCode.ERR_BAD_ATTR_FILTER, source); + } + boolean known = false; + if (name instanceof String) { + for (MetaField f : base.getChildren(MetaField.class, true)) { + if (name.equals(f.getShortName())) { known = true; break; } + } + } + if (!known) { + throw new MetaDataException( + ErrorMessageConstants.ERR_BAD_ATTR_FILTER + ": " + where + ": @filter on \"" + key + + "\" names base-row field \"" + name + "\", which \"" + base.getShortName() + + "\" does not declare.", + ErrorCode.ERR_BAD_ATTR_FILTER, source); + } + } + } + } + + /** + * An entity field a projection {@code @filter} reaches (on the base, or through a path) + * cannot be filtered when it is an aggregate: a WHERE runs before any grouping. Its own + * stored column, an origin.computed or an origin.first are WHERE-legal. + */ + private static boolean entityFieldAggregates(MetaField f) { + // ADR-0039: own — origin.* never inherits (ADR-0029). + for (MetaData c : f.getChildren(MetaData.class, false)) { + if (c instanceof MetaOrigin) return AggregateOrigin.SUBTYPE_AGGREGATE.equals(c.getSubType()); + } + return false; + } + + /** Every field key of a (desugared) filter, through and/or. */ + static java.util.List filterKeys(Object filter) { + java.util.List out = new java.util.ArrayList<>(); + collectFilterKeys(filter, out); + return out; + } + + private static void collectFilterKeys(Object filter, java.util.List out) { + if (!(filter instanceof java.util.Map m)) return; + for (java.util.Map.Entry e : m.entrySet()) { + String key = String.valueOf(e.getKey()); + if ("and".equals(key) || "or".equals(key)) { + if (e.getValue() instanceof Iterable subs) for (Object sub : subs) collectFilterKeys(sub, out); + } else { + out.add(key); + } + } + } + private static void checkProjectionFilterRefs(MetaObject obj, java.util.Map filter, java.util.Set declared, java.util.Set aggregateDerived) { @@ -3932,8 +4203,8 @@ private static void checkProjectionFilterRefs(MetaObject obj, java.util.Map hops, MetaObject terminal, - java.util.Set reverse) { } + java.util.Map reverse) { } private static WalkedViaPath validateViaPath(String viaAttr, MetaRoot root, MetaObject projection, String fieldName, @@ -4626,7 +4903,8 @@ private static WalkedViaPath validateViaPath(String viaAttr, MetaRoot root, // each successful relationship hop appends a segment. java.util.List validSegments = new java.util.ArrayList<>(); validSegments.add(entityName); - java.util.Set reverse = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>()); + // The references walked BACKWARDS, each keyed to the hop name the path wrote. + java.util.Map reverse = new java.util.IdentityHashMap<>(); for (int i = 1; i < segments.length; i++) { String relName = segments[i]; // FR-024: a hop may name a relationship OR a reference-only FK @@ -4643,7 +4921,7 @@ private static WalkedViaPath validateViaPath(String viaAttr, MetaRoot root, if (reversed.size() == 1) { validSegments.add(relName); hops.add(reversed.get(0).reference()); - reverse.add(reversed.get(0).reference()); + reverse.put(reversed.get(0).reference(), relName); currentObj = reversed.get(0).holder(); continue; } @@ -4745,6 +5023,7 @@ private static java.util.List rootObjects(MetaRoot root) { * * @param referrerPkg the effective package of the node carrying the ref ("" for root-level) */ + public static MetaObject resolveRootObject(MetaRoot root, String ref, String referrerPkg) { if (ref == null) return null; // ADR-0059 R2 — the one resolver: ::Rest, ..::Rest, pkg::Name, bare Name. An over-dropping @@ -4906,8 +5185,8 @@ private static String hopCardinality(MetaData rel) { /** As {@link #hopCardinality(MetaData)}; a reference in {@code reverse} was walked backwards, * so it reaches every row that references the walked-from one: to-many. */ - private static String hopCardinality(MetaData rel, java.util.Set reverse) { - if (reverse != null && reverse.contains(rel)) return MetaRelationship.CARDINALITY_MANY; + private static String hopCardinality(MetaData rel, java.util.Map reverse) { + if (reverse != null && reverse.containsKey(rel)) return MetaRelationship.CARDINALITY_MANY; if (isReferenceHop(rel)) return MetaRelationship.CARDINALITY_ONE; return rel.hasMetaAttr(MetaRelationship.ATTR_CARDINALITY) ? rel.getMetaAttr(MetaRelationship.ATTR_CARDINALITY).getValueAsString() @@ -4978,6 +5257,12 @@ private static MetaObject deriveBaseEntity(MetaObject obj, MetaRoot root, String } } if (targets.size() == 1) return targets.iterator().next(); + // A projection that extends another projection, and anchors nothing of its own, has the + // base entity its super has (its inherited identity and fields are anchored there). + MetaData sup = obj.hasSuperData() ? obj.getSuperData() : null; + if (targets.isEmpty() && sup instanceof MetaObject && MetaObject.SUBTYPE_PROJECTION.equals(sup.getSubType())) { + return deriveBaseEntity((MetaObject) sup, root, fieldName, originSource); + } if (targets.size() > 1) { StringBuilder names = new StringBuilder(); for (MetaObject t : targets) { if (names.length() > 0) names.append(", "); names.append('"').append(t.getName()).append('"'); } @@ -4995,6 +5280,17 @@ private static MetaObject deriveBaseEntity(MetaObject obj, MetaRoot root, String ErrorCode.ERR_INVALID_ORIGIN, originSource); } + /** {@link #deriveBaseEntity}, silent: {@code null} when no base can be derived (the origin + * passes report why). */ + private static MetaObject deriveBaseEntityOrNull(MetaObject obj, MetaRoot root, String fieldName, + com.metaobjects.source.ErrorSource originSource) { + try { + return deriveBaseEntity(obj, root, fieldName, originSource); + } catch (MetaDataException e) { + return null; + } + } + /** True when {@code target} is {@code base} or {@code host}, or on either's extends chain. */ private static boolean isBaseRelationTarget(MetaObject target, MetaObject base, MetaObject host) { for (MetaData cur = base; cur != null; cur = cur.getSuperData()) if (cur == target) return true; @@ -5049,16 +5345,18 @@ private static void checkPassthroughCardinality(java.util.List hops, private static void checkPassthroughCardinality(java.util.List hops, MetaObject obj, String fieldName, com.metaobjects.source.ErrorSource originSource, - java.util.Set reverse) { + java.util.Map reverse) { for (MetaData rel : hops) { if (MetaRelationship.CARDINALITY_MANY.equals(hopCardinality(rel, reverse))) { + boolean reversed = reverse != null && reverse.containsKey(rel); throw new MetaDataException( "ERR_ORIGIN_CARDINALITY" - + ": origin.passthrough on " + obj.getName() + "." + fieldName - + ": @via hop \"" + rel.getName() + "\" is to-many" - + (reverse != null && reverse.contains(rel) - ? " (reverse hop over identity.reference \"" + rel.getName() + "\")" : "") - + " — you meant aggregate (ADR-0029).", + + ": origin.passthrough on " + obj.getShortName() + "." + fieldName + + ": @via hop \"" + (reversed ? reverse.get(rel) : rel.getShortName()) + "\" is to-many " + + (reversed + ? "(reverse hop over identity.reference \"" + rel.getShortName() + "\")" + : "(@cardinality \"" + MetaRelationship.CARDINALITY_MANY + "\")") + + " — a row-multiplying passthrough — you meant aggregate (ADR-0029).", ErrorCode.ERR_ORIGIN_CARDINALITY, originSource); } } @@ -5072,7 +5370,7 @@ private static void checkAggregateCardinality(java.util.List hops, private static void checkAggregateCardinality(java.util.List hops, MetaObject obj, String fieldName, com.metaobjects.source.ErrorSource originSource, - java.util.Set reverse) { + java.util.Map reverse) { if (hops.isEmpty()) return; boolean provablyToOne = true; for (MetaData rel : hops) { @@ -5375,6 +5673,7 @@ private static boolean extendsChainReaches(MetaData node, MetaData target) { // ========================================================================= static void validateDerivedFieldProvidability(MetaRoot root) { + java.util.Set reached = projectionReachedEntityFields(root); for (MetaData c : root.getChildren(MetaData.class, false)) { if (!(c instanceof MetaObject)) continue; MetaObject obj = (MetaObject) c; @@ -5386,23 +5685,76 @@ static void validateDerivedFieldProvidability(MetaRoot root) { if (hasReadCapable) continue; for (MetaData fc : obj.getChildren(MetaData.class, false)) { if (!(fc instanceof MetaField)) continue; - boolean hasOrigin = false; + MetaData origin = null; for (MetaData oc : fc.getChildren(MetaData.class, false)) { - if (oc instanceof MetaOrigin) { hasOrigin = true; break; } + if (oc instanceof MetaOrigin) { origin = oc; break; } } - if (hasOrigin) { + if (origin != null) { + // A derived field a projection reaches (its @filter or a passthrough @from) + // is lowered inline there, so it needs no read source. Only the inlinable origins. + if ((FirstOrigin.SUBTYPE_FIRST.equals(origin.getSubType()) + || ComputedOrigin.SUBTYPE_COMPUTED.equals(origin.getSubType())) + && reached.contains(fc)) { + continue; + } throw new MetaDataException( "ERR_DERIVED_FIELD_NO_READ_SOURCE" + ": derived field \"" + obj.getName() + "." + fc.getName() + "\" carries an origin.* but entity \"" + obj.getName() - + "\" declares no read-capable source — declare a read-only source " - + "or move the field to an object.projection (FR-024 §7).", + + "\" declares no read-capable source — declare a read-only source, " + + "move the field to an object.projection (FR-024 §7), or reach it from a " + + "projection's @filter or @from (an origin.first or origin.computed is then inlined there).", ErrorCode.ERR_DERIVED_FIELD_NO_READ_SOURCE, fc.getSource()); } } } } + /** + * The entity fields projections REACH without outputting them: a projection {@code @filter} + * key naming a base field or a to-one path, and a passthrough {@code @from}. A derived field + * reached this way is lowered inline where it is reached. Mirrors TS + * {@code projectionReachedEntityFields}. + */ + static java.util.Set projectionReachedEntityFields(MetaRoot root) { + java.util.Set reached = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>()); + for (MetaData c : root.getChildren(MetaData.class, false)) { + if (!(c instanceof MetaObject) || !MetaObject.SUBTYPE_PROJECTION.equals(c.getSubType())) continue; + MetaObject obj = (MetaObject) c; + MetaObject base = deriveBaseEntityOrNull(obj, root, "@filter", obj.getSource()); + // The effective filter: an extender reaches what its supers' filters name, too. + Object filter = com.metaobjects.object.EffectiveProjectionFilter.of(obj); + if (base != null && base != obj) { + for (String key : filterKeys(filter)) { + com.metaobjects.relationship.FilterPath.Result path = + com.metaobjects.relationship.FilterPath.resolve(base, key); + if (path != null) { + if (path.path() != null) reached.add(path.path().field()); + continue; + } + for (MetaField f : base.getChildren(MetaField.class, true)) { + if (key.equals(f.getShortName())) { reached.add(f); break; } + } + } + } + // ADR-0039: resolving — inherited fields carry their own origins. + for (MetaField field : obj.getChildren(MetaField.class, true)) { + for (MetaData oc : field.getChildren(MetaData.class, false)) { + if (!(oc instanceof PassthroughOrigin)) continue; + String from = ((PassthroughOrigin) oc).getFrom(); + if (from == null) continue; + try { + reached.add(validateFromOrOfPath(from, root, obj, field.getName(), "origin.passthrough.@from", + oc.getSource()).field); + } catch (MetaDataException silent) { + // The origin pass reports an unresolved @from. + } + } + } + } + return reached; + } + // ========================================================================= // FR-037 R1 — field-level @mutability cross-attribute rules. // ERR_MUTABILITY_AUTOSET_CONFLICT / ERR_MUTABILITY_DOWNGRADE / diff --git a/server/java/metadata/src/main/java/com/metaobjects/object/EffectiveProjectionFilter.java b/server/java/metadata/src/main/java/com/metaobjects/object/EffectiveProjectionFilter.java new file mode 100644 index 0000000000..afdaa0b3e4 --- /dev/null +++ b/server/java/metadata/src/main/java/com/metaobjects/object/EffectiveProjectionFilter.java @@ -0,0 +1,60 @@ +package com.metaobjects.object; + +import com.metaobjects.MetaData; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A projection's EFFECTIVE row scope. {@code @filter} composes down an {@code extends} chain + * rather than overriding: a projection that extends a filtered projection keeps the base's row + * scope (a gate on an abstract base must survive in every extender). Every level's own + * {@code @filter}, root-most first, ANDed. One reader for the loader and the view lowering. + * Mirrors TS {@code effectiveProjectionFilter} / C# {@code EffectiveProjectionFilter}. + */ +public final class EffectiveProjectionFilter { + + private static final String AND = "and"; + + private EffectiveProjectionFilter() {} + + /** + * The {@code @filter} a projection's view applies: its own and every super's, root-most + * first. One declaration is returned as declared; two or more are ANDed in one flat + * {@code { and: [...] }}. {@code null} when no level declares one. + */ + public static Object of(MetaData projection) { + List levels = new ArrayList<>(); + Set seen = Collections.newSetFromMap(new IdentityHashMap<>()); + for (MetaData node = projection; node != null && seen.add(node); + node = node.hasSuperData() ? node.getSuperData() : null) { + // ADR-0039: own — each level contributes the @filter declared on it; the resolving + // read would return only the nearest one, which is the override this replaces. + if (node.hasMetaAttr(MetaObject.ATTR_FILTER, false)) { + levels.add(0, node.getMetaAttr(MetaObject.ATTR_FILTER, false).getValue()); + } + } + if (levels.isEmpty()) return null; + if (levels.size() == 1) return levels.get(0); + List members = new ArrayList<>(); + for (Object level : levels) members.addAll(andMembers(level)); + Map composed = new LinkedHashMap<>(); + composed.put(AND, members); + return composed; + } + + /** A level that is itself a bare {@code { and: [...] }} contributes its members, keeping the AND flat. */ + private static List andMembers(Object filter) { + if (filter instanceof Map m && m.size() == 1 && m.get(AND) instanceof List list) { + return new ArrayList<>(list); + } + List one = new ArrayList<>(); + one.add(filter); + return one; + } +} diff --git a/server/java/metadata/src/main/java/com/metaobjects/origin/AggregateOrigin.java b/server/java/metadata/src/main/java/com/metaobjects/origin/AggregateOrigin.java index b965800f86..54aa097158 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/origin/AggregateOrigin.java +++ b/server/java/metadata/src/main/java/com/metaobjects/origin/AggregateOrigin.java @@ -63,6 +63,8 @@ public static void registerTypes(MetaDataRegistry registry) { // Optional scoping filter (attr.filter object) → SQL FILTER (WHERE ...). def.optionalAttributeWithConstraints(ATTR_FILTER) .ofType(FilterAttribute.SUBTYPE_FILTER).asSingle(); + def.optionalAttributeWithConstraints(ATTR_SEGMENT) + .ofType(StringAttribute.SUBTYPE_STRING).asSingle(); // #195 collect support: @distinct (dedupe) + @orderBy (element order). def.optionalAttributeWithConstraints(ATTR_DISTINCT) .ofType(BooleanAttribute.SUBTYPE_BOOLEAN).asSingle(); diff --git a/server/java/metadata/src/main/java/com/metaobjects/origin/FirstOrigin.java b/server/java/metadata/src/main/java/com/metaobjects/origin/FirstOrigin.java index 66debda3d6..024bc824ea 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/origin/FirstOrigin.java +++ b/server/java/metadata/src/main/java/com/metaobjects/origin/FirstOrigin.java @@ -54,6 +54,8 @@ public static void registerTypes(MetaDataRegistry registry) { .ofType(StringAttribute.SUBTYPE_STRING).asArray(); def.optionalAttributeWithConstraints(ATTR_FILTER) .ofType(FilterAttribute.SUBTYPE_FILTER).asSingle(); + def.optionalAttributeWithConstraints(ATTR_SEGMENT) + .ofType(StringAttribute.SUBTYPE_STRING).asSingle(); }); } } diff --git a/server/java/metadata/src/main/java/com/metaobjects/origin/MetaOrigin.java b/server/java/metadata/src/main/java/com/metaobjects/origin/MetaOrigin.java index ab238a77e5..54a205b87a 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/origin/MetaOrigin.java +++ b/server/java/metadata/src/main/java/com/metaobjects/origin/MetaOrigin.java @@ -82,6 +82,24 @@ public abstract class MetaOrigin extends MetaData { */ public static final String ATTR_FILTER = "filter"; + /** + * Optional on {@code origin.aggregate} / {@code origin.first}: the name of a + * {@code segment.filter} declared on the related entity. Its predicate scopes the related + * rows, ANDed with {@code @filter} (mirrors {@code measure.aggregate @segment}). + */ + public static final String ATTR_SEGMENT = "segment"; + + /** The null-safe comparisons a correlated operand may make (IS [NOT] DISTINCT FROM). */ + public static final String FILTER_OP_IS_DISTINCT_FROM = "isDistinctFrom"; + public static final String FILTER_OP_IS_NOT_DISTINCT_FROM = "isNotDistinctFrom"; + + /** The ops a correlated (base-row field) operand may take in an origin {@code @filter}. */ + public static final java.util.List CORRELATED_FILTER_OPS = java.util.List.of( + "eq", "ne", "gt", "gte", "lt", "lte", FILTER_OP_IS_DISTINCT_FROM, FILTER_OP_IS_NOT_DISTINCT_FROM); + + /** The key of a correlated operand: {@code { field: "" }}. */ + public static final String FILTER_OPERAND_FIELD = "field"; + /** * #185 — boolean acknowledgement that a passthrough field's declared type * deliberately differs from its {@code @from} source field's type. Optional on diff --git a/server/java/metadata/src/main/java/com/metaobjects/relationship/FilterPath.java b/server/java/metadata/src/main/java/com/metaobjects/relationship/FilterPath.java new file mode 100644 index 0000000000..fbf45ceb30 --- /dev/null +++ b/server/java/metadata/src/main/java/com/metaobjects/relationship/FilterPath.java @@ -0,0 +1,78 @@ +package com.metaobjects.relationship; + +import com.metaobjects.MetaData; +import com.metaobjects.field.MetaField; +import com.metaobjects.identity.MetaIdentity; +import com.metaobjects.loader.References; +import com.metaobjects.object.MetaObject; + +import java.util.List; + +/** + * A projection {@code @filter} key may be a PATH: to-one hops from the base entity, then a field + * of the entity they reach ({@code caseId.status}, {@code orderId.customerId.isActive}). A hop is + * an {@code identity.reference} (a forward FK) or a relationship with {@code @cardinality one}, + * named as an origin {@code @via} names its hops. A to-many hop would multiply the base rows, so + * it is refused. One resolver for the loader and the view lowering. Mirrors TS + * {@code filter-path.ts} / C# {@code FilterPath}. + * + * @param hops the hop names walked + * @param entity the entity reached + * @param field its field + */ +public record FilterPath(List hops, MetaObject entity, MetaField field) { + + /** The outcome of {@link #resolve}: a path, or the reason it does not resolve. */ + public record Result(FilterPath path, String error) { } + + /** + * Resolve a dotted filter key against {@code base}. {@code null} when {@code key} has no dot + * (a plain field name, resolved by the caller); a {@link Result} carrying the failure when a + * hop or the field does not resolve. + */ + public static Result resolve(MetaObject base, String key) { + String[] segments = key.split("\\.", -1); + if (segments.length < 2) return null; + List hops = List.of(segments).subList(0, segments.length - 1); + String fieldName = segments[segments.length - 1]; + MetaObject current = base; + for (String hop : hops) { + Object next = toOneHop(current, hop); + if (next instanceof String error) return new Result(null, error); + current = (MetaObject) next; + } + // ADR-0039: resolving — the reached field may be inherited. + for (MetaData c : current.getChildren(MetaData.class, true)) { + if (c instanceof MetaField && fieldName.equals(c.getShortName())) { + return new Result(new FilterPath(List.copyOf(hops), current, (MetaField) c), null); + } + } + return new Result(null, "\"" + current.getShortName() + "\" declares no field \"" + fieldName + "\""); + } + + /** The target of one to-one hop from {@code obj}, or a String saying why there is none. */ + private static Object toOneHop(MetaObject obj, String hop) { + // ADR-0039: resolving — a hop may be inherited through extends. + for (MetaData c : obj.getChildren(MetaData.class, true)) { + if (c instanceof MetaIdentity && MetaIdentity.SUBTYPE_REFERENCE.equals(c.getSubType()) + && hop.equals(c.getShortName())) { + MetaObject target = References.refTarget(c, MetaIdentity.ATTR_REFERENCES); + return target != null ? target + : "reference \"" + obj.getShortName() + "." + hop + "\" does not resolve"; + } + } + for (MetaData c : obj.getChildren(MetaData.class, true)) { + if (c instanceof MetaRelationship && hop.equals(c.getShortName())) { + String cardinality = c.hasMetaAttr(MetaRelationship.ATTR_CARDINALITY, true) + ? c.getMetaAttr(MetaRelationship.ATTR_CARDINALITY, true).getValueAsString() : null; + if (!MetaRelationship.CARDINALITY_ONE.equals(cardinality)) { + return "\"" + obj.getShortName() + "." + hop + "\" is to-many; a filter path may only walk to-one hops"; + } + MetaObject target = References.refTarget(c, MetaRelationship.ATTR_OBJECT_REF); + return target != null ? target + : "relationship \"" + obj.getShortName() + "." + hop + "\" does not resolve"; + } + } + return "\"" + obj.getShortName() + "\" declares no identity.reference or to-one relationship named \"" + hop + "\""; + } +} diff --git a/server/java/metadata/src/main/java/com/metaobjects/reporting/ReportShape.java b/server/java/metadata/src/main/java/com/metaobjects/reporting/ReportShape.java index 4ce25e6437..b7cde72619 100644 --- a/server/java/metadata/src/main/java/com/metaobjects/reporting/ReportShape.java +++ b/server/java/metadata/src/main/java/com/metaobjects/reporting/ReportShape.java @@ -386,9 +386,9 @@ private static boolean isPrimaryKeyField(MetaObject entity, MetaField field) * True when every hop of {@code hops}, walked from {@code from}, is a belongs-to hop over a * required foreign key: the hops the view joins INNER, so every row of the report has a * match. This is the join-type rule of the view lowering (TypeScript {@code walkViaPath}, - * #209): the reference is held by the entity the hop leaves, its first FK field is required, - * and every earlier hop is INNER too. {@code @enforce: false} does not change it. A hop that - * does not resolve, an ambiguous reference or a has-many hop is not INNER. + * #209): the reference is held by the entity the hop leaves, is enforced, its first FK field + * is required, its target hides no rows ({@link #hidesRows}), and every earlier hop matches + * too. A hop that does not resolve, an ambiguous reference or a has-many hop does not. */ private static boolean viaJoinsInner(List hops, MetaObject from, MetaRoot root) { MetaObject current = from; @@ -418,14 +418,33 @@ private static boolean viaJoinsInner(List hops, MetaObject from, MetaRoo ? refNode : RelationshipReferences.resolveRelationshipReference( current, rel.getShortName(), target, rel.getSourceRefField()); - if (reference == null || reference.getFields().isEmpty()) return false; + if (reference == null || !reference.isEnforced() || reference.getFields().isEmpty()) return false; MetaField fk = current.findMetaField(reference.getFields().get(0)).orElse(null); if (fk == null || !isRequiredField(fk)) return false; + if (hidesRows(target)) return false; current = target; } return true; } + /** + * True when a join onto {@code obj} carries a row scope of its own: a TPH subtype, or an + * abstract level beneath a TPH base (both scoped by the discriminator), whose FK may name a + * sibling's row. The model has no soft-delete or query-filter vocabulary, so this is the only + * row-hiding scope. + */ + private static boolean hidesRows(MetaObject obj) { + // ADR-0039: own — the discriminator is declared on the level that owns it. + if (obj.hasMetaAttr(MetaObject.ATTR_DISCRIMINATOR, false)) return false; + java.util.Set seen = java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>()); + seen.add(obj); + for (MetaData s = obj.getSuperData(); s != null && seen.add(s); s = s.getSuperData()) { + // ADR-0039: own — the TPH base is the ancestor that declares @discriminator. + if (s.hasMetaAttr(MetaObject.ATTR_DISCRIMINATOR, false)) return true; + } + return false; + } + /** Effective required-ness, resolving (ADR-0039): the {@code @required} attr OR a validator.required child. */ private static boolean isRequiredField(MetaField field) { return ReportingAttrs.isTrue(field, MetaField.ATTR_REQUIRED) @@ -436,7 +455,7 @@ private static boolean isRequiredField(MetaField field) { * Table C ({@code required} of a dimension). Without {@code @spine}: only a dimension over an * {@code @of} field whose effective {@code @required} is true and, when it has a {@code @via}, * only when every hop is a required belongs-to reference, the hops the view joins INNER - * ({@link #viaJoinsInner}). With + * onto targets that hide no rows ({@link #viaJoinsInner}). With * {@code @spine}: only a dimension whose {@code @via} hops equal the spine's (a column of * the spine entity itself, whose rows are the report's rows) over an {@code @of} field that * is {@code @required} or a primary-key column of the entity {@code @of} names. A dimension diff --git a/server/java/metadata/src/test/java/com/metaobjects/loader/AtLeastOneMaxDefaultLoadTest.java b/server/java/metadata/src/test/java/com/metaobjects/loader/AtLeastOneMaxDefaultLoadTest.java new file mode 100644 index 0000000000..e2cc1c3493 --- /dev/null +++ b/server/java/metadata/src/test/java/com/metaobjects/loader/AtLeastOneMaxDefaultLoadTest.java @@ -0,0 +1,99 @@ +package com.metaobjects.loader; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.metaobjects.MetaDataException; +import com.metaobjects.registry.ObjectClassRegistry; +import com.metaobjects.source.JsonSource; +import org.junit.After; +import org.junit.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +/** + * {@code validator.atLeastOne} takes no {@code @max}. The shared fixture + * {@code error-validator-at-least-one-max-unknown} runs under the strict loader, where the + * attribute is unregistered; this runs the same input through the default (non-strict) loader + * too, which would otherwise keep {@code @max} and ignore it. Oracle: that fixture's + * {@code expected-errors.json}, exactly one {@code ERR_UNKNOWN_ATTR} at the validator, under + * either loader. + */ +public class AtLeastOneMaxDefaultLoadTest { + + @After + public void reset() { + ObjectClassRegistry.resetGlobal(); + } + + private static Path fixture() { + Path dir = Paths.get("").toAbsolutePath(); + while (dir != null) { + Path candidate = dir.resolve("fixtures/conformance/error-validator-at-least-one-max-unknown"); + if (Files.isDirectory(candidate)) return candidate; + dir = dir.getParent(); + } + throw new AssertionError("fixtures/conformance/error-validator-at-least-one-max-unknown not found"); + } + + private static List expected(Path fixture) throws IOException { + JsonObject doc = JsonParser.parseString( + Files.readString(fixture.resolve("expected-errors.json"), StandardCharsets.UTF_8)).getAsJsonObject(); + List out = new ArrayList<>(); + for (JsonElement e : doc.getAsJsonArray("errors")) { + JsonObject err = e.getAsJsonObject(); + out.add(err.get("code").getAsString() + " " + err.getAsJsonObject("source").get("jsonPath").getAsString()); + } + return out; + } + + private static String describe(MetaDataException e) { + String code = e.getCode().map(Enum::name).orElse("ERR_UNKNOWN"); + String path = e.getEnvelope().filter(JsonSource.class::isInstance) + .map(s -> ((JsonSource) s).jsonPath()).orElse(""); + return code + " " + path; + } + + private static List load(Path fixture, boolean strict) throws IOException { + MetaDataLoader loader = new MetaDataLoader(LoaderOptions.create(false, false, strict), + MetaDataLoader.SUBTYPE_MANUAL, "acme"); + loader.init(); + List sources = new ArrayList<>(); + try (Stream files = Files.list(fixture.resolve("input"))) { + for (Path f : files.filter(p -> p.toString().endsWith(".json")).sorted().toList()) { + sources.add(new InMemoryStringSource(Files.readString(f, StandardCharsets.UTF_8), f.getFileName().toString())); + } + } + List seen = new ArrayList<>(); + MetaDataException thrown = null; + try { + loader.load(sources); + } catch (MetaDataException e) { + thrown = e; + } + for (MetaDataException recorded : loader.getErrors()) seen.add(describe(recorded)); + if (thrown != null) seen.add(describe(thrown)); + return seen; + } + + @Test + public void theDefaultLoadRefusesItOnce() throws IOException { + Path fixture = fixture(); + assertEquals(expected(fixture), load(fixture, false)); + } + + @Test + public void theStrictLoadRefusesItOnce() throws IOException { + Path fixture = fixture(); + assertEquals(expected(fixture), load(fixture, true)); + } +} diff --git a/server/java/metadata/src/test/java/com/metaobjects/loader/PortScopedAttrTest.java b/server/java/metadata/src/test/java/com/metaobjects/loader/PortScopedAttrTest.java index 804e62e91f..9b2c99aa34 100644 --- a/server/java/metadata/src/test/java/com/metaobjects/loader/PortScopedAttrTest.java +++ b/server/java/metadata/src/test/java/com/metaobjects/loader/PortScopedAttrTest.java @@ -10,8 +10,8 @@ import static org.junit.Assert.assertTrue; /** - * {@code @collectionName} is shared vocabulary marked {@code implementedIn: ["csharp"]} - * (the DbSet property it names is generated there). Java registers and + * {@code @collectionName} is shared vocabulary marked {@code implementedIn: ["csharp", "typescript"]} + * (the DbSet property, REST segment and generated names it sets are generated there). Java registers and * type-checks it, so a model that carries it loads strictly here. The never-released * {@code @dbSetName} it replaced is not vocabulary; an attribute the registry does not carry still * fails, and a wrong value type fails. diff --git a/server/java/metadata/src/test/java/com/metaobjects/reporting/ReportShapeTest.java b/server/java/metadata/src/test/java/com/metaobjects/reporting/ReportShapeTest.java index 34eefb9b05..3fe6595a58 100644 --- a/server/java/metadata/src/test/java/com/metaobjects/reporting/ReportShapeTest.java +++ b/server/java/metadata/src/test/java/com/metaobjects/reporting/ReportShapeTest.java @@ -836,7 +836,7 @@ public void viaDimensionIsRequiredWhenEveryHopIsARequiredBelongsTo() { ReportShape plain = ReportShape.of(object(root, "Plain"), root); assertTrue("two required hops over a required field", field(plain, "realmName").required()); assertTrue("one required hop", field(plain, "sessionRealmId").required()); - assertTrue("@enforce: false does not change the join type", field(plain, "looseRealmName").required()); + assertFalse("an @enforce: false hop can name a missing row", field(plain, "looseRealmName").required()); assertFalse("the target field is optional", field(plain, "realmMotto").required()); assertFalse("a hop over a nullable foreign key is a LEFT join", field(plain, "maybeRealmName").required()); @@ -917,4 +917,28 @@ public void aViaDimensionsBareCrossPackageOfDerivesTheTerminalsFieldType() { assertSame("ofEntity resolves in the terminal's package too", object(root, "Realm"), shape.ofEntity(realmName)); } + // A joined dimension's required comes from the model, never from the join type the view + // lowering picks: a required, enforced belongs-to path onto a target that hides no rows. The shared + // table is the fixture's expected-report-required.json, which every port reads. + @Test + public void aDimensionsRequiredIsTheSharedTables() throws IOException { + Path fixture = corpusDir().getParent().resolve("conformance/report-dimension-required-by-model"); + com.google.gson.JsonObject doc = com.google.gson.JsonParser.parseString( + Files.readString(fixture.resolve("expected-report-required.json"), StandardCharsets.UTF_8)) + .getAsJsonObject(); + String reportKey = doc.get("report").getAsString(); + java.util.Map expected = new java.util.LinkedHashMap<>(); + for (java.util.Map.Entry e : doc.getAsJsonObject("required").entrySet()) { + expected.put(e.getKey(), e.getValue().getAsBoolean()); + } + MetaRoot root = MetaDataLoader.fromDirectory("report-required", fixture.resolve("input")).getRoot(); + MetaObject report = null; + for (MetaObject o : root.getChildren(MetaObject.class, false)) { + if (reportKey.equals(o.getName())) report = o; + } + assertNotNull(reportKey, report); + java.util.Map actual = new java.util.LinkedHashMap<>(); + for (ReportShape.Field f : ReportShape.of(report, root).fields()) actual.put(f.name(), f.required()); + assertEquals(expected, actual); + } } diff --git a/server/java/omdb/src/main/java/com/metaobjects/manager/db/SimpleMappingHandlerDB.java b/server/java/omdb/src/main/java/com/metaobjects/manager/db/SimpleMappingHandlerDB.java index d0434f41b5..e0269cb561 100644 --- a/server/java/omdb/src/main/java/com/metaobjects/manager/db/SimpleMappingHandlerDB.java +++ b/server/java/omdb/src/main/java/com/metaobjects/manager/db/SimpleMappingHandlerDB.java @@ -148,9 +148,11 @@ protected void loadColumns( Collection fields, BaseTableDef table, Ob String col = getColumnRef( mf ); if ( col == null ) continue; - // Make sure it's not for view's only + // Make sure it's not for view's only. A table never stores a derived (origin.*) + // field: only a view computes one. if ( table instanceof TableDef ) { if ( TRUE.equals( getPersistenceAttribute( mf, IS_VIEWONLY ))) continue; + if ( mf.isDerived() ) continue; } // Create the column definition diff --git a/server/java/omdb/src/test/java/com/metaobjects/manager/db/DerivedFieldWithoutReadSourceMappingTest.java b/server/java/omdb/src/test/java/com/metaobjects/manager/db/DerivedFieldWithoutReadSourceMappingTest.java new file mode 100644 index 0000000000..3fc7670884 --- /dev/null +++ b/server/java/omdb/src/test/java/com/metaobjects/manager/db/DerivedFieldWithoutReadSourceMappingTest.java @@ -0,0 +1,57 @@ +package com.metaobjects.manager.db; + +import com.metaobjects.loader.MetaDataLoader; +import com.metaobjects.manager.db.defs.BaseTableDef; +import com.metaobjects.manager.db.defs.ColumnDef; +import com.metaobjects.manager.db.defs.TableDef; +import com.metaobjects.object.MetaObject; +import org.junit.Test; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * An entity may carry a derived ({@code origin.*}) field that only a projection reads, with no + * read view of its own: the shared fixture {@code entity-derived-filter-gate} + * ({@code Member.hasActiveAgreement}, an {@code origin.first} a projection's {@code @filter} + * reaches). Its table, the one {@code meta migrate} creates, holds only {@code id}. So the table + * mapping OMDB reads and writes {@code members} through must name no column for the derived field. + */ +public class DerivedFieldWithoutReadSourceMappingTest { + + private static Path fixtureInput() { + Path dir = Paths.get("").toAbsolutePath(); + while (dir != null) { + Path candidate = dir.resolve("fixtures/conformance/entity-derived-filter-gate/input"); + if (Files.isDirectory(candidate)) return candidate; + dir = dir.getParent(); + } + throw new AssertionError("fixtures/conformance/entity-derived-filter-gate not found"); + } + + private static List columns(ObjectMappingDB mapping) { + return ((BaseTableDef) mapping.getDBDef()).getColumns().stream() + .map(ColumnDef::getName).collect(Collectors.toList()); + } + + @Test + public void theTableMappingNamesNoColumnForTheDerivedField() { + MetaDataLoader loader = MetaDataLoader.fromDirectory("entity-derived-filter-gate", fixtureInput()); + MetaObject member = null; + for (MetaObject o : loader.getRoot().getChildren(MetaObject.class, false)) { + if ("Member".equals(o.getShortName())) member = o; + } + SimpleMappingHandlerDB handler = new SimpleMappingHandlerDB(); + + ObjectMappingDB read = (ObjectMappingDB) handler.getReadMapping(member); + assertTrue(read.getDBDef() instanceof TableDef); + assertEquals(List.of("id"), columns(read)); + assertEquals(List.of("id"), columns((ObjectMappingDB) handler.getCreateMapping(member))); + } +} diff --git a/server/python/src/metaobjects/core_types.py b/server/python/src/metaobjects/core_types.py index f93f4c7f01..003a737eb2 100644 --- a/server/python/src/metaobjects/core_types.py +++ b/server/python/src/metaobjects/core_types.py @@ -134,6 +134,7 @@ ORIGIN_ATTR_DISTINCT, ORIGIN_ATTR_EXPR, ORIGIN_ATTR_FILTER, + ORIGIN_ATTR_SEGMENT, ORIGIN_ATTR_FROM, ORIGIN_ATTR_OF, ORIGIN_ATTR_ORDER_BY, @@ -1028,6 +1029,7 @@ def _register_subtypes( AttrSchema(name=ORIGIN_ATTR_FILTER, value_type=ATTR_SUBTYPE_FILTER, required=False), AttrSchema(name=ORIGIN_ATTR_DISTINCT, value_type=ATTR_SUBTYPE_BOOLEAN, required=False), AttrSchema(name=ORIGIN_ATTR_ORDER_BY, value_type=ATTR_SUBTYPE_STRING, required=False, is_array=True), + AttrSchema(name=ORIGIN_ATTR_SEGMENT, value_type=ATTR_SUBTYPE_STRING, required=False), ], child_rules=[ChildRule(TYPE_ATTR, "*")], ) @@ -1059,6 +1061,7 @@ def _register_subtypes( AttrSchema(name=ORIGIN_ATTR_VIA, value_type=ATTR_SUBTYPE_STRING, required=False), AttrSchema(name=ORIGIN_ATTR_ORDER_BY, value_type=ATTR_SUBTYPE_STRING, required=True, is_array=True), AttrSchema(name=ORIGIN_ATTR_FILTER, value_type=ATTR_SUBTYPE_FILTER, required=False), + AttrSchema(name=ORIGIN_ATTR_SEGMENT, value_type=ATTR_SUBTYPE_STRING, required=False), ], child_rules=[ChildRule(TYPE_ATTR, "*")], ) diff --git a/server/python/src/metaobjects/loader/validate_source_escapes.py b/server/python/src/metaobjects/loader/validate_source_escapes.py index 0382672d4d..0b7f604f6e 100644 --- a/server/python/src/metaobjects/loader/validate_source_escapes.py +++ b/server/python/src/metaobjects/loader/validate_source_escapes.py @@ -29,10 +29,8 @@ from ..errors import ErrorCode, MetaError from ..meta.core.field.meta_field import MetaField -from ..meta.core.object.object_constants import ( - OBJECT_PROJECTION_ATTR_FILTER, - OBJECT_SUBTYPE_PROJECTION, -) +from ..meta.core.object.effective_projection_filter import effective_projection_filter +from ..meta.core.object.object_constants import OBJECT_SUBTYPE_PROJECTION from ..meta.meta_data import MetaData from ..meta.persistence.source.meta_source import MetaSource from ..meta.persistence.source.source_constants import ( @@ -188,9 +186,9 @@ def validate_source_escapes( # body (and its WHERE), so wrapping it is deferred cleverness # (design doc D5) — reject. if has_sql_host and obj.sub_type == OBJECT_SUBTYPE_PROJECTION: - # ADR-0039: own — the @filter is declared locally on this - # projection (mirrors _validate_projection_filter). - filter_value = obj.attr(OBJECT_PROJECTION_ATTR_FILTER) + # The EFFECTIVE @filter: a gate inherited from a super projection + # lowers to the same outer WHERE (mirrors _validate_projection_filter). + filter_value = effective_projection_filter(obj) if filter_value is not None: errors.append( MetaError( diff --git a/server/python/src/metaobjects/loader/validation_passes.py b/server/python/src/metaobjects/loader/validation_passes.py index 8ff9af3e8e..49992127a9 100644 --- a/server/python/src/metaobjects/loader/validation_passes.py +++ b/server/python/src/metaobjects/loader/validation_passes.py @@ -10,6 +10,7 @@ import json import math import re +from collections.abc import Mapping from typing import Any, Callable, NamedTuple from ..errors import ErrorCode, MetaError @@ -95,6 +96,7 @@ TYPE_OBJECT, TYPE_ORIGIN, TYPE_RELATIONSHIP, + TYPE_SEGMENT, TYPE_REQUIREMENT, TYPE_SOURCE, TYPE_TEMPLATE, @@ -118,6 +120,11 @@ ORIGIN_ATTR_EXPR, ORIGIN_ATTR_FILTER, ORIGIN_ATTR_FROM, + ORIGIN_ATTR_SEGMENT, + CORRELATED_FILTER_OPS, + FILTER_OP_IS_DISTINCT_FROM, + FILTER_OP_IS_NOT_DISTINCT_FROM, + FILTER_OPERAND_FIELD, ORIGIN_ATTR_OF, ORIGIN_ATTR_ORDER_BY, ORIGIN_ATTR_VIA, @@ -142,7 +149,6 @@ ) from ..shared.separators import PACKAGE_SEP from ..meta.core.object.object_constants import ( - OBJECT_PROJECTION_ATTR_FILTER, OBJECT_SUBTYPE_ENTITY, OBJECT_SUBTYPE_PROJECTION, OBJECT_SUBTYPE_VALUE, @@ -158,6 +164,9 @@ from ..source import resolved_source from ..naming_refs import did_you_mean_hint, resolve_object_ref, referrer_package_of from ..naming_refs import ref_target as bound_ref_target +from ..meta.core.field.flattened_member import resolve_flattened_member +from ..meta.core.object.effective_projection_filter import effective_projection_filter +from ..meta.core.relationship.filter_path import resolve_filter_path from ..meta.core.relationship.relationship_references import ( reference_candidates_for, reference_fields, @@ -218,6 +227,9 @@ def run_validations( _validate_datagrid_filter_values(root, errors) # #207 — object.projection view-level @filter reference validation. _validate_projection_filter(root, errors) + # Origin @segment names a segment of the related entity; a correlated @filter operand names + # a base-row field. + _validate_origin_segments_and_correlation(root, errors) # FR-044 — the reporting vocabulary (dimension / measure / segment / # object.report) and relative-date filter values (rules D1-F2). validate_reporting(root, errors) @@ -267,7 +279,7 @@ def run_validations( # identity.primary @sequenceStart / @sequenceName need @generation "increment". _validate_identity_sequence(root, errors) # cross-field validators — exactlyOne names two or more fields; @condition names entity fields. - _validate_cross_field_validators(root, errors) + _validate_cross_field_validators(root, errors, strict) # field.map — exactly one of @valueType (scalar) or @objectRef (VO), and # @valueType (when set) must name a known scalar subtype. _validate_field_map(root, errors) @@ -1528,6 +1540,124 @@ def _check_datagrid_filter_clauses( # origin.* never inherits (ADR-0029/0039), mirroring the TS ownAttr/ownChildren. +def _validate_origin_segments_and_correlation(root: MetaData, errors: list[MetaError]) -> None: + """Origin segments and correlated filters, on every origin.first / origin.aggregate: + + * ``@segment`` names a segment.filter declared on the related entity (the ``@of`` entity, + else the ``@via`` terminal) — ERR_INVALID_ORIGIN otherwise; + * an ``@filter`` operand ``{ field: "" }`` names a field of the base row (the + projection's base entity, or the entity hosting the field), with an op that compares two + columns; the null-safe ops take only such an operand — ERR_BAD_ATTR_FILTER otherwise. + + Mirrors the TS ``validateOriginSegmentsAndCorrelation``. + """ + for obj in root.own_children(): + if obj.type != TYPE_OBJECT or obj.sub_type not in (OBJECT_SUBTYPE_PROJECTION, OBJECT_SUBTYPE_ENTITY): + continue + pkg = obj.package or "" + # ADR-0039 sanctioned own: origin.* never inherits; each is judged where declared. + for field in (c for c in obj.own_children() if c.type == TYPE_FIELD): + for origin in field.own_children(): + if origin.type != TYPE_ORIGIN or origin.sub_type not in ( + ORIGIN_SUBTYPE_FIRST, ORIGIN_SUBTYPE_AGGREGATE + ): + continue + where = f"origin.{origin.sub_type} on {obj.name}.{field.name}" + segment = origin.attr(ORIGIN_ATTR_SEGMENT) + if isinstance(segment, str): + related = _origin_related_entity(origin, root, pkg) + if related is not None and not any( + # ADR-0039: resolving — a segment may be inherited through extends. + c.type == TYPE_SEGMENT and c.name == segment for c in related.children() + ): + errors.append(MetaError( + f'{where}: @segment "{segment}" is not a segment declared on ' + f'"{related.name}", the entity whose rows it scopes.', + ErrorCode.ERR_INVALID_ORIGIN, envelope=origin.source)) + filter_value = origin.attr(ORIGIN_ATTR_FILTER) + if filter_value is None: + continue + base = _derive_base_entity(obj, root, pkg, field.name, origin.source, []) + if base is not None: + _check_correlated_operands(filter_value, base, where, origin.source, errors) + + +def _origin_related_entity(origin: MetaData, root: MetaData, pkg: str) -> MetaData | None: + """The entity an origin.first / origin.aggregate spans: its ``@of`` entity, else its ``@via`` + terminal. ``None`` when neither resolves (the origin pass reports why).""" + of_ref = origin.attr(ORIGIN_ATTR_OF) + if isinstance(of_ref, str): + target = _validate_entity_field_ref(of_ref, ORIGIN_ATTR_OF, "", root, pkg, [], origin, "") + if target is not None: + return target[0] + via = origin.attr(ORIGIN_ATTR_VIA) + if isinstance(via, str): + walked = _validate_via_path(via, "", root, pkg, [], origin, "") + if walked is not None: + return walked.terminal + return None + + +def _check_correlated_operands( + filter_value: object, base: MetaData, where: str, source: object, errors: list[MetaError] +) -> None: + if not isinstance(filter_value, dict): + return + for key, clause in filter_value.items(): + if key in (_FILTER_COMPOSE_AND, _FILTER_COMPOSE_OR): + if isinstance(clause, list): + for sub in clause: + _check_correlated_operands(sub, base, where, source, errors) + continue + if not isinstance(clause, dict): + continue + for op, value in clause.items(): + is_operand = isinstance(value, dict) and FILTER_OPERAND_FIELD in value + null_safe = op in (FILTER_OP_IS_DISTINCT_FROM, FILTER_OP_IS_NOT_DISTINCT_FROM) + if not is_operand: + if null_safe: + errors.append(MetaError( + f'{where}: @filter on "{key}" uses "{op}", which compares with a base-row field: ' + f'write {{ {op}: {{ {FILTER_OPERAND_FIELD}: "" }} }}.', + ErrorCode.ERR_BAD_ATTR_FILTER, envelope=source)) + continue + name = value[FILTER_OPERAND_FIELD] + if op not in CORRELATED_FILTER_OPS: + errors.append(MetaError( + f'{where}: @filter on "{key}" compares with a base-row field using "{op}"; a ' + f"correlated operand takes {', '.join(CORRELATED_FILTER_OPS)}.", + ErrorCode.ERR_BAD_ATTR_FILTER, envelope=source)) + continue + # ADR-0039: resolving — a base field may be inherited. + if not (isinstance(name, str) and any( + c.type == TYPE_FIELD and c.name == name for c in base.children() + )): + errors.append(MetaError( + f'{where}: @filter on "{key}" names base-row field "{name}", which "{base.name}" ' + f"does not declare.", + ErrorCode.ERR_BAD_ATTR_FILTER, envelope=source)) + + +def _entity_field_aggregates(f: MetaData) -> bool: + # ADR-0039: own — origin.* never inherits (ADR-0029). + return any(c.type == TYPE_ORIGIN and c.sub_type == ORIGIN_SUBTYPE_AGGREGATE for c in f.own_children()) + + +def _filter_keys(filter_value: object, out: list[str] | None = None) -> list[str]: + """Every field key of a (desugared) filter, through and/or.""" + out = [] if out is None else out + if not isinstance(filter_value, dict): + return out + for key, val in filter_value.items(): + if key in (_FILTER_COMPOSE_AND, _FILTER_COMPOSE_OR): + if isinstance(val, list): + for sub in val: + _filter_keys(sub, out) + else: + out.append(key) + return out + + def _validate_projection_filter( root: MetaData, errors: list[MetaError], @@ -1537,44 +1667,64 @@ def _validate_projection_filter( for obj in root.own_children(): if obj.type != TYPE_OBJECT or obj.sub_type != OBJECT_SUBTYPE_PROJECTION: continue - # ADR-0039 sanctioned own: the @filter is declared locally on this projection. - filter_value = obj.attr(OBJECT_PROJECTION_ATTR_FILTER) + # The EFFECTIVE @filter — every level of an extends chain, supers first, ANDed — is + # what the view applies, so it is what is checked. + filter_value = effective_projection_filter(obj) # Non-object shapes are rejected by the attr-schema pass (ERR_BAD_ATTR_VALUE). if not isinstance(filter_value, dict): continue - # Classify the projection's OWN fields (the declared set IS the exposure — - # FR-024/ADR-0028): the declared-field name set + the aggregate-derived - # subset. origin.* never inherits (ADR-0029), so the origin reads are own. - declared: set[str] = set() - aggregate_derived: set[str] = set() - for f in obj.own_children(): + # The projection's EFFECTIVE fields (the declared set IS the exposure — FR-024 / + # ADR-0028), each marked aggregate-derived or not. ADR-0039: resolving — a field + # inherited from a super projection is a column of this view; its origin is read own. + fields: dict[str, bool] = {} + for f in obj.children(): if f.type != TYPE_FIELD: continue - declared.add(f.name) - origin = next( - (c for c in f.own_children() if c.type == TYPE_ORIGIN), None - ) - if ( + origin = next((c for c in f.own_children() if c.type == TYPE_ORIGIN), None) + fields[f.name] = ( origin is not None and origin.sub_type != ORIGIN_SUBTYPE_PASSTHROUGH and origin.sub_type != ORIGIN_SUBTYPE_COMPUTED - ): - aggregate_derived.add(f.name) + ) + reported: set[str] = set() + # A field of the BASE entity the view does not output is filterable too (a WHERE reads + # the base row); a projection field of the same name wins. No base, no fallback: the + # origin passes report why one cannot be derived. + base = _derive_base_entity(obj, root, obj.package or "", "@filter", obj.source, []) + if base is not None and base is not obj: + for f in base.children(): + if f.type == TYPE_FIELD and f.name not in fields: + fields[f.name] = _entity_field_aggregates(f) + # A key that is a PATH of to-one hops from the base, then a field of the entity + # reached (``caseId.status``). Not output; the view joins what it walks. + for key in _filter_keys(filter_value): + if key in fields: + continue + path = resolve_filter_path(base, key) + if path is None: + continue + if isinstance(path, str): + errors.append(MetaError( + f'projection "{obj.name}" @filter path "{key}" does not resolve: {path}.', + ErrorCode.ERR_BAD_ATTR_FILTER, envelope=obj.source)) + fields[key] = False + reported.add(key) + continue + fields[key] = _entity_field_aggregates(path.field) - _check_projection_filter_refs( - filter_value, declared, aggregate_derived, obj, errors - ) + _check_projection_filter_refs(filter_value, fields, reported, obj, errors) def _check_projection_filter_refs( filter_obj: dict, - declared: set[str], - aggregate_derived: set[str], + fields: dict[str, bool], + reported: set[str], obj: MetaData, errors: list[MetaError], ) -> None: - """Recursively check each field-ref key of a (possibly composed) @filter.""" + """Recursively check each field-ref key of a (possibly composed) @filter. *fields* maps each + referable name to whether it is aggregate-derived; *reported* names keys already refused.""" for key, clause in filter_obj.items(): if key == _FILTER_COMPOSE_AND or key == _FILTER_COMPOSE_OR: # Recurse into each OBJECT sub-clause. A non-array clause / non-object @@ -1584,24 +1734,24 @@ def _check_projection_filter_refs( continue for sub in clause: if isinstance(sub, dict): - _check_projection_filter_refs( - sub, declared, aggregate_derived, obj, errors - ) + _check_projection_filter_refs(sub, fields, reported, obj, errors) continue # A field-ref key. - if key not in declared: + if key in reported: + continue + if key not in fields: errors.append( MetaError( f"projection '{obj.name}' @filter references '{key}', which is not " - f"a declared field of the projection. A view-level @filter may only " - f"reference the projection's own declared fields.", + f"a declared field of the projection or of its base entity. A view-level " + f"@filter may only reference those fields.", ErrorCode.ERR_BAD_ATTR_FILTER, envelope=obj.source, ) ) continue - if key in aggregate_derived: + if fields[key]: errors.append( MetaError( f"projection '{obj.name}' @filter references '{key}', an " @@ -1747,6 +1897,11 @@ def _validate_entity_field_ref( ) return None field_node = next((f for f in entity.fields() if f.name == field_name), None) + if field_node is None: + # A member of a flattened value object, through the field that embeds it. + flattened = resolve_flattened_member(entity, field_name) + if flattened is not None: + field_node = flattened.member if field_node is None: # FR5d — entity resolved, field on it did not. target = full ref. known = sorted(f.name for f in entity.fields()) @@ -1789,8 +1944,9 @@ class WalkedViaPath(NamedTuple): hops: list[MetaData] terminal: MetaData - #: ids of the hops that walk an identity.reference backwards (to-many by construction). - reverse: frozenset[int] = frozenset() + #: ids of the hops that walk an identity.reference backwards (to-many by construction), + #: each mapped to the hop name the path wrote. + reverse: Mapping[int, str] | None = None def _validate_via_path( @@ -1850,7 +2006,7 @@ def _validate_via_path( # resolve so authors can fix multi-hop typos quickly. valid_segments: list[str] = [current_name] hops: list[MetaData] = [] - reverse: set[int] = set() + reverse: dict[int, str] = {} for rel_name in segments[1:]: rels = _relationships_by_name(current_entity) # FR-024: a hop may name a relationship OR a reference-only FK @@ -1866,7 +2022,7 @@ def _validate_via_path( holder, ref = reversed_hops[0] valid_segments.append(rel_name) hops.append(ref) - reverse.add(id(ref)) + reverse[id(ref)] = rel_name current_entity = holder continue if len(reversed_hops) > 1: @@ -1935,7 +2091,7 @@ def _validate_via_path( current_entity = next_entity # current_entity is the terminal: every earlier exit returned None. - return WalkedViaPath(hops, current_entity, frozenset(reverse)) + return WalkedViaPath(hops, current_entity, reverse) # --------------------------------------------------------------------------- @@ -1945,11 +2101,11 @@ def _validate_via_path( # --------------------------------------------------------------------------- -def _hop_cardinality(rel: MetaData, reverse: frozenset[int] = frozenset()) -> str | None: +def _hop_cardinality(rel: MetaData, reverse: Mapping[int, str] | None = None) -> str | None: """A hop's effective @cardinality, or None when not declared. A reference hop (a forward FK) is inherently to-one — a child names the parent it points at; walked backwards (in *reverse*) it is to-many.""" - if id(rel) in reverse: + if reverse and id(rel) in reverse: return CARDINALITY_MANY if _is_reference_hop(rel): return CARDINALITY_ONE @@ -2047,6 +2203,11 @@ def _derive_base_entity( targets.append(owner) if len(targets) == 1: return targets[0] + # 3) A projection extending another projection, declaring no anchor of its own, has the + # base entity its super has (its inherited identity and fields are anchored there). + sup = obj.super_data + if not targets and sup is not None and sup.type == TYPE_OBJECT and sup.sub_type == OBJECT_SUBTYPE_PROJECTION: + return _derive_base_entity(sup, root, sup.package or referrer_pkg, field_name, origin_source, errors) if len(targets) > 1: names = ", ".join(f'"{t.name}"' for t in targets) errors.append( @@ -2126,10 +2287,11 @@ def _infer_via_single_hop( def _check_passthrough_cardinality( hops: list[MetaData], + obj: MetaData, field_name: str, origin_source: object, errors: list[MetaError], - reverse: frozenset[int] = frozenset(), + reverse: Mapping[int, str] | None = None, ) -> None: """ADR-0029 decision 6 — a passthrough via-path must be effectively to-one at EVERY hop. A hop is to-many only when it DECLARES @cardinality "many".""" @@ -2137,10 +2299,11 @@ def _check_passthrough_cardinality( if _hop_cardinality(rel, reverse) == CARDINALITY_MANY: errors.append( MetaError( - f"origin.passthrough on {field_name}: @via hop '{rel.name}' is to-many " + f'origin.passthrough on {obj.name}.{field_name}: @via hop ' + f'"{reverse[id(rel)] if reverse and id(rel) in reverse else rel.name}" is to-many ' + ( f'(reverse hop over identity.reference "{rel.name}")' - if id(rel) in reverse + if reverse and id(rel) in reverse else f'(@cardinality "{CARDINALITY_MANY}")' ) + " — a row-multiplying passthrough — you meant aggregate (ADR-0029).", @@ -2156,7 +2319,7 @@ def _check_aggregate_cardinality( field_name: str, origin_source: object, errors: list[MetaError], - reverse: frozenset[int] = frozenset(), + reverse: Mapping[int, str] | None = None, ) -> None: """ADR-0029 decision 6 — an aggregate via-path must contain at least one to-many hop. Conservative: fires only when PROVABLY to-one (every hop @@ -2457,7 +2620,7 @@ def _validate_origin_paths( walked = _validate_via_path(via, ctx, root, host_pkg, errors, origin, referrer) if walked is not None: _check_passthrough_cardinality( - walked.hops, node.name, origin.source, errors, walked.reverse + walked.hops, obj, node.name, origin.source, errors, walked.reverse ) elif from_target is not None and not is_value_host: # FR-024 §6 — no @via: derive the base entity; a @from on the @@ -2475,7 +2638,7 @@ def _validate_origin_paths( ) if hops is not None: _check_passthrough_cardinality( - hops, node.name, origin.source, errors + hops, obj, node.name, origin.source, errors ) elif origin.sub_type == ORIGIN_SUBTYPE_AGGREGATE: @@ -2772,6 +2935,31 @@ def _resolve_field( f"origin.computed on {obj.name}.{node.name}: {m}", ErrorCode.ERR_INVALID_ORIGIN, envelope=src)) continue + # A literal compared with a field must be able to equal it: of its kind, and for + # an enum one of its @values (the rule a validator's @condition follows). Resolved + # as the inference resolved the ref: a projection field first, else a base field. + def _field_of( + name: str, _base: MetaData = base, _obj: MetaData = obj, _self: str = node.name + ) -> MetaData | None: + if _obj is not _base and name != _self: + for own in _obj.children(): + if own.type != TYPE_FIELD or own.name != name: + continue + own_origin = _own_origin_of(own) + if own_origin is not None and own_origin.sub_type == ORIGIN_SUBTYPE_AGGREGATE: + return None + return own + for f in _base.children(): + if f.type == TYPE_FIELD and f.name == name: + return f + return None + + literal_problem = literal_comparison_problem(expr, _field_of) + if literal_problem is not None: + errors.append(MetaError( + f"origin.computed on {obj.name}.{node.name}: @expr {literal_problem}", + ErrorCode.ERR_INVALID_ORIGIN, envelope=src)) + continue if inferred.type is not None and inferred.type != node.sub_type: errors.append(MetaError( f"origin.computed on {obj.name}.{node.name}: @expr infers " @@ -3556,9 +3744,50 @@ def _validate_identity_passthrough(root: MetaData, errors: list[MetaError]) -> N # Mirrors TS validateDerivedFieldProvidability. +def _projection_reached_entity_fields(root: MetaData) -> set[int]: + """ids of the entity fields projections REACH without outputting them: a projection + ``@filter`` key naming a base field or a to-one path, and a passthrough ``@from``. A derived + field reached this way is lowered inline where it is reached. Mirrors the TS + ``projectionReachedEntityFields``.""" + reached: set[int] = set() + for obj in root.own_children(): + if obj.type != TYPE_OBJECT or obj.sub_type != OBJECT_SUBTYPE_PROJECTION: + continue + pkg = obj.package or "" + base = _derive_base_entity(obj, root, pkg, "@filter", obj.source, []) + # The effective filter: an extender reaches what its supers' filters name, too. + filter_value = effective_projection_filter(obj) + if base is not None and base is not obj: + for key in _filter_keys(filter_value): + path = resolve_filter_path(base, key) + if path is not None: + if not isinstance(path, str): + reached.add(id(path.field)) + continue + f = next((c for c in base.children() if c.type == TYPE_FIELD and c.name == key), None) + if f is not None: + reached.add(id(f)) + # ADR-0039: resolving — inherited fields carry their own origins (read own). + for field in (c for c in obj.children() if c.type == TYPE_FIELD): + for origin in field.own_children(): + if origin.type != TYPE_ORIGIN or origin.sub_type != ORIGIN_SUBTYPE_PASSTHROUGH: + continue + from_ref = origin.attr(ORIGIN_ATTR_FROM) + if not isinstance(from_ref, str): + continue + # Silent: the origin pass reports an unresolved @from. + target = _validate_entity_field_ref( + from_ref, ORIGIN_ATTR_FROM, "", root, pkg, [], origin, "" + ) + if target is not None: + reached.add(id(target[1])) + return reached + + def _validate_derived_field_providability( root: MetaData, errors: list[MetaError] ) -> None: + reached = _projection_reached_entity_fields(root) # ADR-0039: root scan (sanctioned own — never extended); the read-capable-source # check uses the resolving obj.children() (inherited sources count); the derived- # field check inspects OWN fields with OWN origins (sanctioned own — origin.* @@ -3579,15 +3808,22 @@ def _validate_derived_field_providability( # ADR-0039 sanctioned own: derived-field providability inspects the object's # OWN fields carrying an OWN origin.* child (origin.* never inherits, ADR-0029). for field in (c for c in obj.own_children() if c.type == TYPE_FIELD): - if not any(c.type == TYPE_ORIGIN for c in field.own_children()): + origin = next((c for c in field.own_children() if c.type == TYPE_ORIGIN), None) + if origin is None: + continue + # A derived field a projection reaches (its @filter or a passthrough @from) is + # lowered inline there, so it needs no read source. Only the inlinable origins. + if origin.sub_type in (ORIGIN_SUBTYPE_FIRST, ORIGIN_SUBTYPE_COMPUTED) and id(field) in reached: continue errors.append( MetaError( f'derived field "{obj.name}.{field.name}" carries an origin.* but ' f'entity "{obj.name}" declares no read-capable source — derived fields ' f"do not exist on the writable table. Declare a read-only source " - f'(e.g. source.rdb @kind "view" @role "replica") to provide it, or move ' - f"the field to an object.projection (FR-024 §7).", + f'(e.g. source.rdb @kind "view" @role "replica") to provide it, move ' + f"the field to an object.projection (FR-024 §7), or reach it from a " + f"projection's @filter or @from (an origin.first or origin.computed is then " + f"inlined there).", ErrorCode.ERR_DERIVED_FIELD_NO_READ_SOURCE, envelope=field.source, ) @@ -4141,58 +4377,74 @@ def _condition_problem(condition: object, obj: MetaData) -> str | None: f"them), not a field.{inferred.type}." ) - def visit(node: object) -> str | None: - if not isinstance(node, dict): + for n in _expr_nodes(condition): + if n.get("left") is None or n.get("right") is None: + continue + for side in (n["left"], n["right"]): + if isinstance(side, dict) and "value" in side and side["value"] is None: + return "compares with null, which is never true: use isNull or isNotNull." + return literal_comparison_problem(condition, fields.get) + + +def literal_comparison_problem(expr: object, field_of: Any) -> str | None: + """The first literal in *expr* that cannot equal the field it is compared with, or None. A + literal is compared with a field by a comparison (either side) or as a simple case's ``when`` + against a ``{field}`` operand. The rule, the same in every port and shared by a validator's + @condition and an origin.computed @expr: the literal is of the field's kind (a boolean for + field.boolean, a number for a numeric field, a string otherwise), one of its @values for + field.enum, and an ISO-8601 duration for field.duration. A null literal, and a literal facing + anything but a field ref, are not checked here. *field_of* maps a name to its field, or None. + Mirrors TS literalComparisonProblem.""" + + def check(lit: object, ref: object) -> str | None: + if not isinstance(lit, dict) or "value" not in lit or lit["value"] is None: return None - if node.get("left") is not None and node.get("right") is not None: - for lit, ref in ((node["left"], node["right"]), (node["right"], node["left"])): - if not isinstance(lit, dict) or "value" not in lit: - continue - value = lit["value"] - if value is None: - return "compares with null, which is never true: use isNull or isNotNull." - name = ref.get("field") if isinstance(ref, dict) else None - field = fields.get(name) if isinstance(name, str) else None - if field is None: - continue - sub = field.sub_type - kind = ( - "boolean" if sub == FIELD_SUBTYPE_BOOLEAN - else "number" if sub in _CONDITION_NUMBER_SUBTYPES - else "string" - ) - if not _literal_is_kind(value, kind): - return ( - f'compares "{field.name}" (field.{sub}) with {_json_literal(value)}; ' - f"it needs a {kind}." - ) - members = field.get_meta_attr(FIELD_ATTR_VALUES) if sub == FIELD_SUBTYPE_ENUM else None - if isinstance(members, list) and str(value) not in [str(m) for m in members]: - return ( - f'compares "{field.name}" with {_json_literal(value)}, which is not one ' - f"of its @values ({', '.join(str(m) for m in members)})." - ) - if sub == FIELD_SUBTYPE_DURATION and DURATION_RE.fullmatch(value) is None: - return ( - f'compares "{field.name}" with {_json_literal(value)}, which is not ' - f"{_DURATION_LITERAL_FORM}." - ) - for key in ("left", "right", "arg"): - found = visit(node.get(key)) + name = ref.get("field") if isinstance(ref, dict) else None + field = field_of(name) if isinstance(name, str) else None + if field is None: + return None + value = lit["value"] + sub = field.sub_type + kind = ( + "boolean" if sub == FIELD_SUBTYPE_BOOLEAN + else "number" if sub in _CONDITION_NUMBER_SUBTYPES + else "string" + ) + if not _literal_is_kind(value, kind): + return ( + f'compares "{field.name}" (field.{sub}) with {_json_literal(value)}; ' + f"it needs a {kind}." + ) + members = field.get_meta_attr(FIELD_ATTR_VALUES) if sub == FIELD_SUBTYPE_ENUM else None + if isinstance(members, list) and str(value) not in [str(m) for m in members]: + return ( + f'compares "{field.name}" with {_json_literal(value)}, which is not one ' + f"of its @values ({', '.join(str(m) for m in members)})." + ) + if sub == FIELD_SUBTYPE_DURATION and DURATION_RE.fullmatch(value) is None: + return ( + f'compares "{field.name}" with {_json_literal(value)}, which is not ' + f"{_DURATION_LITERAL_FORM}." + ) + return None + + for n in _expr_nodes(expr): + if n.get("left") is not None and n.get("right") is not None: + found = check(n["left"], n["right"]) or check(n["right"], n["left"]) if found is not None: return found - args = node.get("args") - if isinstance(args, list): - for a in args: - found = visit(a) + arms = n.get(EXPR_CASE) + if n.get(EXPR_CASE_OPERAND) is not None and isinstance(arms, list): + for arm in arms: + found = check(arm.get(EXPR_CASE_WHEN), n[EXPR_CASE_OPERAND]) if isinstance(arm, dict) else None if found is not None: return found - return None - - return visit(condition) + return None -def _validate_cross_field_validators(root: MetaData, errors: list[MetaError]) -> None: +def _validate_cross_field_validators(root: MetaData, errors: list[MetaError], strict: bool = False) -> None: + """Cross-field validator rules. *strict*: a strict load already refuses an atLeastOne ``@max`` + as an unregistered attr, so that check runs only on the default load (one error per node).""" from ..meta.core.validator import validator_constants as vc for obj in root.children(): @@ -4202,6 +4454,25 @@ def _validate_cross_field_validators(root: MetaData, errors: list[MetaError]) -> for v in obj.own_children(): if v.type != TYPE_VALIDATOR or v.sub_type not in vc.CROSS_FIELD_VALIDATOR_SUBTYPES: continue + # validator.atLeastOne takes no @max: "exactly one of these fields" is + # validator.exactlyOne. A strict load already refuses the unregistered attr; this + # refuses it under the default load too, where it would be kept and ignored. + # ADR-0039: own — checked on the validator that declares it. + if ( + not strict + and v.sub_type == vc.VALIDATOR_SUBTYPE_AT_LEAST_ONE + and vc.VALIDATOR_ATTR_MAX in v.own_attrs() + ): + errors.append(MetaError( + code=ErrorCode.ERR_UNKNOWN_ATTR, + message=( + f'validator.atLeastOne "{obj.name}.{v.name}" carries @max, which atLeastOne does ' + 'not take. For "exactly one of these fields", declare validator.exactlyOne instead; ' + 'for "at least one", remove @max.' + ), + envelope=v.source, + )) + continue if v.sub_type == vc.VALIDATOR_SUBTYPE_EXACTLY_ONE: fields = v.get_meta_attr(vc.VALIDATOR_ATTR_FIELDS) count = len(fields) if isinstance(fields, list) else (0 if fields is None else 1) diff --git a/server/python/src/metaobjects/meta/core/field/flattened_member.py b/server/python/src/metaobjects/meta/core/field/flattened_member.py new file mode 100644 index 0000000000..77987dc4f7 --- /dev/null +++ b/server/python/src/metaobjects/meta/core/field/flattened_member.py @@ -0,0 +1,43 @@ +"""A member of a ``@storage: flattened`` value object, addressed through the field that embeds it +(``address.city``): one level, the embedding field on the entity, the member on its +``@objectRef`` value object. Its column lives on the entity's table, so a passthrough ``@from`` or +an ``@of`` may name it. Mirrors the TypeScript ``resolveFlattenedMember``. +""" +from __future__ import annotations + +from typing import NamedTuple + +from metaobjects.meta.core.field.field_constants import ( + FIELD_ATTR_OBJECT_REF, + FIELD_ATTR_STORAGE, + FIELD_SUBTYPE_OBJECT, + STORAGE_FLATTENED, +) +from metaobjects.meta.meta_data import MetaData +from metaobjects.naming_refs import ref_target +from metaobjects.shared.base_types import TYPE_FIELD + + +class FlattenedMember(NamedTuple): + embedding: MetaData + member: MetaData + + +def resolve_flattened_member(entity: MetaData, path: str) -> FlattenedMember | None: + """The ``.`` *path* on *entity*, or ``None`` when it names no member of a + flattened value object.""" + head, dot, tail = path.partition(".") + if not dot or not head or not tail or "." in tail: + return None + # Effective children: the embedding field may be inherited via extends. + embedding = next((c for c in entity.children() if c.type == TYPE_FIELD and c.name == head), None) + if embedding is None or embedding.sub_type != FIELD_SUBTYPE_OBJECT: + return None + # ADR-0039: resolving — @storage / @objectRef may be inherited via extends. + if embedding.get_meta_attr(FIELD_ATTR_STORAGE) != STORAGE_FLATTENED: + return None + vo = ref_target(embedding, FIELD_ATTR_OBJECT_REF) + if vo is None: + return None + member = next((c for c in vo.children() if c.type == TYPE_FIELD and c.name == tail), None) + return None if member is None else FlattenedMember(embedding, member) diff --git a/server/python/src/metaobjects/meta/core/object/effective_projection_filter.py b/server/python/src/metaobjects/meta/core/object/effective_projection_filter.py new file mode 100644 index 0000000000..45f41acd4c --- /dev/null +++ b/server/python/src/metaobjects/meta/core/object/effective_projection_filter.py @@ -0,0 +1,34 @@ +"""A projection's EFFECTIVE ``@filter``: the ``@filter`` declared at every level of its extends +chain, supers first, ANDed flat. An extender cannot drop a gate its super declares, so the view +applies all of them. Mirrors the TypeScript ``effectiveProjectionFilter``. +""" +from __future__ import annotations + +from metaobjects.meta.core.object.object_constants import OBJECT_PROJECTION_ATTR_FILTER +from metaobjects.meta.meta_data import MetaData + +_AND = "and" + + +def _and_members(filter_value: object) -> list[object]: + if isinstance(filter_value, dict) and len(filter_value) == 1 and isinstance(filter_value.get(_AND), list): + return list(filter_value[_AND]) + return [filter_value] + + +def effective_projection_filter(projection: MetaData) -> object | None: + """The ANDed ``@filter`` of *projection* and its supers; ``None`` when no level declares one.""" + levels: list[object] = [] + seen: set[int] = set() + node: MetaData | None = projection + while node is not None and id(node) not in seen: + seen.add(id(node)) + # ADR-0039: own — each level contributes the @filter declared on it; the resolving read + # would return only the nearest one, which is the override this composition replaces. + own = node.own_attrs().get(OBJECT_PROJECTION_ATTR_FILTER) + if own is not None: + levels.insert(0, own) + node = node.super_data + if len(levels) <= 1: + return levels[0] if levels else None + return {_AND: [m for level in levels for m in _and_members(level)]} diff --git a/server/python/src/metaobjects/meta/core/relationship/filter_path.py b/server/python/src/metaobjects/meta/core/relationship/filter_path.py new file mode 100644 index 0000000000..8d1b717b0d --- /dev/null +++ b/server/python/src/metaobjects/meta/core/relationship/filter_path.py @@ -0,0 +1,71 @@ +"""A projection ``@filter`` key that is a PATH from the base entity (``caseId.status``): to-one +hops (an identity.reference, or a ``@cardinality: one`` relationship), then a field of the entity +reached. The view joins what it walks; a to-many hop would multiply rows, so it is refused. +Mirrors the TypeScript ``resolveFilterPath``. +""" +from __future__ import annotations + +from typing import NamedTuple, Union + +from metaobjects.meta.core.identity.identity_constants import ( + IDENTITY_REFERENCE_ATTR_REFERENCES, + IDENTITY_SUBTYPE_REFERENCE, +) +from metaobjects.meta.core.relationship.relationship_constants import ( + CARDINALITY_ONE, + RELATIONSHIP_ATTR_CARDINALITY, + RELATIONSHIP_ATTR_OBJECT_REF, +) +from metaobjects.meta.meta_data import MetaData +from metaobjects.naming_refs import ref_target +from metaobjects.shared.base_types import TYPE_FIELD, TYPE_IDENTITY, TYPE_OBJECT, TYPE_RELATIONSHIP + + +class FilterPath(NamedTuple): + hops: list[str] + entity: MetaData + field: MetaData + + +def _to_one_hop(obj: MetaData, hop: str) -> Union[MetaData, str]: + # ADR-0039: resolving — a hop may be inherited through extends. + ref = next( + (c for c in obj.children() + if c.type == TYPE_IDENTITY and c.sub_type == IDENTITY_SUBTYPE_REFERENCE and c.name == hop), + None, + ) + if ref is not None: + target = ref_target(ref, IDENTITY_REFERENCE_ATTR_REFERENCES) + return target if target is not None and target.type == TYPE_OBJECT else ( + f'reference "{obj.name}.{hop}" does not resolve' + ) + rel = next((c for c in obj.children() if c.type == TYPE_RELATIONSHIP and c.name == hop), None) + if rel is not None: + # ADR-0039: resolving — @cardinality may be inherited. + if rel.get_meta_attr(RELATIONSHIP_ATTR_CARDINALITY) != CARDINALITY_ONE: + return f'"{obj.name}.{hop}" is to-many; a filter path may only walk to-one hops' + target = ref_target(rel, RELATIONSHIP_ATTR_OBJECT_REF) + return target if target is not None and target.type == TYPE_OBJECT else ( + f'relationship "{obj.name}.{hop}" does not resolve' + ) + return f'"{obj.name}" declares no identity.reference or to-one relationship named "{hop}"' + + +def resolve_filter_path(base: MetaData, key: str) -> Union[FilterPath, str, None]: + """The path *key* names from *base*; a message when it does not resolve; ``None`` when *key* + is not a path at all (no dot).""" + segments = key.split(".") + if len(segments) < 2: + return None + hops, field_name = segments[:-1], segments[-1] + current = base + for hop in hops: + nxt = _to_one_hop(current, hop) + if isinstance(nxt, str): + return nxt + current = nxt + # ADR-0039: resolving — the reached field may be inherited. + field = next((c for c in current.children() if c.type == TYPE_FIELD and c.name == field_name), None) + if field is None: + return f'"{current.name}" declares no field "{field_name}"' + return FilterPath(hops, current, field) diff --git a/server/python/src/metaobjects/meta/core/reporting/report_shape.py b/server/python/src/metaobjects/meta/core/reporting/report_shape.py index ae5371eab1..206d513337 100644 --- a/server/python/src/metaobjects/meta/core/reporting/report_shape.py +++ b/server/python/src/metaobjects/meta/core/reporting/report_shape.py @@ -37,10 +37,12 @@ from ..field.meta_field import MetaField from ..identity.identity_constants import ( IDENTITY_ATTR_FIELDS, + IDENTITY_REFERENCE_ATTR_ENFORCE, IDENTITY_REFERENCE_ATTR_REFERENCES, IDENTITY_SUBTYPE_REFERENCE, ) from ..object.meta_object import MetaObject +from ..object.object_constants import OBJECT_ATTR_DISCRIMINATOR from ..relationship.relationship_constants import ( RELATIONSHIP_ATTR_OBJECT_REF, RELATIONSHIP_ATTR_SOURCE_REF_FIELD, @@ -336,9 +338,9 @@ def _via_joins_inner(hops: list[str], from_: MetaObject, root: MetaRoot) -> bool """True when every hop of ``hops``, walked from ``from_``, is a belongs-to hop over a required foreign key: the hops the view joins INNER, so every row of the report has a match. This is the join-type rule of the view lowering (TypeScript ``walkViaPath``, #209): - the reference is held by the entity the hop leaves, its first FK field is required, and - every earlier hop is INNER too. ``@enforce: false`` does not change it. A hop that does - not resolve, an ambiguous reference or a has-many hop is not INNER.""" + the reference is held by the entity the hop leaves, is enforced, its first FK field is + required, its target hides no rows (:func:`_hides_rows`), and every earlier hop matches + too. A hop that does not resolve, an ambiguous reference or a has-many hop does not.""" current = from_ for hop_name in hops: # ADR-0039: resolving children(), so a hop inherited through extends is found. @@ -374,16 +376,38 @@ def _via_joins_inner(hops: list[str], from_: MetaObject, root: MetaRoot) -> bool target, source_ref_field if isinstance(source_ref_field, str) else None, ) - if reference is None: + # ADR-0039: resolving — @enforce may be inherited; absent is enforced. + if reference is None or reference.get_meta_attr(IDENTITY_REFERENCE_ATTR_ENFORCE) is False: return False fk_fields = reference_fields(reference) fk = current.find_field(fk_fields[0]) if fk_fields else None if fk is None or not _is_required_field(fk): return False + if _hides_rows(target): + return False current = target return True +def _hides_rows(obj: MetaData) -> bool: + """True when a join onto ``obj`` carries a row scope of its own: a TPH subtype, or an + abstract level beneath a TPH base (both scoped by the discriminator), whose FK may name a + sibling's row. The model has no soft-delete or query-filter vocabulary, so this is the only + row-hiding scope.""" + # ADR-0039: own — the discriminator is declared on the level that owns it. + if OBJECT_ATTR_DISCRIMINATOR in obj.own_attrs(): + return False + seen = {id(obj)} + sup = obj.super_data + while sup is not None and id(sup) not in seen: + seen.add(id(sup)) + # ADR-0039: own — the TPH base is the ancestor that declares @discriminator. + if OBJECT_ATTR_DISCRIMINATOR in sup.own_attrs(): + return True + sup = sup.super_data + return False + + def _dimension_required( dim: MetaDimension, named: MetaObject, diff --git a/server/python/src/metaobjects/meta/persistence/origin/origin_constants.py b/server/python/src/metaobjects/meta/persistence/origin/origin_constants.py index 04d95b23f7..81bab0c2b8 100644 --- a/server/python/src/metaobjects/meta/persistence/origin/origin_constants.py +++ b/server/python/src/metaobjects/meta/persistence/origin/origin_constants.py @@ -60,6 +60,15 @@ # predicate (required there); on origin.first it scopes the eligible rows. Codegen renders # it per target -- the projection view emitter turns it into SQL FILTER (WHERE ...) / CASE WHEN. ORIGIN_ATTR_FILTER = "filter" +# @segment (origin.first / origin.aggregate): names a segment.filter declared on the related +# entity, scoping the rows the origin spans by that segment's @filter. +ORIGIN_ATTR_SEGMENT = "segment" +# Correlated filter operands: an origin @filter value { field: "" } compares a related +# row's column with the base row's. The null-safe ops take only such an operand. +FILTER_OP_IS_DISTINCT_FROM = "isDistinctFrom" +FILTER_OP_IS_NOT_DISTINCT_FROM = "isNotDistinctFrom" +FILTER_OPERAND_FIELD = "field" +CORRELATED_FILTER_OPS = ("eq", "ne", "gt", "gte", "lt", "lte", FILTER_OP_IS_DISTINCT_FROM, FILTER_OP_IS_NOT_DISTINCT_FROM) # #195 — shared origin attrs. @distinct (collect-only) applies set semantics; # @orderBy carries ordering keys ('field[:asc|desc]', nulls-last) used by # @agg:collect (element order) and required by origin.first (row selection). diff --git a/server/python/src/metaobjects/runtime/object_manager.py b/server/python/src/metaobjects/runtime/object_manager.py index 5c384a64d0..51557149b7 100644 --- a/server/python/src/metaobjects/runtime/object_manager.py +++ b/server/python/src/metaobjects/runtime/object_manager.py @@ -43,7 +43,7 @@ from ..meta.meta_root import MetaRoot from ..meta.core.object.meta_object import MetaObject from ..naming_refs import ref_target -from ..meta.core.object.object_constants import OBJECT_SUBTYPE_REPORT +from ..meta.core.object.object_constants import OBJECT_SUBTYPE_ENTITY, OBJECT_SUBTYPE_REPORT from ..meta.core.reporting.report_read_model import report_read_model from ..meta.core.field.meta_field import MetaField from ..meta.core.field import field_constants as fc @@ -709,9 +709,11 @@ def find_many( entity = self._require_entity(entity_name) # #214: reads route to the replica VIEW for a write-through entity (carrying the # derived origin.* columns), else the entity's own primary source. All effective - # fields (incl. derived) are selected — the view exposes them. + # fields (incl. derived) are selected — the view exposes them. An entity with no read + # view reads its table, which stores no derived field, so those are left out. table = self._read_source_name(entity) - cols = [_column_of(f, self._column_naming) for f in entity.fields()] + read_fields = _read_fields(entity) + cols = [_column_of(f, self._column_naming) for f in read_fields] sql = f'SELECT {", ".join(self._d.quote(c) for c in cols)} FROM {self._read_relation(entity)}' params: list[Any] = [] # FR-017 TPH: a subtype read is scoped to its discriminator value (a row of @@ -739,10 +741,10 @@ def find_many( result = self._driver.select(sql, tuple(params)) # Map raw column → metadata field name for cross-port row-shape parity. # Values stay native (ADR-0019); the boundary canonicalizes them. - col_to_field = {_column_of(f, self._column_naming): f.name for f in entity.fields()} - self.last_column_oids = self._type_codes(result, col_to_field, entity.fields()) + col_to_field = {_column_of(f, self._column_naming): f.name for f in read_fields} + self.last_column_oids = self._type_codes(result, col_to_field, read_fields) # int-backed enums come back as integers; hand the caller the symbol. - fields_by_name = {f.name: f for f in entity.fields()} + fields_by_name = {f.name: f for f in read_fields} return [ _decode_read_row(fields_by_name, {col_to_field.get(k, k): v for k, v in row.items()}, self._d) for row in result.rows @@ -1425,6 +1427,16 @@ def _decode_read_row( ) +def _read_fields(entity: MetaObject) -> list[MetaField]: + """The fields a read selects. An entity that is not write-through reads its own table, + which has no column for a derived (``origin.*``) field: an entity's primary source is + always writable (the loader refuses a read-only primary). Every other object reads a view + that carries them.""" + if entity.sub_type == OBJECT_SUBTYPE_ENTITY and not entity.is_write_through(): + return [f for f in entity.fields() if not f.is_derived()] + return list(entity.fields()) + + def _auto_set_fields(entity: MetaObject) -> tuple[list[MetaField], list[MetaField]]: """The entity's ``@autoSet`` temporal fields split into ``(onCreate, onUpdate)``. diff --git a/server/python/src/metaobjects/spec_metamodel/object.json b/server/python/src/metaobjects/spec_metamodel/object.json index 8c9f73ae9e..c633eaa653 100644 --- a/server/python/src/metaobjects/spec_metamodel/object.json +++ b/server/python/src/metaobjects/spec_metamodel/object.json @@ -31,7 +31,7 @@ { "type": "segment", "subType": "*", "name": "*", "min": 0, "max": null }, { "type": "attr", "subType": "string", "name": "discriminator", "min": 0, "max": 1, "description": "FR-014: names the field on this entity (resolvable via extends:) that holds the subtype-discriminator value. Subtypes of this entity declare @discriminatorValue to bind their rows to a discriminator value. The discriminator field itself is an ordinary field declaration (typically field.enum or field.int / field.string)." }, { "type": "attr", "subType": "string", "name": "discriminatorValue", "min": 0, "max": 1, "description": "FR-014: on a subtype of an entity with @discriminator — the value that identifies rows of this subtype in the shared discriminator field. Wire form is always a string; the underlying field's subtype (enum / int / string) controls codegen + storage coercion. Required on every concrete subtype of a discriminated entity." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] }, { @@ -53,8 +53,8 @@ "whenToUse": "You need a derived, read-only view over entities — a report, summary, or joined/aggregated read model. Declare it (with origin.* children) instead of hand-writing the SELECT/joins; it stays read-only and regenerates.", "rules": "object.projection is a derived read-only representation: every field is extends-bound, origin-derived, or self-declared-under-external-assembly, and all are read-only at the subtype level. Identity is optional and, when present, MUST extend an entity identity. Sources are restricted to read-only @kinds. The declared field set IS the exposure — an inclusive list, fail-closed. A projection NEVER declares relationships (derivation is expressed via @via, not a relationship child) and NEVER co-locates templates — hence its child set omits both relationship and template. The assembly origins (origin.aggregate / origin.computed / origin.first) live HERE, not on object.value; a SOURCELESS projection is the legal shape for a wire-assembled read model, and a template-level @payloadRef/@responseRef may target one (#210). See ADR-0028 (object taxonomy, projection).", "children": [ - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. Resolves against the projection's own declared fields; an aggregate-derived field is not addressable (fail-closed)." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. A key names one of the projection's own fields, else a field of its base entity the view need not output, else a PATH of to-one hops (identity.reference or @cardinality one relationship names) from the base then a field of the entity reached ('orderId.customerId.isActive'), joined but not output. A reached entity field may itself be derived (entity-derived: an origin.computed or origin.first declared on the entity, inlined where the filter reaches it). An aggregate-derived field is not addressable (fail-closed); a to-many hop is refused. A value may be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." }, + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] }, { @@ -70,7 +70,7 @@ { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional segment of @from scoping the rows." }, { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row scope over @from's fields; may use relative-date values." }, { "type": "attr", "subType": "string", "name": "spine", "min": 0, "max": 1, "description": "Optional to-one path from @from to the entity whose rows supply the report's rows (e.g. 'Purchase.program'), written like a dimension's @via. With it the report has one row per distinct dimension tuple among THAT entity's rows, including the ones no row of @from refers to: a count there is 0 and any other measure is null unless it declares @default. Every listed dimension must be reached through this path. @segment and @filter still scope the rows of @from and never remove a row. A row of @from whose reference is null or matches nothing is in no row of the report." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] } ] diff --git a/server/python/src/metaobjects/spec_metamodel/origin.json b/server/python/src/metaobjects/spec_metamodel/origin.json index 00f949a3e3..d5b5b85415 100644 --- a/server/python/src/metaobjects/spec_metamodel/origin.json +++ b/server/python/src/metaobjects/spec_metamodel/origin.json @@ -27,7 +27,8 @@ { "type": "attr", "subType": "string", "name": "agg", "min": 1, "max": 1, "allowedValues": ["count", "sum", "avg", "min", "max", "any", "all", "collect"], "description": "The reducing function applied over the related row-set: count/sum/avg/min/max (numeric/ordinal reduces over @of); any/all (predicate quantifiers over @filter — @of forbidden; empty set → any=false, all=true); collect (array rollup — of the @of column, or of the carrying field.object's declared @objectRef value object when @of is omitted; the field must be isArray)." }, { "type": "attr", "subType": "string", "name": "of", "min": 0, "max": 1, "description": "Dotted Entity.field reference identifying the column being aggregated (e.g. 'Week.durationMinutes'). Required for count/sum/avg/min/max; OPTIONAL for collect, where absent means a whole-object rollup of the field's declared @objectRef value object; forbidden for any/all (which quantify over rows via @filter, not a column)." }, { "type": "attr", "subType": "string", "name": "via", "min": 0, "max": 1, "description": "Dotted relationship path from the base entity to the aggregated rows (e.g. 'Program.weeks' or 'Program.weeks.workouts'). May be omitted only when exactly one single-hop relationship leads from the base entity to the @of entity (FR-024, ADR-0029)." }, - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." }, + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." }, + { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional name of a segment.filter declared on the related entity (the @of entity, else the @via terminal). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." }, { "type": "attr", "subType": "boolean", "name": "distinct", "min": 0, "max": 1, "description": "Set (collect-only) to dedupe collected values (set semantics). Not supported on a whole-object collect (@of omitted): it is a guaranteed no-op whenever the value object carries the primary key, and a silent no-op is worse than a refusal." }, { "type": "attr", "subType": "string", "name": "orderBy", "isArray": true, "min": 0, "max": 1, "description": "Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. On @agg:collect sets element order (non-distinct only); on origin.first (required) selects the row. Semantic — carries no SQL syntax." } ] @@ -51,7 +52,8 @@ { "type": "attr", "subType": "string", "name": "of", "min": 1, "max": 1, "description": "Dotted Entity.field reference identifying the column projected from the selected row (e.g. 'ChildA.label'). Type-preserving: the field's declared subType must equal this column's subType (#185 doctrine)." }, { "type": "attr", "subType": "string", "name": "via", "min": 0, "max": 1, "description": "Dotted relationship path from the base entity to the related rows (e.g. 'Parent.childAs'). May be omitted only when exactly one single-hop relationship leads from the base entity to the @of entity (FR-024, ADR-0029)." }, { "type": "attr", "subType": "string", "name": "orderBy", "isArray": true, "min": 1, "max": 1, "description": "Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. Selects the single row (the first after ordering). The related PK ascending is appended as the deterministic tie-breaker. Semantic — carries no SQL syntax." }, - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." } + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." }, + { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional name of a segment.filter declared on the related entity (the @of entity). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." } ] } ] diff --git a/server/python/tests/loader/test_at_least_one_max_default_load.py b/server/python/tests/loader/test_at_least_one_max_default_load.py new file mode 100644 index 0000000000..fe954c043d --- /dev/null +++ b/server/python/tests/loader/test_at_least_one_max_default_load.py @@ -0,0 +1,31 @@ +"""``validator.atLeastOne`` takes no ``@max``. The shared fixture +``error-validator-at-least-one-max-unknown`` runs under the strict loader, where the attribute is +unregistered; this runs the same input through the default (non-strict) loader, which would +otherwise keep ``@max`` and ignore it. Oracle: that fixture's ``expected-errors.json`` — exactly +one ``ERR_UNKNOWN_ATTR``, at the validator node, under either loader. +""" +from __future__ import annotations + +import json +from pathlib import Path + +import pytest + +from metaobjects import load_directory +from metaobjects.core_types import core_provider # noqa: F401 — side-effect registration + +_FIXTURE = ( + Path(__file__).resolve().parents[4] / "fixtures" / "conformance" / "error-validator-at-least-one-max-unknown" +) + + +def _expected() -> list[tuple[str, str]]: + doc = json.loads((_FIXTURE / "expected-errors.json").read_text()) + return [(e["code"], e["source"]["jsonPath"]) for e in doc["errors"]] + + +@pytest.mark.parametrize("strict", [False, True]) +def test_at_least_one_max_is_refused_once(strict: bool) -> None: + result = load_directory(_FIXTURE / "input", strict=strict) + actual = [(e.code.name, getattr(e.envelope, "json_path", None)) for e in result.errors] + assert actual == _expected() diff --git a/server/python/tests/loader/test_port_scoped_attr.py b/server/python/tests/loader/test_port_scoped_attr.py index 00526279d1..eccdfae3c7 100644 --- a/server/python/tests/loader/test_port_scoped_attr.py +++ b/server/python/tests/loader/test_port_scoped_attr.py @@ -1,5 +1,5 @@ -"""``@collectionName`` is shared vocabulary marked ``implementedIn: ["csharp"]`` (the -DbSet property it names is generated there). Python registers and type-checks it, +"""``@collectionName`` is shared vocabulary marked ``implementedIn: ["csharp", "typescript"]`` +(the DbSet property, REST segment and generated names it sets are generated there). Python registers and type-checks it, so a model that carries it loads strictly here, and Python does nothing with it yet. The never-released ``@dbSetName`` it replaced is not vocabulary; an attribute the registry does not carry still fails, and a wrong value type fails. diff --git a/server/python/tests/runtime/test_object_manager_derived_without_read_source.py b/server/python/tests/runtime/test_object_manager_derived_without_read_source.py new file mode 100644 index 0000000000..1fb0b5155f --- /dev/null +++ b/server/python/tests/runtime/test_object_manager_derived_without_read_source.py @@ -0,0 +1,32 @@ +"""An entity may carry a derived (``origin.*``) field that only a projection reads, with no read +view of its own: the shared fixture ``entity-derived-filter-gate`` (``Member.hasActiveAgreement``, +an ``origin.first`` a projection's ``@filter`` reaches). Its table, the one ``meta migrate`` +creates, has no column for that field, so the ObjectManager reads the table without it. Before, +the SELECT named ``has_active_agreement`` and the read failed on every engine. +""" +from __future__ import annotations + +import sqlite3 +from pathlib import Path + +from metaobjects import load_directory +from metaobjects.runtime import ObjectManager, SqliteDriver + +_FIXTURE = Path(__file__).resolve().parents[4] / "fixtures" / "conformance" / "entity-derived-filter-gate" / "input" + + +def _om() -> ObjectManager: + result = load_directory(_FIXTURE) + assert [e.code for e in result.errors] == [] + conn = sqlite3.connect(":memory:") + conn.execute('CREATE TABLE "members" ("id" INTEGER PRIMARY KEY NOT NULL)') + conn.execute('INSERT INTO "members" ("id") VALUES (7)') + return ObjectManager(result.root, SqliteDriver(conn)) + + +def test_a_list_read_selects_no_column_for_the_derived_field() -> None: + assert _om().find_many("Member") == [{"id": 7}] + + +def test_a_by_id_read_selects_no_column_for_the_derived_field() -> None: + assert _om().find_by_id("Member", 7) == {"id": 7} diff --git a/server/python/tests/test_report_shape.py b/server/python/tests/test_report_shape.py index d2e5bd2f43..e1f1034d6e 100644 --- a/server/python/tests/test_report_shape.py +++ b/server/python/tests/test_report_shape.py @@ -791,7 +791,7 @@ def test_a_via_dimension_is_required_when_every_hop_is_a_required_belongs_to() - req = _via_required_of(root, "Plain") assert req["realmName"] is True # two required hops over a required field assert req["sessionRealmId"] is True # one required hop - assert req["looseRealmName"] is True # @enforce: false does not change the join type + assert req["looseRealmName"] is False # an @enforce: false hop can name a missing row assert req["realmMotto"] is False # the target field is optional assert req["maybeRealmName"] is False # a hop over a nullable foreign key is a LEFT join @@ -932,3 +932,18 @@ def rows(name: str) -> list[str]: "cost decimal False -", "budgetBurn decimal False -", ] + + +# A joined dimension's required comes from the model, never from the join type the view lowering +# picks: a required, enforced belongs-to path onto a target that hides no rows. The shared table is the +# fixture's expected-report-required.json, which every port reads. +def test_a_dimensions_required_is_the_shared_tables() -> None: + fixture = CORPUS.parent / "conformance" / "report-dimension-required-by-model" + doc = json.loads((fixture / "expected-report-required.json").read_text()) + result = load_directory(fixture / "input") + assert not result.errors, "\n".join(e.message for e in result.errors) + report = next( + c for c in result.root.children() if c.type == TYPE_OBJECT and c.resolution_key() == doc["report"] + ) + shape = report_shape(cast(MetaObject, report), result.root) + assert {f.name: f.required for f in shape.fields} == doc["required"] diff --git a/server/typescript/packages/cli/test/types-command.test.ts b/server/typescript/packages/cli/test/types-command.test.ts index 4cec49d1b4..b9e83fad1d 100644 --- a/server/typescript/packages/cli/test/types-command.test.ts +++ b/server/typescript/packages/cli/test/types-command.test.ts @@ -249,7 +249,7 @@ describe("meta types --format", () => { expect(text).toContain("[java; pending elsewhere]"); expect(text).toContain("[java+kotlin]"); expect(text).not.toContain("[java-only]"); - expect(text).toContain("[csharp; pending elsewhere]"); + expect(text).toContain("[csharp+ts; pending elsewhere]"); expect(text).not.toContain("[csharp-only]"); expect(text).not.toContain("[ts-only]"); const doc = await json(["object.entity", "--kind", "attr", "--limit", "0"]); @@ -258,7 +258,7 @@ describe("meta types --format", () => { // Implemented in Java only, so the deprecated derived `tsOnly` is false. expect(attr("object").tsOnly).toBe(false); expect(attr("objectAdapter").ports).toEqual(["java", "kotlin"]); - expect(attr("collectionName").implementedIn).toEqual(["csharp"]); + expect(attr("collectionName").implementedIn).toEqual(["csharp", "typescript"]); // A view's attr inherits its type's kind-1 marking. const img = await json(["view.image", "--kind", "attr", "--limit", "0"]); expect(img.matches.find((m: any) => m.name === "view.image @accept").implementedIn).toEqual(["typescript"]); diff --git a/server/typescript/packages/codegen-ts-tanstack/src/templates/hooks-file.ts b/server/typescript/packages/codegen-ts-tanstack/src/templates/hooks-file.ts index 8f06de4189..29b84ad8e4 100644 --- a/server/typescript/packages/codegen-ts-tanstack/src/templates/hooks-file.ts +++ b/server/typescript/packages/codegen-ts-tanstack/src/templates/hooks-file.ts @@ -174,7 +174,7 @@ function renderReadOnlyHooksFile(entity: MetaObject, entityModule: string, ctx: // The id parameter must follow the entity's DECLARED primary-key type — a uuid/string // PK typed as `number` is simply wrong for the data (and a tsc error at every call site). const pkType = getPkInfo(entity, ctx).tsType; - const entityNamePlural = hookListNameSegment(entityName); + const entityNamePlural = hookListNameSegment(entity); const lcEntity = entityName.charAt(0).toLowerCase() + entityName.slice(1); const keysVar = `${lcEntity}Keys`; const m2mEntries = m2mEntriesFor(entity, ctx); @@ -259,7 +259,7 @@ function renderFullHooksFile(entity: MetaObject, entityModule: string, ctx: Rend // The id parameter must follow the entity's DECLARED primary-key type — a uuid/string // PK typed as `number` is simply wrong for the data (and a tsc error at every call site). const pkType = getPkInfo(entity, ctx).tsType; - const entityNamePlural = hookListNameSegment(entityName); + const entityNamePlural = hookListNameSegment(entity); const lcEntity = entityName.charAt(0).toLowerCase() + entityName.slice(1); const keysVar = `${lcEntity}Keys`; const m2mEntries = m2mEntriesFor(entity, ctx); @@ -463,7 +463,7 @@ export function use${baseName}( }); } -export function use${hookListNameSegment(baseName)}( +export function use${hookListNameSegment(base)}( filter?: ${baseName}Filter, opts?: Omit<${useQueryOptionsSym}<${baseName}[]>, "queryKey" | "queryFn">, ): ${useQueryResultSym}<${baseName}[]> { @@ -485,7 +485,7 @@ export function use${hookListNameSegment(baseName)}( const updateInput = `Partial<${createInput}>`; const subPath = `\`\${${baseName}.$path}/${seg}\``; return code` -export function use${hookListNameSegment(subName)}( +export function use${hookListNameSegment(subEntity)}( filter?: ${subName}Filter, opts?: Omit<${useQueryOptionsSym}<${subName}[]>, "queryKey" | "queryFn">, ): ${useQueryResultSym}<${subName}[]> { diff --git a/server/typescript/packages/codegen-ts/src/flattened.ts b/server/typescript/packages/codegen-ts/src/flattened.ts index d4313158d1..0a1e9cff81 100644 --- a/server/typescript/packages/codegen-ts/src/flattened.ts +++ b/server/typescript/packages/codegen-ts/src/flattened.ts @@ -11,6 +11,7 @@ import { isMetaObject, refTarget, resolveFlattenedColumnName, + resolveFlattenedMember, physicalNamingOf, FIELD_SUBTYPE_OBJECT, FIELD_ATTR_OBJECT_REF, @@ -63,6 +64,17 @@ export function flattenedMembersOf(field: MetaField, strategy: ColumnNamingStrat }); } +/** + * The key a field PATH names on the flat surface: a plain field name as is, a member of a + * flattened value object (`address.countryId`, as an identity.reference @fields entry may name + * it) as its flat key (`addressCountryId`), the Drizzle table property its column is under. + */ +export function flatKeyOfPath(entity: MetaObject, path: string): string { + if (!path.includes(".")) return path; + const flattened = resolveFlattenedMember(entity, path); + return flattened === undefined ? path : flattened.embedding.name + upperFirst(flattened.member.name); +} + const MUTABILITY_TIGHTNESS = [MUTABILITY_READ_ONLY, MUTABILITY_WRITE_ONCE] as const; /** diff --git a/server/typescript/packages/codegen-ts/src/generators/api-model.ts b/server/typescript/packages/codegen-ts/src/generators/api-model.ts index 78c8a4a3ca..e026e77249 100644 --- a/server/typescript/packages/codegen-ts/src/generators/api-model.ts +++ b/server/typescript/packages/codegen-ts/src/generators/api-model.ts @@ -548,7 +548,7 @@ function dataAccessSymbols( const mod = entityModulePath(layout, obj, `${name}.queries`); const find = findByIdFnName(name); - const list = listFnName(name); + const list = listFnName(obj); const create = createFnName(name); const update = updateFnName(name); const del = deleteByIdFnName(name); diff --git a/server/typescript/packages/codegen-ts/src/naming.ts b/server/typescript/packages/codegen-ts/src/naming.ts index 20d69ab32a..7fabac65ce 100644 --- a/server/typescript/packages/codegen-ts/src/naming.ts +++ b/server/typescript/packages/codegen-ts/src/naming.ts @@ -7,11 +7,13 @@ import { applyColumnNamingStrategy, DEFAULT_COLUMN_NAMING_STRATEGY, + collectionNameOf, pluralize, pluralizeTableNameLegacy, toKebabCase, toSnakeCase, type ColumnNamingStrategy, + type MetaData, } from "@metaobjectsdev/metadata"; export { pluralize, toSnakeCase } from "@metaobjectsdev/metadata"; @@ -125,9 +127,20 @@ export function findByIdFnName(entityName: string): string { return `find${entityName}ById`; } -/** Generated list helper name: `list` (PascalCase plural). */ -export function listFnName(entityName: string): string { - return `list${pluralize(entityName)}`; +/** Generated list helper name: `list` (PascalCase plural, {@link collectionPascal}). */ +export function listFnName(entity: string | MetaData): string { + return `list${collectionPascal(entity)}`; +} + +/** + * The `` of a generated symbol: an object's collection name (its own `@collectionName`, + * else the project's `naming.collections` pluralizer over its name), first letter upper-cased. + * Given only a bare name, with no model at hand, the standard pluralization of it. For an + * object that declares neither, the two agree. + */ +export function collectionPascal(entity: string | MetaData): string { + const plural = typeof entity === "string" ? pluralize(entity) : collectionNameOf(entity); + return plural.charAt(0).toUpperCase() + plural.slice(1); } /** @@ -143,8 +156,9 @@ export function listFnName(entityName: string): string { * in exactly that case, never otherwise: `Post` → `Posts` (unaffected), `Stats` → * `StatsList`. */ -export function hookListNameSegment(entityName: string): string { - const plural = pluralize(entityName); +export function hookListNameSegment(entity: string | MetaData): string { + const entityName = typeof entity === "string" ? entity : entity.name; + const plural = collectionPascal(entity); return plural === entityName ? `${entityName}List` : plural; } @@ -212,13 +226,13 @@ export function reverseFinderFkSegment(fkFieldName: string): string { } /** Generated reverse single-value finder name: `findBy`. */ -export function reverseFinderFnName(sourceEntityName: string, fkFieldName: string): string { - return `find${pluralize(sourceEntityName)}By${reverseFinderFkSegment(fkFieldName)}`; +export function reverseFinderFnName(sourceEntity: string | MetaData, fkFieldName: string): string { + return `find${collectionPascal(sourceEntity)}By${reverseFinderFkSegment(fkFieldName)}`; } /** Generated reverse batched finder name: `findByIn`. */ -export function reverseFinderInFnName(sourceEntityName: string, fkFieldName: string): string { - return `${reverseFinderFnName(sourceEntityName, fkFieldName)}In`; +export function reverseFinderInFnName(sourceEntity: string | MetaData, fkFieldName: string): string { + return `${reverseFinderFnName(sourceEntity, fkFieldName)}In`; } /** diff --git a/server/typescript/packages/codegen-ts/src/naming/collection-name-collision.ts b/server/typescript/packages/codegen-ts/src/naming/collection-name-collision.ts index b6ecd4f253..827ca7579a 100644 --- a/server/typescript/packages/codegen-ts/src/naming/collection-name-collision.ts +++ b/server/typescript/packages/codegen-ts/src/naming/collection-name-collision.ts @@ -16,7 +16,8 @@ // WHOLE set, with a message naming both entities and the colliding name. import { type ErrorCode, OBJECT_SUBTYPE_VALUE, type MetaObject } from "@metaobjectsdev/metadata"; -import { pluralize, toSnakeCase } from "@metaobjectsdev/metadata"; +import { collectionSegment } from "@metaobjectsdev/metadata"; +import { collectionPascal } from "../naming.js"; import { emitsInstanceArtifacts } from "../instance-artifacts.js"; import { isTphSubtype } from "../tph.js"; import { effectivePackage } from "../docs-paths.js"; @@ -69,7 +70,7 @@ function assertNoModuleNameCollisions(entities: readonly MetaObject[], layout: O const ownerOfPlural = new Map(); for (const obj of entities) { if (obj.subType === OBJECT_SUBTYPE_VALUE || !emitsInstanceArtifacts(obj)) continue; - const plural = pluralize(obj.name); + const plural = collectionPascal(obj); const scope = layout === "package" ? (effectivePackage(obj) ?? "") : ""; const key = `${scope}\u0000${plural}`; const existing = ownerOfPlural.get(key); @@ -77,7 +78,8 @@ function assertNoModuleNameCollisions(entities: readonly MetaObject[], layout: O throw new Error( `${ERR_COLLECTION_NAME_COLLISION}: "${existing.resolutionKey()}" and "${obj.resolutionKey()}" both pluralize ` + `to the collection name "${plural}" in one module scope (${scope === "" ? "the entity modules' directory" : `package ${scope}`}; ` + - `generated hook/query/finder/list names, the collection variable) — rename one entity, or ` + + `generated hook/query/finder/list names, the collection variable) — rename one entity, give one ` + + `a distinct @collectionName, or ` + `place the modules by package (outputLayout: "package"). Default PHYSICAL table names are ` + `unaffected by this rule.`, ); @@ -87,8 +89,9 @@ function assertNoModuleNameCollisions(entities: readonly MetaObject[], layout: O } /** - * The route half. The REST collection segment is the entity NAME snake_cased and then - * pluralized (`entity-ui-descriptor.ts`), so `ApiKey` and `APIKey` both serve `/api_keys` + * The route half. The REST collection segment is the declared `@collectionName` or the + * configured pluralizer's output, snake_cased, else the entity NAME snake_cased and then + * pluralized (`collectionSegment`), so `ApiKey` and `APIKey` both serve `/api_keys` * while their pluralized PascalCase names differ and pass the check above. Counts only * objects that actually get a route: not a value object, not abstract, not sourceless, * not a TPH subtype (folded into its base's table and served under the base's route). @@ -98,13 +101,13 @@ function assertNoRouteSegmentCollisions(entities: readonly MetaObject[]): void { for (const obj of entities) { if (obj.subType === OBJECT_SUBTYPE_VALUE) continue; if (!emitsInstanceArtifacts(obj) || isTphSubtype(obj)) continue; - const segment = pluralize(toSnakeCase(obj.name)); + const segment = collectionSegment(obj); const existing = ownerOfSegment.get(segment); if (existing !== undefined && existing.resolutionKey() !== obj.resolutionKey()) { throw new Error( `${ERR_COLLECTION_NAME_COLLISION}: "${existing.resolutionKey()}" and "${obj.resolutionKey()}" both resolve ` + - `to the REST collection segment "${segment}" — rename one entity so its snake_cased, ` + - `pluralized route segment is distinct.`, + `to the REST collection segment "${segment}" — rename one entity, or give one a distinct ` + + `@collectionName, so its route segment is distinct.`, ); } if (existing === undefined) ownerOfSegment.set(segment, obj); diff --git a/server/typescript/packages/codegen-ts/src/projection/extract-view-spec.ts b/server/typescript/packages/codegen-ts/src/projection/extract-view-spec.ts index 639c83b7ee..70c55c9c14 100644 --- a/server/typescript/packages/codegen-ts/src/projection/extract-view-spec.ts +++ b/server/typescript/packages/codegen-ts/src/projection/extract-view-spec.ts @@ -57,6 +57,11 @@ import { findReverseReferenceHops, resolveRelationshipReference, resolveObjectRef, + resolveFieldPathColumn, + ORIGIN_ATTR_SEGMENT, + FILTER_OPERAND_FIELD, + FILTER_OP_IS_DISTINCT_FROM, + FILTER_OP_IS_NOT_DISTINCT_FROM, refTarget, type AggregateFunction, type ReferenceLookup, @@ -161,10 +166,19 @@ function resolveAggregateFilter( continue; } const field = entity.fields().find((f) => f.name === key); - if (!field) continue; + // The loader resolves every key; one that reaches here unresolved would drop a predicate. + if (!field) throw new Error(`origin @filter key "${key}" names no field of "${entity.name}".`); const opObj = desugarClause(val); const op = Object.keys(opObj)[0]; - if (!op) continue; + if (!op) throw new Error(`origin @filter on "${key}" has no operator.`); + const operand = opObj[op]; + if (op === FILTER_OP_IS_DISTINCT_FROM || op === FILTER_OP_IS_NOT_DISTINCT_FROM + || (typeof operand === "object" && operand !== null && !Array.isArray(operand) + && FILTER_OPERAND_FIELD in operand)) { + throw new Error( + `origin @filter on "${key}" compares with a base-row field, which the view lowering does not support yet.`, + ); + } clauses.push({ kind: "cmp", ref: `${alias}.${sourceColumnNameFor(field, ctx)}`, @@ -695,7 +709,11 @@ function baseColumnNameFor( */ export function joinColumnFor(entity: MetaObject, fieldName: string, ctx: ExtractContext): string { const f = entity.fields().find((x) => x.name === fieldName); - return f ? sourceColumnNameFor(f, ctx) : columnNameFromField(fieldName, ctx.columnNamingStrategy); + if (f) return sourceColumnNameFor(f, ctx); + // A reference may name a member of a flattened value object (`address.countryId`): its + // column is on the entity's own table, under the embedding's prefix. + return resolveFieldPathColumn(entity, fieldName, ctx.columnNamingStrategy) + ?? columnNameFromField(fieldName, ctx.columnNamingStrategy); } /** The physical column of an entity's primary-key field — the LEFT-JOIN phantom guard @@ -1084,6 +1102,46 @@ function findAliasInTree( return recurse(joinTree.joins); } +/** + * The alias of the join a walked `@via` path ends on: follow its hops down the join tree by + * relationship name (pathsToJoins collapsed every path into this trie, so the walk is exact). + * Undefined when the path is empty or does not end on `targetEntity` — the caller then refuses. + */ +function aliasForVia(joinTree: JoinTree, path: Path, targetEntity: string): string | undefined { + if (path.length === 0) return joinTree.baseEntity === targetEntity ? joinTree.baseAlias : undefined; + let nodes: readonly JoinNode[] = joinTree.joins; + let node: JoinNode | undefined; + for (const step of path) { + node = nodes.find((n) => n.relationship === step.relationship); + if (node === undefined) return undefined; + nodes = node.children; + } + return node !== undefined && node.targetEntity === targetEntity ? node.alias : undefined; +} + +/** + * The loaders accept this field's origin, but the view lowering cannot express it. Refuse it + * by name: dropping the column would leave a view without a column the generated type declares. + */ +function cannotLower(projection: MetaObject, field: MetaData, why: string): never { + throw new Error( + `Projection ${projection.name}: field "${field.name}" cannot be lowered into the view: ${why}.`, + ); +} + +/** An origin's `@filter`, resolved for this field, or refused naming the field it is on. */ +function originFilterFor( + projection: MetaObject, + field: MetaData, + ...args: Parameters +): ReturnType { + try { + return resolveAggregateFilter(...args); + } catch (e) { + cannotLower(projection, field, (e as Error).message.replace(/\.$/, "")); + } +} + function buildSelectSpec( projection: MetaObject, base: MetaObject, @@ -1133,51 +1191,72 @@ function buildSelectSpec( continue; } + // ADR-0039: own (category 4) — origin.* never inherits (ADR-0029). + if (origin.ownAttr(ORIGIN_ATTR_SEGMENT) !== undefined) { + cannotLower(projection, field, "its @segment is not lowered into a view yet"); + } + if (origin.subType === ORIGIN_SUBTYPE_PASSTHROUGH) { // ADR-0039: own (category 4) — origin.* never inherits (ADR-0029). const from = origin.ownAttr(ORIGIN_PASSTHROUGH_ATTR_FROM) as string; const dotIdx = from.indexOf("."); - if (dotIdx < 1) continue; + if (dotIdx < 1) cannotLower(projection, field, 'its @of/@from is not "Entity.field"'); // @from may be package-qualified ("pkg::Entity.field"). Resolve package-aware and // key the alias lookup on the SAME resolutionKey() the joinTree stored (#244). const fieldName = from.slice(dotIdx + 1); const targetEntity = resolveEntityRef(root, from.slice(0, dotIdx), projPkg); - if (!targetEntity) continue; - const sourceAlias = findAliasInTree(joinTree, targetEntity.resolutionKey()); - if (sourceAlias === undefined) continue; + if (!targetEntity) cannotLower(projection, field, "its @of/@from names no entity"); + // The alias of the join this origin's own @via walked: two references onto one target + // join it twice, and the entity alone cannot say which join a field reads. + // ADR-0039: own (category 4) — origin.* never inherits (ADR-0029). + const via = origin.ownAttr(ORIGIN_PASSTHROUGH_ATTR_VIA) as string | undefined; + const sourceAlias = via !== undefined + ? aliasForVia(joinTree, walkViaPath(via, root, projPkg, ctx), targetEntity.resolutionKey()) + : findAliasInTree(joinTree, targetEntity.resolutionKey()); + if (sourceAlias === undefined) cannotLower(projection, field, "the entity it reads is not joined into the view"); // EFFECTIVE fields — the source column may be inherited via `extends` // (e.g. created_at/updated_at/created_by from an audited base), which - // ownChildren() would miss. + // ownChildren() would miss. A member of a flattened value object (`address.city`) is a + // column of the entity's own table. const targetField = targetEntity.fields().find((f) => f.name === fieldName); - if (!targetField) continue; + const sourceColumn = targetField !== undefined + ? sourceColumnNameFor(targetField, ctx) + : resolveFieldPathColumn(targetEntity, fieldName, ctx.columnNamingStrategy); + if (sourceColumn === undefined) cannotLower(projection, field, "its @of/@from names no field of that entity"); + // A derived field of the entity has no column on its table; the view would have to inline + // its origin where the passthrough reaches it. + // ADR-0039: own — origin.* never inherits (ADR-0029). + if (targetField?.ownChildren().some((c) => c.type === TYPE_ORIGIN) === true) { + cannotLower(projection, field, `its @from reads "${targetEntity.name}.${fieldName}", a derived field, which the view lowering does not inline yet`); + } columns.push({ kind: "passthrough", fieldName: field.name, dbColAlias: dbCol, sourceAlias, - sourceColumn: sourceColumnNameFor(targetField, ctx), + sourceColumn, }); } else if (origin.subType === ORIGIN_SUBTYPE_AGGREGATE) { // ADR-0039: own (category 4) — origin.* never inherits (ADR-0029). const agg = origin.ownAttr(ORIGIN_AGGREGATE_ATTR_AGG) as string | undefined; - if (!agg) continue; + if (!agg) cannotLower(projection, field, "it declares no @agg"); const filterAttr = origin.ownAttr(ORIGIN_AGGREGATE_ATTR_FILTER); if (agg === AGG_ANY || agg === AGG_ALL) { // #195 predicate quantifier — no @of; the related entity is @via's terminal // hop, and @filter is the (required) quantified predicate. const via = origin.ownAttr(ORIGIN_AGGREGATE_ATTR_VIA) as string | undefined; - if (!via) continue; + if (!via) cannotLower(projection, field, "it declares no @via"); const relatedEntity = viaTerminalEntity(via, root, projPkg); - if (!relatedEntity) continue; - const sourceAlias = findAliasInTree(joinTree, relatedEntity.resolutionKey()); - if (sourceAlias === undefined) continue; + if (!relatedEntity) cannotLower(projection, field, "its @via does not resolve"); + const sourceAlias = aliasForVia(joinTree, walkViaPath(via, root, projPkg, ctx), relatedEntity.resolutionKey()); + if (sourceAlias === undefined) cannotLower(projection, field, "the entity it reads is not joined into the view"); const joinedPk = primaryKeyColumn(relatedEntity, ctx); - if (joinedPk === undefined) continue; + if (joinedPk === undefined) cannotLower(projection, field, "the related entity has no primary key"); const pred = filterAttr !== undefined - ? resolveAggregateFilter(filterAttr, relatedEntity, sourceAlias, ctx) + ? originFilterFor(projection, field, filterAttr, relatedEntity, sourceAlias, ctx) : undefined; - if (pred === undefined) continue; // loader requires @filter on any/all + if (pred === undefined) cannotLower(projection, field, "its @filter does not resolve"); columns.push({ kind: "predicateAgg", fieldName: field.name, @@ -1204,27 +1283,27 @@ function buildSelectSpec( // ERR_COLLECT_MEMBER_UNRESOLVED before codegen runs); they are defence for a // caller that reached codegen without loading, never a silent-drop path. const via = origin.ownAttr(ORIGIN_AGGREGATE_ATTR_VIA) as string | undefined; - if (!via) continue; + if (!via) cannotLower(projection, field, "it declares no @via"); const relatedEntity = viaTerminalEntity(via, root, projPkg); - if (!relatedEntity) continue; - const sourceAlias = findAliasInTree(joinTree, relatedEntity.resolutionKey()); - if (sourceAlias === undefined) continue; + if (!relatedEntity) cannotLower(projection, field, "its @via does not resolve"); + const sourceAlias = aliasForVia(joinTree, walkViaPath(via, root, projPkg, ctx), relatedEntity.resolutionKey()); + if (sourceAlias === undefined) cannotLower(projection, field, "the entity it reads is not joined into the view"); const joinedPk = primaryKeyColumn(relatedEntity, ctx); - if (joinedPk === undefined) continue; + if (joinedPk === undefined) cannotLower(projection, field, "the related entity has no primary key"); // ADR-0059 R7 — the VO the loader bound for @objectRef, resolved where it was declared. // ADR-0039: resolving — @objectRef may be inherited via extends. const vo = refTarget(field, FIELD_ATTR_OBJECT_REF); - if (!vo) continue; + if (!vo) cannotLower(projection, field, "its @objectRef does not resolve"); // ADR-0039: resolving — a value object may inherit members via extends, and the // terminal entity may inherit fields. The VO's member list IS the exposure: a // field the terminal has but the VO omits is deliberately not projected (#270). const members: { memberName: string; sourceColumn: string }[] = []; for (const m of vo.children().filter((c): c is MetaField => c.type === TYPE_FIELD)) { const target = relatedEntity.fields().find((f) => f.name === m.name); - if (target === undefined) continue; // loader: ERR_COLLECT_MEMBER_UNRESOLVED + if (target === undefined) cannotLower(projection, field, "a member of its value object is not a field of the related entity"); members.push({ memberName: m.name, sourceColumn: sourceColumnNameFor(target, ctx) }); } - if (members.length === 0) continue; + if (members.length === 0) cannotLower(projection, field, "no member of its value object resolves on the related entity"); columns.push({ kind: "collectObjectAgg", fieldName: field.name, @@ -1241,22 +1320,26 @@ function buildSelectSpec( // collect + the scalar reduces (count/sum/avg/min/max) all name @of. const of_ = origin.ownAttr(ORIGIN_AGGREGATE_ATTR_OF) as string | undefined; - if (!of_) continue; + if (!of_) cannotLower(projection, field, "it declares no @of"); const dotIdx = of_.indexOf("."); - if (dotIdx < 1) continue; + if (dotIdx < 1) cannotLower(projection, field, 'its @of/@from is not "Entity.field"'); const fieldName = of_.slice(dotIdx + 1); const targetEntity = resolveEntityRef(root, of_.slice(0, dotIdx), projPkg); - if (!targetEntity) continue; - const sourceAlias = findAliasInTree(joinTree, targetEntity.resolutionKey()); - if (sourceAlias === undefined) continue; + if (!targetEntity) cannotLower(projection, field, "its @of/@from names no entity"); + // The join its own @via walked (see the passthrough branch). ADR-0039: own (category 4). + const aggVia = origin.ownAttr(ORIGIN_AGGREGATE_ATTR_VIA) as string | undefined; + const sourceAlias = aggVia !== undefined + ? aliasForVia(joinTree, walkViaPath(aggVia, root, projPkg, ctx), targetEntity.resolutionKey()) + : findAliasInTree(joinTree, targetEntity.resolutionKey()); + if (sourceAlias === undefined) cannotLower(projection, field, "the entity it reads is not joined into the view"); const targetField = targetEntity.fields().find((f) => f.name === fieldName); - if (!targetField) continue; + if (!targetField) cannotLower(projection, field, "its @of/@from names no field of that entity"); if (agg === AGG_COLLECT) { // #195 array rollup — collect @of across the related set. @distinct = set // semantics; @orderBy (non-distinct) sets element order, else value-ascending. const joinedPk = primaryKeyColumn(targetEntity, ctx); - if (joinedPk === undefined) continue; + if (joinedPk === undefined) cannotLower(projection, field, "the related entity has no primary key"); const distinct = origin.ownAttr(ORIGIN_ATTR_DISTINCT) === true; const orderBy = resolveOrderByKeys(origin.ownAttr(ORIGIN_ATTR_ORDER_BY), targetEntity, ctx); columns.push({ @@ -1276,7 +1359,7 @@ function buildSelectSpec( // Optional scoping filter — resolved against the aggregated entity (the @of // entity, reached at `sourceAlias`), e.g. max(version) over only active rows. const filter = filterAttr !== undefined - ? resolveAggregateFilter(filterAttr, targetEntity, sourceAlias, ctx) + ? originFilterFor(projection, field, filterAttr, targetEntity, sourceAlias, ctx) : undefined; columns.push({ kind: "aggregate", @@ -1287,26 +1370,28 @@ function buildSelectSpec( sourceColumn: sourceColumnNameFor(targetField, ctx), ...(filter !== undefined ? { filter } : {}), }); + } else { + cannotLower(projection, field, `@agg "${agg}" has no view lowering`); } } else if (origin.subType === ORIGIN_SUBTYPE_COMPUTED) { // #195 row-level computed value from the base entity's own fields (@expr tree, // no @via). ADR-0039: own — origin.* never inherits (ADR-0029). const rawExpr = origin.ownAttr(ORIGIN_COMPUTED_ATTR_EXPR); const expr = resolveExprNode(rawExpr, base, joinTree.baseAlias, ctx); - if (expr === undefined) continue; + if (expr === undefined) cannotLower(projection, field, "its @expr uses a node the view lowering does not support yet"); columns.push({ kind: "computed", fieldName: field.name, dbColAlias: dbCol, expr }); } else if (origin.subType === ORIGIN_SUBTYPE_FIRST) { // #195 correlated latest-row — argmax-then-project. Resolves to a correlated // subquery (NOT a join). ADR-0039: own — origin.* never inherits (ADR-0029). const of_ = origin.ownAttr(ORIGIN_FIRST_ATTR_OF) as string | undefined; - if (!of_) continue; + if (!of_) cannotLower(projection, field, "it declares no @of"); const dotIdx = of_.indexOf("."); - if (dotIdx < 1) continue; + if (dotIdx < 1) cannotLower(projection, field, 'its @of/@from is not "Entity.field"'); const ofFieldName = of_.slice(dotIdx + 1); const childEntity = resolveEntityRef(root, of_.slice(0, dotIdx), projPkg); - if (!childEntity) continue; + if (!childEntity) cannotLower(projection, field, "its @of names no entity"); const ofField = childEntity.fields().find((f) => f.name === ofFieldName); - if (!ofField) continue; + if (!ofField) cannotLower(projection, field, "its @of names no field of that entity"); // The base↔child correlation FK — resolved exactly as buildJoinTree resolves a // single hop (the identity.reference is the FK-direction SSOT). Single-hop @via; // a multi-hop @via on origin.first is not lowered here (rare, and validated away). @@ -1344,23 +1429,23 @@ function buildSelectSpec( ); } const ref = refs[0]; - if (!ref) continue; + if (!ref) cannotLower(projection, field, "no identity.reference correlates the base with the @of entity"); const fkField = ref.referenceIdentity.fields[0]; - if (!fkField) continue; + if (!fkField) cannotLower(projection, field, "its correlating reference names no field"); const pkField = ref.referenceIdentity.resolvedTargetPkField(root) ?? "id"; const referenceHolder: "source" | "target" = ref.holder.name === base.name ? "source" : "target"; const fkHolder = referenceHolder === "source" ? base : childEntity; const pkHolder = referenceHolder === "source" ? childEntity : base; const childPk = primaryKeyColumn(childEntity, ctx); - if (childPk === undefined) continue; + if (childPk === undefined) cannotLower(projection, field, "the @of entity has no primary key"); // A FRESH alias — the subquery is an independent correlated scope, never the // JOIN-tree alias (reserved against usedAliases so it can never collide). const childAlias = shortAliasFor(childEntity.name, usedAliases); const orderBy = resolveOrderByKeys(origin.ownAttr(ORIGIN_ATTR_ORDER_BY), childEntity, ctx); const filterAttr = origin.ownAttr(ORIGIN_FIRST_ATTR_FILTER); const filter = filterAttr !== undefined - ? resolveAggregateFilter(filterAttr, childEntity, childAlias, ctx) + ? originFilterFor(projection, field, filterAttr, childEntity, childAlias, ctx) : undefined; columns.push({ kind: "first", @@ -1377,6 +1462,8 @@ function buildSelectSpec( orderBy, ...(filter !== undefined ? { filter } : {}), }); + } else { + cannotLower(projection, field, `origin.${origin.subType} has no view lowering`); } } @@ -1440,6 +1527,12 @@ export function extractViewSpec( // from the base alias (o.*), derived (origin.*) fields from the joins. A plain // projection anchors its base via an extends binding (baseEntityFor). const writeThrough = isWriteThrough(projection); + if (!writeThrough && projection.superData !== undefined) { + throw new Error( + `Projection ${projection.name} cannot be lowered into a view: it extends ${projection.superData.name}, ` + + `and the view lowering does not inherit the fields or @filter of the projection it extends yet.`, + ); + } const base = writeThrough ? projection : baseEntityFor(projection, root); const usedAliases = new Set(); const baseAlias = shortAliasFor(base.name, usedAliases); diff --git a/server/typescript/packages/codegen-ts/src/relation-resolver.ts b/server/typescript/packages/codegen-ts/src/relation-resolver.ts index a2a0ba3a0c..923f6bcfc2 100644 --- a/server/typescript/packages/codegen-ts/src/relation-resolver.ts +++ b/server/typescript/packages/codegen-ts/src/relation-resolver.ts @@ -24,6 +24,7 @@ import { variableNameFromEntity } from "./naming.js"; import { hasWritableRdbSource } from "./source-detect.js"; import { tphStorageObject } from "./tph.js"; import { isProjection } from "./projection/projection-detector.js"; +import { flatKeyOfPath } from "./flattened.js"; export interface RelationEntry { /** Name of the relationship (e.g., "author") */ @@ -182,8 +183,10 @@ export function buildRelationMap( const matching = resolveRelationshipReference(obj, child.name, target, declaredRefField); if (!matching) continue; - const fkField = matching.fields[0]; - if (!fkField) continue; + const fkPath = matching.fields[0]; + if (!fkPath) continue; + // A reference over a flattened value-object member names its flat key on the table. + const fkField = flatKeyOfPath(obj, fkPath); push(obj, { name: child.name, @@ -211,8 +214,9 @@ export function buildRelationMap( for (const junction of collectJunctions(root)) { for (const ref of junction.referenceIdentities()) { const target = refTarget(ref, IDENTITY_REFERENCE_ATTR_REFERENCES); - const fkField = ref.fields[0]; - if (!isMetaObject(target) || !fkField) continue; + const fkPath = ref.fields[0]; + if (!isMetaObject(target) || !fkPath) continue; + const fkField = flatKeyOfPath(junction, fkPath); // The relation member is named after the target entity (camel singular); // multiple references to the same entity (self-join junction) are // disambiguated by the FK field name. diff --git a/server/typescript/packages/codegen-ts/src/render-context.ts b/server/typescript/packages/codegen-ts/src/render-context.ts index dbb6ee7d18..5f69ec7b3a 100644 --- a/server/typescript/packages/codegen-ts/src/render-context.ts +++ b/server/typescript/packages/codegen-ts/src/render-context.ts @@ -1,14 +1,14 @@ // RenderContext — cross-cutting state passed to every template. import type { MetaRoot, MetaData } from "@metaobjectsdev/metadata"; -import { FIELD_ATTR_OBJECT_REF, physicalNamingOf, refTarget, stripPackage } from "@metaobjectsdev/metadata"; +import { FIELD_ATTR_OBJECT_REF, collectionNameOf, declaredCollectionName, physicalNamingOf, refTarget, stripPackage } from "@metaobjectsdev/metadata"; import type { Dialect } from "./column-mapper.js"; import { normalizeTimestampMode } from "./dialect-module.js"; import type { PkInfo } from "./pk-resolver.js"; import type { RelationMap } from "./relation-resolver.js"; import type { ColumnNamingStrategy } from "./metaobjects-config.js"; import type { OutputLayout, ResolvedTarget } from "./import-path.js"; -import { variableNameFromEntity } from "./naming.js"; +import { toCamelCase, variableNameFromEntity } from "./naming.js"; /** * How to format relative import specifiers in generated files. @@ -188,9 +188,23 @@ export function makeRenderContext(opts: RenderContextInput): RenderContext { dbImport: opts.dbImport, runtime: true, }; + // An object's collection name (its @collectionName, or the configured naming.collections + // pluralizer) names its Drizzle collection variable too, first letter lowered. Only where one + // of the two is declared, so a project with neither keeps its variables byte for byte; the + // TS config's own collectionNameOverrides still win, and pluralizeCollections: false still + // turns pluralizing off. + const modelCollections: Record = {}; + if (opts.pluralizeCollections !== false) { + const configured = physicalNamingOf(opts.loadedRoot).collections; + for (const obj of opts.loadedRoot.objects()) { + if (configured === undefined && declaredCollectionName(obj) === undefined) continue; + const name = collectionNameOf(obj); + modelCollections[obj.name] = toCamelCase(name.charAt(0).toLowerCase() + name.slice(1)); + } + } const collectionNameOpts = { pluralize: opts.pluralizeCollections ?? true, - overrides: opts.collectionNameOverrides ?? {}, + overrides: { ...modelCollections, ...(opts.collectionNameOverrides ?? {}) }, }; // ADR-0044/#228 — the value-object emitted-name map + its two accessors. When // absent (bare template unit-tests), the map is empty, so both accessors return diff --git a/server/typescript/packages/codegen-ts/src/templates/entity-ui-descriptor.ts b/server/typescript/packages/codegen-ts/src/templates/entity-ui-descriptor.ts index b06e87b16f..c80396cd8d 100644 --- a/server/typescript/packages/codegen-ts/src/templates/entity-ui-descriptor.ts +++ b/server/typescript/packages/codegen-ts/src/templates/entity-ui-descriptor.ts @@ -50,8 +50,7 @@ import { VALIDATOR_ATTR_PATTERN, FIELD_ATTR_MAX_LENGTH, FIELD_ATTR_REQUIRED, - pluralize, - toSnakeCase, + collectionSegment, } from "@metaobjectsdev/metadata"; import { inferViewKind, currencyMetaFor, labelFor, humanize, valueObjectFor, numericInputControl } from "./field-meta.js"; import { flatSurfaceFields } from "../flattened.js"; @@ -123,7 +122,9 @@ export interface EntityUiDescriptor { * An object's OWN pluralized resource path — the one derivation of that spelling, and the * input {@link restPath} composes an address from. It is NOT `$path` on its own. * - * ONE rule for entities and projections alike: snake_case the name, then pluralize. + * ONE rule for entities and projections alike ({@link collectionSegment}): the declared + * `@collectionName` or the configured `naming.collections` pluralizer, snake_cased; with neither, + * snake_case the name, then pluralize. * A projection used to be KEBAB-cased here while an entity was SNAKE-cased, composed in * the opposite ORDER, and that split was grandfathered rather than designed — the only * reason recorded for it was that the projection const had always emitted it. It is @@ -139,8 +140,9 @@ export interface EntityUiDescriptor { * when you specifically want an object's own pluralized name, never to build an address. */ export function resourcePath(entity: MetaData): string { - // One rule, every object kind and every port: snake_case, then pluralize. - return `/${pluralize(toSnakeCase(entity.name))}`; + // One rule, every object kind: its declared @collectionName or the configured pluralizer's + // output, snake_cased; with neither, snake_case then pluralize, as every port serves it. + return `/${collectionSegment(entity)}`; } /** diff --git a/server/typescript/packages/codegen-ts/src/templates/queries-file.ts b/server/typescript/packages/codegen-ts/src/templates/queries-file.ts index ac4121ade3..f28d2767ac 100644 --- a/server/typescript/packages/codegen-ts/src/templates/queries-file.ts +++ b/server/typescript/packages/codegen-ts/src/templates/queries-file.ts @@ -22,7 +22,7 @@ import { schemaInputType, } from "./queries.js"; import { getPkInfo, getPkFields } from "../pk-resolver.js"; -import { pluralize, findByIdFnName, listFnName, createFnName, insertPreservingFnName, updateFnName, singleRowVarName } from "../naming.js"; +import { findByIdFnName, listFnName, createFnName, insertPreservingFnName, updateFnName, singleRowVarName } from "../naming.js"; import { GENERATED_HEADER, GENERATED_EDIT_NOTE, sidecarLine } from "../constants.js"; import { isTphDiscriminatorBase, tphConcreteSubtypes } from "./tph-discriminator.js"; import { isProjection, isWriteThrough } from "../projection/projection-detector.js"; @@ -186,7 +186,7 @@ import { ${viewVar}, type ${entityName} } from ${JSON.stringify(entityFileName)} } const reads = code` -${findById}export async function ${listFnName(entityName)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { +${findById}export async function ${listFnName(obj)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { let q = db.select().from(${viewVar}).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); @@ -346,7 +346,7 @@ export async function find${baseName}ById(db: Db, ${pkField}: ${pkType}): Promis return row ? ${parseSym}(row) : null; } -export async function list${pluralize(baseName)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${baseTypeSym}[]> { +export async function ${listFnName(base)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${baseTypeSym}[]> { let q = db.select().from(${tableSym}).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); @@ -377,7 +377,7 @@ export async function list${pluralize(baseName)}(db: Db, opts?: { limit?: number const subPartialSym = hasInsertRefinements(sub) ? imp(`${sub.name}InsertBaseSchema@${subFileSpec}`) : subInsertSym; subtypeSections.push(code` -export async function list${pluralize(sub.name)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${subTypeSym}[]> { +export async function ${listFnName(sub)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${subTypeSym}[]> { let q = db.select().from(${tableSym}).where(${eqSym}(${tableSym}.${discField}, ${valueLit})).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); diff --git a/server/typescript/packages/codegen-ts/src/templates/queries.ts b/server/typescript/packages/codegen-ts/src/templates/queries.ts index d51777ea5e..e9364108ca 100644 --- a/server/typescript/packages/codegen-ts/src/templates/queries.ts +++ b/server/typescript/packages/codegen-ts/src/templates/queries.ts @@ -12,6 +12,7 @@ import { import type { RenderContext } from "../render-context.js"; import { getPkInfo, pkInfoOf, subTypeToTsType } from "../pk-resolver.js"; import { supportsReturning } from "../dialect-module.js"; +import { flatKeyOfPath } from "../flattened.js"; import { findByIdFnName, listFnName, @@ -49,7 +50,7 @@ export function renderListFn(entity: MetaObject, ctx: RenderContext, readVar?: s const entityName = entity.name; // Pluralize the PascalCase entity name, preserving capitalization // (e.g., "Category" -> "Categories", not "Categorys"). - const fnName = listFnName(entityName); + const fnName = listFnName(entity); return code` export async function ${fnName}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { @@ -231,9 +232,11 @@ interface ReverseFk { export function reverseFksFor(entity: MetaObject): ReverseFk[] { const out: ReverseFk[] = []; for (const ref of entity.referenceIdentities()) { - const fkField = ref.fields[0]; + const fkPath = ref.fields[0]; const targetRef = ref.targetEntity; - if (!fkField || !targetRef) continue; + if (!fkPath || !targetRef) continue; + // A reference over a flattened value-object member names its flat key on the table. + const fkField = flatKeyOfPath(entity, fkPath); const bound = refTarget(ref, IDENTITY_REFERENCE_ATTR_REFERENCES); const target = isMetaObject(bound) ? bound : undefined; out.push({ fkField, targetEntity: target?.name ?? stripPackage(targetRef), target }); @@ -255,8 +258,8 @@ export function renderReverseFinderFns(entity: MetaObject, fk: ReverseFk, ctx: R const ownField = entity.findField(fk.fkField); const valueType = subTypeToTsType(targetPk?.fieldSubType ?? ownField?.subType ?? "long"); - const singleName = reverseFinderFnName(entityName, fk.fkField); - const batchName = reverseFinderInFnName(entityName, fk.fkField); + const singleName = reverseFinderFnName(entity, fk.fkField); + const batchName = reverseFinderInFnName(entity, fk.fkField); const eqSym = imp("eq@drizzle-orm"); const inArraySym = imp("inArray@drizzle-orm"); diff --git a/server/typescript/packages/codegen-ts/test/flattened-reference-compile.test.ts b/server/typescript/packages/codegen-ts/test/flattened-reference-compile.test.ts new file mode 100644 index 0000000000..f153968c9c --- /dev/null +++ b/server/typescript/packages/codegen-ts/test/flattened-reference-compile.test.ts @@ -0,0 +1,242 @@ +// A reference over a member of a flattened value object (`Plan.address.countryId`), and the +// view shapes the loaders accept around it. Each case is lowered end to end, or refused by +// name: never a dropped column, a join on a column that does not exist, or the wrong join. +// +// - the generated model tier (entity, names, queries with the reverse finder) compiles: the +// finder reads the member's flat key (`addressCountryId`), the Drizzle table property; +// - `meta migrate`'s foreign key and a view's join name the member's real column; +// - a passthrough `@from` onto a flattened member reads that column; +// - two references onto one target join it twice, and each passthrough reads the join its +// own `@via` walked; +// - an origin `@segment`, which the view lowering does not express yet, is refused; +// - a projection that extends another projection, whose fields and `@filter` the view +// lowering does not inherit yet, is refused naming the projection. + +import { describe, expect, test, setDefaultTimeout } from "bun:test"; +import { COMPILE_TEST_TIMEOUT_MS } from "./compile-timeout.js"; +import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import ts from "typescript"; +import { InMemoryStringSource, MetaDataLoader, type MetaObject, type MetaRoot } from "@metaobjectsdev/metadata"; +import { buildExpectedSchema } from "@metaobjectsdev/migrate-ts"; +import { entityFile } from "../src/generators/entity-file.js"; +import { namesFile } from "../src/generators/names-file.js"; +import { queriesFile } from "../src/generators/queries-file.js"; +import { barrel } from "../src/generators/barrel.js"; +import { makeRenderContext } from "../src/render-context.js"; +import { buildPkMap } from "../src/pk-resolver.js"; +import { buildRelationMap } from "../src/relation-resolver.js"; +import { buildProjectionViews } from "../src/projection/build-projection-views.js"; +import type { EmittedFile, GenContext } from "../src/generator.js"; + +setDefaultTimeout(COMPILE_TEST_TIMEOUT_MS); + +const pk = { "identity.primary": { name: "pk", "@fields": "id", "@generation": "increment" } }; + +const ENTITIES = [ + { "object.entity": { name: "Country", children: [ + { "source.rdb": { "@table": "countries" } }, + { "field.long": { name: "id" } }, + { "field.string": { name: "name", "@maxLength": 80 } }, + pk, + ]}}, + { "object.value": { name: "Address", children: [ + { "field.string": { name: "city", "@maxLength": 80 } }, + { "field.long": { name: "countryId" } }, + ]}}, + { "object.entity": { name: "Plan", children: [ + { "source.rdb": { "@table": "plans" } }, + { "field.long": { name: "id" } }, + { "field.object": { name: "address", "@objectRef": "Address", "@storage": "flattened" } }, + pk, + { "identity.reference": { name: "countryRef", "@fields": "address.countryId", "@references": "Country" } }, + { "relationship.association": { name: "country", "@cardinality": "one", "@objectRef": "Country" } }, + ]}}, +]; + +async function load(children: unknown[]): Promise { + const result = await new MetaDataLoader({ strict: true }).load([ + new InMemoryStringSource(JSON.stringify({ "metadata.root": { package: "demo", children } }), { id: "flattened-reference.json" }), + ]); + expect(result.errors.map((e) => e.message)).toEqual([]); + return result.root; +} + +async function generate(root: MetaRoot): Promise { + const renderContext = makeRenderContext({ + dialect: "postgres", loadedRoot: root, outDir: "/x", dbImport: "./db", + pkMap: buildPkMap(root), relationMap: buildRelationMap(root), includeNames: true, + }); + const genCtx = (g: { filter?: (e: MetaObject) => boolean }): GenContext => ({ + entities: root.objects(), loadedRoot: root, matches: (e) => g.filter?.(e) ?? true, projectRoot: "/x", + config: { outDir: "/x", extStyle: "none", dbImport: "./db", dialect: "postgres" } as never, + renderContext, warn: () => {}, + }); + const generators = [entityFile(), namesFile(), queriesFile(), barrel()]; + return (await Promise.all(generators.map((g) => g.generate(genCtx(g))))).flat(); +} + +function viewSql(root: MetaRoot): string { + return buildProjectionViews(root, { dialect: "postgres" }).map((v) => v.sql).join("\n"); +} + +describe("a reference over a flattened value-object member", () => { + test("the model tier compiles, and the reverse finder reads the member's flat key", async () => { + const files = await generate(await load(ENTITIES)); + const queries = files.find((f) => f.path === "Plan.queries.ts")!.content; + expect(queries).toContain("findPlansByAddressCountry("); + expect(queries).toContain("plans.addressCountryId"); + // Under the package so drizzle-orm and zod resolve to their real types. + const dir = mkdtempSync(join(import.meta.dir, "tmp-flattened-reference-")); + try { + for (const f of files) writeFileSync(join(dir, f.path), f.content); + const program = ts.createProgram(files.map((f) => join(dir, f.path)), { + strict: true, noEmit: true, target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext, + moduleResolution: ts.ModuleResolutionKind.Bundler, skipLibCheck: true, + }); + const diagnostics = ts.getPreEmitDiagnostics(program).map((d) => + `${d.file?.fileName.slice(dir.length + 1) ?? ""} ${ts.flattenDiagnosticMessageText(d.messageText, "\n")}`); + expect(diagnostics).toEqual([]); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + test("meta migrate's foreign key names the member's column", async () => { + const schema = buildExpectedSchema(await load(ENTITIES), { dialect: "postgres" }); + const plans = schema.tables.find((t) => t.name === "plans")!; + expect(plans.foreignKeys.map((fk) => fk.columns)).toEqual([["address_country_id"]]); + }); + + test("a view joins through the member's column, and a passthrough reads a member", async () => { + const root = await load([...ENTITIES, + { "object.projection": { name: "PlanExtract", children: [ + { "source.rdb": { "@kind": "view", "@view": "v_plan_extract" } }, + { "field.long": { name: "id", extends: "Plan.id" } }, + { "field.string": { name: "city", children: [{ "origin.passthrough": { "@from": "Plan.address.city" } }] } }, + { "field.string": { name: "countryName", children: [{ "origin.passthrough": { "@from": "Country.name", "@via": "Plan.countryRef" } }] } }, + { "identity.primary": { name: "pk", extends: "Plan.pk" } }, + ]}}, + ]); + const sql = viewSql(root); + expect(sql).toContain("p.address_city AS city"); + expect(sql).toContain("c.id = p.address_country_id"); + }); +}); + +describe("two references onto one target", () => { + test("each passthrough reads the join its own @via walked", async () => { + const root = await load([ + { "object.entity": { name: "User", children: [ + { "source.rdb": { "@table": "users" } }, + { "field.long": { name: "id" } }, + { "field.string": { name: "name", "@maxLength": 80, "@required": true } }, + pk, + ]}}, + { "object.entity": { name: "Account", children: [ + { "source.rdb": { "@table": "accounts" } }, + { "field.long": { name: "id" } }, + { "field.long": { name: "ownerId", "@required": true } }, + { "field.long": { name: "approverId" } }, + pk, + { "identity.reference": { name: "owner", "@fields": "ownerId", "@references": "User" } }, + { "identity.reference": { name: "approver", "@fields": "approverId", "@references": "User" } }, + ]}}, + { "object.projection": { name: "AccountView", children: [ + { "source.rdb": { "@kind": "view", "@view": "v_accounts" } }, + { "field.long": { name: "id", extends: "Account.id" } }, + { "field.string": { name: "ownerName", children: [{ "origin.passthrough": { "@from": "User.name", "@via": "Account.owner" } }] } }, + { "field.string": { name: "approverName", children: [{ "origin.passthrough": { "@from": "User.name", "@via": "Account.approver" } }] } }, + { "identity.primary": { name: "pk", extends: "Account.pk" } }, + ]}}, + ]); + const sql = viewSql(root); + expect(sql).toContain("u.name AS owner_name"); + expect(sql).toContain("u0.name AS approver_name"); + expect(sql).toContain("u.id = a.owner_id"); + expect(sql).toContain("u0.id = a.approver_id"); + }); +}); + +describe("an origin the view lowering does not express yet", () => { + test("an origin @segment is refused by name, never dropped", async () => { + const root = await load([ + { "object.entity": { name: "Case", children: [ + { "source.rdb": { "@table": "cases" } }, + { "field.long": { name: "id" } }, + pk, + ]}}, + { "object.entity": { name: "CaseEvent", children: [ + { "source.rdb": { "@table": "case_events" } }, + { "field.long": { name: "id" } }, + { "field.long": { name: "caseId", "@required": true } }, + { "field.timestamp": { name: "createdAt" } }, + { "field.boolean": { name: "active" } }, + pk, + { "identity.reference": { name: "caseRef", "@fields": "caseId", "@references": "Case" } }, + { "segment.filter": { name: "live", "@filter": { active: true } } }, + ]}}, + { "object.projection": { name: "CaseSummary", children: [ + { "source.rdb": { "@kind": "view", "@view": "v_case_summary" } }, + { "field.long": { name: "id", extends: "Case.id" } }, + { "field.timestamp": { name: "firstLive", children: [{ "origin.aggregate": { + "@agg": "min", "@of": "CaseEvent.createdAt", "@via": "Case.caseEvents", "@segment": "live", + } }] } }, + { "identity.primary": { name: "pk", extends: "Case.pk" } }, + ]}}, + ]); + expect(() => viewSql(root)).toThrow(/CaseSummary: field "firstLive" cannot be lowered into the view: its @segment/); + }); +}); + +// The shared fixtures for these shapes, lowered by the one view builder `meta migrate` uses: +// each lowers, or throws the refusal named here. None may load and then lose a column. +const CONFORMANCE = join(import.meta.dir, "../../../../../fixtures/conformance"); + +async function loadFixture(name: string): Promise { + const dir = join(CONFORMANCE, name, "input"); + const sources = readdirSync(dir).filter((f) => f.endsWith(".json")).map((f) => + new InMemoryStringSource(readFileSync(join(dir, f), "utf8"), { id: f, format: "json" })); + const result = await new MetaDataLoader().load(sources); + expect(result.errors.map((e) => e.message)).toEqual([]); + return result.root; +} + +describe("the shared fixtures, through the view builder", () => { + test.each([ + "origin-passthrough-flattened-member", + "origin-passthrough-two-references-one-target", + "reference-flattened-member", + ])("%s lowers", async (name) => { + expect(viewSql(await loadFixture(name))).toContain("SELECT"); + }); + + test.each([ + ["origin-computed-case-over-joined", /field "label" cannot be lowered into the view: its @expr/], + ["origin-computed-literal-unifies", /field "statusOrZero" cannot be lowered into the view: its @expr/], + ["origin-correlated-filter-and-segment", /cannot be lowered into the view: its @segment/], + ["origin-segment-scoped", /cannot be lowered into the view: its @segment/], + ["origin-filter-relative-now", /relative-date filter value .* cannot be lowered to a view/], + ["projection-filter-relative-now", /relative-date filter value .* cannot be lowered to a view/], + ["projection-filter-joined-path", /view @filter field "caseId.status" did not resolve to a view column/], + ["projection-filter-unprojected-base-field", /view @filter field "deleted" did not resolve to a view column/], + ["entity-derived-filter-gate", /did not resolve to a view column/], + ["projection-extends-abstract-projection", /Projection DataAggregationPlans cannot be lowered into a view: it extends PlanExtract,/], + ["projection-extends-filtered-projection", /Projection ExportPlans cannot be lowered into a view: it extends LivePlans,/], + ["projection-extends-projection-own-identity", /Projection ExportPlans cannot be lowered into a view: it extends GatedPlans,/], + ] as const)("%s is refused by name", async (name, refusal) => { + const root = await loadFixture(name); + expect(() => viewSql(root)).toThrow(refusal); + }); + + test("a correlated operand in an aggregate @filter is refused", async () => { + // The fixture's two correlated origins also carry @segment, which is refused first; + // without it, the base-row comparison is what the builder meets. + const dir = join(CONFORMANCE, "origin-correlated-filter-and-segment", "input"); + const sources = readdirSync(dir).filter((f) => f.endsWith(".json")).map((f) => + new InMemoryStringSource(readFileSync(join(dir, f), "utf8").replace(/"@segment": "live",\s*/g, ""), { id: f, format: "json" })); + const result = await new MetaDataLoader().load(sources); + expect(result.errors.map((e) => e.message)).toEqual([]); + expect(() => viewSql(result.root)).toThrow(/field "statusPlaced" cannot be lowered into the view: .*base-row field/); + }); +}); diff --git a/server/typescript/packages/codegen-ts/test/naming/collection-name-wiring.test.ts b/server/typescript/packages/codegen-ts/test/naming/collection-name-wiring.test.ts new file mode 100644 index 0000000000..f69eb37050 --- /dev/null +++ b/server/typescript/packages/codegen-ts/test/naming/collection-name-wiring.test.ts @@ -0,0 +1,138 @@ +// An object's collection name (its own `@collectionName`, else the project's `naming.collections` +// pluralizer over its name) names every generated collection symbol: `list`, the reverse +// finders, the Drizzle collection variable and the REST segment (`$path`). A project that +// declares neither keeps every name byte for byte, and the generated model tier compiles +// under each setting. + +import { describe, expect, test, setDefaultTimeout } from "bun:test"; +import { COMPILE_TEST_TIMEOUT_MS } from "../compile-timeout.js"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import ts from "typescript"; +import { + DEFAULT_PHYSICAL_NAMING, InMemoryStringSource, MetaDataLoader, type MetaObject, type MetaRoot, +} from "@metaobjectsdev/metadata"; +import { entityFile } from "../../src/generators/entity-file.js"; +import { namesFile } from "../../src/generators/names-file.js"; +import { queriesFile } from "../../src/generators/queries-file.js"; +import { routesFile } from "../../src/generators/routes-file.js"; +import { barrel } from "../../src/generators/barrel.js"; +import { makeRenderContext } from "../../src/render-context.js"; +import { buildPkMap } from "../../src/pk-resolver.js"; +import { buildRelationMap } from "../../src/relation-resolver.js"; +import { assertNoCollectionNameCollisions } from "../../src/naming/collection-name-collision.js"; +import { hookListNameSegment, listFnName, reverseFinderFnName } from "../../src/naming.js"; +import type { EmittedFile, GenContext } from "../../src/generator.js"; + +setDefaultTimeout(COMPILE_TEST_TIMEOUT_MS); + +const pk = { "identity.primary": { name: "pk", "@fields": "id", "@generation": "increment" } }; + +const MODEL = [ + { "object.entity": { name: "Faq", "@collectionName": "FAQs", children: [ + { "source.rdb": { "@table": "faq" } }, + { "field.long": { name: "id" } }, + { "field.string": { name: "question", "@maxLength": 200 } }, + pk, + ]}}, + { "object.entity": { name: "Order", children: [ + { "source.rdb": { "@table": "orders" } }, + { "field.long": { name: "id" } }, + { "field.long": { name: "faqId" } }, + pk, + { "identity.reference": { name: "faqRef", "@fields": "faqId", "@references": "Faq" } }, + ]}}, +]; + +async function load(collections?: string): Promise { + const result = await new MetaDataLoader({ strict: true }).load([ + new InMemoryStringSource(JSON.stringify({ "metadata.root": { package: "demo", children: MODEL } }), { id: "collections.json" }), + ]); + expect(result.errors.map((e) => e.message)).toEqual([]); + if (collections !== undefined) result.root.physicalNaming = { ...DEFAULT_PHYSICAL_NAMING, collections }; + return result.root; +} + +async function generate(root: MetaRoot): Promise> { + const renderContext = makeRenderContext({ + dialect: "postgres", loadedRoot: root, outDir: "/x", dbImport: "./db", + pkMap: buildPkMap(root), relationMap: buildRelationMap(root), includeNames: true, + }); + const genCtx = (g: { filter?: (e: MetaObject) => boolean }): GenContext => ({ + entities: root.objects(), loadedRoot: root, matches: (e) => g.filter?.(e) ?? true, projectRoot: "/x", + config: { outDir: "/x", extStyle: "none", dbImport: "./db", dialect: "postgres" } as never, + renderContext, warn: () => {}, + }); + const generators = [entityFile(), namesFile(), queriesFile(), routesFile(), barrel()]; + const files: EmittedFile[] = (await Promise.all(generators.map((g) => g.generate(genCtx(g))))).flat(); + return new Map(files.map((f) => [f.path, f.content])); +} + +function compile(files: Map): string[] { + // Under the package so drizzle-orm and zod resolve; the route tier is framework-bound and + // proven by the api-contract lane, as in every compile gate. + const dir = mkdtempSync(join(import.meta.dir, "tmp-collection-names-")); + try { + const paths: string[] = []; + for (const [path, content] of files) { + if (path.includes(".routes")) continue; + writeFileSync(join(dir, path), content); + paths.push(join(dir, path)); + } + const program = ts.createProgram(paths, { + strict: true, noEmit: true, target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext, + moduleResolution: ts.ModuleResolutionKind.Bundler, skipLibCheck: true, + }); + return ts.getPreEmitDiagnostics(program).map((d) => + `${d.file?.fileName.slice(dir.length + 1) ?? ""} ${ts.flattenDiagnosticMessageText(d.messageText, "\n")}`); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +} + +describe("collection names in the generated model tier", () => { + test("a declared @collectionName names its collection symbols; an undeclared object keeps its own", async () => { + const root = await load(); + const files = await generate(root); + const faq = root.findObject("Faq")!; + const order = root.findObject("Order")!; + expect(listFnName(faq)).toBe("listFAQs"); + expect(listFnName(order)).toBe("listOrders"); + expect(files.get("Faq.queries.ts")).toContain("export async function listFAQs("); + expect(files.get("Faq.ts")).toContain("export const fAQs = pgTable("); + expect(files.get("Faq.ts")).toContain(`"/faqs"`); + expect(files.get("Order.queries.ts")).toContain("export async function listOrders("); + expect(files.get("Order.queries.ts")).toContain("export async function findOrdersByFaq("); + expect(compile(files)).toEqual([]); + }); + + test("naming.collections: none keeps names singular, and a list hook never shadows the detail hook", async () => { + const root = await load("none"); + const files = await generate(root); + const order = root.findObject("Order")!; + expect(listFnName(order)).toBe("listOrder"); + expect(reverseFinderFnName(order, "faqId")).toBe("findOrderByFaq"); + expect(hookListNameSegment(order)).toBe("OrderList"); + expect(files.get("Order.queries.ts")).toContain("export async function listOrder("); + expect(files.get("Order.ts")).toContain("export const order = pgTable("); + expect(files.get("Order.ts")).toContain(`"/order"`); + // A declared name still wins over the configured pluralizer. + expect(files.get("Faq.queries.ts")).toContain("export async function listFAQs("); + expect(compile(files)).toEqual([]); + }); + + test("two objects whose collection names meet are refused by name", async () => { + const result = await new MetaDataLoader({ strict: true }).load([ + new InMemoryStringSource(JSON.stringify({ "metadata.root": { package: "demo", children: [ + ...MODEL, + { "object.entity": { name: "Question", "@collectionName": "FAQs", children: [ + { "source.rdb": { "@table": "questions" } }, + { "field.long": { name: "id" } }, + pk, + ]}}, + ] } }), { id: "collide.json" }), + ]); + expect(result.errors.map((e) => e.message)).toEqual([]); + expect(() => assertNoCollectionNameCollisions(result.root.objects())).toThrow(/"demo::Question".*"FAQs"/); + }); +}); diff --git a/server/typescript/packages/codegen-ts/test/projection/report-required-joins.test.ts b/server/typescript/packages/codegen-ts/test/projection/report-required-joins.test.ts index 62535a3bc7..7d452916c8 100644 --- a/server/typescript/packages/codegen-ts/test/projection/report-required-joins.test.ts +++ b/server/typescript/packages/codegen-ts/test/projection/report-required-joins.test.ts @@ -6,7 +6,8 @@ // // This reads the join types out of the view SQL the lowering emits and holds `reportShape` to // them. The expected table is derived by hand from the #209 rule: a hop is INNER when the -// entity it leaves holds a required foreign key and every hop before it is INNER. +// entity it leaves holds a required foreign key and every hop before it is INNER. `required` is +// stricter: an unenforced reference, or one onto a TPH subtype, is never required. // A dimension's `@via` crosses only identity.reference and `@cardinality: one` hops (a to-many // hop does not load), so the hop that is LEFT OUTER for a reason other than a nullable key is // the to-one relationship whose foreign key sits on the entity it reaches. @@ -62,7 +63,9 @@ const DIMENSIONS: Record filter.placedAt = { gte: { now: "-P7D" } }; expect(() => extractViewSpec(proj, root, { columnNamingStrategy: "snake_case" })).toThrow( // The message names the aggregate's filter, not the projection's row-scope one. - /^origin\.aggregate @filter over Order on "placedAt": a relative-date filter value.*cannot be lowered to a view/, + /field "recentOrders" cannot be lowered into the view: origin\.aggregate @filter over Order on "placedAt": a relative-date filter value.*cannot be lowered to a view/, ); }); @@ -173,7 +173,7 @@ describe("the origin.aggregate @filter refuses a relative-date value too", () => filter.placedAt = { now: "-P7D" }; expect(() => extractViewSpec(proj, root, { columnNamingStrategy: "snake_case" })).toThrow( // The message names the aggregate's filter, not the projection's row-scope one. - /^origin\.aggregate @filter over Order on "placedAt": a relative-date filter value.*cannot be lowered to a view/, + /field "recentOrders" cannot be lowered into the view: origin\.aggregate @filter over Order on "placedAt": a relative-date filter value.*cannot be lowered to a view/, ); }); }); diff --git a/server/typescript/packages/integration-tests/src/mysql-container.ts b/server/typescript/packages/integration-tests/src/mysql-container.ts index 71308e57ee..6296f49420 100644 --- a/server/typescript/packages/integration-tests/src/mysql-container.ts +++ b/server/typescript/packages/integration-tests/src/mysql-container.ts @@ -8,10 +8,15 @@ // MySQL's entrypoint runs a temporary server to initialise the data directory, stops it, and // starts the real one. The temporary server listens on port 0, so readiness is "the final // server logged `port: 3306`" plus a real connection to the created database. +// +// Every docker call made while the container starts is asynchronous and bounded. A blocking +// spawnSync inside a test worker that `bun test --parallel` is running under heavy load could +// miss its child's exit and spin the worker at full CPU indefinitely, holding the integration +// lane open for hours (observed with the readiness poll's repeated `docker logs`). An async +// child that the runner kills just rejects, and the hook fails on its own timeout. -import { spawnSync } from "node:child_process"; +import { execFile, spawnSync } from "node:child_process"; import { randomUUID } from "node:crypto"; -import { publishedPort } from "./postgres-container.ts"; import { reaperLabelArgs } from "./test-reaper.ts"; import mysql from "mysql2/promise"; @@ -20,6 +25,8 @@ const IMAGE = "mysql:8.4"; const PASSWORD = "test"; const DATABASE = "mo_test"; const READY_DEADLINE_MS = Number(process.env.MO_MYSQL_READY_TIMEOUT_MS ?? 180_000); +/** One docker CLI call; a pull of the image on first use is the slow case. */ +const DOCKER_CALL_TIMEOUT_MS = 120_000; export interface MysqlContainerHandle { /** `mysql://…` URL for mysql2. */ @@ -32,13 +39,14 @@ export async function startMysql(): Promise { if (existing !== undefined && existing !== "") return { url: existing, stop: () => {} }; const name = `metaobjects-mysql-${randomUUID().slice(0, 8)}`; - const stop = () => { spawnSync("docker", ["rm", "-f", "-v", name], { stdio: "ignore" }); }; + // Teardown is one quick call after the tests, the same as postgres-container.ts. + const stop = () => { spawnSync("docker", ["rm", "-f", "-v", name], { stdio: "ignore", timeout: 60_000 }); }; // Docker assigns the host port at bind time; see the port note in postgres-container.ts. let url: string; try { - docker(["run", "-d", ...(await reaperLabelArgs()), "--name", name, "-e", `MYSQL_ROOT_PASSWORD=${PASSWORD}`, + await docker(["run", "-d", ...(await reaperLabelArgs()), "--name", name, "-e", `MYSQL_ROOT_PASSWORD=${PASSWORD}`, "-e", `MYSQL_DATABASE=${DATABASE}`, "-p", "127.0.0.1::3306", IMAGE, "--innodb-use-native-aio=0"]); - url = `mysql://root:${PASSWORD}@127.0.0.1:${publishedPort(name, 3306)}/${DATABASE}`; + url = `mysql://root:${PASSWORD}@127.0.0.1:${await publishedPort(name)}/${DATABASE}`; } catch (e) { stop(); throw e; @@ -46,16 +54,23 @@ export async function startMysql(): Promise { const deadline = Date.now() + READY_DEADLINE_MS; while (Date.now() < deadline) { - const logs = spawnSync("docker", ["logs", name], { encoding: "utf8" }); - const text = `${logs.stdout ?? ""}${logs.stderr ?? ""}`; + const text = await docker(["logs", name]).catch(() => ""); if (text.includes("ready for connections") && text.includes("port: 3306") && (await canConnect(url))) { return { url, stop }; } await new Promise((r) => setTimeout(r, 500)); } - const logs = spawnSync("docker", ["logs", "--tail", "40", name], { encoding: "utf8" }); + const logs = await docker(["logs", "--tail", "40", name]).catch((e: unknown) => String(e)); stop(); - throw new Error(`mysql container ${name} not ready in ${READY_DEADLINE_MS}ms:\n${logs.stdout}${logs.stderr}`); + throw new Error(`mysql container ${name} not ready in ${READY_DEADLINE_MS}ms:\n${logs}`); +} + +/** The host port docker bound to the container's 3306, as `docker port` prints it. */ +async function publishedPort(name: string): Promise { + const out = await docker(["port", name, "3306/tcp"]); + const m = /:(\d+)\s*$/m.exec(out.split("\n")[0] ?? ""); + if (m === null) throw new Error(`docker port ${name} 3306/tcp printed no port: '${out}'`); + return Number(m[1]); } async function canConnect(url: string): Promise { @@ -69,7 +84,14 @@ async function canConnect(url: string): Promise { } } -function docker(args: string[]): void { - const r = spawnSync("docker", args, { encoding: "utf8" }); - if (r.status !== 0) throw new Error(`docker ${args.join(" ")} failed: ${r.stderr}`); +/** Runs one docker CLI call and resolves its stdout and stderr together (`docker logs` writes + * the server's log to stderr); rejects on a non-zero exit or after the call timeout. */ +function docker(args: string[]): Promise { + return new Promise((resolve, reject) => { + execFile("docker", args, { encoding: "utf8", timeout: DOCKER_CALL_TIMEOUT_MS, maxBuffer: 16 * 1024 * 1024 }, + (error, stdout, stderr) => { + if (error !== null) reject(new Error(`docker ${args.join(" ")} failed: ${stderr || error.message}`)); + else resolve(`${stdout}${stderr}`); + }); + }); } diff --git a/server/typescript/packages/metadata/src/core/field/flattened-member.ts b/server/typescript/packages/metadata/src/core/field/flattened-member.ts new file mode 100644 index 0000000000..a12e1cbfae --- /dev/null +++ b/server/typescript/packages/metadata/src/core/field/flattened-member.ts @@ -0,0 +1,55 @@ +// A path into a member of a FLATTENED value object (`address.city`): the +// member's column lives on the owning table, under the embedding field's prefix +// (resolveFlattenedColumnName). One resolver for the loader and every lowering. + +import type { MetaData } from "../../shared/meta-data.js"; +import { TYPE_FIELD } from "../../shared/base-types.js"; +import { refTarget } from "../../naming-refs.js"; +import { resolveColumnName, resolveFlattenedColumnName, type ColumnNamingStrategy } from "../../naming.js"; +import { FIELD_ATTR_OBJECT_REF, FIELD_ATTR_STORAGE, FIELD_SUBTYPE_OBJECT, STORAGE_FLATTENED } from "./field-constants.js"; + +/** A flattened member path, resolved: the embedding field on the entity and the member on its value object. */ +export interface FlattenedMember { + readonly embedding: MetaData; + readonly member: MetaData; +} + +/** + * Resolve `.` against `entity`: `` a scalar `field.object` with + * `@storage: flattened`, `` a field its `@objectRef` value object declares (effective). + * Undefined for anything else — a plain field name, a jsonb object, an array, an unknown member. + * One level: a value object flattened inside another is not addressed this way. + */ +export function resolveFlattenedMember(entity: MetaData, path: string): FlattenedMember | undefined { + const dot = path.indexOf("."); + if (dot < 1 || dot === path.length - 1 || path.indexOf(".", dot + 1) !== -1) return undefined; + const head = path.slice(0, dot); + const tail = path.slice(dot + 1); + // Effective children: the embedding field may be inherited via extends. + const embedding = entity.children().find((c) => c.type === TYPE_FIELD && c.name === head); + if (embedding === undefined || embedding.subType !== FIELD_SUBTYPE_OBJECT) return undefined; + // ADR-0039: resolving — @storage / @objectRef may be inherited via extends. + if (embedding.attr(FIELD_ATTR_STORAGE) !== STORAGE_FLATTENED) return undefined; + const vo = refTarget(embedding, FIELD_ATTR_OBJECT_REF); + if (vo === undefined) return undefined; + const member = vo.children().find((c) => c.type === TYPE_FIELD && c.name === tail); + return member === undefined ? undefined : { embedding, member }; +} + +/** + * The physical column a field PATH names on `entity` — a plain field + * (`countryId`, effective) or a flattened member (`address.countryId`, the embedding's prefix + * plus the member's column). An `identity.reference @fields` entry may be either, so every + * consumer of reference columns (view joins, `meta migrate`'s foreign keys) resolves through + * here. Undefined when the path names neither. + */ +export function resolveFieldPathColumn( + entity: MetaData, + path: string, + strategy?: ColumnNamingStrategy, +): string | undefined { + const field = entity.children().find((c) => c.type === TYPE_FIELD && c.name === path); + if (field !== undefined) return resolveColumnName(field, strategy); + const flattened = resolveFlattenedMember(entity, path); + return flattened === undefined ? undefined : resolveFlattenedColumnName(flattened.embedding, flattened.member, strategy); +} diff --git a/server/typescript/packages/metadata/src/core/object/effective-projection-filter.ts b/server/typescript/packages/metadata/src/core/object/effective-projection-filter.ts new file mode 100644 index 0000000000..f9456a5100 --- /dev/null +++ b/server/typescript/packages/metadata/src/core/object/effective-projection-filter.ts @@ -0,0 +1,38 @@ +// A projection's EFFECTIVE row scope. `@filter` composes down an `extends` chain +// rather than overriding: a projection that extends a filtered projection keeps the base's row +// scope (a row gate on an abstract base must survive in every extender). Every +// level's own `@filter`, root-most first, ANDed. One reader for the loaders and the view lowering. + +import type { MetaData } from "../../shared/meta-data.js"; +import { OBJECT_PROJECTION_ATTR_FILTER } from "./object-constants.js"; +import { FILTER_COMPOSE_AND } from "../query/query-constants.js"; + +/** + * The `@filter` a projection's view applies: its own and every super's, root-most first. One + * declaration is returned as declared (byte-identical lowering); two or more are ANDed in one + * flat `{ and: [...] }`. Undefined when no level declares one. + */ +export function effectiveProjectionFilter(projection: MetaData): unknown { + const levels: unknown[] = []; + const seen = new Set(); + for (let node: MetaData | undefined = projection; node !== undefined && !seen.has(node); node = node.superData) { + seen.add(node); + // ADR-0039: own — each level contributes the @filter declared on it; the resolving read + // would return only the nearest one, which is the override this composition replaces. + const own = node.ownAttr(OBJECT_PROJECTION_ATTR_FILTER); + if (own !== undefined) levels.unshift(own); + } + if (levels.length <= 1) return levels[0]; + return { [FILTER_COMPOSE_AND]: levels.flatMap(andMembers) }; +} + +/** A level that is itself a bare `{ and: [...] }` contributes its members, keeping the AND flat. */ +function andMembers(filter: unknown): unknown[] { + if (typeof filter === "object" && filter !== null && !Array.isArray(filter)) { + const entries = Object.entries(filter as Record); + if (entries.length === 1 && entries[0]![0] === FILTER_COMPOSE_AND && Array.isArray(entries[0]![1])) { + return entries[0]![1] as unknown[]; + } + } + return [filter]; +} diff --git a/server/typescript/packages/metadata/src/core/object/object-definition.embedded.ts b/server/typescript/packages/metadata/src/core/object/object-definition.embedded.ts index e89158df41..1b7a303798 100644 --- a/server/typescript/packages/metadata/src/core/object/object-definition.embedded.ts +++ b/server/typescript/packages/metadata/src/core/object/object-definition.embedded.ts @@ -147,9 +147,10 @@ export const OBJECT_DEFINITION: ProviderDefinition = { "min": 0, "max": 1, "implementedIn": [ - "csharp" + "csharp", + "typescript" ], - "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." + "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] }, @@ -184,7 +185,7 @@ export const OBJECT_DEFINITION: ProviderDefinition = { "name": "filter", "min": 0, "max": 1, - "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. Resolves against the projection's own declared fields; an aggregate-derived field is not addressable (fail-closed)." + "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. A key names one of the projection's own fields, else a field of its base entity the view need not output, else a PATH of to-one hops (identity.reference or @cardinality one relationship names) from the base then a field of the entity reached ('orderId.customerId.isActive'), joined but not output. A reached entity field may itself be derived (entity-derived: an origin.computed or origin.first declared on the entity, inlined where the filter reaches it). An aggregate-derived field is not addressable (fail-closed); a to-many hop is refused. A value may be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." }, { "type": "attr", @@ -193,9 +194,10 @@ export const OBJECT_DEFINITION: ProviderDefinition = { "min": 0, "max": 1, "implementedIn": [ - "csharp" + "csharp", + "typescript" ], - "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." + "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] }, @@ -263,9 +265,10 @@ export const OBJECT_DEFINITION: ProviderDefinition = { "min": 0, "max": 1, "implementedIn": [ - "csharp" + "csharp", + "typescript" ], - "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." + "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] } diff --git a/server/typescript/packages/metadata/src/core/relationship/filter-path.ts b/server/typescript/packages/metadata/src/core/relationship/filter-path.ts new file mode 100644 index 0000000000..36f2eafba4 --- /dev/null +++ b/server/typescript/packages/metadata/src/core/relationship/filter-path.ts @@ -0,0 +1,66 @@ +// A projection @filter key may be a PATH: to-one hops +// from the base entity, then a field of the entity they reach (`caseId.status`, +// `orderId.customerId.isActive`). A hop is an identity.reference (a forward FK) or +// a relationship with @cardinality one, named as an origin @via names its hops. A to-many hop +// would multiply the base rows, so it is refused. One resolver for the loaders and the view +// lowering. + +import type { MetaData } from "../../shared/meta-data.js"; +import type { MetaObject } from "../object/meta-object.js"; +import { TYPE_FIELD, TYPE_IDENTITY, TYPE_RELATIONSHIP } from "../../shared/base-types.js"; +import { refTarget } from "../../naming-refs.js"; +import { isMetaObject } from "../../shared/node-guards.js"; +import { IDENTITY_REFERENCE_ATTR_REFERENCES, IDENTITY_SUBTYPE_REFERENCE } from "../identity/identity-constants.js"; +import { + RELATIONSHIP_ATTR_CARDINALITY, RELATIONSHIP_ATTR_OBJECT_REF, CARDINALITY_ONE, +} from "./relationship-constants.js"; + +/** A resolved filter path: the hop names walked, the entity reached, and its field. */ +export interface FilterPath { + readonly hops: readonly string[]; + readonly entity: MetaObject; + readonly field: MetaData; +} + +/** The target of one to-one hop from `obj`, or why there is none. */ +function toOneHop(obj: MetaObject, hop: string): MetaObject | string { + // ADR-0039: resolving — a hop may be inherited through extends. + const ref = obj.children().find( + (c) => c.type === TYPE_IDENTITY && c.subType === IDENTITY_SUBTYPE_REFERENCE && c.name === hop, + ); + if (ref !== undefined) { + const target = refTarget(ref, IDENTITY_REFERENCE_ATTR_REFERENCES); + return isMetaObject(target) ? target : `reference "${obj.name}.${hop}" does not resolve`; + } + const rel = obj.children().find((c) => c.type === TYPE_RELATIONSHIP && c.name === hop); + if (rel !== undefined) { + if (rel.attr(RELATIONSHIP_ATTR_CARDINALITY) !== CARDINALITY_ONE) { + return `"${obj.name}.${hop}" is to-many; a filter path may only walk to-one hops`; + } + const target = refTarget(rel, RELATIONSHIP_ATTR_OBJECT_REF); + return isMetaObject(target) ? target : `relationship "${obj.name}.${hop}" does not resolve`; + } + return `"${obj.name}" declares no identity.reference or to-one relationship named "${hop}"`; +} + +/** + * Resolve a dotted filter key against `base`. Undefined when `key` has no dot (a plain field + * name, resolved by the caller); a string naming the failure when a hop or the field does not + * resolve. + */ +export function resolveFilterPath(base: MetaObject, key: string): FilterPath | string | undefined { + const segments = key.split("."); + if (segments.length < 2) return undefined; + const hops = segments.slice(0, -1); + const fieldName = segments[segments.length - 1]!; + let current = base; + for (const hop of hops) { + const next = toOneHop(current, hop); + if (typeof next === "string") return next; + current = next; + } + // ADR-0039: resolving — the reached field may be inherited. + const field = current.children().find((c) => c.type === TYPE_FIELD && c.name === fieldName); + if (field === undefined) return `"${current.name}" declares no field "${fieldName}"`; + return { hops, entity: current, field }; +} diff --git a/server/typescript/packages/metadata/src/core/reporting/report-shape.ts b/server/typescript/packages/metadata/src/core/reporting/report-shape.ts index 1378b87365..aca0224c8e 100644 --- a/server/typescript/packages/metadata/src/core/reporting/report-shape.ts +++ b/server/typescript/packages/metadata/src/core/reporting/report-shape.ts @@ -10,6 +10,7 @@ import { isMetaObject } from "../../shared/node-guards.js"; import { refTarget, resolveObjectRef } from "../../naming-refs.js"; import { CHILD_REF_SEPARATOR, PACKAGE_SEPARATOR } from "../../shared/structural.js"; import type { MetaObject } from "../object/meta-object.js"; +import { OBJECT_ATTR_DISCRIMINATOR } from "../object/object-constants.js"; import type { MetaField } from "../field/meta-field.js"; import { FIELD_ATTR_OBJECT_REF, @@ -353,14 +354,16 @@ function isTimeGrain(grain: string | undefined): grain is TimeGrain { } /** - * True when every hop of `hops`, walked from `from`, is a belongs-to hop over a required - * foreign key: the hops the view joins INNER, so every row of the report has a match. This - * is the join-type rule of the view lowering (codegen-ts `walkViaPath`, #209): the reference - * is held by the entity the hop leaves, its first FK field is required, and every earlier hop - * is INNER too. `@enforce: false` does not change it. A hop that does not resolve, an - * ambiguous reference or a has-many hop is not INNER. + * True when every hop of `hops`, walked from `from`, is certain to match: a + * belongs-to hop (the reference is held by the entity the hop leaves) whose first FK field is + * required, whose reference is enforced (not `@enforce: false`, which can name a row that does + * not exist), onto a target that hides no rows (not a TPH subtype, whose join carries the + * discriminator). That is exactly when a LEFT OUTER join returns what INNER would, so the column + * is never null-extended, whichever join type the view lowering picks; this is read from the + * model, never from the join type. A hop that does not resolve, an ambiguous reference or a + * has-many hop is not certain. */ -function viaJoinsInner(hops: readonly string[], from: MetaObject, root: MetaRoot): boolean { +function viaAlwaysMatches(hops: readonly string[], from: MetaObject, root: MetaRoot): boolean { let current = from; for (const hopName of hops) { const resolved = resolveViaHop(current, hopName); @@ -378,19 +381,37 @@ function viaJoinsInner(hops: readonly string[], from: MetaObject, root: MetaRoot current, hopName, target, typeof sourceRefField === "string" ? sourceRefField : undefined, ); } - if (reference === undefined) return false; + if (reference === undefined || !reference.enforce) return false; const fkName = reference.fields[0]; const fk = fkName === undefined ? undefined : current.findField(fkName); if (fk === undefined || !fk.isRequired) return false; + if (hidesRows(target)) return false; current = target; } return true; } +/** + * True when a join onto `obj` carries a row scope of its own: a TPH subtype, or an abstract level + * beneath a TPH base (both scoped by the discriminator), whose FK may name a sibling's row. The + * model has no soft-delete or query-filter vocabulary, so this is the only row-hiding scope. + */ +function hidesRows(obj: MetaObject): boolean { + // ADR-0039: own — the discriminator is declared on the level that owns it. + if (obj.ownAttr(OBJECT_ATTR_DISCRIMINATOR) !== undefined) return false; + const seen = new Set([obj]); + for (let s = obj.superData; s !== undefined && !seen.has(s); s = s.superData) { + seen.add(s); + // ADR-0039: own — the TPH base is the ancestor that declares @discriminator. + if (s.ownAttr(OBJECT_ATTR_DISCRIMINATOR) !== undefined) return true; + } + return false; +} + /** * Table C (`required` of a dimension). Without `@spine`: only a dimension over an `@of` field * that is effectively required (`@required`, or a `validator.required` child), and, when it has a `@via`, only when every hop is a - * required belongs-to reference, the hops the view joins INNER ({@link viaJoinsInner}). With `@spine`: only a dimension + * required, enforced belongs-to reference onto a target that hides no rows ({@link viaAlwaysMatches}). With `@spine`: only a dimension * whose `@via` hops equal the spine's (a column of the spine entity itself, whose rows are * the report's rows) over an `@of` field that is `@required` or a primary-key column of the * entity `@of` names. A dimension beyond the spine is reached by a LEFT OUTER join. @@ -410,7 +431,7 @@ function dimensionRequired( if (!of.isRequired) return false; const hops = reportingViaHops(via, reportingMemberOwner(dim, from), from, root); if (hops === undefined) throw unresolved(reportName, `dimension '${dim.name}' @via '${via}'`); - return viaJoinsInner(hops, from, root); + return viaAlwaysMatches(hops, from, root); } if (via === undefined) return false; // a column of @from: null in a spine row with no facts const hops = reportingViaHops(via, reportingMemberOwner(dim, from), from, root); diff --git a/server/typescript/packages/metadata/src/index.ts b/server/typescript/packages/metadata/src/index.ts index 9d15ac675c..ea1f64c039 100644 --- a/server/typescript/packages/metadata/src/index.ts +++ b/server/typescript/packages/metadata/src/index.ts @@ -166,11 +166,14 @@ export type { IdentityPassthroughResolution } from "./core/identity/validate-ide // Relationship export { MetaRelationship } from "./core/relationship/meta-relationship.js"; // Cross-entity reference lookup +export { resolveFlattenedMember, resolveFieldPathColumn, type FlattenedMember } from "./core/field/flattened-member.js"; export { findReferenceBetween, findReferencesBetween, findReverseReferenceHops, reverseHopName, type ReverseReferenceHop, } from "./core/relationship/find-reference.js"; export type { ReferenceLookup } from "./core/relationship/find-reference.js"; +export { resolveFilterPath, type FilterPath } from "./core/relationship/filter-path.js"; +export { effectiveProjectionFilter } from "./core/object/effective-projection-filter.js"; export { referenceCandidatesFor, referencePairingKeys, @@ -386,6 +389,7 @@ export { resetNamingStrategiesForTesting, registerPluralizer, registeredPluralizers, isRegisteredPluralizer, pluralizer, BUILTIN_PLURALIZER_NAMES, DEFAULT_PLURALIZER, collectionNameOf, declaredCollectionName, + collectionSegment, collectionSegmentOf, snakeCollectionName, TABLE_NAMING_RULES, DEFAULT_PHYSICAL_NAMING, physicalNamingOf, applyTableNamingRule, PHYSICAL_NAMING_MIGRATION_GUIDE, PHYSICAL_NAMING_MIGRATION_GUIDE_PATH, physicalNamingRemovedMessage, resolveTableName, resolveColumnName, resolveFlattenedColumnPrefix, resolveFlattenedColumnName, diff --git a/server/typescript/packages/metadata/src/loader/meta-data-loader.ts b/server/typescript/packages/metadata/src/loader/meta-data-loader.ts index 726c1671b3..0fa15aaab9 100644 --- a/server/typescript/packages/metadata/src/loader/meta-data-loader.ts +++ b/server/typescript/packages/metadata/src/loader/meta-data-loader.ts @@ -26,7 +26,7 @@ import type { LoaderWarning } from "../source.js"; import { codeSource, resolvedSource } from "../source.js"; import { parseJson } from "../parser-json.js"; import { validateDataGridSortFields, validateFilterableHasIndex, validateFilterableHasSupportedOps, validateSortableHasSupportedSubtype, validateOriginPaths, validateDerivedFieldProvidability, validateDataGridFilterValues, validateFieldObjectStorage, validateFieldMap, - validateEnumMaxLength, validateIdentitySequence, validateCrossFieldValidators, validateTemplatePayloadRefs, validateFieldDefaults, validateRelationships, validateOneSideReferenceResolution, validateM2MJunctionPairing, validateIndexLookupFields, validateProjectionFilter, validateRetiredRequirementLinks, validateColumnNameCollisions } from "./validation-passes.js"; + validateEnumMaxLength, validateIdentitySequence, validateCrossFieldValidators, validateTemplatePayloadRefs, validateFieldDefaults, validateRelationships, validateOneSideReferenceResolution, validateM2MJunctionPairing, validateIndexLookupFields, validateProjectionFilter, validateOriginSegmentsAndCorrelation, validateRetiredRequirementLinks, validateColumnNameCollisions } from "./validation-passes.js"; import { validateReporting } from "./reporting-validation.js"; import { runRegisteredValidation } from "./validation-registry.js"; import { validateSourceRoles } from "../persistence/source/validate-source-roles.js"; @@ -662,6 +662,7 @@ export class MetaDataLoader { // projection's own declared, WHERE-addressable fields (passthrough/computed/plain); // an aggregate-derived or dangling ref is fail-closed (ERR_BAD_ATTR_FILTER). errors.push(...validateProjectionFilter(root)); + errors.push(...validateOriginSegmentsAndCorrelation(root)); // FR-044 — reporting vocabulary cross-node rules (dimensions, measures, // segments, reports, relative-date filter values). @@ -732,7 +733,7 @@ export class MetaDataLoader { errors.push(...validateIdentitySequence(root)); // Cross-field validators: exactlyOne names two or more fields; @condition names real fields. - errors.push(...validateCrossFieldValidators(root)); + errors.push(...validateCrossFieldValidators(root, this._strict)); // Tenth pass: one-primary multi-source rule — if an object has ≥1 source, // exactly one must carry role "primary" (ERR_SOURCE_NO_PRIMARY / diff --git a/server/typescript/packages/metadata/src/loader/validation-passes.ts b/server/typescript/packages/metadata/src/loader/validation-passes.ts index d6e19de579..165d42858a 100644 --- a/server/typescript/packages/metadata/src/loader/validation-passes.ts +++ b/server/typescript/packages/metadata/src/loader/validation-passes.ts @@ -10,7 +10,7 @@ // Private: _findObject, _findField, _findRelationship, // _validateFromPath, _validateViaPath (helpers, not exported). -import { TYPE_VALIDATOR } from "../shared/base-types.js"; +import { TYPE_VALIDATOR, TYPE_SEGMENT } from "../shared/base-types.js"; import { CROSS_FIELD_VALIDATOR_SUBTYPES, VALIDATOR_ATTR_CONDITION, @@ -18,12 +18,17 @@ import { VALIDATOR_ATTR_FIELDS, VALIDATOR_ATTR_WHEN, VALIDATOR_SUBTYPE_EXACTLY_ONE, + VALIDATOR_SUBTYPE_AT_LEAST_ONE, + VALIDATOR_ATTR_MAX, VALIDATOR_SUBTYPE_PRESENT_IFF, VALIDATOR_SUBTYPE_REQUIRED_WHEN, } from "../core/validator/validator-constants.js"; import { parseDurationLiteral } from "../core/field/duration-wire.js"; import type { MetaData } from "../shared/meta-data.js"; import type { MetaObject } from "../core/object/meta-object.js"; +import { resolveFlattenedMember } from "../core/field/flattened-member.js"; +import { resolveFilterPath } from "../core/relationship/filter-path.js"; +import { effectiveProjectionFilter } from "../core/object/effective-projection-filter.js"; import type { MetaReferenceIdentity } from "../core/identity/meta-identity.js"; import { contradictionHint, contradictionsFor } from "../attr-contradictions.js"; import { ParseError, type ErrorCode } from "../errors.js"; @@ -64,7 +69,6 @@ import { OBJECT_SUBTYPE_ENTITY, OBJECT_SUBTYPE_VALUE, OBJECT_SUBTYPE_PROJECTION, - OBJECT_PROJECTION_ATTR_FILTER, } from "../core/object/object-constants.js"; import { MetaSource } from "../persistence/source/meta-source.js"; import { resolveColumnName, resolveFlattenedColumnName } from "../naming.js"; @@ -128,6 +132,11 @@ import { ORIGIN_FIRST_ATTR_OF, ORIGIN_FIRST_ATTR_VIA, ORIGIN_FIRST_ATTR_FILTER, + ORIGIN_ATTR_SEGMENT, + FILTER_OP_IS_DISTINCT_FROM, + FILTER_OP_IS_NOT_DISTINCT_FROM, + CORRELATED_FILTER_OPS, + FILTER_OPERAND_FIELD, AGG_ANY, AGG_ALL, AGG_COLLECT, @@ -675,7 +684,9 @@ function _validateFromPath( ); return undefined; } - const sourceField = _findField(sourceObj, targetFieldName); + // A member of a flattened value object (`Plan.address.city`) is a column of the owning table. + const sourceField = _findField(sourceObj, targetFieldName) + ?? resolveFlattenedMember(sourceObj, targetFieldName)?.member; if (!sourceField) { // FR5d — entity resolved, field on it did not. target = full ref. errors.push( @@ -717,8 +728,8 @@ interface WalkedViaPath { hops: MetaData[]; terminal: MetaData; /** The hops walked BACKWARDS: an identity.reference of the next entity, walked from - * the entity it targets, so the hop is to-many. */ - reverse: ReadonlySet; + * the entity it targets, so the hop is to-many — keyed to the hop name the path wrote. */ + reverse: ReadonlyMap; } /** @@ -777,7 +788,7 @@ function _validateViaPath( // entity name; each successful relationship hop appends a segment. const validSegments: string[] = [entityName]; const hops: MetaData[] = []; - const reverse = new Set(); + const reverse = new Map(); for (const relName of relSegments) { // FR-024: a hop may name a relationship OR a reference-only FK // (identity.reference) — the reference IS a navigable many-to-one edge. @@ -790,7 +801,7 @@ function _validateViaPath( if (reversed.length === 1) { validSegments.push(relName); hops.push(reversed[0]!.referenceIdentity); - reverse.add(reversed[0]!.referenceIdentity); + reverse.set(reversed[0]!.referenceIdentity, relName); currentObj = reversed[0]!.holder; continue; } @@ -877,7 +888,7 @@ function _validateViaPath( /** A hop's effective @cardinality, or undefined when not declared. A reference * hop (a forward FK) is inherently to-one — a child names the parent it points at. */ -function _hopCardinality(rel: MetaData, reverse?: ReadonlySet): string | undefined { +function _hopCardinality(rel: MetaData, reverse?: ReadonlyMap): string | undefined { // A reference walked backwards reaches every row that references the walked-from one. if (reverse?.has(rel) === true) return CARDINALITY_MANY; if (_isReferenceHop(rel)) return CARDINALITY_ONE; @@ -965,6 +976,12 @@ function _deriveBaseEntity( } } if (targets.size === 1) return [...targets][0]; + // A projection that extends another projection, and anchors nothing of its own, has the + // base entity its super has (its inherited identity and fields are anchored there). + const sup = obj.superResolved; + if (targets.size === 0 && sup !== undefined && sup.type === TYPE_OBJECT && sup.subType === OBJECT_SUBTYPE_PROJECTION) { + return _deriveBaseEntity(sup, root, fieldName, originSource, errors); + } if (targets.size > 1) { const names = [...targets].map((t) => `"${t.name}"`).join(", "); errors.push( @@ -1078,13 +1095,13 @@ function _checkPassthroughCardinality( fieldName: string, originSource: ErrorSource, errors: ParseError[], - reverse?: ReadonlySet, + reverse?: ReadonlyMap, ): void { for (const rel of hops) { if (_hopCardinality(rel, reverse) === CARDINALITY_MANY) { errors.push( new ParseError( - `origin.passthrough on ${obj.name}.${fieldName}: @via hop "${rel.name}" is to-many ` + + `origin.passthrough on ${obj.name}.${fieldName}: @via hop "${reverse?.get(rel) ?? rel.name}" is to-many ` + (reverse?.has(rel) ? `(reverse hop over identity.reference "${rel.name}")` : `(@cardinality "${CARDINALITY_MANY}")`) + @@ -1109,7 +1126,7 @@ function _checkAggregateCardinality( fieldName: string, originSource: ErrorSource, errors: ParseError[], - reverse?: ReadonlySet, + reverse?: ReadonlyMap, ): void { if (hops.length === 0) return; const provablyToOne = hops.every((rel) => _hopCardinality(rel, reverse) === CARDINALITY_ONE); @@ -1696,6 +1713,22 @@ export function validateOriginPaths(root: MetaData): ParseError[] { } continue; } + // A literal compared with a field must be able to equal it: of its kind, and for an enum + // one of its @values (the same rule a validator's @condition follows). Resolved as the + // inference resolved the ref: a projection field first, else a base field. + const literalProblem = literalComparisonProblem(expr, (name) => { + if (obj !== base && name !== field.name) { + const own = obj.children().find((f) => f.type === TYPE_FIELD && f.name === name); + if (own !== undefined) return ownOriginOf(name)?.subType === ORIGIN_SUBTYPE_AGGREGATE ? undefined : own; + } + return base.children().find((f) => f.type === TYPE_FIELD && f.name === name); + }); + if (literalProblem !== undefined) { + errors.push(new ParseError( + `origin.computed on ${obj.name}.${field.name}: @expr ${literalProblem}`, + { code: "ERR_INVALID_ORIGIN", source: src })); + continue; + } if (inferred.type !== undefined && inferred.type !== field.subType) { errors.push(new ParseError( `origin.computed on ${obj.name}.${field.name}: @expr infers field.${inferred.type} but the field is declared field.${field.subType} — a computed column's type is derived from its expression and must match (no @convert escape).`, @@ -1783,6 +1816,7 @@ export function validateOriginPaths(root: MetaData): ParseError[] { export function validateDerivedFieldProvidability(root: MetaData): ParseError[] { const errors: ParseError[] = []; + const reached = projectionReachedEntityFields(root); // ADR-0039: root has no super; children()==ownChildren() but resolving is the default. for (const obj of root .children() @@ -1795,12 +1829,18 @@ export function validateDerivedFieldProvidability(root: MetaData): ParseError[] // ADR-0039: own — origin-bearing fields are validated on the OWN-declared // layer (mirrors validateOriginPaths; origin.* never inherits, ADR-0029). for (const field of obj.ownChildren().filter((c) => c.type === TYPE_FIELD)) { - if (!field.ownChildren().some((c) => c.type === TYPE_ORIGIN)) continue; + const origin = field.ownChildren().find((c) => c.type === TYPE_ORIGIN); + if (origin === undefined) continue; + // A derived field a projection reaches (its @filter or a passthrough + // @from) is lowered inline there, so it needs no read source. Only the inlinable origins. + if ((origin.subType === ORIGIN_SUBTYPE_FIRST || origin.subType === ORIGIN_SUBTYPE_COMPUTED) + && reached.has(field)) continue; errors.push( new ParseError( `derived field "${obj.name}.${field.name}" carries an origin.* but entity "${obj.name}" declares no ` + `read-capable source — derived fields do not exist on the writable table. Declare a read-only source ` + - `(e.g. source.rdb @kind "view" @role "replica") to provide it, or move the field to an object.projection (FR-024 §7).`, + `(e.g. source.rdb @kind "view" @role "replica") to provide it, move the field to an object.projection (FR-024 §7), ` + + `or reach it from a projection's @filter or @from (an origin.first or origin.computed is then inlined there).`, { code: "ERR_DERIVED_FIELD_NO_READ_SOURCE", source: field.source }, ), ); @@ -1809,6 +1849,139 @@ export function validateDerivedFieldProvidability(root: MetaData): ParseError[] return errors; } + +// --------------------------------------------------------------------------- +// The entity fields projections REACH without outputting them: a +// projection @filter key naming a base field or a to-one path, and a passthrough @from. A +// derived field reached this way is lowered inline where it is reached. +// --------------------------------------------------------------------------- + +/** Every entity field node some projection's @filter key or passthrough @from names. */ +export function projectionReachedEntityFields(root: MetaData): Set { + const reached = new Set(); + for (const obj of root.children().filter((c) => c.type === TYPE_OBJECT && c.subType === OBJECT_SUBTYPE_PROJECTION)) { + const silent: ParseError[] = []; + const base = _deriveBaseEntity(obj, root, "@filter", obj.source, silent); + // The effective filter — an extender reaches what its supers' filters name, too. + const filter = effectiveProjectionFilter(obj); + if (base !== undefined && base !== obj) { + for (const key of filterKeys(filter)) { + const path = resolveFilterPath(base as MetaObject, key); + if (path !== undefined && typeof path !== "string") reached.add(path.field); + else if (path === undefined) { + const f = base.children().find((c) => c.type === TYPE_FIELD && c.name === key); + if (f !== undefined) reached.add(f); + } + } + } + // ADR-0039: resolving — inherited fields carry their own origins. + for (const field of obj.children().filter((c) => c.type === TYPE_FIELD)) { + for (const origin of field.ownChildren().filter((c) => c.type === TYPE_ORIGIN && c.subType === ORIGIN_SUBTYPE_PASSTHROUGH)) { + const from = origin.ownAttr(ORIGIN_PASSTHROUGH_ATTR_FROM); + if (typeof from !== "string") continue; + const target = _validateFromPath(from, root, obj, field.name, origin.source, silent); + if (target !== undefined) reached.add(target.field); + } + } + } + return reached; +} + +/** + * Origin segments and correlated filters, on every origin.first / origin.aggregate: + * - `@segment` names a segment.filter declared on the related entity (the @of entity, else + * the @via terminal) — ERR_INVALID_ORIGIN otherwise; + * - an `@filter` operand `{ field: "" }` names a field of the base row (the projection's + * base entity, or the entity hosting the field), with an op that compares two columns; + * the null-safe ops take only such an operand — ERR_BAD_ATTR_FILTER otherwise. + */ +export function validateOriginSegmentsAndCorrelation(root: MetaData): ParseError[] { + const errors: ParseError[] = []; + for (const obj of root.children().filter((c) => c.type === TYPE_OBJECT + && (c.subType === OBJECT_SUBTYPE_PROJECTION || c.subType === OBJECT_SUBTYPE_ENTITY))) { + for (const field of obj.ownChildren().filter((c) => c.type === TYPE_FIELD)) { + for (const origin of field.ownChildren().filter((c) => c.type === TYPE_ORIGIN + && (c.subType === ORIGIN_SUBTYPE_FIRST || c.subType === ORIGIN_SUBTYPE_AGGREGATE))) { + const where = `origin.${origin.subType} on ${obj.name}.${field.name}`; + const segment = origin.ownAttr(ORIGIN_ATTR_SEGMENT); + if (typeof segment === "string") { + const related = originRelatedEntity(origin, obj, root); + if (related !== undefined && !related.children().some((c) => c.type === TYPE_SEGMENT && c.name === segment)) { + errors.push(new ParseError( + `${where}: @segment "${segment}" is not a segment declared on "${related.name}", the entity whose rows it scopes.`, + { code: "ERR_INVALID_ORIGIN", source: origin.source }, + )); + } + } + const filter = origin.ownAttr(ORIGIN_FIRST_ATTR_FILTER); + if (filter === undefined) continue; + const silent: ParseError[] = []; + const base = _deriveBaseEntity(obj, root, field.name, origin.source, silent); + if (base === undefined) continue; + checkCorrelatedOperands(filter, base, where, origin.source, errors); + } + } + } + return errors; +} + +/** The entity whose rows an origin.first / origin.aggregate spans: its @of entity, else its @via terminal. */ +function originRelatedEntity(origin: MetaData, obj: MetaData, root: MetaData): MetaData | undefined { + const of_ = origin.ownAttr(ORIGIN_FIRST_ATTR_OF); + if (typeof of_ === "string" && of_ !== "") { + return _validateFromPath(of_, root, obj, origin.parent?.name ?? "", origin.source, [])?.entity; + } + const referrerPkg = obj.package ?? obj.fileDefaultPackage ?? ""; + const via = origin.ownAttr(ORIGIN_AGGREGATE_ATTR_VIA); + if (typeof via !== "string") return undefined; + const [head, ...hops] = via.split("."); + let current = resolveObjectRef(root, head ?? "", referrerPkg).node; + for (const hop of hops) { + if (current === undefined) return undefined; + const rel = current.children().find((c) => (c.type === TYPE_RELATIONSHIP || c.type === TYPE_IDENTITY) && c.name === hop); + current = rel === undefined + ? findReverseReferenceHops(current as MetaObject, hop, isMetaRoot(root) ? root.objects() : [])[0]?.holder + : refTarget(rel, rel.type === TYPE_RELATIONSHIP ? RELATIONSHIP_ATTR_OBJECT_REF : IDENTITY_REFERENCE_ATTR_REFERENCES); + } + return current; +} + +function checkCorrelatedOperands(filter: unknown, base: MetaData, where: string, source: ErrorSource, errors: ParseError[]): void { + if (typeof filter !== "object" || filter === null || Array.isArray(filter)) return; + for (const [key, clause] of Object.entries(filter as Record)) { + if (key === FILTER_COMPOSE_AND || key === FILTER_COMPOSE_OR) { + if (Array.isArray(clause)) for (const sub of clause) checkCorrelatedOperands(sub, base, where, source, errors); + continue; + } + if (typeof clause !== "object" || clause === null || Array.isArray(clause)) continue; + for (const [op, value] of Object.entries(clause as Record)) { + const isOperand = typeof value === "object" && value !== null && !Array.isArray(value) && FILTER_OPERAND_FIELD in value; + const nullSafe = op === FILTER_OP_IS_DISTINCT_FROM || op === FILTER_OP_IS_NOT_DISTINCT_FROM; + if (!isOperand) { + if (nullSafe) { + errors.push(new ParseError( + `${where}: @filter on "${key}" uses "${op}", which compares with a base-row field: write { ${op}: { ${FILTER_OPERAND_FIELD}: "" } }.`, + { code: "ERR_BAD_ATTR_FILTER", source }, + )); + } + continue; + } + const name = (value as Record)[FILTER_OPERAND_FIELD]; + if (!CORRELATED_FILTER_OPS.includes(op)) { + errors.push(new ParseError( + `${where}: @filter on "${key}" compares with a base-row field using "${op}"; a correlated operand takes ${CORRELATED_FILTER_OPS.join(", ")}.`, + { code: "ERR_BAD_ATTR_FILTER", source }, + )); + } else if (typeof name !== "string" || !base.children().some((c) => c.type === TYPE_FIELD && c.name === name)) { + errors.push(new ParseError( + `${where}: @filter on "${key}" names base-row field "${String(name)}", which "${base.name}" does not declare.`, + { code: "ERR_BAD_ATTR_FILTER", source }, + )); + } + } + } +} + // --------------------------------------------------------------------------- // Cross-field validators (comparison / requiredWhen / presentIff / atLeastOne / exactlyOne), // which the database enforces as CHECKs: @@ -1838,6 +2011,10 @@ function expressionNodes(node: unknown, out: Record[] = []): Re return out; } +function isObj(v: unknown): v is Record { + return typeof v === "object" && v !== null && !Array.isArray(v); +} + /** The field names an attr.expression tree references. */ function expressionFieldRefs(node: unknown): string[] { return expressionNodes(node).flatMap((n) => (typeof n["field"] === "string" ? [n["field"]] : [])); @@ -1873,53 +2050,82 @@ function conditionProblem(condition: unknown, obj: MetaObject): string | undefin if (inferred.type !== FIELD_SUBTYPE_BOOLEAN) { return `must be a boolean expression (a comparison, a null test, or and / or / not of them), not a field.${String(inferred.type)}.`; } - let problem: string | undefined; - const visit = (node: unknown): void => { - if (problem !== undefined || typeof node !== "object" || node === null) return; - const n = node as Record; + const hasNull = expressionNodes(condition).some((n) => + n["left"] !== undefined && n["right"] !== undefined && + [n["left"], n["right"]].some((side) => isObj(side) && "value" in side && side["value"] === null)); + if (hasNull) return `compares with null, which is never true: use isNull or isNotNull.`; + return literalComparisonProblem(condition, (name) => fields.get(name)); +} + +/** + * The first literal in `expr` that cannot equal the field it is compared with, or undefined. A + * literal is compared with a field by a comparison (`{op, left, right}`, either side) or as a + * simple case's `when` against a `{field}` operand. The rule, the same in every port and shared by + * a validator's @condition and an origin.computed @expr: the literal is of the field's kind (a + * boolean for field.boolean, a number for a numeric field, a string otherwise), one of its @values + * for field.enum, and an ISO-8601 duration for field.duration. A null literal, and a literal + * facing anything but a field ref, are not checked here. + */ +export function literalComparisonProblem( + expr: unknown, + fieldOf: (name: string) => MetaData | undefined, +): string | undefined { + const check = (lit: unknown, ref: unknown): string | undefined => { + if (!isObj(lit) || !("value" in lit) || lit["value"] === null) return undefined; + if (!isObj(ref) || typeof ref["field"] !== "string") return undefined; + const field = fieldOf(ref["field"]); + if (field === undefined) return undefined; + const value = lit["value"]; + const kind = field.subType === FIELD_SUBTYPE_BOOLEAN ? "boolean" + : _CONDITION_NUMBER_SUBTYPES.has(field.subType) ? "number" : "string"; + if (typeof value !== kind) { + return `compares "${field.name}" (field.${field.subType}) with ${JSON.stringify(value)}; it needs a ${kind}.`; + } + const members = field.subType === FIELD_SUBTYPE_ENUM ? field.attr(FIELD_ATTR_VALUES) : undefined; + if (Array.isArray(members) && !members.map(String).includes(String(value))) { + return `compares "${field.name}" with ${JSON.stringify(value)}, which is not one of its @values (${members.join(", ")}).`; + } + if (field.subType === FIELD_SUBTYPE_DURATION && parseDurationLiteral(value as string) === undefined) { + return `compares "${field.name}" with ${JSON.stringify(value)}, which is not ${DURATION_LITERAL_FORM}.`; + } + return undefined; + }; + for (const n of expressionNodes(expr)) { if (n["left"] !== undefined && n["right"] !== undefined) { - for (const [a, b] of [[n["left"], n["right"]], [n["right"], n["left"]]] as const) { - const lit = a as Record; - const ref = b as Record; - if (!("value" in lit)) continue; - if (lit["value"] === null) { - problem = `compares with null, which is never true: use isNull or isNotNull.`; - return; - } - const field = typeof ref["field"] === "string" ? fields.get(ref["field"]) : undefined; - if (field === undefined) continue; - const value = lit["value"]; - const kind = field.subType === FIELD_SUBTYPE_BOOLEAN ? "boolean" - : _CONDITION_NUMBER_SUBTYPES.has(field.subType) ? "number" : "string"; - if (typeof value !== kind) { - problem = `compares "${field.name}" (field.${field.subType}) with ${JSON.stringify(value)}; it needs a ${kind}.`; - return; - } - const members = field.subType === FIELD_SUBTYPE_ENUM ? field.attr(FIELD_ATTR_VALUES) : undefined; - if (Array.isArray(members) && !members.map(String).includes(String(value))) { - problem = `compares "${field.name}" with ${JSON.stringify(value)}, which is not one of its @values (${members.join(", ")}).`; - return; - } - if (field.subType === FIELD_SUBTYPE_DURATION && parseDurationLiteral(value as string) === undefined) { - problem = `compares "${field.name}" with ${JSON.stringify(value)}, which is not ${DURATION_LITERAL_FORM}.`; - return; - } + const problem = check(n["left"], n["right"]) ?? check(n["right"], n["left"]); + if (problem !== undefined) return problem; + } + if (EXPR_CASE_OPERAND in n && Array.isArray(n[EXPR_CASE])) { + for (const arm of n[EXPR_CASE] as unknown[]) { + const problem = isObj(arm) ? check(arm[EXPR_CASE_WHEN], n[EXPR_CASE_OPERAND]) : undefined; + if (problem !== undefined) return problem; } } - for (const key of ["left", "right", "arg"]) visit(n[key]); - if (Array.isArray(n["args"])) for (const a of n["args"]) visit(a); - }; - visit(condition); - return problem; + } + return undefined; } -export function validateCrossFieldValidators(root: MetaData): ParseError[] { +export function validateCrossFieldValidators(root: MetaData, strict = false): ParseError[] { const errors: ParseError[] = []; for (const obj of root.children().filter((c) => c.type === TYPE_OBJECT)) { const fieldNames = new Set((obj as MetaObject).fields().map((f) => f.name)); // ADR-0039: own — each validator is checked where it is declared. for (const v of obj.ownChildren()) { if (v.type !== TYPE_VALIDATOR || !CROSS_FIELD_VALIDATOR_SUBTYPES.includes(v.subType)) continue; + // validator.atLeastOne takes no @max (1.3.0): "exactly one of these fields" is + // validator.exactlyOne. A strict load already refuses the unregistered attr; this refuses it + // under the default (non-strict) load too, where it would otherwise be kept and ignored — + // silently a weaker constraint than its author wrote. + // ADR-0039: own — the attr is checked on the validator that declares it. + if (v.subType === VALIDATOR_SUBTYPE_AT_LEAST_ONE && v.ownAttr(VALIDATOR_ATTR_MAX) !== undefined) { + if (strict) continue; // the attr-schema pass already refused it + errors.push(new ParseError( + `validator.atLeastOne "${obj.name}.${v.name}" carries @max, which atLeastOne does not take. ` + + `For "exactly one of these fields", declare validator.exactlyOne instead; for "at least one", remove @max.`, + { code: "ERR_UNKNOWN_ATTR", source: v.source }, + )); + continue; + } if (v.subType === VALIDATOR_SUBTYPE_EXACTLY_ONE) { const fields = [v.attr(VALIDATOR_ATTR_FIELDS)].flat().filter((f) => typeof f === "string"); if (fields.length < 2) { @@ -2986,6 +3192,30 @@ function checkFilterClauses( interface ProjectionFilterField { readonly derived: boolean; readonly ops: readonly string[]; + /** The path already failed to resolve, and said why. */ + readonly reported?: boolean; +} + +/** + * An entity field a projection @filter reaches (on the base, or through a path) cannot be + * filtered when it is an aggregate: a WHERE runs before any grouping. Its own stored column, an + * origin.computed or an origin.first (a correlated scalar subquery) are WHERE-legal (entity-derived). + */ +function entityFieldAggregates(f: MetaData): boolean { + // ADR-0039: own — origin.* never inherits (ADR-0029). + const origin = f.ownChildren().find((c) => c.type === TYPE_ORIGIN); + return origin !== undefined && origin.subType === ORIGIN_SUBTYPE_AGGREGATE; +} + +/** Every field key of a (desugared) filter, through and/or. */ +function filterKeys(filter: unknown, out: string[] = []): string[] { + if (typeof filter !== "object" || filter === null || Array.isArray(filter)) return out; + for (const [key, val] of Object.entries(filter as Record)) { + if (key === FILTER_COMPOSE_AND || key === FILTER_COMPOSE_OR) { + if (Array.isArray(val)) for (const sub of val) filterKeys(sub, out); + } else out.push(key); + } + return out; } export function validateProjectionFilter(root: MetaData): ParseError[] { @@ -2994,17 +3224,19 @@ export function validateProjectionFilter(root: MetaData): ParseError[] { for (const obj of root .children() .filter((c) => c.type === TYPE_OBJECT && c.subType === OBJECT_SUBTYPE_PROJECTION)) { - // ADR-0039: own — the @filter is declared locally on this projection. - const filter = obj.ownAttr(OBJECT_PROJECTION_ATTR_FILTER); + // The EFFECTIVE @filter — every level of an extends chain, supers first, ANDed — is + // what the view applies, so it is what is checked (an inherited gate is checked against + // the extender's fields too). + const filter = effectiveProjectionFilter(obj); // Non-object shapes are rejected by the attr schema check (FilterAttr.validateValue). if (typeof filter !== "object" || filter === null || Array.isArray(filter)) continue; - // Classify the projection's OWN fields (the declared set IS the exposure — + // Classify the projection's EFFECTIVE fields (the declared set IS the exposure — // FR-024/ADR-0028): aggregate-derived-ness + the op-band legal for the field's - // declared subType. origin.* never inherits (ADR-0029), so the origin reads - // below are own (category 4). + // declared subType. ADR-0039: resolving — a field inherited from a super projection is + // a column of this view and carries its own origin child, read own below. const fields = new Map(); - for (const f of obj.ownChildren().filter((c) => c.type === TYPE_FIELD)) { + for (const f of obj.children().filter((c) => c.type === TYPE_FIELD)) { const origin = f.ownChildren().find((c) => c.type === TYPE_ORIGIN); const derived = origin !== undefined && @@ -3014,6 +3246,31 @@ export function validateProjectionFilter(root: MetaData): ParseError[] { // stores as an integer, so `like` is not in its band. fields.set(f.name, { derived, ops: opsForField(f) }); } + // A field of the BASE entity the view does not output is filterable too (a WHERE reads + // the base row); a projection field of the same name wins. No base, no fallback: the + // origin passes report why one cannot be derived. + const base = _deriveBaseEntity(obj, root, "@filter", obj.source, []); + if (base !== undefined && base !== obj) { + for (const f of base.children().filter((c) => c.type === TYPE_FIELD)) { + if (!fields.has(f.name)) fields.set(f.name, { derived: entityFieldAggregates(f), ops: opsForField(f) }); + } + // A key that is a PATH of to-one hops from the base, then a + // field of the entity reached (`caseId.status`). Not output; the view joins what it walks. + for (const key of filterKeys(filter)) { + if (fields.has(key)) continue; + const path = resolveFilterPath(base as MetaObject, key); + if (path === undefined) continue; + if (typeof path === "string") { + errors.push(new ParseError( + `projection "${obj.name}" @filter path "${key}" does not resolve: ${path}.`, + { code: "ERR_BAD_ATTR_FILTER", source: obj.source }, + )); + fields.set(key, { derived: false, ops: [], reported: true }); + continue; + } + fields.set(key, { derived: entityFieldAggregates(path.field), ops: opsForField(path.field) }); + } + } checkProjectionFilterRefs(filter as Record, fields, obj.name, obj.source, errors); } return errors; @@ -3053,10 +3310,11 @@ function checkProjectionFilterRefs( continue; } const field = fields.get(key); + if (field?.reported === true) continue; if (field === undefined) { err( `projection "${projectionName}" @filter references "${key}", which is not a declared field of the ` + - `projection. A view-level @filter may only reference the projection's own declared fields.`, + `projection or of its base entity. A view-level @filter may only reference those fields.`, ); continue; } diff --git a/server/typescript/packages/metadata/src/naming.ts b/server/typescript/packages/metadata/src/naming.ts index 10d9344864..d21b0bee7b 100644 --- a/server/typescript/packages/metadata/src/naming.ts +++ b/server/typescript/packages/metadata/src/naming.ts @@ -448,6 +448,40 @@ export function collectionNameOf(obj: MetaData): string { return declaredCollectionName(obj) ?? pluralizer(physicalNamingOf(obj).collections ?? DEFAULT_PLURALIZER)(obj.name); } +/** + * The REST collection segment an object is served under: {@link collectionSegmentOf} over its + * name, its own `@collectionName` and the project's `naming.collections`. Every route, `$path` + * and REST path in the documentation reads it. + */ +export function collectionSegment(obj: MetaData): string { + return collectionSegmentOf(obj.name, declaredCollectionName(obj), physicalNamingOf(obj).collections); +} + +/** + * The REST collection segment for an object named `name` (shared with C#: + * `fixtures/naming-conformance/collection-segments.json`): + * - a declared `@collectionName` wins, snake_cased ({@link snakeCollectionName}); + * - else a configured pluralizer's output over the name, snake_cased the same way; + * - else the rule every port has always served: snake_case, then {@link pluralize}. + * A pluralizer is applied only when the project configures one, so an unconfigured project's + * segments never move (`FAQ` stays `faqs`, never `fa_qs`). + */ +export function collectionSegmentOf(name: string, declared?: string, configuredPluralizer?: string): string { + if (declared !== undefined) return snakeCollectionName(declared); + if (configuredPluralizer !== undefined) return snakeCollectionName(pluralizer(configuredPluralizer)(name)); + return pluralize(toSnakeCase(name)); +} + +/** + * snake_case a collection name with an acronym-aware split: a trailing plural `s` / `es` after a + * capital is a suffix of the acronym, not a word of its own (`FAQs` -> `faqs`, `TaxIDs` -> + * `tax_ids`, `PDFBoxes` -> `pdf_boxes`). Any other name is {@link toSnakeCase}. + */ +export function snakeCollectionName(s: string): string { + const m = /^(.*[A-Z])(e?s)$/.exec(s); + return m !== null ? toSnakeCase(m[1]!) + m[2]! : toSnakeCase(s); +} + /** The collection name an object declares itself (`@collectionName`); undefined when it declares none. */ export function declaredCollectionName(obj: MetaData): string | undefined { // ADR-0039: own — a collection name is the declaring object's alone (see collectionNameOf). diff --git a/server/typescript/packages/metadata/src/persistence/origin/origin-constants.ts b/server/typescript/packages/metadata/src/persistence/origin/origin-constants.ts index 47f994b0d1..4891508cbf 100644 --- a/server/typescript/packages/metadata/src/persistence/origin/origin-constants.ts +++ b/server/typescript/packages/metadata/src/persistence/origin/origin-constants.ts @@ -107,3 +107,18 @@ export const ORIGIN_AGG_VALUES = [ AGG_COLLECT, ] as const; export type OriginAggValue = (typeof ORIGIN_AGG_VALUES)[number]; + +/** A segment.filter of the related entity scoping the related rows + * (origin.first, origin.aggregate), ANDed with @filter — mirrors measure.aggregate @segment. */ +export const ORIGIN_ATTR_SEGMENT = "segment"; + +/** The null-safe comparisons an origin @filter may make with a base-row + * field operand (`{ field: "" }`): IS DISTINCT FROM / IS NOT DISTINCT FROM. */ +export const FILTER_OP_IS_DISTINCT_FROM = "isDistinctFrom"; +export const FILTER_OP_IS_NOT_DISTINCT_FROM = "isNotDistinctFrom"; +/** The ops a correlated (base-field) operand may take in an origin @filter. */ +export const CORRELATED_FILTER_OPS: readonly string[] = [ + "eq", "ne", "gt", "gte", "lt", "lte", FILTER_OP_IS_DISTINCT_FROM, FILTER_OP_IS_NOT_DISTINCT_FROM, +]; +/** The key of a correlated operand: `{ field: "" }`. */ +export const FILTER_OPERAND_FIELD = "field"; diff --git a/server/typescript/packages/metadata/src/persistence/origin/origin-definition.embedded.ts b/server/typescript/packages/metadata/src/persistence/origin/origin-definition.embedded.ts index d20ab6f526..be18d1dc7e 100644 --- a/server/typescript/packages/metadata/src/persistence/origin/origin-definition.embedded.ts +++ b/server/typescript/packages/metadata/src/persistence/origin/origin-definition.embedded.ts @@ -93,7 +93,15 @@ export const ORIGIN_DEFINITION: ProviderDefinition = { "name": "filter", "min": 0, "max": 1, - "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." + "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." + }, + { + "type": "attr", + "subType": "string", + "name": "segment", + "min": 0, + "max": 1, + "description": "Optional name of a segment.filter declared on the related entity (the @of entity, else the @via terminal). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." }, { "type": "attr", @@ -168,7 +176,15 @@ export const ORIGIN_DEFINITION: ProviderDefinition = { "name": "filter", "min": 0, "max": 1, - "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." + "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." + }, + { + "type": "attr", + "subType": "string", + "name": "segment", + "min": 0, + "max": 1, + "description": "Optional name of a segment.filter declared on the related entity (the @of entity). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." } ] } diff --git a/server/typescript/packages/metadata/src/persistence/source/validate-source-escapes.ts b/server/typescript/packages/metadata/src/persistence/source/validate-source-escapes.ts index 942ac42477..764a473359 100644 --- a/server/typescript/packages/metadata/src/persistence/source/validate-source-escapes.ts +++ b/server/typescript/packages/metadata/src/persistence/source/validate-source-escapes.ts @@ -25,7 +25,8 @@ import type { MetaData } from "../../shared/meta-data.js"; import { ParseError } from "../../errors.js"; import type { LoaderWarning } from "../../source.js"; import { TYPE_OBJECT, TYPE_SOURCE, TYPE_FIELD } from "../../shared/base-types.js"; -import { OBJECT_SUBTYPE_PROJECTION, OBJECT_PROJECTION_ATTR_FILTER } from "../../core/object/object-constants.js"; +import { OBJECT_SUBTYPE_PROJECTION } from "../../core/object/object-constants.js"; +import { effectiveProjectionFilter } from "../../core/object/effective-projection-filter.js"; import { MetaSource } from "./meta-source.js"; import { MetaField } from "../../core/field/meta-field.js"; import { SOURCE_ATTR_SQL, SOURCE_SUBTYPE_RDB } from "./source-constants.js"; @@ -138,9 +139,9 @@ export function validateSourceEscapes(root: MetaData): SourceEscapeValidationRes // of a TOOL-SYNTHESIZED body; with @sql the author owns the body (and its // WHERE), so wrapping it is deferred cleverness (design doc D5) — reject. if (hasSqlHost && obj.subType === OBJECT_SUBTYPE_PROJECTION) { - // ADR-0039: own — the @filter is declared locally on this projection - // (mirrors validateProjectionFilter). - const filter = obj.ownAttr(OBJECT_PROJECTION_ATTR_FILTER); + // The effective @filter (mirrors validateProjectionFilter) — an inherited row scope + // cannot be applied to an author-owned body either, so it is refused, never dropped. + const filter = effectiveProjectionFilter(obj); if (filter !== undefined) { errors.push( new ParseError( diff --git a/server/typescript/packages/metadata/test/at-least-one-max-default-load.test.ts b/server/typescript/packages/metadata/test/at-least-one-max-default-load.test.ts new file mode 100644 index 0000000000..fd9f683a52 --- /dev/null +++ b/server/typescript/packages/metadata/test/at-least-one-max-default-load.test.ts @@ -0,0 +1,27 @@ +// validator.atLeastOne takes no @max. The shared fixture error-validator-at-least-one-max-unknown +// runs under the strict loader, where the attribute is unregistered; this runs the same input +// through the default (non-strict) loader too, which would otherwise keep @max and ignore it. +// Oracle: that fixture's expected-errors.json, exactly one ERR_UNKNOWN_ATTR at the validator, +// under either loader. + +import { describe, test, expect } from "bun:test"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import { MetaDataLoader } from "../src/loader/meta-data-loader.js"; +import { ParseError } from "../src/errors.js"; + +const FIXTURE = join(import.meta.dir, "../../../../../fixtures/conformance/error-validator-at-least-one-max-unknown"); + +const expected = (JSON.parse(readFileSync(join(FIXTURE, "expected-errors.json"), "utf8")) as { + errors: { code: string; source: { jsonPath: string } }[]; +}).errors.map((e) => [e.code, e.source.jsonPath]); + +describe("validator.atLeastOne with @max", () => { + test.each([false, true])("is refused once (strict: %p)", async (strict) => { + const { errors } = await MetaDataLoader.fromDirectory(join(FIXTURE, "input"), { strict }); + const actual = errors.map((e) => e instanceof ParseError + ? [e.code, (e.source as { jsonPath?: string } | undefined)?.jsonPath] + : [e.message]); + expect(actual).toEqual(expected); + }); +}); diff --git a/server/typescript/packages/metadata/test/naming.test.ts b/server/typescript/packages/metadata/test/naming.test.ts index 78929119d6..19eb1b0f49 100644 --- a/server/typescript/packages/metadata/test/naming.test.ts +++ b/server/typescript/packages/metadata/test/naming.test.ts @@ -23,7 +23,7 @@ import { MetaDataLoader } from "../src/loader/meta-data-loader.js"; import { InMemoryStringSource } from "../src/loader/meta-data-source.js"; import { resolveTableSchema, resolveTableName, primaryRdbSource, - pluralize, pluralizeTableNameLegacy, + pluralize, pluralizeTableNameLegacy, collectionSegmentOf, } from "../src/naming.js"; import { MetaModelError } from "../src/errors.js"; import { MetaObject } from "../src/core/object/meta-object.js"; @@ -389,6 +389,26 @@ describe("fixtures/naming-conformance/already-plural-pluralize.json", () => { ); }); +// The REST collection segment, shared with C#: an unconfigured project keeps the segment every +// port has always served, and an acronym-ending name never splits its plural suffix off as a word. +describe("fixtures/naming-conformance/collection-segments.json", () => { + const fixturePath = resolve( + dirname(fileURLToPath(import.meta.url)), + "..", "..", "..", "..", "..", + "fixtures", "naming-conformance", "collection-segments.json", + ); + const { cases } = JSON.parse(readFileSync(fixturePath, "utf8")) as { + cases: { name: string; collectionName?: string; pluralizer?: string; segment: string }[]; + }; + + it.each(cases.map((c) => [c.name, c.collectionName ?? null, c.pluralizer ?? null, c.segment] as const))( + "%s (declared %s, pluralizer %s) -> %s", + (name, declared, configured, segment) => { + expect(collectionSegmentOf(name, declared ?? undefined, configured ?? undefined)).toBe(segment); + }, + ); +}); + describe("toPascalColumn", () => { it("upper-cases the first letter only", () => { expect(toPascalColumn("requestURL")).toBe("RequestURL"); diff --git a/server/typescript/packages/metadata/test/object-definition-completeness.test.ts b/server/typescript/packages/metadata/test/object-definition-completeness.test.ts index af2617950c..cf7cac81a4 100644 --- a/server/typescript/packages/metadata/test/object-definition-completeness.test.ts +++ b/server/typescript/packages/metadata/test/object-definition-completeness.test.ts @@ -86,7 +86,7 @@ const REPORT_ATTRS: Record = { spine: { valueType: "string", required: false }, }; -// Cross-port attrs some ports act on: @collectionName (implementedIn C#) on entity / +// Cross-port attrs some ports act on: @collectionName (implementedIn C# and TypeScript) on entity / // projection / report, and the JVM's @object / @objectAdapter on object.base, which every subtype // composes. const COLLECTION_NAME: Record = { diff --git a/server/typescript/packages/metadata/test/origin-definition-completeness.test.ts b/server/typescript/packages/metadata/test/origin-definition-completeness.test.ts index 027fef3018..ace8020c3d 100644 --- a/server/typescript/packages/metadata/test/origin-definition-completeness.test.ts +++ b/server/typescript/packages/metadata/test/origin-definition-completeness.test.ts @@ -52,6 +52,8 @@ const EXPECTED: Record> = { filter: { valueType: "filter", required: false }, distinct: { valueType: "boolean", required: false }, orderBy: { valueType: "string", required: false }, + // Origin @segment. + segment: { valueType: "string", required: false }, }, collection: { via: { valueType: "string", required: true }, @@ -64,6 +66,8 @@ const EXPECTED: Record> = { via: { valueType: "string", required: false }, orderBy: { valueType: "string", required: true }, filter: { valueType: "filter", required: false }, + // Origin @segment. + segment: { valueType: "string", required: false }, }, }; diff --git a/server/typescript/packages/metadata/test/report-required-conformance.test.ts b/server/typescript/packages/metadata/test/report-required-conformance.test.ts new file mode 100644 index 0000000000..0db7f86ce2 --- /dev/null +++ b/server/typescript/packages/metadata/test/report-required-conformance.test.ts @@ -0,0 +1,25 @@ +// A report dimension's `required` comes from the model, never from the join type the view +// lowering picks (INNER or LEFT OUTER). The shared table lives in +// fixtures/conformance/report-dimension-required-by-model/expected-report-required.json; C# +// (ReportShapeTests) reads the same file. + +import { describe, test, expect } from "bun:test"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; +import { MetaDataLoader } from "../src/loader/meta-data-loader.js"; +import { reportShape } from "../src/core/reporting/report-shape.js"; + +const FIXTURE = join(import.meta.dir, "../../../../../fixtures/conformance/report-dimension-required-by-model"); + +describe("report-dimension-required-by-model", () => { + test("every field's required is the shared table's", async () => { + const { root, errors } = await MetaDataLoader.fromDirectory(join(FIXTURE, "input")); + expect(errors).toEqual([]); + const expected = JSON.parse(readFileSync(join(FIXTURE, "expected-report-required.json"), "utf8")) as { + report: string; required: Record; + }; + const report = root.objects().find((o) => o.resolutionKey() === expected.report)!; + const actual = Object.fromEntries(reportShape(report, root).fields.map((f) => [f.name, f.required])); + expect(actual).toEqual(expected.required); + }); +}); diff --git a/server/typescript/packages/metadata/test/report-shape.test.ts b/server/typescript/packages/metadata/test/report-shape.test.ts index 52ac3b5d3b..4f7f96cbae 100644 --- a/server/typescript/packages/metadata/test/report-shape.test.ts +++ b/server/typescript/packages/metadata/test/report-shape.test.ts @@ -642,7 +642,7 @@ describe("reportShape required of a dimension reached through @via", () => { const req = requiredOf(root, "Plain"); expect(req["realmName"]).toBe(true); // two required hops over a required field expect(req["sessionRealmId"]).toBe(true); // one required hop - expect(req["looseRealmName"]).toBe(true); // @enforce: false does not change the join type + expect(req["looseRealmName"]).toBe(false); // an @enforce: false hop can name a missing row expect(req["realmMotto"]).toBe(false); // the target field is optional expect(req["maybeRealmName"]).toBe(false); // a hop over a nullable foreign key is a LEFT join }); diff --git a/server/typescript/packages/migrate-ts/src/expected-schema.ts b/server/typescript/packages/migrate-ts/src/expected-schema.ts index ccb42f0906..e0bbf04d12 100644 --- a/server/typescript/packages/migrate-ts/src/expected-schema.ts +++ b/server/typescript/packages/migrate-ts/src/expected-schema.ts @@ -67,7 +67,7 @@ import { STORAGE_FLATTENED, DOC_ATTR_DESCRIPTION, applyColumnNamingStrategy, physicalNamingOf, - resolveTableName, resolveColumnName, resolveFlattenedColumnName, resolveTableSchema, resolveIndexName, + resolveTableName, resolveColumnName, resolveFlattenedColumnName, resolveFieldPathColumn, resolveTableSchema, resolveIndexName, resolveEnumMaxLength, IDENTITY_ATTR_SEQUENCE_NAME, IDENTITY_ATTR_SEQUENCE_START, @@ -1237,9 +1237,13 @@ function buildForeignKeys( const fkFieldJsNames = readIdentityFields(refChild); if (fkFieldJsNames.length === 0) continue; + // An entry may name a member of a flattened value object (`address.countryId`), whose + // column is on this table under the embedding's prefix. const fkCols = fkFieldJsNames.map((jsName) => { const fkField = findField(holder, jsName); - return fkField ? resolveColumnName(fkField, strategy) : applyColumnNamingStrategy(jsName, strategy); + return fkField + ? resolveColumnName(fkField, strategy) + : resolveFieldPathColumn(holder, jsName, strategy) ?? applyColumnNamingStrategy(jsName, strategy); }); // Target columns: prefer explicit multi-field dotted form, else delegate diff --git a/server/typescript/packages/test-generators/src/templates/queries-file.ts b/server/typescript/packages/test-generators/src/templates/queries-file.ts index d7362d6181..22264e2430 100644 --- a/server/typescript/packages/test-generators/src/templates/queries-file.ts +++ b/server/typescript/packages/test-generators/src/templates/queries-file.ts @@ -22,7 +22,7 @@ import { schemaInputType, } from "./queries.js"; import { getPkInfo, getPkFields } from "@metaobjectsdev/codegen-ts/engine"; -import { pluralize, findByIdFnName, listFnName, createFnName, insertPreservingFnName, updateFnName, singleRowVarName } from "@metaobjectsdev/codegen-ts/engine"; +import { findByIdFnName, listFnName, createFnName, insertPreservingFnName, updateFnName, singleRowVarName } from "@metaobjectsdev/codegen-ts/engine"; import { GENERATED_HEADER, GENERATED_EDIT_NOTE, sidecarLine } from "@metaobjectsdev/codegen-ts/engine"; import { isTphDiscriminatorBase, tphConcreteSubtypes } from "./tph-discriminator.js"; import { isProjection, isWriteThrough } from "@metaobjectsdev/codegen-ts/engine"; @@ -186,7 +186,7 @@ import { ${viewVar}, type ${entityName} } from ${JSON.stringify(entityFileName)} } const reads = code` -${findById}export async function ${listFnName(entityName)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { +${findById}export async function ${listFnName(obj)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { let q = db.select().from(${viewVar}).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); @@ -346,7 +346,7 @@ export async function find${baseName}ById(db: Db, ${pkField}: ${pkType}): Promis return row ? ${parseSym}(row) : null; } -export async function list${pluralize(baseName)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${baseTypeSym}[]> { +export async function ${listFnName(base)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${baseTypeSym}[]> { let q = db.select().from(${tableSym}).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); @@ -377,7 +377,7 @@ export async function list${pluralize(baseName)}(db: Db, opts?: { limit?: number const subPartialSym = hasInsertRefinements(sub) ? imp(`${sub.name}InsertBaseSchema@${subFileSpec}`) : subInsertSym; subtypeSections.push(code` -export async function list${pluralize(sub.name)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${subTypeSym}[]> { +export async function ${listFnName(sub)}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${subTypeSym}[]> { let q = db.select().from(${tableSym}).where(${eqSym}(${tableSym}.${discField}, ${valueLit})).$dynamic(); if (opts?.limit !== undefined) q = q.limit(opts.limit); if (opts?.offset !== undefined) q = q.offset(opts.offset); diff --git a/server/typescript/packages/test-generators/src/templates/queries.ts b/server/typescript/packages/test-generators/src/templates/queries.ts index 95f427fd6e..7dda46bc5a 100644 --- a/server/typescript/packages/test-generators/src/templates/queries.ts +++ b/server/typescript/packages/test-generators/src/templates/queries.ts @@ -12,6 +12,7 @@ import { import type { RenderContext } from "@metaobjectsdev/codegen-ts/engine"; import { getPkInfo, pkInfoOf, subTypeToTsType } from "@metaobjectsdev/codegen-ts/engine"; import { supportsReturning } from "@metaobjectsdev/codegen-ts/engine"; +import { flatKeyOfPath } from "@metaobjectsdev/codegen-ts/engine"; import { findByIdFnName, listFnName, @@ -49,7 +50,7 @@ export function renderListFn(entity: MetaObject, ctx: RenderContext, readVar?: s const entityName = entity.name; // Pluralize the PascalCase entity name, preserving capitalization // (e.g., "Category" -> "Categories", not "Categorys"). - const fnName = listFnName(entityName); + const fnName = listFnName(entity); return code` export async function ${fnName}(db: Db, opts?: { limit?: number; offset?: number }): Promise<${entityName}[]> { @@ -231,9 +232,11 @@ interface ReverseFk { export function reverseFksFor(entity: MetaObject): ReverseFk[] { const out: ReverseFk[] = []; for (const ref of entity.referenceIdentities()) { - const fkField = ref.fields[0]; + const fkPath = ref.fields[0]; const targetRef = ref.targetEntity; - if (!fkField || !targetRef) continue; + if (!fkPath || !targetRef) continue; + // A reference over a flattened value-object member names its flat key on the table. + const fkField = flatKeyOfPath(entity, fkPath); const bound = refTarget(ref, IDENTITY_REFERENCE_ATTR_REFERENCES); const target = isMetaObject(bound) ? bound : undefined; out.push({ fkField, targetEntity: target?.name ?? stripPackage(targetRef), target }); @@ -255,8 +258,8 @@ export function renderReverseFinderFns(entity: MetaObject, fk: ReverseFk, ctx: R const ownField = entity.findField(fk.fkField); const valueType = subTypeToTsType(targetPk?.fieldSubType ?? ownField?.subType ?? "long"); - const singleName = reverseFinderFnName(entityName, fk.fkField); - const batchName = reverseFinderInFnName(entityName, fk.fkField); + const singleName = reverseFinderFnName(entity, fk.fkField); + const batchName = reverseFinderInFnName(entity, fk.fkField); const eqSym = imp("eq@drizzle-orm"); const inArraySym = imp("inArray@drizzle-orm"); diff --git a/site-reference/providers.html b/site-reference/providers.html index 690badf69a..1314c59016 100644 --- a/site-reference/providers.html +++ b/site-reference/providers.html @@ -70,9 +70,9 @@

metaobjects-core-types

  • object.entity: @collectionName, @discriminator, @discriminatorValue
  • object.projection: @collectionName, @filter
  • object.report: @collectionName, @dimensions, @filter, @from, @measures, @segment, @spine
  • -
  • origin.aggregate: @agg, @distinct, @filter, @of, @orderBy, @via
  • +
  • origin.aggregate: @agg, @distinct, @filter, @of, @orderBy, @segment, @via
  • origin.computed: @expr
  • -
  • origin.first: @filter, @of, @orderBy, @via
  • +
  • origin.first: @filter, @of, @orderBy, @segment, @via
  • origin.passthrough: @convert, @from, @via
  • relationship.aggregation: @cardinality, @objectRef, @onDelete, @onUpdate, @sourceRefField, @symmetric, @through
  • relationship.association: @cardinality, @objectRef, @onDelete, @onUpdate, @sourceRefField, @symmetric, @through
  • diff --git a/site-reference/types/object.html b/site-reference/types/object.html index 02da1becf9..b4305938b9 100644 --- a/site-reference/types/object.html +++ b/site-reference/types/object.html @@ -119,7 +119,7 @@

    object.entity

    metaobjects-core-types -The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). +The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). @discriminator @@ -197,7 +197,7 @@

    object.projection

    metaobjects-core-types -The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). +The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). @filter @@ -206,7 +206,7 @@

    object.projection

    metaobjects-core-types -Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. Resolves against the projection's own declared fields; an aggregate-derived field is not addressable (fail-closed). +Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. A key names one of the projection's own fields, else a field of its base entity the view need not output, else a PATH of to-one hops (identity.reference or @cardinality one relationship names) from the base then a field of the entity reached ('orderId.customerId.isActive'), joined but not output. A reached entity field may itself be derived (entity-derived: an origin.computed or origin.first declared on the entity, inlined where the filter reaches it). An aggregate-derived field is not addressable (fail-closed); a to-many hop is refused. A value may be a relative date ({ now: "", startOf? }) on a date or timestamp field. @object @@ -260,7 +260,7 @@

    object.report

    metaobjects-core-types -The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). +The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own). @dimensions diff --git a/site-reference/types/origin.html b/site-reference/types/origin.html index f024464eb5..545feab624 100644 --- a/site-reference/types/origin.html +++ b/site-reference/types/origin.html @@ -93,7 +93,7 @@

    origin.aggregate

    metaobjects-core-types -Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: "", startOf? }) on a date or timestamp field. +Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: "", startOf? }) on a date or timestamp field, or { field: "" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom. @of @@ -114,6 +114,15 @@

    origin.aggregate

    Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. On @agg:collect sets element order (non-distinct only); on origin.first (required) selects the row. Semantic — carries no SQL syntax. +@segment +string +no + + +metaobjects-core-types +Optional name of a segment.filter declared on the related entity (the @of entity, else the @via terminal). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates. + + @via string no @@ -186,7 +195,7 @@

    origin.first

    metaobjects-core-types -Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: "", startOf? }) on a date or timestamp field. +Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: "", startOf? }) on a date or timestamp field, or { field: "" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom. @of @@ -207,6 +216,15 @@

    origin.first

    Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. Selects the single row (the first after ordering). The related PK ascending is appended as the deterministic tie-breaker. Semantic — carries no SQL syntax. +@segment +string +no + + +metaobjects-core-types +Optional name of a segment.filter declared on the related entity (the @of entity). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates. + + @via string no diff --git a/spec/metamodel/object.json b/spec/metamodel/object.json index 8c9f73ae9e..c633eaa653 100644 --- a/spec/metamodel/object.json +++ b/spec/metamodel/object.json @@ -31,7 +31,7 @@ { "type": "segment", "subType": "*", "name": "*", "min": 0, "max": null }, { "type": "attr", "subType": "string", "name": "discriminator", "min": 0, "max": 1, "description": "FR-014: names the field on this entity (resolvable via extends:) that holds the subtype-discriminator value. Subtypes of this entity declare @discriminatorValue to bind their rows to a discriminator value. The discriminator field itself is an ordinary field declaration (typically field.enum or field.int / field.string)." }, { "type": "attr", "subType": "string", "name": "discriminatorValue", "min": 0, "max": 1, "description": "FR-014: on a subtype of an entity with @discriminator — the value that identifies rows of this subtype in the shared discriminator field. Wire form is always a string; the underlying field's subtype (enum / int / string) controls codegen + storage coercion. Required on every concrete subtype of a discriminated entity." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] }, { @@ -53,8 +53,8 @@ "whenToUse": "You need a derived, read-only view over entities — a report, summary, or joined/aggregated read model. Declare it (with origin.* children) instead of hand-writing the SELECT/joins; it stays read-only and regenerates.", "rules": "object.projection is a derived read-only representation: every field is extends-bound, origin-derived, or self-declared-under-external-assembly, and all are read-only at the subtype level. Identity is optional and, when present, MUST extend an entity identity. Sources are restricted to read-only @kinds. The declared field set IS the exposure — an inclusive list, fail-closed. A projection NEVER declares relationships (derivation is expressed via @via, not a relationship child) and NEVER co-locates templates — hence its child set omits both relationship and template. The assembly origins (origin.aggregate / origin.computed / origin.first) live HERE, not on object.value; a SOURCELESS projection is the legal shape for a wire-assembled read model, and a template-level @payloadRef/@responseRef may target one (#210). See ADR-0028 (object taxonomy, projection).", "children": [ - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. Resolves against the projection's own declared fields; an aggregate-derived field is not addressable (fail-closed)." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row-scope predicate (a portable attr.filter object: eq/ne/gt/gte/lt/lte/like/in/isNull with and/or, desugared to { field: { op: value } } at parse time) selecting which rows the view returns — lowered to an outer SQL WHERE. A key names one of the projection's own fields, else a field of its base entity the view need not output, else a PATH of to-one hops (identity.reference or @cardinality one relationship names) from the base then a field of the entity reached ('orderId.customerId.isActive'), joined but not output. A reached entity field may itself be derived (entity-derived: an origin.computed or origin.first declared on the entity, inlined where the filter reaches it). An aggregate-derived field is not addressable (fail-closed); a to-many hop is refused. A value may be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." }, + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] }, { @@ -70,7 +70,7 @@ { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional segment of @from scoping the rows." }, { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional row scope over @from's fields; may use relative-date values." }, { "type": "attr", "subType": "string", "name": "spine", "min": 0, "max": 1, "description": "Optional to-one path from @from to the entity whose rows supply the report's rows (e.g. 'Purchase.program'), written like a dimension's @via. With it the report has one row per distinct dimension tuple among THAT entity's rows, including the ones no row of @from refers to: a count there is 0 and any other measure is null unless it declares @default. Every listed dimension must be reached through this path. @segment and @filter still scope the rows of @from and never remove a row. A row of @from whose reference is null or matches nothing is in no row of the report." }, - { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the C# DbSet property the generated context gives it. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } + { "type": "attr", "subType": "string", "name": "collectionName", "min": 0, "max": 1, "implementedIn": ["csharp", "typescript"], "description": "The name of this object's collection, in the object's own casing (e.g. 'CaseHistory', 'Queues'): the REST collection segment (snake_cased) and the reverse-finder names in C# and TypeScript, the C# DbSet property, and the TypeScript list function, list hook and Drizzle table variable. It need not be a grammatical plural. Absent, the project's pluralizer derives it from the object's name (naming.collections in .metaobjects/config.json; built in: standard, none; an application may register its own)." } ] } ] diff --git a/spec/metamodel/origin.json b/spec/metamodel/origin.json index 00f949a3e3..d5b5b85415 100644 --- a/spec/metamodel/origin.json +++ b/spec/metamodel/origin.json @@ -27,7 +27,8 @@ { "type": "attr", "subType": "string", "name": "agg", "min": 1, "max": 1, "allowedValues": ["count", "sum", "avg", "min", "max", "any", "all", "collect"], "description": "The reducing function applied over the related row-set: count/sum/avg/min/max (numeric/ordinal reduces over @of); any/all (predicate quantifiers over @filter — @of forbidden; empty set → any=false, all=true); collect (array rollup — of the @of column, or of the carrying field.object's declared @objectRef value object when @of is omitted; the field must be isArray)." }, { "type": "attr", "subType": "string", "name": "of", "min": 0, "max": 1, "description": "Dotted Entity.field reference identifying the column being aggregated (e.g. 'Week.durationMinutes'). Required for count/sum/avg/min/max; OPTIONAL for collect, where absent means a whole-object rollup of the field's declared @objectRef value object; forbidden for any/all (which quantify over rows via @filter, not a column)." }, { "type": "attr", "subType": "string", "name": "via", "min": 0, "max": 1, "description": "Dotted relationship path from the base entity to the aggregated rows (e.g. 'Program.weeks' or 'Program.weeks.workouts'). May be omitted only when exactly one single-hop relationship leads from the base entity to the @of entity (FR-024, ADR-0029)." }, - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." }, + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows the aggregate spans (required for any/all, where it is the quantified predicate). A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time; codegen renders it per target (e.g. SQL FILTER (WHERE ...) or SQLite CASE WHEN for a relational view). A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." }, + { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional name of a segment.filter declared on the related entity (the @of entity, else the @via terminal). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." }, { "type": "attr", "subType": "boolean", "name": "distinct", "min": 0, "max": 1, "description": "Set (collect-only) to dedupe collected values (set semantics). Not supported on a whole-object collect (@of omitted): it is a guaranteed no-op whenever the value object carries the primary key, and a silent no-op is worse than a refusal." }, { "type": "attr", "subType": "string", "name": "orderBy", "isArray": true, "min": 0, "max": 1, "description": "Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. On @agg:collect sets element order (non-distinct only); on origin.first (required) selects the row. Semantic — carries no SQL syntax." } ] @@ -51,7 +52,8 @@ { "type": "attr", "subType": "string", "name": "of", "min": 1, "max": 1, "description": "Dotted Entity.field reference identifying the column projected from the selected row (e.g. 'ChildA.label'). Type-preserving: the field's declared subType must equal this column's subType (#185 doctrine)." }, { "type": "attr", "subType": "string", "name": "via", "min": 0, "max": 1, "description": "Dotted relationship path from the base entity to the related rows (e.g. 'Parent.childAs'). May be omitted only when exactly one single-hop relationship leads from the base entity to the @of entity (FR-024, ADR-0029)." }, { "type": "attr", "subType": "string", "name": "orderBy", "isArray": true, "min": 1, "max": 1, "description": "Ordering keys as 'field[:asc|desc]' (default asc) over the related entity's fields; nulls sort last. Selects the single row (the first after ordering). The related PK ascending is appended as the deterministic tie-breaker. Semantic — carries no SQL syntax." }, - { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field." } + { "type": "attr", "subType": "filter", "name": "filter", "min": 0, "max": 1, "description": "Optional structured predicate scoping which related rows are eligible for selection. A portable attr.filter object (eq/ne/in/isNull with and/or), desugared to canonical { field: { op: value } } at parse time. A clause value may also be a relative date ({ now: \"\", startOf? }) on a date or timestamp field, or { field: \"\" }, a field of the base row, compared with eq/ne/gt/gte/lt/lte or the null-safe isDistinctFrom / isNotDistinctFrom." }, + { "type": "attr", "subType": "string", "name": "segment", "min": 0, "max": 1, "description": "Optional name of a segment.filter declared on the related entity (the @of entity). Its predicate scopes the related rows, ANDed with @filter, exactly as measure.aggregate @segment does; it may use relative dates." } ] } ]