summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/openapi
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/server/api/openapi')
-rw-r--r--packages/backend/src/server/api/openapi/gen-spec.ts47
-rw-r--r--packages/backend/src/server/api/openapi/schemas.ts10
2 files changed, 42 insertions, 15 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..0e71510b48 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) {
const errors = {} as any;
if (endpoint.meta.errors) {
@@ -58,6 +59,11 @@ export function genOpenapiSpec(config: Config) {
const resSchema = endpoint.meta.res ? convertSchemaToOpenApiSchema(endpoint.meta.res) : {};
let desc = (endpoint.meta.description ? endpoint.meta.description : 'No description provided.') + '\n\n';
+
+ if (endpoint.meta.secure) {
+ desc += '**Internal Endpoint**: This endpoint is an API for the misskey mainframe and is not intended for use by third parties.\n';
+ }
+
desc += `**Credential required**: *${endpoint.meta.requireCredential ? 'Yes' : 'No'}*`;
if (endpoint.meta.kind) {
const kind = endpoint.meta.kind;
@@ -79,6 +85,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 +105,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 +133,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 +210,7 @@ export function genOpenapiSpec(config: Config) {
};
spec.paths['/' + endpoint.name] = {
+ ...(endpoint.meta.allowGet ? { get: info } : {}),
post: info,
};
}
diff --git a/packages/backend/src/server/api/openapi/schemas.ts b/packages/backend/src/server/api/openapi/schemas.ts
index 1a1d973e56..2716f5f162 100644
--- a/packages/backend/src/server/api/openapi/schemas.ts
+++ b/packages/backend/src/server/api/openapi/schemas.ts
@@ -7,10 +7,16 @@ import type { Schema } from '@/misc/json-schema.js';
import { refs } from '@/misc/json-schema.js';
export function convertSchemaToOpenApiSchema(schema: Schema) {
- const res: any = schema;
+ // optional, refはスキーマ定義に含まれないので分離しておく
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ const { optional, ref, ...res }: any = schema;
if (schema.type === 'object' && schema.properties) {
- res.required = Object.entries(schema.properties).filter(([k, v]) => !v.optional).map(([k]) => k);
+ const required = Object.entries(schema.properties).filter(([k, v]) => !v.optional).map(([k]) => k);
+ if (required.length > 0) {
+ // 空配列は許可されない
+ res.required = required;
+ }
for (const k of Object.keys(schema.properties)) {
res.properties[k] = convertSchemaToOpenApiSchema(schema.properties[k]);