From 8b22fae2dd63a3586716ca3b7bd5a62c185b3372 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 30 Oct 2023 08:21:44 -0700 Subject: [PATCH] [OpenAPI3] Fix: Using `@body _: void` in operation parameters and treat it as no body. (#2609) ``` op test(@body _: void): string; ``` Will not crash and be treated as no request body --- .../fix-void-request-body_2023-10-27-17-39.json | 10 ++++++++++ packages/openapi3/src/openapi.ts | 3 ++- packages/openapi3/test/parameters.test.ts | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 common/changes/@typespec/openapi3/fix-void-request-body_2023-10-27-17-39.json diff --git a/common/changes/@typespec/openapi3/fix-void-request-body_2023-10-27-17-39.json b/common/changes/@typespec/openapi3/fix-void-request-body_2023-10-27-17-39.json new file mode 100644 index 0000000000..a087615581 --- /dev/null +++ b/common/changes/@typespec/openapi3/fix-void-request-body_2023-10-27-17-39.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@typespec/openapi3", + "comment": "Fix: Stops emitting an error when using `@body _: void` in operation parameters and treat it as no body.", + "type": "none" + } + ], + "packageName": "@typespec/openapi3" +} \ No newline at end of file diff --git a/packages/openapi3/src/openapi.ts b/packages/openapi3/src/openapi.ts index 84c6667912..f6ec56a353 100644 --- a/packages/openapi3/src/openapi.ts +++ b/packages/openapi3/src/openapi.ts @@ -40,6 +40,7 @@ import { isRecordModelType, isSecret, isTemplateDeclaration, + isVoidType, listServices, Model, ModelProperty, @@ -1129,7 +1130,7 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt } function emitRequestBody(body: HttpOperationRequestBody | undefined, visibility: Visibility) { - if (body === undefined) { + if (body === undefined || isVoidType(body.type)) { return; } diff --git a/packages/openapi3/test/parameters.test.ts b/packages/openapi3/test/parameters.test.ts index 4f28df2a89..a436f7aacc 100644 --- a/packages/openapi3/test/parameters.test.ts +++ b/packages/openapi3/test/parameters.test.ts @@ -300,6 +300,11 @@ describe("openapi3: parameters", () => { strictEqual(res.paths["/"].get.parameters[0].name, "top"); }); + it("omit request body if type is void", async () => { + const res = await openApiFor(`op test(@body foo: void ): void;`); + strictEqual(res.paths["/"].post.requestBody, undefined); + }); + describe("content type parameter", () => { it("header named with 'Content-Type' gets resolved as content type for operation.", async () => { const res = await openApiFor(