Skip to content

Commit a85f5ba

Browse files
mikeharderanushkasingh16
authored andcommitted
[swagger.js] Add option includeOperations to toJSONAsync() (Azure#38693)
- Useful for testing "get operations" on all specs in repo
1 parent b23c318 commit a85f5ba

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

.github/shared/cmd/spec-model.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ import { debugLogger } from "../src/logger.js";
44
import { SpecModel } from "../src/spec-model.js";
55

66
const USAGE =
7-
"Usage: npx spec-model path/to/spec [--debug] [--include-refs] [--relative-paths] [--no-embed-errors]\n" +
8-
"Example: npx spec-model specification/widget";
7+
"Usage: npx spec-model path/to/spec [--debug] [--include-operations] [--include-refs] [--relative-paths] [--no-embed-errors]\n" +
8+
"Examples:\n" +
9+
" npx spec-model specification/widget\n" +
10+
" npx spec-model specification --include-operations --include-refs > out.txt; grep out.txt '\"error\":'";
911

1012
// Exclude first two args (node, script file)
1113
let args = process.argv.slice(2);
1214

1315
const debug = args.includes("--debug");
1416
args = args.filter((a) => a != "--debug");
1517

18+
const includeOperations = args.includes("--include-operations");
19+
args = args.filter((a) => a != "--include-operations");
20+
1621
const includeRefs = args.includes("--include-refs");
1722
args = args.filter((a) => a != "--include-refs");
1823

@@ -42,7 +47,12 @@ const specModel = new SpecModel(specPath, { logger });
4247

4348
console.log(
4449
JSON.stringify(
45-
await specModel.toJSONAsync({ embedErrors: !noEmbedErrors, includeRefs, relativePaths }),
50+
await specModel.toJSONAsync({
51+
embedErrors: !noEmbedErrors,
52+
includeOperations,
53+
includeRefs,
54+
relativePaths,
55+
}),
4656
null,
4757
2,
4858
),

.github/shared/src/spec-model.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { SpecModelError } from "./spec-model-error.js";
2626
/**
2727
* @typedef {Object} ToJSONOptions
2828
* @prop {boolean} [embedErrors]
29+
* @prop {boolean} [includeOperations] Whether to include the operations in each swagger. Increases runtime about 20x, from 6s to 120s for whole specs repo.
2930
* @prop {boolean} [includeRefs]
3031
* @prop {boolean} [relativePaths]
3132
*/

.github/shared/src/swagger.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { embedError } from "./spec-model.js";
2424
/**
2525
* @typedef {Object} SwaggerJSON
2626
* @property {string} path
27+
* @property {Operation[]} [operations]
2728
* @property {Object[]} [refs]
2829
*/
2930

@@ -292,14 +293,20 @@ export class Swagger {
292293
* @returns {Promise<SwaggerJSON|ErrorJSON>}
293294
*/
294295
async toJSONAsync(options = {}) {
295-
const { includeRefs, relativePaths } = options;
296+
const { includeOperations, includeRefs, relativePaths } = options;
296297

297298
return await embedError(
298299
async () => ({
299300
path:
300301
relativePaths && this.#tag?.readme?.specModel
301302
? relative(this.#tag?.readme?.specModel.folder, this.#path)
302303
: this.#path,
304+
operations: includeOperations
305+
? [...(await this.getOperations()).values()].map((o) => {
306+
// Create new object with properties in preferred output order
307+
return { path: o.path, httpMethod: o.httpMethod, id: o.id };
308+
})
309+
: undefined,
303310
refs: includeRefs
304311
? await mapAsync(
305312
[...(await this.getRefs()).values()].sort((a, b) => a.path.localeCompare(b.path)),

.github/shared/test/spec-model.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,18 @@ describe("SpecModel", () => {
109109

110110
const jsonRefsRelative = /** @type {SpecModelJSON} */ (
111111
await specModel.toJSONAsync({
112+
includeOperations: true,
112113
includeRefs: true,
113114
relativePaths: true,
114115
})
115116
);
116117
const readmeJSONRelative = /** @type {ReadmeJSON} */ (jsonRefsRelative.readmes[0]);
117118
const readmePathRelative = readmeJSONRelative.path;
118119
expect(isAbsolute(readmePathRelative)).toBe(false);
120+
expect(
121+
/** @type {SwaggerJSON}*/ (/** @type {TagJSON} */ (readmeJSONRelative.tags[0]).inputFiles[0])
122+
.operations,
123+
).toBeDefined();
119124
expect(
120125
/** @type {SwaggerJSON}*/ (/** @type {TagJSON} */ (readmeJSONRelative.tags[0]).inputFiles[0])
121126
.refs,

0 commit comments

Comments
 (0)