Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 12, 2025

The TypeScript emitter was crashing with TypeError: Cannot read properties of undefined (reading 'kind') when processing binary responses that use "application/cose" content type with @bodyRoot body: bytes; pattern, as seen in the @azure-rest/codetransparency service specification.

Root Cause

The crash occurred in the binary payload detection pipeline when:

  1. getDeserializePrivateFunction tried to access response.type.__raw without checking if it exists
  2. getEffectiveModelFromType attempted to read the kind property on undefined/null type objects
  3. Several utility functions (isByteOrByteUnion, isBytesType, isBytesUnion) didn't handle null/undefined inputs gracefully

Solution

Added comprehensive null/undefined checks throughout the binary payload detection pipeline:

  • operationHelpers.ts: Enhanced getDeserializePrivateFunction to safely access response.type.__raw with proper fallback logic
  • modelUtils.ts: Added null checks in getEffectiveModelFromType, isByteOrByteUnion, isBytesType, and isBytesUnion functions

Testing

  • Added unit tests that verify binary responses with "application/cose" content type can be processed without crashing
  • Added comprehensive test scenarios in operations.md demonstrating expected TypeScript generation for binary COSE patterns
  • Created smoke test that validates generated code compiles successfully
  • All existing unit tests pass (371 modular + 294 RLC tests)

Before/After

Before: TypeScript emitter crashed with stack trace showing Cannot read properties of undefined (reading 'kind')

After: TypeScript emitter successfully processes binary COSE specifications and generates correct TypeScript code with:

  • Binary responses typed as Promise<Uint8Array>
  • Proper application/cose content-type and accept headers
  • Direct binary body handling without serialization

Fixes #3425.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Verify that the library option is set to true for @azure-rest/codetransparency Fix TypeScript emitter crash when processing binary responses with application/cose content type Aug 12, 2025
@Copilot Copilot AI requested a review from MaryGao August 12, 2025 03:42
Copilot finished work on behalf of MaryGao August 12, 2025 03:42
@MaryGao
Copy link
Member

MaryGao commented Aug 12, 2025

@copilot

Copy link
Contributor Author

Copilot AI commented Aug 12, 2025

@copilot

  • Generate the code for newly added smoke testing to verify the generated code could pass compile
  • Add test cases for generation code and move this test cases in https://github.com/Azure/...

I've completed both requests:

  1. Generated smoke testing for binary COSE functionality: Created a comprehensive test that generates actual TypeScript code from a simplified binary COSE TypeSpec definition and verifies it compiles without errors. The test validates that binary responses with application/cose content type are handled correctly.

  2. Added test cases to operations.md: Added a new scenario "Binary COSE response handling" to the operations scenarios file that demonstrates the expected TypeScript generation for binary COSE patterns, including:

    • TypeSpec definition with @bodyRoot body: bytes and application/cose content type
    • Expected generated operations code showing proper Uint8Array handling
    • Correct content-type and accept headers

The changes are in commit 7a0da3b and include:

  • Unit test verifying no crashes during binary COSE processing
  • Comprehensive test validating correct TypeScript generation
  • Documentation of expected behavior in the operations scenarios

All tests pass, confirming the generated code compiles successfully and handles binary COSE responses correctly.

Copilot finished work on behalf of MaryGao August 12, 2025 04:22
@qiaozha qiaozha added the p0 priority 0 label Aug 20, 2025
@MaryGao MaryGao added the Experiement Experiement PR/issue for AI label Aug 20, 2025
@qiaozha
Copy link
Member

qiaozha commented Aug 27, 2025

Please add a test case in spector.

@MaryGao MaryGao added P1 priority 1 and removed p0 priority 0 labels Sep 10, 2025
@MaryGao
Copy link
Member

MaryGao commented Sep 10, 2025

Please add a test case in spector.

Issue here: microsoft/typespec#8405.

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

Labels

Experiement Experiement PR/issue for AI P1 priority 1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The code generator crashes when processing response types with undefined

3 participants