Skip to content

Commit f503b7c

Browse files
committed
fix(reporting): resolve validator.required for report dimensions; type currency filter operand as number
A report dimension over a field required by a validator.required child was typed nullable in the report shape (all ports); C# entity generation ignored the child too. The generated filter type gave field.currency a string operand.
1 parent 3b9d454 commit f503b7c

10 files changed

Lines changed: 97 additions & 20 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ every port's `verify`), the derived-key parity fix across the ports, and the lis
2727
nested-average fixes found by the first adopter to map real report pages onto the feature.
2828

2929
**How much smaller the code gets.** The size reduction comes in two steps, and each is its own
30-
number. The first landed on 1.0.x: 439 lines of handler code retired. The second is reporting:
31-
with it, the same pages come to about 60 lines of SQL and about 180 lines of metadata.
30+
number. The first landed on 1.0.x: 439 lines of handler code retired. The second is reporting,
31+
and it is a SQL reduction only: in an adopter, hand-written SQL went from 75 lines to 15 (-80%).
32+
Total hand-written lines did not fall, so do not read it as a total line reduction.
3233

3334
**Two diffs to expect when you upgrade.**
3435

@@ -494,6 +495,23 @@ until you regenerate.
494495
flag, as it does when no table changes. Gated in `migrate-ts` unit and drift tests, the CLI's
495496
`verify-db-view-recreate` test, and new `integration-tests` lanes on a real SQLite (with the D1
496497
diff) and a real Postgres.
498+
- **A report dimension over a field required by a `validator.required` child is typed non-null
499+
in every port (FR-044).** `reportShape` read only the inline `@required` attribute, so a
500+
dimension over `Program.title` required through a `validator.required` child came out
501+
nullable (`text(...)`, `z.string().nullable()`, `| null`) although the view column is never
502+
null. It now resolves required-ness as the field's loader and codegen already do (the attr or
503+
the child, own or inherited), in the TypeScript, C#, Java (and so Kotlin) and Python
504+
metadata. C# entity generation had the same gap (a `validator.required` field came out
505+
nullable) and is fixed in the same change; the TypeScript, Java, Kotlin and Python entity and
506+
projection fields were already correct. A model that declares
507+
`@required: true` generates what it did before; regenerate and review the diff if you use
508+
the child form on a dimension's field.
509+
- **TypeScript: the generated filter type gives a `field.currency` field a number operand
510+
(FR-044).** `{ revenue: { gt: 0 } }` on a report list hook did not type-check, because the
511+
filter type fell through to `string` while the allowlist and row schema treat currency as
512+
integer minor units. The other ports have no typed filter operand and already treat currency
513+
as a `long`.
514+
497515
- **TypeScript: a report's decimal fields reach the wire as strings on SQLite, as on Postgres
498516
(FR-044).** A ratio is typed `decimal`, the TypeScript read schema types a decimal as
499517
`string`, and SQLite has no decimal: the view computes a `REAL`, which the driver hands the

‎fixtures/persistence-conformance/canonical/meta.fitness.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"children": [
88
{ "source.rdb": { "@table": "programs" } },
99
{ "field.long": { "name": "id" } },
10-
{ "field.string": { "name": "title", "@required": true, "@maxLength": 200 } },
10+
{ "field.string": { "name": "title", "@maxLength": 200, "children": [ { "validator.required": {} } ] } },
1111
{ "field.currency": { "name": "priceCents", "@required": true, "@currency": "USD" } },
1212
{ "field.enum": { "name": "status", "@required": true, "@values": ["DRAFT", "PUBLISHED", "ARCHIVED"] } },
1313
{ "field.timestamp": { "name": "createdAt", "@column": "created_ts", "@required": true, "@localTime": true } },

‎server/csharp/MetaObjects.Codegen/CSharpNaming.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,13 +1068,13 @@ public static string Pluralize(string name)
10681068
}
10691069

10701070
/// <summary>
1071-
/// Whether a field is non-nullable in the generated entity: explicitly @required,
1072-
/// or part of the primary identity.
1071+
/// Whether a field is non-nullable in the generated entity: required (the @required attr
1072+
/// or a validator.required child), or part of the primary identity.
10731073
/// </summary>
10741074
public static bool IsRequired(MetaObject entity, MetaField field)
10751075
{
1076-
// ADR-0039: resolving — @required may be inherited from an abstract base via extends.
1077-
if (field.Attr(FIELD_ATTR_REQUIRED) is true) return true;
1076+
// ADR-0039: resolving — the attr and the validator set both inherit via extends.
1077+
if (field.IsRequired) return true;
10781078
var pk = entity.PrimaryIdentity();
10791079
return pk is not null && pk.Fields.Contains(field.Name);
10801080
}

‎server/csharp/MetaObjects/Core/Reporting/ReportShape.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,19 @@ private static bool IsPrimaryKeyField(MetaObject entity, MetaField field)
157157

158158
/// <summary>
159159
/// Table C (<c>required</c> of a dimension). Without <c>@spine</c>: only a dimension with
160-
/// no <c>@via</c> over an <c>@of</c> field whose effective <c>@required</c> is true. With
160+
/// no <c>@via</c> over an <c>@of</c> field that is effectively required (<c>@required</c> or a <c>validator.required</c> child). With
161161
/// <c>@spine</c>: only a dimension whose <c>@via</c> hops equal the spine's (a column of
162162
/// the spine entity itself, whose rows are the report's rows) over an <c>@of</c> field that
163-
/// is <c>@required</c> or a primary-key column of the entity <c>@of</c> names. A dimension
163+
/// is required (same rule) or a primary-key column of the entity <c>@of</c> names. A dimension
164164
/// beyond the spine is reached by a LEFT OUTER join.
165165
/// </summary>
166166
private static bool DimensionRequired(
167167
MetaDimension dim, MetaObject named, MetaField of, MetaObject from, MetaRoot root,
168168
string[]? spine, string reportName)
169169
{
170170
string? via = dim.Via();
171-
// The @required ATTR only, read resolving (ADR-0039); a validator.required child does not count.
172-
bool ofRequired = of.Attr(FIELD_ATTR_REQUIRED) is true;
171+
// Effective required-ness, resolving (ADR-0039): the @required attr OR a validator.required child.
172+
bool ofRequired = of.IsRequired;
173173
if (spine is null) return via is null && ofRequired;
174174
if (via is null) return false; // a column of @from: null in a spine row with no facts
175175
var hops = ReportingViaHops(via, ReportingMemberOwner(dim, from), from, root)

‎server/java/metadata/src/main/java/com/metaobjects/reporting/ReportShape.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.metaobjects.object.MetaObject;
3333
import com.metaobjects.reporting.ReportAccessors.ReportDimensionItem;
3434
import com.metaobjects.source.MetaSource;
35+
import com.metaobjects.validator.RequiredValidator;
3536

3637
import java.util.ArrayList;
3738
import java.util.Collections;
@@ -346,8 +347,9 @@ private static boolean isPrimaryKeyField(MetaObject entity, MetaField<?> field)
346347
private static boolean dimensionRequired(MetaDimension dim, MetaObject named, MetaField<?> of, MetaObject from,
347348
MetaRoot root, List<String> spine, MetaObject report) {
348349
String via = dim.getVia();
349-
// Attr only: a validator.required child does not make the column non-null.
350-
boolean ofRequired = ReportingAttrs.isTrue(of, MetaField.ATTR_REQUIRED);
350+
// Effective required-ness, resolving (ADR-0039): the @required attr OR a validator.required child.
351+
boolean ofRequired = ReportingAttrs.isTrue(of, MetaField.ATTR_REQUIRED)
352+
|| of.getValidators().stream().anyMatch(v -> RequiredValidator.SUBTYPE_REQUIRED.equals(v.getSubType()));
351353
if (spine == null) return via == null && ofRequired;
352354
if (via == null) return false; // a column of @from: null in a spine row with no facts
353355
List<String> hops = reportingViaHops(via, memberOwner(dim, from), from, root);

‎server/python/src/metaobjects/meta/core/reporting/report_shape.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,11 @@ def _dimension_required(
232232
a primary-key column of the entity ``@of`` names. A dimension beyond the spine is
233233
reached by a LEFT OUTER join."""
234234
via = dim.via()
235-
# ADR-0039 resolving: the @of field's effective @required (the attr only; a
236-
# validator.required child does not count).
237-
of_required = of.get_meta_attr(FIELD_ATTR_REQUIRED) is True
235+
# ADR-0039 resolving: the @of field's effective required-ness, the @required attr OR a
236+
# validator.required child (children() is the effective set, own + inherited).
237+
of_required = of.get_meta_attr(FIELD_ATTR_REQUIRED) is True or any(
238+
c.type == "validator" and c.sub_type == "required" for c in of.children()
239+
)
238240
if spine is None:
239241
return via is None and of_required
240242
if via is None:

‎server/typescript/packages/codegen-ts/src/templates/filter-type.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import {
1111
FIELD_SUBTYPE_LONG,
1212
FIELD_SUBTYPE_DOUBLE,
1313
FIELD_SUBTYPE_FLOAT,
14+
FIELD_SUBTYPE_CURRENCY,
1415
FIELD_SUBTYPE_ENUM,
1516
opsForField,
1617
} from "@metaobjectsdev/metadata";
1718
import { isSortableField } from "./filter-shared.js";
1819
import { enumValues } from "../enum-meta.js";
1920

21+
// currency is integer minor units (allowlist `integer: true`, row schema `z.number().int()`).
2022
// VALUE-type classification only (distinct from the OPERATOR band, which comes from
2123
// opsForSubType). decimal is deliberately NOT here: its operator band stays NUMERIC
2224
// (eq/ne/gt/gte/lt/lte/in/isNull, see OPS_BY_SUBTYPE) but its VALUE type is `string`,
@@ -26,6 +28,7 @@ const NUMBER_VALUE_SUBTYPES = new Set<string>([
2628
FIELD_SUBTYPE_LONG,
2729
FIELD_SUBTYPE_DOUBLE,
2830
FIELD_SUBTYPE_FLOAT,
31+
FIELD_SUBTYPE_CURRENCY,
2932
]);
3033

3134
/**

‎server/typescript/packages/codegen-ts/test/templates/filter-type.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,28 @@ describe("renderFilterType", () => {
118118
// ...and the string-only `like` operator absent.
119119
expect(out).not.toMatch(/total\?:\s*string\s*\|\s*\{[^}]*like\?:/);
120120
});
121+
122+
// field.currency is integer minor units (the allowlist marks it integer, the row schema is
123+
// z.number().int()), so its filter operand is a number, not a string.
124+
test("currency filterable field has number value type", async () => {
125+
const meta = {
126+
"metadata.root": {
127+
package: "shop::money",
128+
children: [
129+
{
130+
"object.entity": {
131+
name: "Invoice",
132+
children: [{ "field.currency": { name: "revenue", "@filterable": true, "@currency": "USD" } }],
133+
},
134+
},
135+
],
136+
},
137+
};
138+
const { root } = await new MetaDataLoader().load([new InMemoryStringSource(JSON.stringify(meta))]);
139+
const entity = root.objects().find((c) => c.name === "Invoice")!;
140+
const out = renderFilterType(entity).toString();
141+
expect(out).toMatch(/revenue\?:\s*number\s*\|\s*\{/);
142+
expect(out).toMatch(/revenue\?:[\s\S]*?gt\?:\s*number/);
143+
expect(out).not.toMatch(/revenue\?:[\s\S]*?gt\?:\s*string/);
144+
});
121145
});

‎server/typescript/packages/metadata/src/core/reporting/report-shape.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { CHILD_REF_SEPARATOR, PACKAGE_SEPARATOR } from "../../shared/structural.
1212
import type { MetaObject } from "../object/meta-object.js";
1313
import type { MetaField } from "../field/meta-field.js";
1414
import {
15-
FIELD_ATTR_REQUIRED,
1615
FIELD_SUBTYPE_CURRENCY,
1716
FIELD_SUBTYPE_DATE,
1817
FIELD_SUBTYPE_DECIMAL,
@@ -238,7 +237,7 @@ function isTimeGrain(grain: string | undefined): grain is TimeGrain {
238237

239238
/**
240239
* Table C (`required` of a dimension). Without `@spine`: only a dimension with no `@via`
241-
* over an `@of` field whose effective `@required` is true. With `@spine`: only a dimension
240+
* over an `@of` field that is effectively required (`@required`, or a `validator.required` child). With `@spine`: only a dimension
242241
* whose `@via` hops equal the spine's (a column of the spine entity itself, whose rows are
243242
* the report's rows) over an `@of` field that is `@required` or a primary-key column of the
244243
* entity `@of` names. A dimension beyond the spine is reached by a LEFT OUTER join.
@@ -253,12 +252,12 @@ function dimensionRequired(
253252
reportName: string,
254253
): boolean {
255254
const via = dim.via();
256-
if (spine === undefined) return via === undefined && of.attr(FIELD_ATTR_REQUIRED) === true;
255+
if (spine === undefined) return via === undefined && of.isRequired;
257256
if (via === undefined) return false; // a column of @from: null in a spine row with no facts
258257
const hops = reportingViaHops(via, reportingMemberOwner(dim, from), from, root);
259258
if (hops === undefined) throw unresolved(reportName, `dimension '${dim.name}' @via '${via}'`);
260259
const onSpine = hops.length === spine.length && hops.every((h, i) => h === spine[i]);
261-
return onSpine && (of.attr(FIELD_ATTR_REQUIRED) === true || isPrimaryKeyField(named, of));
260+
return onSpine && (of.isRequired || isPrimaryKeyField(named, of));
262261
}
263262

264263
function dimensionField(

‎server/typescript/packages/metadata/test/report-shape.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,35 @@ describe("reportShape Table C (FR-044 @spine and measure @default)", () => {
329329
expect(required["catalogName"]).toBe(false); // Catalog.name: @required, beyond the spine
330330
});
331331

332+
test("a validator.required child counts as @required for a dimension (spine and plain)", async () => {
333+
// Same model, but Program.title and Purchase.minutes are required by a validator child.
334+
const viaValidator = (e: typeof program | typeof purchase, field: string) => {
335+
const copy = structuredClone(e);
336+
const kids = copy["object.entity"].children as Record<string, Record<string, unknown>>[];
337+
for (const k of kids) {
338+
for (const body of Object.values(k)) {
339+
if (body["name"] === field) {
340+
delete body["@required"];
341+
body["children"] = [{ "validator.required": {} }];
342+
}
343+
}
344+
}
345+
return copy;
346+
};
347+
const root = await loadInline([
348+
file("acme", [
349+
catalog,
350+
viaValidator(program, "title"),
351+
viaValidator(purchase, "minutes"),
352+
spineSales,
353+
plainSales,
354+
catalogSales,
355+
]),
356+
]);
357+
expect(requiredOf(root, "SpineSales")["programTitle"]).toBe(true);
358+
expect(requiredOf(root, "PlainSales")["minutes"]).toBe(true);
359+
});
360+
332361
test("a two-hop spine: a dimension whose @via equals the whole spine is on it", async () => {
333362
const required = requiredOf(await salesModel(), "CatalogSales");
334363
expect(required["catalogId"]).toBe(true);

0 commit comments

Comments
 (0)