Skip to content

Commit

Permalink
Merge pull request microsoft#8 from mikekistler/mdk/fix-openapi3-resp…
Browse files Browse the repository at this point in the history
…onse-headers

Fix generation of openapi3 response headers
  • Loading branch information
mikekistler authored Oct 23, 2021
2 parents d649b44 + a03572e commit 5ce4bfd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/openapi3",
"comment": "Fix generation of openapi3 response headers",
"type": "patch"
}
],
"packageName": "@cadl-lang/openapi3"
}
8 changes: 5 additions & 3 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ function createOAPIEmitter(program: Program, options: OpenAPIEmitterOptions) {

let contentType = "application/json";
let contentEntry: any = {};
let headers: any = {};

let bodyModel = responseModel;
if (responseModel.kind === "Model") {
Expand Down Expand Up @@ -433,9 +434,7 @@ function createOAPIEmitter(program: Program, options: OpenAPIEmitterOptions) {
}
break;
default:
const header = getResponseHeader(prop);
contentEntry.headers = contentEntry.headers ?? {};
contentEntry.headers[headerName] = header;
headers[headerName] = getResponseHeader(prop);
break;
}
}
Expand All @@ -449,6 +448,9 @@ function createOAPIEmitter(program: Program, options: OpenAPIEmitterOptions) {
[contentType]: contentEntry,
},
};
if (Object.keys(headers).length > 0) {
response.headers = headers;
}
currentEndpoint.responses[statusCode] = response;
}

Expand Down
22 changes: 22 additions & 0 deletions packages/openapi3/test/test-openapi-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,28 @@ describe("openapi3: operations", () => {
});
});

describe("openapi3: responses", () => {
it("define responses with response headers", async () => {
const res = await openApiFor(
`
model ETagHeader {
@header eTag: string;
}
model Key {
key: string;
}
@resource("/")
namespace root {
@get()
op read(): Key & ETagHeader;
}
`
);
ok(res.paths["/"].get.responses["200"].headers);
ok(res.paths["/"].get.responses["200"].headers["e-tag"]);
});
});

async function oapiForModel(name: string, modelDef: string) {
const oapi = await openApiFor(`
${modelDef};
Expand Down

0 comments on commit 5ce4bfd

Please sign in to comment.