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/openapi-vendor-json-content-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@executor-js/plugin-openapi": patch
---

Preserve vendor +json Content-Type on OpenAPI object request bodies.
8 changes: 2 additions & 6 deletions packages/plugins/openapi/src/sdk/invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,12 +838,8 @@ const applyRequestBody = (
});

if (isJsonContentType(contentType)) {
// Pre-serialized JSON strings pass through with the declared media
// type preserved (important for `application/vnd.foo+json` etc.).
if (typeof bodyValue === "string") {
return sent(HttpClientRequest.bodyText(request, bodyValue, contentType));
}
return sent(HttpClientRequest.bodyJsonUnsafe(request, bodyValue));
const text = typeof bodyValue === "string" ? bodyValue : JSON.stringify(bodyValue);
return sent(HttpClientRequest.bodyText(request, text, contentType));
}

if (isFormUrlEncoded(contentType)) {
Expand Down
30 changes: 30 additions & 0 deletions packages/plugins/openapi/src/sdk/non-json-body.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,36 @@ describe("OpenAPI non-JSON request body dispatch", () => {
}),
);

it.effect("application/vnd.api+json: object body keeps the declared content type", () =>
Effect.gen(function* () {
const { server, captured } = yield* startEchoServer({
name: "createNote",
path: "/notes",
payload: JsonNameObject,
transformSpec: replaceRequestBodyContent("/notes", "post", {
"application/vnd.api+json": {
schema: {
type: "object",
properties: {
name: { type: "string" },
},
},
},
}),
});

const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() }));
const conn = yield* addOpenApiTestConnection(executor, server, { slug: "notes-jsonapi" });

yield* executor.execute(conn.address("body.createNote"), {
body: { name: "Acme" },
});

expect(captured.contentType).toBe("application/vnd.api+json");
expect(decodeJsonNameBody(captured.body.toString("utf8"))).toEqual({ name: "Acme" });
}),
);

it.effect("multipart/form-data: only encoder-supported file shapes are advertised", () =>
Effect.gen(function* () {
const { server } = yield* startEchoServer({
Expand Down
Loading