diff --git a/.changeset/plain-views-rhyme.md b/.changeset/plain-views-rhyme.md new file mode 100644 index 000000000..bbf19d189 --- /dev/null +++ b/.changeset/plain-views-rhyme.md @@ -0,0 +1,5 @@ +--- +"openapi-fetch": patch +--- + +fix omit serializing for already serialized data diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js index be3b153a3..b54435a55 100644 --- a/packages/openapi-fetch/src/index.js +++ b/packages/openapi-fetch/src/index.js @@ -597,7 +597,7 @@ export function defaultPathSerializer(pathname, pathParams) { * @type {import("./index.js").defaultBodySerializer} */ export function defaultBodySerializer(body, headers) { - if (body instanceof FormData) { + if (body instanceof FormData || typeof body === "string" || body instanceof URLSearchParams || body instanceof Blob) { return body; } if (headers) { diff --git a/packages/openapi-fetch/test/common/request.test.ts b/packages/openapi-fetch/test/common/request.test.ts index 7aa87e563..edb773e89 100644 --- a/packages/openapi-fetch/test/common/request.test.ts +++ b/packages/openapi-fetch/test/common/request.test.ts @@ -213,30 +213,32 @@ describe("request", () => { }); test.each(BODY_ACCEPTING_METHODS)("`''` body (with body serializer) - %s", async (method) => { + const body = '' const bodySerializer = vi.fn((body) => `Serialized: ${JSON.stringify(body)}`); const { bodyUsed, bodyText } = await fireRequestAndGetBodyInformation({ bodySerializer, method, fetchOptions: { - body: "", + body, }, }); expect(bodyUsed).toBe(true); - expect(bodyText).toBe('Serialized: ""'); + expect(bodyText).toBe(`Serialized: ${body}`); expect(bodySerializer).toBeCalled(); }); test.each(BODY_ACCEPTING_METHODS)("`''` body (without body serializer) - %s", async (method) => { + const body = '' const { bodyUsed, bodyText } = await fireRequestAndGetBodyInformation({ method, fetchOptions: { - body: "", + body, }, }); expect(bodyUsed).toBe(true); - expect(bodyText).toBe('""'); + expect(bodyText).toBe(body); }); test.each(BODY_ACCEPTING_METHODS)("`0` body (with body serializer) - %s", async (method) => {