From 9cb0d38501217e0e195b2ea95c47a5bcaf2116b3 Mon Sep 17 00:00:00 2001 From: David Knaack Date: Mon, 18 May 2026 16:53:41 +0200 Subject: [PATCH 1/2] feat: Inherit Content-Type from `Blob` bodies --- .changeset/ripe-papers-open.md | 5 ++ .../src/openapi-request-builder.spec.ts | 49 +++++++++++++++++++ .../openapi/src/openapi-request-builder.ts | 11 ++++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .changeset/ripe-papers-open.md diff --git a/.changeset/ripe-papers-open.md b/.changeset/ripe-papers-open.md new file mode 100644 index 0000000000..e0ea947215 --- /dev/null +++ b/.changeset/ripe-papers-open.md @@ -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. diff --git a/packages/openapi/src/openapi-request-builder.spec.ts b/packages/openapi/src/openapi-request-builder.spec.ts index bd28749efd..1e0b7ee20c 100644 --- a/packages/openapi/src/openapi-request-builder.spec.ts +++ b/packages/openapi/src/openapi-request-builder.spec.ts @@ -146,6 +146,55 @@ 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([''], { 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([''], { 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(['']); + 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: { diff --git a/packages/openapi/src/openapi-request-builder.ts b/packages/openapi/src/openapi-request-builder.ts index 5b2340738f..240e508698 100644 --- a/packages/openapi/src/openapi-request-builder.ts +++ b/packages/openapi/src/openapi-request-builder.ts @@ -376,7 +376,16 @@ export class OpenApiRequestBuilder { } 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 }; } From 10c4b770430f3bb67969aacc8b81410e0586519f Mon Sep 17 00:00:00 2001 From: "sap-cloud-sdk-bot[bot]" <274190970+sap-cloud-sdk-bot[bot]@users.noreply.github.com> Date: Mon, 18 May 2026 14:57:18 +0000 Subject: [PATCH 2/2] Changes from lint:fix --- packages/openapi/src/openapi-request-builder.spec.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/openapi/src/openapi-request-builder.spec.ts b/packages/openapi/src/openapi-request-builder.spec.ts index 1e0b7ee20c..2f0504bd61 100644 --- a/packages/openapi/src/openapi-request-builder.spec.ts +++ b/packages/openapi/src/openapi-request-builder.spec.ts @@ -155,7 +155,9 @@ describe('openapi-request-builder', () => { expect(httpClient.executeHttpRequest).toHaveBeenCalledWith( sanitizeDestination(destination), expect.objectContaining({ - headers: { requestConfig: { 'content-type': 'application/octet-stream' } }, + headers: { + requestConfig: { 'content-type': 'application/octet-stream' } + }, data: blob }), expect.anything()