Skip to content

Commit 3e85496

Browse files
committed
feat: Fall back to an alternate string if operationId is undefined.
1 parent e280c0b commit 3e85496

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/generateValidRootSchema.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type * as Types from "./types";
2+
3+
export const generateValidRootSchema = (input: Types.OpenApi.Document): Types.OpenApi.Document => {
4+
if (!input.paths) {
5+
return input;
6+
}
7+
/** update undefined operation id */
8+
for (const [path, methods] of Object.entries(input.paths || {})) {
9+
for (const [method, operation] of Object.entries(methods || {})) {
10+
if (!operation.operationId) {
11+
operation.operationId = `${method.toLowerCase()}${path.charAt(0).toUpperCase() + path.slice(1)}`;
12+
}
13+
}
14+
}
15+
return input;
16+
};

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EOL } from "os";
22

33
import * as Api from "./api";
44
import type * as Types from "./types";
5+
import { generateValidRootSchema } from "./generateValidRootSchema";
56

67
export interface Option {
78
allowOperationIds?: string[];
@@ -17,7 +18,7 @@ export class CodeGenerator {
1718
private option?: Option,
1819
) {
1920
if (typeof entryPointOrDocument === "string") {
20-
this.rootSchema = Api.FileSystem.loadJsonOrYaml(entryPointOrDocument);
21+
this.rootSchema = generateValidRootSchema(Api.FileSystem.loadJsonOrYaml(entryPointOrDocument));
2122
this.resolvedReferenceDocument = Api.ResolveReference.resolve(
2223
entryPointOrDocument,
2324
entryPointOrDocument,

0 commit comments

Comments
 (0)