修改在Nestjs中的Openapi(Swagger)架构中出现的DTO名称
我面临着一个问题,即我的DTO类型被命名为一件事,但我希望它们在OpenAPI DOC页面中以其他名称出现。 例如,我有一个在控制器中使用的UserDTO类,但希望它在模式部分中简单地显示为“用户”(以及其他任何地方都适用)。有可能吗?我可以使用任何装饰器吗? 我知道我可以简单地修改类名,但是在其他地方已经使用了其他不同的用户类。 我到处搜索,无济于事。
顺便说一句,我正在使用Typescript和Nestjs。 每个帮助将不胜感激,谢谢!
I am facing a problem where my DTO types are named one thing, but I want them to appear with a different name in the OpenAPI doc page.
For example, I have a UserDto class that I use in my controller, but wanted it to appear as simply "User" in the schemas section (and everywhere else this applies). Is that possible? Is there any decorator I can use?
I know I can simply modify the class name, but there is already a different user class used elsewhere.
I have searched everywhere with no avail.
BTW, I am using typescript and nestjs.
Every help will be appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
开箱即用,Nest.js尚未提供现成的解决方案。有一个打开的拉请求(如前所述) https://github.com/nestjs/nestjs/swagger/拉/983 ,但是何时合并是未知的。
您可以使用以下方法之一在模式中更改DTO名称:
但是在严格的模式下,打字稿将显示一个错误:
属性'名称'与内置属性'函数相冲突。
静态 .com/nestjs/swagger/pull/983“ rel =“ noreferrer”>拉请求并使用它,直到所需功能出现在nest.js中。
装饰器将其属性添加到DTO的包装类别中所需值的名称属性。
如
。将更改为JavaScript
Out of the box, Nest.js doesn't yet offer a ready-made solution. There is an open pull request (as mentioned earlier) https://github.com/nestjs/swagger/pull/983, but when it will be merged is unknown.
You can change the DTO name in schemas using one of the following approaches:
But in strict mode, TypeScript will show an error like:
Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'UserDto'.
The decorator adds the name property with the needed value to the wrapper class for the DTO.
Use as suggested in the proposal:
And don't forget that in TypeScript 5 the decorator API will change to something close to the implementation in JavaScript ????
使用
apischema
装饰器:Use the
ApiSchema
decorator:我在我的情况下使用@apimodel
这样解决了
I solved in my case using @ApiModel
like this