Repository navigation
Description
Summary
MetaObjects.Codegen's C# payload generator (stable name payload, "Per-template
strict typed payload record(s)") emits public required <Type> on every field of a
template.output-referenced object.value, regardless of whether the field carries
@required: true — a field with no @required attribute at all comes out identically
required to one that explicitly declares it. Per spec/metamodel/field.json, @required
is an optional boolean attr ("When true, the field is NOT NULL... ") that defaults to
absent/false, so the generated C# type should treat an unmarked field as optional
(nullable), not required.
Repro
Minimal object.value + template.output pair, one field explicitly @required: true,
one field with no @required attr at all, plus an unrelated field.decimal:
{
"metadata.root": {
"package": "test::repro",
"children": [
{
"object.value": {
"name": "ReproPayload",
"children": [
{ "field.string": { "name": "requiredField", "@required": true } },
{ "field.string": { "name": "optionalField" } },
{ "field.decimal": { "name": "decimalField" } }
]
}
},
{
"template.output": {
"name": "ReproOutput",
"@payloadRef": "ReproPayload",
"@textRef": "repro/output",
"@format": "text"
}
}
]
}
}Generated ReproPayload.payload.cs:
public sealed record ReproPayload
{
public required string requiredField { get; init; }
public required string optionalField { get; init; } // <- no @required in metadata, still `required`
public required double decimalField { get; init; }
}Expected (per field.json's stated default): optionalField and decimalField should
be string?/double? (or otherwise non-required), since neither carries @required: true.
A second, separate observation from the same repro: field.decimal emits C# double,
not decimal — worth confirming whether that's intentional for payload records
specifically (vs. the entity generator, which maps field.decimal to decimal), since
if so it's an undocumented cross-generator inconsistency worth a callout in the C#
reference docs.
Real-world impact
Found while trying to adopt the payload generator for an LLM-output payload bound to a
template.output that parses a model's classification response. The hand-written record
being replaced deliberately leaves several fields nullable, because the model does not
always populate them — none of those fields carry @required in the metadata. Adopting
the generator as-is would make <Template>Parser.TryParse reject any real response
omitting one of those fields, a correctness regression relative to the hand-written type
it would replace. Reverted before this reached committed code (caught in a
scratch/isolated test), but it blocks adoption for this and presumably any other
template.output payload with a legitimately optional field.
Environment
MetaObjects/MetaObjects.Render/MetaObjects.Codegen0.23.2 (NuGet)- .NET 8
Activity
fix(codegen): payload optionality comes from @required, in every port (…
dmealing commentedon Aug 19, 2026
Fixed on
mainin718ce6fe4— C# and Kotlin.The rule, now uniform across all five ports: a payload field is optional unless it declares
@required.spec/metamodel/field.jsontypes the attr as an optional boolean defaulting to absent, so absent ⇒ optional — exactly as you argued. The decision is one cross-language contract; the rendering stays idiomatic: C#T?, KotlinT? = null, TypeScriptname?: T, PythonT | None = None.Your repro now emits:
Scope: only C# and Kotlin changed. TypeScript and Python already read
@required. Java needed no change —SpringTypeMapperalready emits boxedInteger/Long/Boolean, so every record component is nullable by construction and absent ⇒ optional already held there.The sharpest part of your report was the one you didn't make explicitly: C# was contradicting itself. Its own FR-010 extractor derives per-field required-ness from
@requiredand classifies an absent optional as a benignLOST_OPTIONAL— andExtractorGenerator.csdocuments in prose that PayloadCodegen "does not honor @required". One port, two tiers, opposite predicates. That is whyTryParserejected responses the extract tier would have accepted.Why it survived four releases: the tests pinned it. C#'s payload suite asserted
public requiredon fixtures where no field carried@required, so it could never distinguish "reads the attr" from "hardcodes it" — it would have failed under a correct implementation. A demo test asserted that omitting an unmarked field "fails to compile" while its comment called them "required members". Both now declare what they assert, and the suite gained the arm nobody had: omitting an optional member must compile.Every flipped assertion was checked against its own fixture rather than regenerated, which caught a real error mid-fix — some fields in the shared conformance corpus genuinely are
@required: true, so a blanket flip would have swapped one wrong golden for another. That corpus turned out to carry both arms already, meaning the ports had been asserting contradictory output for one shared model; it now gates optionality as well as collision naming.On your second observation —
field.decimalemittingdouble: confirmed as a real cross-generator inconsistency (the entity generator maps it todecimal), and deliberately not bundled into this fix. It is a wire-precision question rather than an optionality one, and changing a payload's numeric type is a different kind of adopter-visible change from changing its nullability. Please open it separately if it blocks you — happy to take it, and your instinct that it is at minimum a documentation gap is right either way.Thanks for the precise repro and for stating the real-world impact: "reverted before this reached committed code" plus the named fields that are legitimately absent is what made the case unambiguous.
fix(codegen-csharp): a payload's field.decimal is `decimal`, not `dou…
docs(adr): ADR-0052 — a template subtype's axis is DIRECTION