diff options
| author | おさむのひと <46447427+samunohito@users.noreply.github.com> | 2023-11-22 17:08:56 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-22 17:08:56 +0900 |
| commit | c284d41b5bff3244e3b79396aff8e87bc80425a4 (patch) | |
| tree | 55deb3cf606b6987144903b8db9ea374edb8082c /packages/backend/src/server/api/openapi/gen-spec.ts | |
| parent | taichanne30 -> tai-cha (#12405) (diff) | |
| download | sharkey-c284d41b5bff3244e3b79396aff8e87bc80425a4.tar.gz sharkey-c284d41b5bff3244e3b79396aff8e87bc80425a4.tar.bz2 sharkey-c284d41b5bff3244e3b79396aff8e87bc80425a4.zip | |
swagger-cli validateがvalidとなるapi.jsonを作れるようにする (#12403)
* api.jsonがswagger-cli validateでエラーにならないように生成ロジックを修正
* フィールドの消し方に不備があったので変更
* バックエンドを起動しなくてもapi.jsonを作れるようにした
* deepCopyしてからレスポンス部分を作るようにした
* fix CHANGELOG.md
* securitySchemesの定義を復活&ApiCallServiceの実装的にベアラトークンなのでその形で
* bodyが無い(空オブジェクト)のときはrequestBodyを描画しないようにする
* allowGetがtrueな項目はget用の記載も作成
---------
Co-authored-by: osamu <46447427+sam-osamu@users.noreply.github.com>
Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
Diffstat (limited to 'packages/backend/src/server/api/openapi/gen-spec.ts')
| -rw-r--r-- | packages/backend/src/server/api/openapi/gen-spec.ts | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/packages/backend/src/server/api/openapi/gen-spec.ts b/packages/backend/src/server/api/openapi/gen-spec.ts index 4f972d3f7e..30bf6b8b3e 100644 --- a/packages/backend/src/server/api/openapi/gen-spec.ts +++ b/packages/backend/src/server/api/openapi/gen-spec.ts @@ -4,7 +4,7 @@ */ import type { Config } from '@/config.js'; -import endpoints from '../endpoints.js'; +import endpoints, { IEndpoint } from '../endpoints.js'; import { errors as basicErrors } from './errors.js'; import { schemas, convertSchemaToOpenApiSchema } from './schemas.js'; @@ -33,16 +33,17 @@ export function genOpenapiSpec(config: Config) { schemas: schemas, securitySchemes: { - ApiKeyAuth: { - type: 'apiKey', - in: 'body', - name: 'i', + bearerAuth: { + type: 'http', + scheme: 'bearer', }, }, }, }; - for (const endpoint of endpoints.filter(ep => !ep.meta.secure)) { + // 書き換えたりするのでディープコピーしておく。そのまま編集するとメモリ上の値が汚れて次回以降の出力に影響する + const copiedEndpoints = JSON.parse(JSON.stringify(endpoints)) as IEndpoint[]; + for (const endpoint of copiedEndpoints.filter(ep => !ep.meta.secure)) { const errors = {} as any; if (endpoint.meta.errors) { @@ -79,6 +80,13 @@ export function genOpenapiSpec(config: Config) { schema.required = [...schema.required ?? [], 'file']; } + if (schema.required && schema.required.length <= 0) { + // 空配列は許可されない + schema.required = undefined; + } + + const hasBody = (schema.type === 'object' && schema.properties && Object.keys(schema.properties).length >= 1); + const info = { operationId: endpoint.name, summary: endpoint.name, @@ -92,17 +100,19 @@ export function genOpenapiSpec(config: Config) { } : {}), ...(endpoint.meta.requireCredential ? { security: [{ - ApiKeyAuth: [], + bearerAuth: [], }], } : {}), - requestBody: { - required: true, - content: { - [requestType]: { - schema, + ...(hasBody ? { + requestBody: { + required: true, + content: { + [requestType]: { + schema, + }, }, }, - }, + } : {}), responses: { ...(endpoint.meta.res ? { '200': { @@ -118,6 +128,11 @@ export function genOpenapiSpec(config: Config) { description: 'OK (without any results)', }, }), + ...(endpoint.meta.res?.optional === true || endpoint.meta.res?.nullable === true ? { + '204': { + description: 'OK (without any results)', + }, + } : {}), '400': { description: 'Client error', content: { @@ -190,6 +205,7 @@ export function genOpenapiSpec(config: Config) { }; spec.paths['/' + endpoint.name] = { + ...(endpoint.meta.allowGet ? { get: info } : {}), post: info, }; } |