2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-03-10 05:22:37 +00:00
|
|
|
import type { Schema } from '@/misc/json-schema.js';
|
|
|
|
import { refs } from '@/misc/json-schema.js';
|
2019-04-23 13:35:26 +00:00
|
|
|
|
2024-01-13 07:54:25 +00:00
|
|
|
export function convertSchemaToOpenApiSchema(schema: Schema, type: 'param' | 'res') {
|
|
|
|
// optional, nullable, refはスキーマ定義に含まれないので分離しておく
|
2023-11-22 08:08:56 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2024-01-13 07:54:25 +00:00
|
|
|
const { optional, nullable, ref, ...res }: any = schema;
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
if (schema.type === 'object' && schema.properties) {
|
2024-01-13 07:54:25 +00:00
|
|
|
if (type === 'res') {
|
|
|
|
const required = Object.entries(schema.properties).filter(([k, v]) => !v.optional).map(([k]) => k);
|
|
|
|
if (required.length > 0) {
|
2023-11-22 08:08:56 +00:00
|
|
|
// 空配列は許可されない
|
2024-01-13 07:54:25 +00:00
|
|
|
res.required = required;
|
|
|
|
}
|
2023-11-22 08:08:56 +00:00
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
|
|
|
|
for (const k of Object.keys(schema.properties)) {
|
2024-01-13 07:54:25 +00:00
|
|
|
res.properties[k] = convertSchemaToOpenApiSchema(schema.properties[k], type);
|
2019-04-23 13:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (schema.type === 'array' && schema.items) {
|
2024-01-13 07:54:25 +00:00
|
|
|
res.items = convertSchemaToOpenApiSchema(schema.items, type);
|
2019-04-23 13:35:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 07:54:25 +00:00
|
|
|
for (const o of ['anyOf', 'oneOf', 'allOf'] as const) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
if (o in schema) res[o] = schema[o]!.map(schema => convertSchemaToOpenApiSchema(schema, type));
|
|
|
|
}
|
2022-01-18 13:27:10 +00:00
|
|
|
|
2024-01-13 07:54:25 +00:00
|
|
|
if (type === 'res' && schema.ref) {
|
2023-10-22 01:52:06 +00:00
|
|
|
const $ref = `#/components/schemas/${schema.ref}`;
|
|
|
|
if (schema.nullable || schema.optional) {
|
|
|
|
res.allOf = [{ $ref }];
|
|
|
|
} else {
|
|
|
|
res.$ref = $ref;
|
|
|
|
}
|
2019-04-23 13:35:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 07:54:25 +00:00
|
|
|
if (schema.nullable) {
|
|
|
|
if (Array.isArray(schema.type) && !schema.type.includes('null')) {
|
|
|
|
res.type.push('null');
|
|
|
|
} else if (typeof schema.type === 'string') {
|
|
|
|
res.type = [res.type, 'null'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 13:35:26 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-02-23 19:08:08 +00:00
|
|
|
export const schemas = {
|
|
|
|
Error: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
error: {
|
|
|
|
type: 'object',
|
|
|
|
description: 'An error object.',
|
|
|
|
properties: {
|
|
|
|
code: {
|
|
|
|
type: 'string',
|
2019-02-23 19:27:09 +00:00
|
|
|
description: 'An error code. Unique within the endpoint.',
|
2019-02-23 19:08:08 +00:00
|
|
|
},
|
|
|
|
message: {
|
|
|
|
type: 'string',
|
|
|
|
description: 'An error message.',
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
type: 'string',
|
|
|
|
format: 'uuid',
|
|
|
|
description: 'An error ID. This ID is static.',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2019-02-23 19:08:08 +00:00
|
|
|
},
|
2021-12-09 14:58:30 +00:00
|
|
|
required: ['code', 'id', 'message'],
|
2019-02-23 19:08:08 +00:00
|
|
|
},
|
|
|
|
},
|
2021-12-09 14:58:30 +00:00
|
|
|
required: ['error'],
|
2019-02-23 19:08:08 +00:00
|
|
|
},
|
|
|
|
|
2021-09-11 16:12:23 +00:00
|
|
|
...Object.fromEntries(
|
2024-01-13 07:54:25 +00:00
|
|
|
Object.entries(refs).map(([key, schema]) => [key, convertSchemaToOpenApiSchema(schema, 'res')]),
|
2021-09-11 16:12:23 +00:00
|
|
|
),
|
2019-02-23 19:08:08 +00:00
|
|
|
};
|