Skip to content

Commit

Permalink
[OpenAPI3] Fix: Using @body _: void in operation parameters and tre…
Browse files Browse the repository at this point in the history
…at it as no body. (#2609)

```
op test(@Body _: void): string;
```
 Will not crash and be treated as no request body
  • Loading branch information
timotheeguerin authored Oct 30, 2023
1 parent 533c974 commit 8b22fae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 2 additions & 1 deletion packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
isRecordModelType,
isSecret,
isTemplateDeclaration,
isVoidType,
listServices,
Model,
ModelProperty,
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/openapi3/test/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8b22fae

Please sign in to comment.