Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

7kyun
Copy link

@7kyun 7kyun commented Sep 19, 2024

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
  • @midway/swagger
Description of change

swagger组件中对直接配置了scheme 的写法做了一些兼容

起因是打算封装一个固定响应格式的装饰器,发现以下写法,直接使用schema不生效

import { ApiExtraModel, ApiOkResponse, getSchemaPath, ReferenceObject, SchemaObject, Type } from '@midwayjs/swagger';
import { ResponseVO } from '@/model/view/Basic';

export interface IResponseOptions<T extends Type<any>> {
  type: T;
  description?: string;
  isArray?: boolean;
  isPaginated?: boolean;
}

const baseTypeNames = ['String', 'Number', 'Boolean'];

export const ApiBasicResponse = <T extends Type<any>>(options: IResponseOptions<T>): MethodDecorator => {
  const { type } = options;
  const typeName = type?.name || 'null';
  let responseData: SchemaObject | ReferenceObject;
  if (options.isArray) {
    responseData = { type: 'array', items: { $ref: getSchemaPath(type) } };
  } else if (options.isPaginated) {
    responseData = {
      type: 'object',
      properties: {
        items: { type: 'array', items: { $ref: getSchemaPath(type) } },
        total: { type: 'number' },
        success: { type: 'boolean' },
      },
    };
  } else if (type) {
    if (baseTypeNames.includes(typeName)) {
      responseData = { type: typeName.toLocaleLowerCase() };
    } else {
      responseData = { $ref: getSchemaPath(type) };
    }
  } else {
    responseData = {
      type: 'null',
      nullable: true,
      default: null,
    };
  }

  const decorators: MethodDecorator[] = [
    ApiOkResponse({
      description: options.description || '请求成功响应',
      schema: {
        title: `响应数据 ${typeName}`,
        allOf: [
          {
            $ref: getSchemaPath(ResponseVO),
          },
          {
            properties: {
              data: responseData,
            },
          },
        ],
      },
    }),
  ];

  return (target, propertyKey, descriptor) => {
    ApiExtraModel([ResponseVO, type].filter(Boolean))(target.constructor);
    decorators.forEach(decorator => decorator(target, propertyKey, descriptor));
  };
};

另外还有一个Bug,使用ApiExtraModels绑定到Method也不生效,看源码貌似只对绑定到Class的数据进行了处理,因时间关系没有再行修复,而是直接改一下实现,手动绑定到类中

ApiExtraModel([ResponseVO, type].filter(Boolean))(target.constructor);

@7kyun 7kyun changed the title fix: swagger组件中,ApiResponse 的制定 schema 参数的处理 fix: swagger组件中,ApiResponse 的 schema 参数的处理 Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant