-
Notifications
You must be signed in to change notification settings - Fork 578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: swagger组件中,ApiResponse 的 schema 参数的处理 #4078
Conversation
这是我直接在github修改提交的PR,所以lint有点问题,后面有空了再拉代码改一下,补个单测再提交吧,目前直接写 ApiOkResponse({
description: options.description || '请求成功响应',
content: {
'application/json': {
schema: {
title: `响应数据 ${typeName}`,
allOf: [
{
$ref: getSchemaPath(ResponseVO),
},
{
properties: {
data: responseData,
},
},
],
},
},
},
}) |
看了眼,schema 的确必须在 content 底下 |
其实Swagger还有这两个问题
// 无效
ApiExtraModel([ResponseVO, type].filter(Boolean))(target.constructor, propertyKey, descriptor);
// 有效
ApiExtraModel([ResponseVO, type].filter(Boolean))(target.constructor);
enum StatusEnum {
Disabled,
Enabled,
}
ApiProperty({
type: 'enum',
enum: StatusEnum,
}) 看源码部分的处理是 // packages/swagger/src/common/enum.utils.ts
export function getEnumValues(enumType: SwaggerEnumType): string[] | number[] {
if (Array.isArray(enumType)) {
return enumType as string[];
}
if (typeof enumType !== 'object') {
return [];
}
const values = [];
const uniqueValues = {};
for (const key in enumType) {
const value = enumType[key];
/* eslint-disable no-prototype-builtins */
// filter out cases where enum key also becomes its value (A: B, B: A)
if (
!uniqueValues.hasOwnProperty(value) &&
!uniqueValues.hasOwnProperty(key)
) {
values.push(value);
uniqueValues[value] = value;
}
}
return values;
} 核心实现可以修改为 const numericValues = Object.values(enumType)
.filter((value) => typeof value === 'number')
.map((value) => value.toString());
return Object.keys(enumType)
.filter((key) => !numericValues.includes(key))
.map((key) => enumType[key]); |
ApiExtraModel 只支持类啊 |
应该是统一用 |
我修复了,你看下 |
辛苦大佬,粗看暂时没啥问题,等后面发布的时候再更新下看看 🤩 |
@midwayjs/swagger@3.18.1 |
@czy88840616 |
Checklist
npm test
passesAffected core subsystem(s)
Description of change
swagger组件中对直接配置了scheme 的写法做了一些兼容
起因是打算封装一个固定响应格式的装饰器,发现以下写法,直接使用schema不生效