Skip to content
Closed
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: 4 additions & 1 deletion apps/api/src/openapi-snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { describe, it, expect, vi } from "vitest";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { serializeOpenAPIDocument } from "./openapi/serialize.js";

// Stub checkMigrations before index.ts can import it
vi.mock("db-migrations/check-migrations", () => ({
Expand Down Expand Up @@ -61,6 +62,8 @@ describe("OpenAPI spec snapshot", () => {
new URL("../../../website/src/openapi/scope-openapi.json", import.meta.url),
);
const websiteSpec = await readFile(websiteSpecPath, "utf8");
expect(websiteSpec).toBe(JSON.stringify(doc));
const serialized = serializeOpenAPIDocument(doc);
expect(websiteSpec).toBe(serialized);
expect(serializeOpenAPIDocument(generateOpenAPIDocument())).toBe(serialized);
}, 30_000); // index.ts pulls in the entire route surface; 5s default is too tight under full-suite load.
});
3 changes: 2 additions & 1 deletion apps/api/src/openapi/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { writeFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import "../index.js";
import { generateOpenAPIDocument } from "./registry.js";
import { serializeOpenAPIDocument } from "./serialize.js";

const websiteSpecPath = fileURLToPath(
new URL("../../../../website/src/openapi/scope-openapi.json", import.meta.url),
);
const document = generateOpenAPIDocument();

await writeFile(websiteSpecPath, JSON.stringify(document));
await writeFile(websiteSpecPath, serializeOpenAPIDocument(document));
console.log(`Wrote OpenAPI specification to ${websiteSpecPath}`);
117 changes: 117 additions & 0 deletions apps/api/src/openapi/serialize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe, expect, it } from "vitest";
import type { OpenAPIObject } from "openapi3-ts/oas31";
import { serializeOpenAPIDocument } from "./serialize.js";

describe("serializeOpenAPIDocument", () => {
it("uses two-space indentation, LF line endings, and exactly one trailing newline", () => {
const document: OpenAPIObject = {
openapi: "3.1.0",
info: { version: "1.0.0", title: "Test API" },
paths: {},
};

expect(serializeOpenAPIDocument(document)).toBe(
'{\n' +
' "info": {\n' +
' "title": "Test API",\n' +
' "version": "1.0.0"\n' +
' },\n' +
' "openapi": "3.1.0",\n' +
' "paths": {}\n' +
'}\n',
);
});

it("ignores nested object insertion order while preserving arrays, content, and inputs", () => {
// Equivalent documents deliberately differ in key order, including objects in arrays.
const document: OpenAPIObject = {
openapi: "3.1.0",
info: { version: "1.0.0", title: "Test API" },
servers: [
{ url: "/z", description: "Primary" },
{ url: "/a", description: "Secondary" },
],
components: {
schemas: {
Item: {
type: "object",
required: ["z", "a"],
properties: {
z: { type: "string", enum: ["last", "first"] },
a: { type: ["string", "null"], example: null },
},
},
},
},
};
const reordered: OpenAPIObject = {
components: {
schemas: {
Item: {
properties: {
a: { example: null, type: ["string", "null"] },
z: { enum: ["last", "first"], type: "string" },
},
required: ["z", "a"],
type: "object",
},
},
},
servers: [
{ description: "Primary", url: "/z" },
{ description: "Secondary", url: "/a" },
],
info: { title: "Test API", version: "1.0.0" },
openapi: "3.1.0",
};
const original = JSON.stringify(document);
const originalReordered = JSON.stringify(reordered);

const serialized = serializeOpenAPIDocument(document);
expect(serialized).toBe(serializeOpenAPIDocument(reordered));
expect(serialized).toBe(serializeOpenAPIDocument(document));

// Parsed equality preserves array positions; raw equality also detects input key mutation.
const parsed: unknown = JSON.parse(serialized);
expect(parsed).toEqual(document);
expect(JSON.stringify(document)).toBe(original);
expect(JSON.stringify(reordered)).toBe(originalReordered);
});

it("limits a schema description change to one serialized line", () => {
const document: OpenAPIObject = {
openapi: "3.1.0",
info: { title: "Test API", version: "1.0.0" },
components: {
schemas: {
Item: { type: "string", description: "Original description" },
Unchanged: { type: "integer", minimum: 0 },
},
},
};
const changed: OpenAPIObject = {
...document,
components: {
schemas: {
...document.components?.schemas,
Item: { type: "string", description: "Updated description" },
},
},
};

// Compare every line, not just file length or parsed content, to enforce diff locality.
const before = serializeOpenAPIDocument(document).split("\n");
const after = serializeOpenAPIDocument(changed).split("\n");
expect(after).toHaveLength(before.length);
const changedLines = before.flatMap((line, index) =>
line === after[index] ? [] : [{ before: line, after: after[index] }],
);
expect(changedLines).toEqual([{
before: ' "description": "Original description",',
after: ' "description": "Updated description",',
}]);
});
});
24 changes: 24 additions & 0 deletions apps/api/src/openapi/serialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import type { OpenAPIObject } from "openapi3-ts/oas31";

/**
* Serialize a website specification with stable keys, two-space indentation,
* LF line endings, and one trailing newline, without changing array order.
* @param document Generated OpenAPI document; never mutated.
*/
export function serializeOpenAPIDocument(document: OpenAPIObject): string {
return JSON.stringify(document, (_key: string, value: unknown): unknown => {
if (value === null || typeof value !== "object" || Array.isArray(value)) {
return value;
}

// Use code-unit ordering rather than the host's locale for reproducible keys.
return Object.fromEntries(
Object.entries(value).sort(([left], [right]) =>
left < right ? -1 : left > right ? 1 : 0,
),
);
}, 2) + "\n";
}
7 changes: 7 additions & 0 deletions docs/architecture/app-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,10 @@ The static documentation site consumes the committed artifact at
registry with `pnpm --filter api generate:openapi` rather than fetching
the spec from a deployed environment. The API snapshot test verifies
that the artifact remains current.

The generator and website-file comparison share a serializer that recursively
orders object keys deterministically, preserves array order, and writes JSON
with two-space indentation, LF line endings, and exactly one trailing newline.
Unchanged API definitions produce byte-identical output; after the initial
formatting migration, small schema edits produce localized line diffs without
changing the parsed OpenAPI content.
5 changes: 5 additions & 0 deletions website/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ from the root pnpm workspace.
in this same `microsoft/scope` checkout. Install the root workspace
dependencies first; the generator updates
[src/openapi/scope-openapi.json](src/openapi/scope-openapi.json).
Output uses deterministically ordered object keys, two-space indentation,
LF line endings, and exactly one trailing newline. Array order is preserved.
Repeated generation from unchanged API definitions produces byte-identical
output, and small schema changes produce localized line diffs. The API
snapshot test enforces the same serialization contract.

- To list a new article or talk on the Community page, add one YAML
file under `src/content/articles/` or `src/content/talks/`. See
Expand Down
12,102 changes: 12,101 additions & 1 deletion website/src/openapi/scope-openapi.json

Large diffs are not rendered by default.

Loading