Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/ripe-papers-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-cloud-sdk/openapi': minor
---

[New Functionality] For `Blob` bodies in HTTP-requests, use the `Blob`'s `Content-Type` as the request's `Content-Type` by default.
51 changes: 51 additions & 0 deletions packages/openapi/src/openapi-request-builder.spec.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an edge case that I would discuss with you :

FileApi (in ai-sdk-js) hardcodes headerParameters: { 'content-type': '*/*' } in generated code.

So this auto-detection won't kick in for file upload endpoints — the */* will win instead. Should we track a follow-up to clean up that generated default?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,57 @@ describe('openapi-request-builder', () => {
);
});

it('uses Blob content-type as content-type header when body is a Blob with a type', async () => {
const blob = new Blob(['<data>'], { type: 'application/octet-stream' });
const requestBuilder = new OpenApiRequestBuilder('post', '/test', {
body: blob
});
await requestBuilder.executeRaw(destination);
expect(httpClient.executeHttpRequest).toHaveBeenCalledWith(
sanitizeDestination(destination),
expect.objectContaining({
headers: {
requestConfig: { 'content-type': 'application/octet-stream' }
},
data: blob
}),
expect.anything()
);
});

it('does not override explicit content-type header when body is a Blob', async () => {
const blob = new Blob(['<data>'], { type: 'application/octet-stream' });
const requestBuilder = new OpenApiRequestBuilder('post', '/test', {
body: blob,
headerParameters: { 'content-type': 'application/xml' }
});
await requestBuilder.executeRaw(destination);
expect(httpClient.executeHttpRequest).toHaveBeenCalledWith(
sanitizeDestination(destination),
expect.objectContaining({
headers: { requestConfig: { 'content-type': 'application/xml' } },
data: blob
}),
expect.anything()
);
});

it('does not set content-type header when Blob has no type', async () => {
const blob = new Blob(['<data>']);
const requestBuilder = new OpenApiRequestBuilder('post', '/test', {
body: blob
});
await requestBuilder.executeRaw(destination);
expect(httpClient.executeHttpRequest).toHaveBeenCalledWith(
sanitizeDestination(destination),
expect.objectContaining({
headers: { requestConfig: {} },
data: blob
}),
expect.anything()
);
});

it('executes a request with multipart body using executeRaw', async () => {
const requestBuilder = new OpenApiRequestBuilder('post', '/test', {
body: {
Expand Down
11 changes: 10 additions & 1 deletion packages/openapi/src/openapi-request-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,16 @@ export class OpenApiRequestBuilder<ResponseT = any> {
}

private getHeaders(): OriginOptions {
const options = { requestConfig: this.parameters?.headerParameters || {} };
const headerParameters = { ...(this.parameters?.headerParameters || {}) };
const body = this.parameters?.body;
if (
body instanceof Blob &&
body.type &&
!pickValueIgnoreCase(headerParameters, 'content-type')
) {
headerParameters['content-type'] = body.type;
}
const options = { requestConfig: headerParameters };
if (Object.keys(this.customHeaders).length) {
return { custom: this.customHeaders, ...options };
}
Expand Down
Loading