Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fine-phones-shine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@sap-cloud-sdk/openapi": patch
---

[Fixed Issue] Wrap `Buffer` responses in `Blob` when executing OpenAPI requests that return binary data.

77 changes: 77 additions & 0 deletions packages/openapi/src/openapi-request-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,83 @@ describe('openapi-request-builder', () => {
expect((entryMap.get('fileField') as Blob).type).toBe('application/pdf');
});

it('wraps Buffer response in Blob when execute() returns binary data', async () => {
const buffer = Buffer.from('binary content');
httpSpy.mockResolvedValueOnce({
data: buffer,
status: 200,
headers: {},
config: {} as any,
statusText: 'OK',
request: {}
} as any);
const result = await new OpenApiRequestBuilder<Blob>(
'get',
'/binary'
).execute(destination);
expect(result).toBeInstanceOf(Blob);
expect(await result.arrayBuffer()).toEqual(
buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength
)
);
});

it('propagates response Content-Type to Blob when wrapping Buffer', async () => {
const buffer = Buffer.from('binary content');
httpSpy.mockResolvedValueOnce({
data: buffer,
status: 200,
headers: { 'content-type': 'application/pdf' },
config: {} as any,
statusText: 'OK',
request: {}
} as any);
const result = await new OpenApiRequestBuilder<Blob>(
'get',
'/binary'
).execute(destination);
expect(result).toBeInstanceOf(Blob);
expect(result.type).toBe('application/pdf');
});

it('propagates response Content-Type case-insensitively when wrapping Buffer', async () => {
const buffer = Buffer.from('binary content');
httpSpy.mockResolvedValueOnce({
data: buffer,
status: 200,
headers: { 'Content-Type': 'image/png' },
config: {} as any,
statusText: 'OK',
request: {}
} as any);
const result = await new OpenApiRequestBuilder<Blob>(
'get',
'/binary'
).execute(destination);
expect(result).toBeInstanceOf(Blob);
expect(result.type).toBe('image/png');
});

it('omits Blob type when response has no Content-Type header', async () => {
const buffer = Buffer.from('binary content');
httpSpy.mockResolvedValueOnce({
data: buffer,
status: 200,
headers: {},
config: {} as any,
statusText: 'OK',
request: {}
} as any);
const result = await new OpenApiRequestBuilder<Blob>(
'get',
'/binary'
).execute(destination);
expect(result).toBeInstanceOf(Blob);
expect(result.type).toBe('');
});

describe('requestConfig', () => {
it('should overwrite default request config with filtered custom request config', async () => {
const requestBuilder = new OpenApiRequestBuilder('get', '/test');
Expand Down
10 changes: 10 additions & 0 deletions packages/openapi/src/openapi-request-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ export class OpenApiRequestBuilder<ResponseT = any> {
): Promise<ResponseT> {
const response = await this.executeRaw(destination);
if (isAxiosResponse(response)) {
if (Buffer.isBuffer(response.data)) {
const contentType = pickValueIgnoreCase(
response.headers,
'content-type'
);
return new Blob(
[response.data],
contentType && { type: contentType }
) as ResponseT;
}
return response.data;
}
throw new Error(
Expand Down
Loading