OpenAPI Swagger 可重用架构部分
我在创建 swagger 可重用架构组件时遇到了一些挑战。
我的项目中有很多 API,其中大多数都有相同的标头和常见的错误响应,当然,每个 API 都有不同的响应模式,为了克服这个挑战,我创建了一个装饰器,如下所示:
swagger-essentials.decorator .ts
type apiOkResponse = Type<unknown>;
export const SwaggerEssentials = (okRes?: apiOkResponse) => {
return applyDecorators(
SwaggerApiHeaders(),
ApiResponse({ type: okRes, status: 200 }),
HttpCode(200),
ApiResponse({ type: ConnectTimeoutDTO, status: 599 }),
ApiResponse({ type: JsonValidationErrorDTO, status: 501 }),
SwaggerCommonResponses(),
);
};
response.dto.ts
class Response {
@ApiProperty()
id: number;
@ApiProperty()
name:string;
}
export class RESPONSE_SCHEMA {
@ApiProperty({ example: 'Success' })
message: string;
@ApiProperty({example: {}})
data: Response;
}
然后在我的控制器中,我像下面这样使用它:
xyz.controller.ts
@Post('/test')
@SwaggerEssentials(RESPONSE_SCHEMA)
fetchDetails(){}
直到这里一切都很好,没有问题,但是由于每个 API 都有不同的响应,因此有两个属性如果成功响应,消息
和数据
保持不变,当然数据会不同。
因此,我希望有一些东西可以摆脱上述两个属性,并且它们会自动定义/附加到实际响应。
我已经尝试过 PartialType()
但不值得解决这个问题。
I'm having some challenges while creating the swagger reusable schema components.
I have a lot of APIs in the project and most of them have the same headers and common error responses, of course, every API has its different response schema, to overcome this challenge I have created a decorator as bellow:
swagger-essentials.decorator.ts
type apiOkResponse = Type<unknown>;
export const SwaggerEssentials = (okRes?: apiOkResponse) => {
return applyDecorators(
SwaggerApiHeaders(),
ApiResponse({ type: okRes, status: 200 }),
HttpCode(200),
ApiResponse({ type: ConnectTimeoutDTO, status: 599 }),
ApiResponse({ type: JsonValidationErrorDTO, status: 501 }),
SwaggerCommonResponses(),
);
};
response.dto.ts
class Response {
@ApiProperty()
id: number;
@ApiProperty()
name:string;
}
export class RESPONSE_SCHEMA {
@ApiProperty({ example: 'Success' })
message: string;
@ApiProperty({example: {}})
data: Response;
}
And then in my controller, I use it like below:
xyz.controller.ts
@Post('/test')
@SwaggerEssentials(RESPONSE_SCHEMA)
fetchDetails(){}
Till here everything is fine and no problem, but as every API has its different response, however, there are two properties that remain the same in case of a successful response which are message
and data
of course the data will be different.
So, I want to have something through which I can get rid of above mentioned two properties and they are automatically defined/attached to the actual response.
I have tried PartialType()
but it's not worthy to solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎也是我所面临的事情。但该代码有一些缺点。
RESPONSE_SCHEMA
应该是通用的,可以累积任何类型的数据
,它与Response
类紧密绑定。这可以在泛型的帮助下修复:现在你的控制器函数将返回这个:
通过这个,你可以在你的 swagger 中获得所需的响应对象。
另外,您可以删除
SwaggerEssentials
中的函数参数并尝试。它应该有效。另外,请重新检查SwaggerApiHeaders()
和SwaggerCommonResponses()
。这些似乎不是 swagger 模块中的类。This seems to be something that I too faced. But there are some downside of the code.
RESPONSE_SCHEMA
which should have been generic to accumulate any type ofdata
is tightly bound toResponse
class. This can be fixed with the help of generics:Now your controller function instead will return this:
By this, you can get the desired response object in your swagger.
Also, you can remove the function parameter in
SwaggerEssentials
and try. It should work. Also, please recheckSwaggerApiHeaders()
andSwaggerCommonResponses()
. These don't seem to be classes in swagger module.