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
5 changes: 5 additions & 0 deletions .changeset/fix-string-body-serializer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Fix `defaultBodySerializer` double-encoding string bodies. Previously, passing a string as the request body (e.g. a pre-serialized JSON string) would result in it being wrapped in an extra layer of JSON quotes. Strings are now passed through as-is.
2 changes: 1 addition & 1 deletion packages/openapi-fetch/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$schema": "https://biomejs.dev/schemas/2.3.14/schema.json",
"extends": "//",
"files": {
"includes": ["src/**", "test/**", "!**/examples/**", "!test/**/schemas/**", "!test/bench/**/*.min.js"]
"includes": ["src/**", "test/**", "!**/examples", "!test/**/schemas", "!test/bench/**/*.min.js"]
},
"linter": {
"rules": {
Expand Down
5 changes: 4 additions & 1 deletion packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const PATH_PARAM_RE = /\{[^{}]+\}/g;
const supportsRequestInitExt = () => {
return (
typeof process === "object" &&
Number.parseInt(process?.versions?.node?.substring(0, 2)) >= 18 &&
Number.parseInt(process?.versions?.node?.substring(0, 2), 10) >= 18 &&
process.versions.undici
);
};
Expand Down Expand Up @@ -625,6 +625,9 @@ export function defaultBodySerializer(body, headers) {
if (body instanceof FormData) {
return body;
}
if (typeof body === "string") {
return body;
}
if (headers) {
const contentType =
headers.get instanceof Function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const caToBuffer = (ca) => {
const API_PORT = process.env.API_PORT || 4578;

const app = express();
app.get("/v1/foo", (req, res) => {
app.get("/v1/foo", (_req, res) => {
res.send("bar");
});

Expand Down
14 changes: 13 additions & 1 deletion packages/openapi-fetch/test/common/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,19 @@ describe("request", () => {
});

expect(bodyUsed).toBe(true);
expect(bodyText).toBe('""');
expect(bodyText).toBe("");
});

test.each(BODY_ACCEPTING_METHODS)("string body is passed through without JSON serialization - %s", async (method) => {
const { bodyUsed, bodyText } = await fireRequestAndGetBodyInformation({
method,
fetchOptions: {
body: "pre-serialized string",
},
});

expect(bodyUsed).toBe(true);
expect(bodyText).toBe("pre-serialized string");
});

test.each(BODY_ACCEPTING_METHODS)("`0` body (with body serializer) - %s", async (method) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-fetch/test/common/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe("response", () => {

describe("response object", () => {
test.each([200, 404, 500] as const)("%s", async (status) => {
const client = createObservedClient<paths>({}, async (req) =>
const client = createObservedClient<paths>({}, async (_req) =>
Response.json({ status, message: "OK" }, { status }),
);
const result = await client.GET(status === 200 ? "/resources" : `/error-${status}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-fetch/test/e2e/index.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, type Page, test } from "@playwright/test";
import { type Page, test } from "@playwright/test";

// note: these tests load Chrome, Firefox, and Safari in Playwright to test a browser-realistic runtime.
// the frontend is prepared via Vite to create a production-accurate app (and throw add’l type errors)
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-fetch/test/http-methods/get.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { createObservedClient } from "../helpers.js";
import type { components, paths } from "./schemas/get.js";
import type { paths } from "./schemas/get.js";

describe("GET", () => {
test("sends correct method", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ test("can modify response", async () => {

test("returns original errors if nothing is returned", async () => {
const actualError = new Error();
const client = createObservedClient<paths>({}, async (req) => {
const client = createObservedClient<paths>({}, async (_req) => {
throw actualError;
});
client.use({
Expand Down
Loading