Skip to content

feat: handle binary responses with proper byte handling#29

Merged
santiagomed merged 1 commit intomainfrom
smedinarolong/binary-response-support
Mar 12, 2026
Merged

feat: handle binary responses with proper byte handling#29
santiagomed merged 1 commit intomainfrom
smedinarolong/binary-response-support

Conversation

@santiagomed
Copy link
Contributor

@santiagomed santiagomed commented Mar 12, 2026

Summary

Previously, binary responses (e.g., application/octet-stream, images, audio, video) were processed incorrectly:

  • TypeScript: response.text() corrupts bytes > 127 due to UTF-8 encoding
  • Python: response.json() fails on binary data

Changes

Now both SDKs detect binary content types and handle them properly:

SDK Method Return Type
TypeScript response.arrayBuffer() ArrayBuffer
Python response.content bytes

Supported Binary Content Types

  • application/octet-stream
  • image/*
  • audio/*
  • video/*

Usage

TypeScript

const data = await client.someEndpoint.download({ id: '123' });
const bytes = new Uint8Array(data); // Convert to Uint8Array if needed

Python

data = client.some_endpoint.download(id='123')
# data is already bytes, use directly or save to file
with open('output.bin', 'wb') as f:
    f.write(data)

Testing

  • Regenerated both SDKs and verified binary handling code is present
  • TypeScript: cargo run -- typescript --latest true
  • Python: cargo run -- python --latest true

Files Changed

  • xdk-gen/templates/typescript/main_client.j2 - Added ArrayBuffer handling
  • xdk-gen/templates/python/client_macros.j2 - Added bytes handling

@santiagomed santiagomed force-pushed the smedinarolong/binary-response-support branch from dd580e3 to 6333160 Compare March 12, 2026 00:22
@santiagomed santiagomed changed the title feat(typescript): handle binary responses with ArrayBuffer feat: handle binary responses with proper byte handling Mar 12, 2026
@santiagomed santiagomed force-pushed the smedinarolong/binary-response-support branch 2 times, most recently from e4b174f to a496b9a Compare March 12, 2026 01:04
Detect binary response types directly from the OpenAPI responses field
in templates (no extra Rust field needed).

Template changes:
- TypeScript: Check for 'application/octet-stream' in responses content,
  pass 'binary: true' to request(), return ArrayBuffer
- Python: Check for 'application/octet-stream' in responses content,
  return response.content as bytes

The binary check is derived at template render time from:
  operation.responses['200'].content['application/octet-stream']

Supported binary content types:
- application/octet-stream

This provides:
- Better type safety (ArrayBuffer/bytes return types)
- No runtime content-type parsing overhead
- Correct IDE autocomplete and type inference
- No redundant data in the operation model
@santiagomed santiagomed force-pushed the smedinarolong/binary-response-support branch from a496b9a to ea429d0 Compare March 12, 2026 01:13
@santiagomed santiagomed merged commit db99bee into main Mar 12, 2026
4 checks passed
@santiagomed santiagomed deleted the smedinarolong/binary-response-support branch March 12, 2026 02:00
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.

2 participants