Skip to content

fix: handle undefined functionTypeMapping in all protocol generators#356

Merged
jonaslagoni merged 1 commit intomainfrom
fix_anonym
Apr 13, 2026
Merged

fix: handle undefined functionTypeMapping in all protocol generators#356
jonaslagoni merged 1 commit intomainfrom
fix_anonym

Conversation

@jonaslagoni
Copy link
Copy Markdown
Contributor

@jonaslagoni jonaslagoni commented Apr 13, 2026

Summary

Fix a crash bug where all protocol generators (NATS, Kafka, MQTT, AMQP, WebSocket, EventSource, HTTP) throw Cannot read properties of undefined (reading 'channel_id') when generator.functionTypeMapping is undefined instead of the expected empty object {}.

Type of Change

  • New feature (adds functionality)
  • Bug fix (fixes an issue)
  • Breaking change (changes existing functionality)
  • Documentation update
  • Performance improvement
  • Refactoring (no functional changes)
  • Test improvements

Changes Made

New Files

  • test/codegen/generators/typescript/channels/protocols/functionTypeMapping.spec.ts - Unit tests verifying all 7 protocol generators handle undefined functionTypeMapping gracefully

Modified Files

  • src/codegen/generators/typescript/channels/protocols/nats/index.ts - Added optional chaining at lines 115, 328
  • src/codegen/generators/typescript/channels/protocols/kafka/index.ts - Added optional chaining at lines 100, 185
  • src/codegen/generators/typescript/channels/protocols/mqtt/index.ts - Added optional chaining at lines 98, 160
  • src/codegen/generators/typescript/channels/protocols/amqp/index.ts - Added optional chaining at lines 99, 195
  • src/codegen/generators/typescript/channels/protocols/websocket/index.ts - Added optional chaining at lines 109, 211
  • src/codegen/generators/typescript/channels/protocols/eventsource/index.ts - Added optional chaining at lines 100, 191
  • src/codegen/generators/typescript/channels/protocols/http/index.ts - Added optional chaining at line 113

Key Implementation Details

Root Cause

The bug occurs because:

  1. The Zod schema defines functionTypeMapping with .optional().default({})
  2. This default is applied during Zod parsing
  3. However, when functionTypeMapping: undefined is explicitly set (not missing), spread operators preserve undefined
  4. All protocol generators access generator.functionTypeMapping[channel.id()] without checking if functionTypeMapping exists

Fix

Added optional chaining (?.) at all 13 access points across 7 protocol generators:

// Before (crashes)
const functionTypeMapping = generator.functionTypeMapping[channel.id()];

// After (safe)
const functionTypeMapping = generator.functionTypeMapping?.[channel.id()];

Testing

Unit Tests (TDD)

✅ Tests written BEFORE implementation and verified to FAIL:

TypeError: Cannot read properties of undefined (reading 'user/signedup')

✅ Tests PASS after implementation:

PASS test/codegen/generators/typescript/channels/protocols/functionTypeMapping.spec.ts
  functionTypeMapping undefined bug
    should handle undefined functionTypeMapping without crashing
      ✓ NATS generator (6 ms)
      ✓ Kafka generator (1 ms)
      ✓ MQTT generator (1 ms)
      ✓ AMQP generator
      ✓ WebSocket generator
      ✓ EventSource generator
      ✓ HTTP client generator (7 ms)
      ✓ all protocols together (1 ms)

Test Suites: 1 passed, 1 total
Tests:       8 passed, 8 total

Full Test Suite

✅ All unit tests pass: npm run test

Build

✅ Build succeeds: npm run build

Lint

✅ Lint passes: npm run lint

Runtime Generate

✅ Runtime code generates successfully: npm run runtime:typescript:generate

Breaking Changes

None - This is a purely additive defensive fix using optional chaining. Existing behavior is unchanged for valid configurations.

Checklist

  • Code follows project conventions
  • All tests pass (unit, blackbox, runtime)
  • No TypeScript errors
  • Lint passes
  • Build succeeds
  • Follows existing patterns
  • Tests verify the bug existed and is now fixed (TDD)

Verification

After implementation, verified no unguarded access remains:

# Should return 0 results (no unguarded access)
rg "generator\.functionTypeMapping\[" src/codegen/generators/typescript/channels/protocols/
# Result: No matches found ✓

# Should show 13 guarded accesses
rg "generator\.functionTypeMapping\?\.\[" src/codegen/generators/typescript/channels/protocols/
# Result: 13 matches ✓

🤖 Generated with Claude Code


Note

Low Risk
Low risk: small defensive change (optional chaining) in TypeScript channel codegen; main risk is minor behavior change in how default function-type filtering is applied when the mapping is missing/undefined.

Overview
Prevents crashes in the TypeScript channels codegen when generator.functionTypeMapping is explicitly undefined by switching all protocol generators (AMQP, EventSource, HTTP client, Kafka, MQTT, NATS, WebSocket) to use optional-chained lookups (generator.functionTypeMapping?.[channel.id()]).

Adds a Jest regression test that runs generateTypeScriptChannels across each protocol (and a combined run) with functionTypeMapping forced to undefined, asserting generation completes without throwing.

Reviewed by Cursor Bugbot for commit 7984353. Bugbot is set up for automated code reviews on this repo. Configure here.

@jonaslagoni jonaslagoni requested a review from ALagoni97 as a code owner April 13, 2026 19:40
@netlify
Copy link
Copy Markdown

netlify bot commented Apr 13, 2026

Deploy Preview for the-codegen-project canceled.

Name Link
🔨 Latest commit 7984353
🔍 Latest deploy log https://app.netlify.com/projects/the-codegen-project/deploys/69dd46930574cb0008acab04

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
the-codegen-project Ready Ready Preview, Comment Apr 13, 2026 7:41pm
the-codegen-project-mcp Ready Ready Preview, Comment Apr 13, 2026 7:41pm

@jonaslagoni jonaslagoni changed the title fix(protocols): handle undefined functionTypeMapping in all protocol generators fix: handle undefined functionTypeMapping in all protocol generators Apr 13, 2026
@jonaslagoni jonaslagoni merged commit a97ecc7 into main Apr 13, 2026
24 checks passed
@jonaslagoni jonaslagoni deleted the fix_anonym branch April 13, 2026 19:47
@jonaslagoni
Copy link
Copy Markdown
Contributor Author

🎉 This PR is included in version 0.70.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant