Skip to content

Commit 27419f4

Browse files
committed
feat(kotlin): read-only Spring controller for a view-backed report (FR-044)
A served object.report (read source @kind: view) now gets its row data class, filter allowlist and a keyless read-only Spring controller, beside the Exposed table it already had. Each generator reaches the report through RestSurfaceGate.restShapeOf, replacing the interim skips, and the table's served check is RestSurfaceGate.isServedReport. A report that is not served generates nothing. - The row's enum property is typed by the enum of the entity the item reads, the class the table types the column by (KotlinGenUtil.reportEnumClasses is the one answer for the table, the row and the controller). - The row carries no builder and no validation annotation: it is read from the view, never bound. - Api docs document a served report as a unit of kind "report": its row, its table, GET <path> and its allowlist. - New generated lane over the shared report/ api-contract corpus (12 scenarios). Two controller defects fixed, both in emitters shared with entities and projections, both byte-neutral for a model that does not hit them: - A filter on a field.decimal or field.float column threw ClassCastException (a 500): the value was coerced to a Double and then cast to the column's BigDecimal or Float. Each now has its own coercer, emitted only when such a column exists. - The row mapper and the filter dispatch named a column by its field name, but the table declares a field named after a member of Exposed's Table under a Column suffix (source is sourceColumn), so that controller did not compile. Both now use KotlinNaming.safeColumnProperty, as the sort dispatch did.
1 parent 8673606 commit 27419f4

13 files changed

Lines changed: 954 additions & 94 deletions

File tree

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinEntityGenerator.kt‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import java.io.PrintWriter
4949
import java.nio.file.Path
5050
import java.nio.file.Paths
5151
import com.metaobjects.generator.util.GeneratedFileWriter
52+
import com.metaobjects.generator.util.RestSurfaceGate
53+
import com.metaobjects.reporting.ReportReadModel
5254

5355
/**
5456
* Generator: one plain Kotlin data class (Jackson-compatible) per `object.entity` and
@@ -99,8 +101,11 @@ open class KotlinEntityGenerator : MultiFileDirectGeneratorBase<MetaObject>() {
99101
// typed property to resolve.
100102
// Local-only abstract check (own attribute, not inherited) so concrete subtypes
101103
// extending an abstract base still emit normally.
102-
for (obj in loader.metaObjects) {
103-
if (obj.subType !in EMITTED_SUBTYPES) continue
104+
for (declared in loader.metaObjects) {
105+
// FR-044: a served report is emitted from its read model (one field per derived
106+
// column); any other report maps to null and emits nothing.
107+
val obj = RestSurfaceGate.restShapeOf(declared) ?: continue
108+
if (!emitsDataClass(obj)) continue
104109
if (KotlinGenUtil.isAbstractEntity(obj)) {
105110
if (emitAbstractShapes) emitAbstractShape(obj, outRoot, loader, emittedEnumFqns)
106111
continue
@@ -130,6 +135,8 @@ open class KotlinEntityGenerator : MultiFileDirectGeneratorBase<MetaObject>() {
130135
private fun emitNetJsonSupport(loader: MetaDataLoader, outRoot: Path) {
131136
val packages = linkedSetOf<String>()
132137
for (obj in loader.metaObjects) {
138+
// A report's row carries no deserializer (it is never bound from a request), so
139+
// a report is not counted here.
133140
if (obj.subType !in EMITTED_SUBTYPES) continue
134141
if (KotlinGenUtil.isAbstractEntity(obj)) continue
135142
if (KotlinTphPlan.isTphSubtype(obj)) continue
@@ -151,9 +158,41 @@ open class KotlinEntityGenerator : MultiFileDirectGeneratorBase<MetaObject>() {
151158
}
152159

153160
protected open fun emit(obj: MetaObject, outRoot: Path, loader: MetaDataLoader, emittedEnumFqns: MutableSet<String>) {
161+
// FR-044: a report's row gets no enum of its own. A derived enum field is typed by
162+
// the class already emitted for the entity it reads from, the same class the
163+
// report's Exposed table types the column by, so the generated row mapper compiles.
164+
// Empty for every other object.
165+
reportEnumClasses = KotlinGenUtil.reportEnumClasses(obj, loader)
166+
try {
167+
emitDataClass(obj, outRoot, loader, emittedEnumFqns)
168+
} finally {
169+
reportEnumClasses = emptyMap()
170+
}
171+
}
172+
173+
/**
174+
* The enum classes of the report row being emitted, by derived field name, for the
175+
* duration of one [emit] call; empty outside it and for any object that is not a report.
176+
* It reaches [resolveElementType] this way, not as a parameter, because that function is
177+
* `protected open` and an adopter's subclass may override it.
178+
*/
179+
private var reportEnumClasses: Map<String, ClassName> = emptyMap()
180+
181+
/**
182+
* Whether [obj] gets a data class: an entity, value object or projection, and the read
183+
* model of a served report (FR-044), whose row a query returns. A declared report node
184+
* never reaches here: [execute] maps it to its read model or drops it.
185+
*/
186+
private fun emitsDataClass(obj: MetaObject): Boolean =
187+
obj.subType in EMITTED_SUBTYPES || obj is ReportReadModel
188+
189+
private fun emitDataClass(obj: MetaObject, outRoot: Path, loader: MetaDataLoader, emittedEnumFqns: MutableSet<String>) {
190+
val reportRow = obj is ReportReadModel
154191
// Emit one Kotlin enum class file per `field.enum` child BEFORE the data class
155192
// so the resolved property type (a ClassName) points at a real file. Deduped per run.
156193
for (field in obj.metaFields) {
194+
// A report's enum field references an entity's class; nothing is materialized.
195+
if (reportRow) break
157196
// FR-019: a @provided shared enum is referenced externally, never materialized — skip it.
158197
if (field is EnumField && !Fr019SharedEnum.isProvidedEnumField(field))
159198
KotlinEnumEmitter.emitEnumFile(obj, field, outRoot, emittedEnumFqns)
@@ -228,7 +267,10 @@ open class KotlinEntityGenerator : MultiFileDirectGeneratorBase<MetaObject>() {
228267
.build()
229268
ctorBuilder.addParameter(param)
230269
val propBuilder = PropertySpec.builder(propName, propType).initializer(propName)
231-
if (!tphBase && !derivedReadOnly && !serverOwned) {
270+
// FR-044: a report's row carries no constraint either. Nothing binds or validates
271+
// it (it is read from the view, never sent), and a `@Size(min = 1)` on a required
272+
// dimension would claim a rule the view does not have: an empty string is a group.
273+
if (!tphBase && !derivedReadOnly && !serverOwned && !reportRow) {
232274
for (annotation in validationAnnotations(field)) {
233275
propBuilder.addAnnotation(annotation)
234276
}
@@ -387,6 +429,8 @@ open class KotlinEntityGenerator : MultiFileDirectGeneratorBase<MetaObject>() {
387429
// FR-019: a @provided shared enum is referenced at its configured external namespace
388430
// (<ns>.E) instead of the materialized in-package class; an unresolved namespace throws.
389431
Fr019SharedEnum.providedClassName(field, fr019Config)?.let { return it }
432+
// FR-044: a report's derived enum field → the class of the entity field it reads.
433+
if (owner is ReportReadModel) reportEnumClasses[field.name]?.let { return it }
390434
// field.enum → typed enum class generated alongside this entity.
391435
KotlinTypeMapper.enumTypeName(field, owner)?.let { return it }
392436
if (field is ObjectField) {
@@ -787,6 +831,8 @@ open class KotlinEntityGenerator : MultiFileDirectGeneratorBase<MetaObject>() {
787831
// into a type KotlinProjectionCompileTest requires to be immutable, which is how this
788832
// exclusion was found rather than reasoned about.
789833
if (obj.subType == MetaObject.SUBTYPE_PROJECTION) return
834+
// Nor on a report's row (FR-044), for the same reason: it arrives from the view.
835+
if (obj is ReportReadModel) return
790836

791837
val builderClass = ClassName(className.packageName, className.simpleName, "Builder")
792838
val builder = TypeSpec.classBuilder("Builder")

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinExposedTableGenerator.kt‎

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import java.nio.file.Path
4444
import java.nio.file.Paths
4545
import org.slf4j.LoggerFactory
4646
import com.metaobjects.generator.util.GeneratedFileWriter
47+
import com.metaobjects.generator.util.RestSurfaceGate
4748

4849
/**
4950
* Generator: one Exposed Table `object` per `object.entity` that has a `source.rdb` child.
@@ -367,12 +368,13 @@ open class KotlinExposedTableGenerator : MultiFileDirectGeneratorBase<MetaObject
367368
* - it binds its view and columns by LITERAL even when the names generator is in the
368369
* run ([bindsThroughNames]): [KotlinNamesGenerator] emits nothing for a report;
369370
* - an enum column references the enum class of the entity the dimension reads
370-
* ([reportEnumClass]): no generator emits a per-report enum;
371+
* ([KotlinGenUtil.reportEnumClasses]): no generator emits a per-report enum;
371372
* - a derived decimal with no declared precision reads at [REPORT_DECIMAL_PRECISION] /
372373
* [REPORT_DECIMAL_SCALE] ([scalarColumnSpec]).
373374
*
374375
* A report has no identity, so the table has no `primaryKey`, and no index or reference.
375-
* Every other Kotlin generator skips reports.
376+
* The data class, filter allowlist and read-only controller of the same report come from
377+
* their own generators, each through `RestSurfaceGate.restShapeOf` (FR-044 Plan 3).
376378
*/
377379
private fun emitReport(
378380
report: MetaObject,
@@ -383,10 +385,11 @@ open class KotlinExposedTableGenerator : MultiFileDirectGeneratorBase<MetaObject
383385
packagesNeedingJacksonMapper: MutableSet<String>,
384386
packagesNeedingUuidStringHelper: MutableSet<String>,
385387
) {
386-
if (KotlinGenUtil.isAbstractEntity(report)) return
388+
// Table A, from the one JVM predicate every REST-surface generator asks: a report
389+
// is served only when it is concrete and its read source is `@kind: view`.
390+
if (!RestSurfaceGate.isServedReport(report)) return
387391
// The source the report is READ from, by the rule that names the lowered view.
388392
val source = ReportShape.readSource(report) as? RdbSource ?: return
389-
if (source.effectiveKind != MetaSource.KIND_VIEW) return
390393

391394
// Table B, derived ONCE for the report; everything below reads this one shape.
392395
val shape = ReportShape.of(report, loader.root)
@@ -395,8 +398,7 @@ open class KotlinExposedTableGenerator : MultiFileDirectGeneratorBase<MetaObject
395398
val model = ReportReadModel.of(report)
396399
val pkg = PackageMapping.splitFqn(report.name).first
397400
reportPlans[model] = ReportTablePlan(
398-
enumClasses = shape.fields().filter { it.typeSource is EnumField }
399-
.associate { it.name to reportEnumClass(shape, it) },
401+
enumClasses = KotlinGenUtil.reportEnumClasses(shape),
400402
unsizedDecimals = shape.fields()
401403
.filter { it.typeSource == null && it.subType == DecimalField.SUBTYPE_DECIMAL }
402404
.mapTo(HashSet()) { it.name },
@@ -497,41 +499,10 @@ open class KotlinExposedTableGenerator : MultiFileDirectGeneratorBase<MetaObject
497499
*/
498500
private fun bindsThroughNames(entity: MetaObject): Boolean = useNames() && entity !is ReportReadModel
499501

500-
/**
501-
* The generated enum class a report's derived enum field [f] is typed by. A report gets
502-
* no entity class and so no enum of its own; its enum column carries the values of the
503-
* field the dimension (or min/max measure) reads, and is typed by THAT field's class:
504-
* the one [KotlinEntityGenerator] emits for the entity the item reads from. Without
505-
* `@via` that is the report's `@from` entity (which is how a field `@from` inherits from
506-
* an abstract base still names a class that exists); with `@via` it is the entity the
507-
* `@of` reference names. [ReportShape.ofEntity] answers both, by the rule that derived
508-
* the field (a bare name resolves in the package of the entity DECLARING the dimension).
509-
*
510-
* @throws GeneratorException naming the report and the dimension when that entity does
511-
* not resolve. The shape resolved the same reference to derive [f], so a loaded model
512-
* cannot reach this; it guards a tree built in code, where typing the column by a
513-
* guessed class would compile against the wrong enum.
514-
*/
515-
private fun reportEnumClass(shape: ReportShape, f: ReportShape.Field): ClassName {
516-
val report = shape.report()
517-
// The entity the field is read from, by the rule that derived the field: nothing
518-
// about packages or @via is restated here.
519-
val owner = shape.ofEntity(f)
520-
?: throw GeneratorException(
521-
"report \"${report.shortName}\": its dimension \"${f.dimension?.shortName}\" reads the enum " +
522-
"\"${f.dimension?.of}\" through @via, and the entity that reference names does not " +
523-
"resolve, so the generated column has no enum class to be typed by."
524-
)
525-
return KotlinTypeMapper.enumTypeName(f.typeSource, owner)
526-
?: throw GeneratorException(
527-
"report \"${report.shortName}\": its ${describeItem(f)} is an enum with no generated enum class."
528-
)
529-
}
530-
531502
/**
532503
* The generated enum class a `field.enum` column of [entity]'s table is typed by:
533504
* [KotlinTypeMapper.enumTypeName] for an entity or projection — the class
534-
* [KotlinEntityGenerator] emits for it — and [reportEnumClass] for a report.
505+
* [KotlinEntityGenerator] emits for it — and [KotlinGenUtil.reportEnumClasses] for a report.
535506
*/
536507
private fun enumClassFor(field: MetaField<*>, entity: MetaObject): ClassName? =
537508
reportPlans[entity]?.enumClasses?.get(field.name) ?: KotlinTypeMapper.enumTypeName(field, entity)

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinFilterAllowlistGenerator.kt‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ open class KotlinFilterAllowlistGenerator : MultiFileDirectGeneratorBase<MetaObj
6363
override fun execute(loader: MetaDataLoader) {
6464
parseArgs()
6565
val outRoot = Paths.get(outDir.absolutePath)
66-
for (entity in loader.metaObjects) {
67-
// FR-044 Plan 3, interim: RestSurfaceGate now admits a served object.report (the Java
68-
// port emits its read-only surface from RestSurfaceGate.restShapeOf). This generator
69-
// does not serve a report yet, and the declared node has no fields to emit from, so a
70-
// report is skipped here until the Kotlin port switches this loop to restShapeOf.
71-
if (com.metaobjects.generator.util.GeneratorUtil.isReport(entity)) continue
66+
for (declared in loader.metaObjects) {
67+
// FR-044: a served report filters on its DERIVED fields, so its allowlist is built
68+
// from its read model (which marks every banded derived field @filterable). Any
69+
// other report maps to null and gets no allowlist; a non-report is itself.
70+
val entity = RestSurfaceGate.restShapeOf(declared) ?: continue
7271
// FR-017 TPH: a discriminator subtype folds into its base — the base's allowlist
7372
// (unioned across subtype columns via isTphBase) is the only one the polymorphic
7473
// controller uses; a per-subtype allowlist is dead. Mirror the controller/table skip.

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinGenUtil.kt‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import com.metaobjects.loader.MetaDataLoader
1717
import com.metaobjects.`object`.MetaObject
1818
import com.metaobjects.origin.AggregateOrigin
1919
import com.metaobjects.origin.MetaOrigin
20+
import com.metaobjects.reporting.ReportReadModel
21+
import com.metaobjects.reporting.ReportShape
2022
import com.metaobjects.source.MetaSource
2123
import com.metaobjects.source.RdbSource
2224
import com.metaobjects.source.SourceResolution
@@ -658,6 +660,54 @@ public object KotlinGenUtil {
658660
fun isIntBackedEnum(field: MetaField<*>): Boolean =
659661
field is com.metaobjects.field.EnumField && field.hasMetaAttr(com.metaobjects.field.EnumField.ATTR_INT_VALUE_MAP)
660662

663+
/**
664+
* FR-044: the generated enum class each derived enum field of a served report is typed
665+
* by, keyed by derived field name. A report gets no enum of its own: its enum field
666+
* carries the values of the field the dimension (or min/max measure) reads, and is typed
667+
* by THAT field's class, the one [KotlinEntityGenerator] emits for the entity the item
668+
* reads from. Without `@via` that is the report's `@from` entity (which is how a field
669+
* `@from` inherits from an abstract base still names a class that exists); with `@via` it
670+
* is the entity the `@of` reference names. [ReportShape.ofEntity] answers both, by the
671+
* rule that derived the field.
672+
*
673+
* The ONE answer for every generator that names the class: the Exposed table types the
674+
* column by it, the data class types the property by it, and the controller's int-backed
675+
* filter arm resolves a member through it. Three answers would be a row mapper that does
676+
* not compile.
677+
*
678+
* @throws GeneratorException naming the report and the item when the entity does not
679+
* resolve. The shape resolved the same reference to derive the field, so a loaded model
680+
* cannot reach this; it guards a tree built in code, where typing the column by a
681+
* guessed class would compile against the wrong enum.
682+
*/
683+
fun reportEnumClasses(shape: ReportShape): Map<String, com.squareup.kotlinpoet.ClassName> {
684+
val report = shape.report()
685+
val out = LinkedHashMap<String, com.squareup.kotlinpoet.ClassName>()
686+
for (f in shape.fields()) {
687+
if (f.typeSource !is com.metaobjects.field.EnumField) continue
688+
val item = "${f.role.wireName()} \"${f.name}\""
689+
// The entity the field is read from, by the rule that derived the field: nothing
690+
// about packages or @via is restated here.
691+
val owner = shape.ofEntity(f)
692+
?: throw GeneratorException(
693+
"report \"${report.shortName}\": its dimension \"${f.dimension?.shortName}\" reads the enum " +
694+
"\"${f.dimension?.of}\" through @via, and the entity that reference names does not " +
695+
"resolve, so the generated column has no enum class to be typed by."
696+
)
697+
out[f.name] = KotlinTypeMapper.enumTypeName(f.typeSource, owner)
698+
?: throw GeneratorException(
699+
"report \"${report.shortName}\": its $item is an enum with no generated enum class."
700+
)
701+
}
702+
return out
703+
}
704+
705+
/** [reportEnumClasses] for a report's read model; empty for any other object. */
706+
fun reportEnumClasses(obj: MetaObject, loader: MetaDataLoader): Map<String, com.squareup.kotlinpoet.ClassName> {
707+
val report = (obj as? ReportReadModel)?.report() ?: return emptyMap()
708+
return reportEnumClasses(ReportShape.of(report, loader.root))
709+
}
710+
661711
/**
662712
* Whether the generated data-class property for [field] is nullable (with a `null` default),
663713
* outside the TPH-base and write-through relaxations [KotlinEntityGenerator] layers on top.

‎server/java/codegen-kotlin/src/main/kotlin/com/metaobjects/generator/kotlin/KotlinNaming.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ object KotlinNaming {
8484
return if (reserved) name + "Column" else name
8585
}
8686

87+
/**
88+
* [KotlinSpringControllerGenerator] and the api docs: what a read-only object is called in
89+
* generated prose, `"report"` for an `object.report` (the declared node or its read model)
90+
* and `"projection"` otherwise. Prose only: no contract asserts it.
91+
*/
92+
fun readOnlyNoun(obj: com.metaobjects.`object`.MetaObject): String =
93+
if (obj.subType == com.metaobjects.`object`.MetaObject.SUBTYPE_REPORT) "report" else "projection"
94+
8795
/** [KotlinSpringControllerGenerator]: `shortName + "Controller"`. */
8896
fun controllerName(shortName: String): String = shortName + "Controller"
8997

0 commit comments

Comments
 (0)