Skip to content

fix(codegen): preserve asset_type discriminator on Individual*Asset slot types (#1498)#1514

Merged
bokelley merged 1 commit into
mainfrom
bokelley/issue-1498-asset-type-discriminator
May 3, 2026
Merged

fix(codegen): preserve asset_type discriminator on Individual*Asset slot types (#1498)#1514
bokelley merged 1 commit into
mainfrom
bokelley/issue-1498-asset-type-discriminator

Conversation

@bokelley

@bokelley bokelley commented May 3, 2026

Copy link
Copy Markdown
Contributor

Closes #1498.

Why

json-schema-to-typescript was flattening the schema's allOf:[baseIndividualAsset] + properties.asset_type.const discriminator on the 14 IndividualXAsset slot types, leaving them as bare BaseIndividualAsset aliases. Adopters writing TS-clean code constructing one of these without asset_type got runtime VALIDATION_ERROR against the wire schema with no compile-time signal — exactly the class of codegen mismatch we've burned down before (date vs date-time, Foo/Foo1 dedupe).

What

  • New applyIndividualAssetDiscriminators post-processor in scripts/generate-types.ts.
  • Rewrites all 14 Individual*Asset = BaseIndividualAsset aliases into discriminated intersections:
export type IndividualImageAsset = BaseIndividualAsset & {
  asset_type: 'image';
  requirements?: ImageAssetRequirements;
};
  • Applied to both core.generated.ts and tools.generated.ts. The requirements?: field is conditionally emitted — only when the per-type *AssetRequirements interface is declared in the same file (avoids TS2304 in tools.generated.ts where these aren't redeclared).
  • Regenerates core.generated.ts, tools.generated.ts, and the downstream schemas.generated.ts (Zod schemas auto-derived from TS types).

Compat

Strictly additive at runtime — wire validator already rejected these. The new TS errors catch a pre-existing bug at compile time. Adopters who construct IndividualImageAsset literals without asset_type will see TS errors that tell them what they were already getting at runtime.

Test plan

  • npm run format:check — clean
  • npm run typecheck — clean
  • npm run build:lib — clean
  • node --test test/examples/hello-creative-adapter-template.test.js — 3/3 pass (the gate most likely to use these types)
  • Verified discriminator catches missing asset_type via standalone TS test:
    src/lib/types/core.generated.ts: IndividualImageAsset literal without asset_type
    → TS2322: Property 'asset_type' is missing
    
  • Full unit-test suite: 5960 pass, 69 fail; baseline (pre-change) was 5883 pass / 66 fail / 2 cancelled. Failures are pre-existing (AgentClient context-management flakes, unrelated to codegen).

🤖 Generated with Claude Code

…lot types (closes #1498)

json-schema-to-typescript was flattening the schema's
allOf:[baseIndividualAsset] + properties.asset_type.const
discriminator on the 14 IndividualXAsset slot types, leaving them as
bare BaseIndividualAsset aliases. Adopters writing TS-clean code that
constructed one of these without asset_type got runtime
VALIDATION_ERROR against the wire schema with no compile-time signal.

Adds a codegen post-processor (applyIndividualAssetDiscriminators)
that rewrites the affected aliases into discriminated intersections:

  type IndividualImageAsset = BaseIndividualAsset & {
    asset_type: 'image';
    requirements?: ImageAssetRequirements;
  };

Same treatment for the other 13. The requirements field is omitted on
tools.generated.ts where the per-type *AssetRequirements interface
isn't redeclared.

No runtime change — the wire validator already rejected these. The
new TS error catches a strictly pre-existing bug at build time.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@bokelley bokelley merged commit 9800c60 into main May 3, 2026
10 checks passed
bokelley added a commit that referenced this pull request May 3, 2026
Pulls in 4 commits:
 - docs(skills): close three #1496 follow-up review items + seller adapter format slot sweep (#1515)
 - docs(migration): add recipe #10b for derived-mode account_id refusal (#1492) (#1512)
 - fix(codegen): preserve asset_type discriminator on Individual*Asset slot types (closes #1498) (#1514)
 - fix(examples): wire assertMediaBuyTransition into hello_seller_adapter_non_guaranteed (closes #1499) (#1513)

Conflict resolution:

- src/lib/types/core.generated.ts: kept the 3.0.6 generation header from
  HEAD; ran `npm run generate-types:all` against the local mirror to
  pull in main's #1514 codegen fix (asset_type discriminator preserved
  on Individual*Asset slots) layered on top of 3.0.6 schemas.
- test/examples/hello-seller-adapter-non-guaranteed.test.js: kept main's
  empty allowlist + new helper-shape conditional. Main's #1513 closes
  #1416 by actually wiring `assertMediaBuyTransition` into the
  non-guaranteed adapter — superseding the follow-up note my branch
  carried.

Verified post-merge:
- npm run build clean
- 6/6 hello-adapter tests for the modified files green
- npm run format:check clean

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(codegen): preserve asset_type discriminator on IndividualImageAsset / IndividualVideoAsset / IndividualHTMLAsset

1 participant