Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating OpenAPI3 emitter to sort response header in Lexicographic order #3015

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Update OpenAPI3 emitter to emit response headers in lexicographic order
2 changes: 1 addition & 1 deletion packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ function createOAPIEmitter(
obj.headers ??= {};
// OpenAPI can't represent different headers per content type.
// So we merge headers here, and report any duplicates unless they are identical
for (const [key, value] of Object.entries(data.headers)) {
for (const [key, value] of Object.entries(data.headers).sort()) {
const headerVal = getResponseHeader(value);
const existing = obj.headers[key];
if (existing) {
Expand Down
40 changes: 40 additions & 0 deletions packages/openapi3/test/response-headers-order.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { strictEqual } from "assert";
import { describe, it } from "vitest";
import { openApiFor } from "./test-host.js";

describe("openapi3: response headers order", () => {
const headerDefinitions = `
model A_Header { @header a : string };
model B_Header { @header b : string };
model C_Header { @header c : string };
`;
it("header already in lexical order", async () => {
const res = await openApiFor(
`
${headerDefinitions}
model Headers { ...A_Header, ...B_Header, ...C_Header };

op read(): {@statusCode _: 200, content: string, headers: Headers};
`
);
strictEqual(Object.keys(res.paths["/"].get.responses["200"].headers).length, 3);
strictEqual(Object.keys(res.paths["/"].get.responses["200"].headers)[0], "a");
strictEqual(Object.keys(res.paths["/"].get.responses["200"].headers)[1], "b");
strictEqual(Object.keys(res.paths["/"].get.responses["200"].headers)[2], "c");
});

it("header not in lexical order", async () => {
const res = await openApiFor(
`
${headerDefinitions}
model Headers { ...C_Header, ...A_Header, ...B_Header };

op read(): {@statusCode _: 200, content: string, headers: Headers};
`
);
strictEqual(Object.keys(res.paths["/"].get.responses["200"].headers).length, 3);
strictEqual(Object.keys(res.paths["/"].get.responses["200"].headers)[0], "a");
strictEqual(Object.keys(res.paths["/"].get.responses["200"].headers)[1], "b");
strictEqual(Object.keys(res.paths["/"].get.responses["200"].headers)[2], "c");
});
});