Skip to content

feat: Fallback to an alternate string if operationId is undefined. #139

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

Merged
merged 3 commits into from
Jun 30, 2025
Merged
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
33 changes: 33 additions & 0 deletions src/generateValidRootSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type * as Types from "./types";

export const generateValidRootSchema = (input: Types.OpenApi.Document): Types.OpenApi.Document => {
if (!input.paths) {
return input;
}
/** update undefined operation id */
for (const [path, methods] of Object.entries(input.paths || {})) {
const targets = {
get: methods.get,
put: methods.put,
post: methods.post,
delete: methods.delete,
options: methods.options,
head: methods.head,
patch: methods.patch,
trace: methods.trace,
} satisfies Record<string, Types.OpenApi.Operation | undefined>;
for (const [method, operation] of Object.entries(targets)) {
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)}`;
}
}
}
return input;
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EOL } from "os";

import * as Api from "./api";
import type * as Types from "./types";
import { generateValidRootSchema } from "./generateValidRootSchema";

export interface Option {
allowOperationIds?: string[];
Expand All @@ -17,7 +18,7 @@ export class CodeGenerator {
private option?: Option,
) {
if (typeof entryPointOrDocument === "string") {
this.rootSchema = Api.FileSystem.loadJsonOrYaml(entryPointOrDocument);
this.rootSchema = generateValidRootSchema(Api.FileSystem.loadJsonOrYaml(entryPointOrDocument));
this.resolvedReferenceDocument = Api.ResolveReference.resolve(
entryPointOrDocument,
entryPointOrDocument,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`Typedef with template api.test.domain 1`] = `
"//
Expand Down Expand Up @@ -572,6 +572,12 @@ export interface Response$postHelloWorldReadonly$Status$200 {
message?: Schemas.Message;
};
}
export interface Response$get$undefined$operation$$id$Status$200 {
"application/json": {
/** Undefined operation response */
message?: string;
};
}
export type ResponseContentType$getHelloWorld = keyof Response$getHelloWorld$Status$200;
export interface Params$getHelloWorld {
parameter: Parameter$getHelloWorld;
Expand All @@ -581,6 +587,7 @@ export type ResponseContentType$postHelloWorldReadonly = keyof Response$postHell
export interface Params$postHelloWorldReadonly {
requestBody: RequestBody$postHelloWorldReadonly["application/json"];
}
export type ResponseContentType$get$undefined$operation$$id = keyof Response$get$undefined$operation$$id$Status$200;
export type HttpMethod = "GET" | "PUT" | "POST" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH" | "TRACE";
export interface ObjectLike {
[key: string]: any;
Expand All @@ -593,10 +600,11 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getHelloWorld$Status$200 | Response$postHelloWorldReadonly$Status$200;
export type SuccessResponses = Response$getHelloWorld$Status$200 | Response$postHelloWorldReadonly$Status$200 | Response$get$undefined$operation$$id$Status$200;
export namespace ErrorResponse {
export type getHelloWorld = void;
export type postHelloWorldReadonly = void;
export type get$undefined$operation$$id = void;
}
export interface Encoding {
readonly contentType?: string;
Expand Down Expand Up @@ -647,6 +655,17 @@ export class Client<RequestOption> {
requestBody: params.requestBody
}, option);
}
public async get$undefined$operation$$id(option?: RequestOption): Promise<Response$get$undefined$operation$$id$Status$200["application/json"]> {
const url = this.baseUrl + \`/undefined/operation/:id\`;
const headers = {
Accept: "application/json"
};
return this.apiClient.request({
httpMethod: "GET",
url,
headers
}, option);
}
}
"
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`Typedef with template api.test.domain 1`] = `
"//
Expand Down Expand Up @@ -575,6 +575,12 @@ export interface Response$postHelloWorldReadonly$Status$200 {
message?: Schemas.Message;
};
}
export interface Response$get$undefined$operation$$id$Status$200 {
"application/json": {
/** Undefined operation response */
message?: string;
};
}
export type ResponseContentType$getHelloWorld = keyof Response$getHelloWorld$Status$200;
export interface Params$getHelloWorld {
parameter: Parameter$getHelloWorld;
Expand All @@ -584,6 +590,7 @@ export type ResponseContentType$postHelloWorldReadonly = keyof Response$postHell
export interface Params$postHelloWorldReadonly {
requestBody: RequestBody$postHelloWorldReadonly["application/json"];
}
export type ResponseContentType$get$undefined$operation$$id = keyof Response$get$undefined$operation$$id$Status$200;
export type HttpMethod = "GET" | "PUT" | "POST" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH" | "TRACE";
export interface ObjectLike {
[key: string]: any;
Expand All @@ -596,10 +603,11 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getHelloWorld$Status$200 | Response$postHelloWorldReadonly$Status$200;
export type SuccessResponses = Response$getHelloWorld$Status$200 | Response$postHelloWorldReadonly$Status$200 | Response$get$undefined$operation$$id$Status$200;
export namespace ErrorResponse {
export type getHelloWorld = void;
export type postHelloWorldReadonly = void;
export type get$undefined$operation$$id = void;
}
export interface Encoding {
readonly contentType?: string;
Expand Down Expand Up @@ -649,6 +657,17 @@ export const createClient = <RequestOption>(apiClient: ApiClient<RequestOption>,
headers,
requestBody: params.requestBody
}, option);
},
get$undefined$operation$$id: (option?: RequestOption): Promise<Response$get$undefined$operation$$id$Status$200["application/json"]> => {
const url = _baseUrl + \`/undefined/operation/:id\`;
const headers = {
Accept: "application/json"
};
return apiClient.request({
httpMethod: "GET",
url,
headers
}, option);
}
};
};
Expand Down
13 changes: 13 additions & 0 deletions test/api.v2.domain/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ paths:
properties:
message:
$ref: "#/components/schemas/Message"
/undefined/operation/:id:
get:
responses:
200:
description: 成功
content:
application/json:
schema:
type: object
properties:
message:
type: string
description: Undefined operation response