diff options
| author | zyoshoka <107108195+zyoshoka@users.noreply.github.com> | 2025-02-08 17:29:24 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-08 08:29:24 +0000 |
| commit | 54fc232a23742ca62232d35c8e976cbec40a61d2 (patch) | |
| tree | d23a3646e5e0eb33e4ae5823ac2b55522a992869 /packages/misskey-js/generator/src/generator.ts | |
| parent | fix(ci): oktetoの導線を削除 (#15427) (diff) | |
| download | sharkey-54fc232a23742ca62232d35c8e976cbec40a61d2.tar.gz sharkey-54fc232a23742ca62232d35c8e976cbec40a61d2.tar.bz2 sharkey-54fc232a23742ca62232d35c8e976cbec40a61d2.zip | |
fix(backend): use unique `operationId` in the OpenAPI schema (#15420)
* fix(backend): use unique `operationId` in the OpenAPI schema
* fix: read with UTF-8 encoding
Diffstat (limited to 'packages/misskey-js/generator/src/generator.ts')
| -rw-r--r-- | packages/misskey-js/generator/src/generator.ts | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/packages/misskey-js/generator/src/generator.ts b/packages/misskey-js/generator/src/generator.ts index 88f2ae9ee9..a77ffeedc2 100644 --- a/packages/misskey-js/generator/src/generator.ts +++ b/packages/misskey-js/generator/src/generator.ts @@ -1,8 +1,9 @@ -import { mkdir, writeFile } from 'fs/promises'; +import assert from 'assert'; +import { mkdir, readFile, writeFile } from 'fs/promises'; import { OpenAPIV3_1 } from 'openapi-types'; import { toPascal } from 'ts-case-convert'; import OpenAPIParser from '@readme/openapi-parser'; -import openapiTS from 'openapi-typescript'; +import openapiTS, { OpenAPI3, OperationObject, PathItemObject } from 'openapi-typescript'; async function generateBaseTypes( openApiDocs: OpenAPIV3_1.Document, @@ -20,7 +21,29 @@ async function generateBaseTypes( } lines.push(''); - const generatedTypes = await openapiTS(openApiJsonPath, { + // NOTE: Align `operationId` of GET and POST to avoid duplication of type definitions + const openApi = JSON.parse(await readFile(openApiJsonPath, 'utf8')) as OpenAPI3; + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + for (const [key, item] of Object.entries(openApi.paths!)) { + assert('post' in item); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + openApi.paths![key] = { + ...('get' in item ? { + get: { + ...item.get, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + operationId: ((item as PathItemObject).get as OperationObject).operationId!.replaceAll('get___', ''), + }, + } : {}), + post: { + ...item.post, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + operationId: ((item as PathItemObject).post as OperationObject).operationId!.replaceAll('post___', ''), + }, + }; + } + + const generatedTypes = await openapiTS(openApi, { exportType: true, transform(schemaObject) { if ('format' in schemaObject && schemaObject.format === 'binary') { @@ -78,7 +101,7 @@ async function generateEndpoints( for (const operation of postPathItems) { const path = operation._path_; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const operationId = operation.operationId!; + const operationId = operation.operationId!.replaceAll('get___', '').replaceAll('post___', ''); const endpoint = new Endpoint(path); endpoints.push(endpoint); @@ -195,7 +218,7 @@ async function generateApiClientJSDoc( for (const operation of postPathItems) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const operationId = operation.operationId!; + const operationId = operation.operationId!.replaceAll('get___', '').replaceAll('post___', ''); if (operation.description) { endpoints.push({ |