kindergarten_java/reading-platform-frontend/orval.config.ts
zhonghua a461f58d3a fix: orval API 生成优化 - 修复 oneOf schema 验证错误
- 新增 scripts/fetch-openapi.js:拉取并修复 OpenAPI 文档
- 内联 ResultObject[] 的 \,移除非法 schema 名
- orval 使用本地 openapi.json,api:update 自动执行 api:fetch
- AdminStatsController: Result<Object[]> 改为 Result<List<Map<String, Object>>>
- .gitignore: 忽略 openapi.json

Made-with: Cursor
2026-03-16 10:45:15 +08:00

69 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineConfig } from 'orval';
/** 内联 schema避免 ResultObject[] 的 $ref 导致 oneOf 验证错误 */
const RESULT_OBJECT_ARRAY_SCHEMA = {
type: 'object',
properties: {
code: { type: 'integer', format: 'int32' },
message: { type: 'string' },
data: { type: 'array', items: { type: 'object', additionalProperties: true } },
},
};
export default defineConfig({
readingPlatform: {
output: {
mode: 'split',
target: 'src/api/generated/index.ts',
schemas: 'src/api/generated/model',
client: 'axios',
override: {
mutator: {
path: 'src/api/generated/mutator.ts',
name: 'customMutator',
},
// 自定义类型名称
name: (type) => {
// 移除命名空间前缀,简化类型名称
return type.replace(/^(Result|ResultPageResult)/, '');
},
},
// 导入优化
imports: {
axios: true,
},
},
input: {
// 使用 api:fetch 生成的 openapi.jsonapi:update 会自动先执行 api:fetch
target: './openapi.json',
// 路径重写:确保 OpenAPI 文档中的路径正确
override: {
// 使用转换器修复路径 - 将 `/api/xxx` 转换为 `/api/v1/xxx`
transformer: (spec) => {
const paths = spec.paths || {};
for (const path of Object.keys(paths)) {
let newKey = path.replace(/\/v1\/v1\//g, '/v1/');
if (newKey === path) newKey = path.replace(/^\/api\/(?!v1\/)/, '/api/v1/');
if (newKey !== path) {
paths[newKey] = paths[path];
delete paths[path];
}
}
for (const pathObj of Object.values(paths)) {
for (const op of Object.values(pathObj)) {
const content = op?.responses?.['200']?.content;
if (!content) continue;
for (const media of Object.values(content)) {
if (media?.schema?.['$ref']?.endsWith('ResultObject[]')) {
media.schema = { ...RESULT_OBJECT_ARRAY_SCHEMA };
}
}
}
}
return spec;
},
},
},
},
});