-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgenerateValidRootSchema.ts
More file actions
49 lines (42 loc) · 1.35 KB
/
Copy pathgenerateValidRootSchema.ts
File metadata and controls
49 lines (42 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type * as Types from "./types";
const normalizePathParameters = (parameters: (Types.OpenApi.Parameter | Types.OpenApi.Reference)[] | undefined): void => {
if (!parameters) {
return;
}
for (const parameter of parameters) {
if ("$ref" in parameter) {
continue;
}
// OpenAPI 3.x spec §3.3.2: path パラメータは常に required: true
if (parameter.in === "path") {
parameter.required = true;
}
}
};
export const generateValidRootSchema = (input: Types.OpenApi.Document): Types.OpenApi.Document => {
if (input.components?.parameters) {
normalizePathParameters(Object.values(input.components.parameters));
}
if (!input.paths) {
return input;
}
const httpMethods = ["get", "put", "post", "delete", "options", "head", "patch", "trace"] as const;
for (const [path, pathItem] of Object.entries(input.paths)) {
normalizePathParameters(pathItem.parameters);
for (const method of httpMethods) {
const operation = pathItem[method];
if (!operation) {
continue;
}
// skip reference object
if ("$ref" in operation) {
continue;
}
if (!operation.operationId) {
operation.operationId = `${method.toLowerCase()}${path.charAt(0).toUpperCase() + path.slice(1)}`;
}
normalizePathParameters(operation.parameters);
}
}
return input;
};