修改在Nestjs中的Openapi(Swagger)架构中出现的DTO名称

发布于 2025-02-07 09:43:25 字数 385 浏览 2 评论 0原文

我面临着一个问题,即我的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.

enter image description here

BTW, I am using typescript and nestjs.
Every help will be appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆伤 2025-02-14 09:43:25

开箱即用,Nest.js尚未提供现成的解决方案。有一个打开的拉请求(如前所述) https://github.com/nestjs/nestjs/swagger/拉/983 ,但是何时合并是未知的。
您可以使用以下方法之一在模式中更改DTO名称:

  1. 在DTO中添加静态名称属性。
  class UserDto {
    static name = 'User';  // <- here

    @ApiProperty()
    firstName: string;

    // ...
  }

但是在严格的模式下,打字稿将显示一个错误:
属性'名称'与内置属性'函数相冲突。

  1. 静态 .com/nestjs/swagger/pull/983“ rel =“ noreferrer”>拉请求并使用它,直到所需功能出现在nest.js中。
    装饰器将其属性添加到DTO的包装类别中所需值的名称属性。
  type Constructor<T = object> = new(...args: any[]) => T;
  type Wrapper<T = object> = { new(): (T & any), prototype: T };
  type DecoratorOptions = { name: string };
  type ApiSchemaDecorator = <T extends Constructor>(options: DecoratorOptions) => (constructor: T) => Wrapper<T>;

  const ApiSchema: ApiSchemaDecorator = ({ name }) => {
    return (constructor) => {
      const wrapper = class extends constructor { };
      Object.defineProperty(wrapper, 'name', {
        value: name,
        writable: false,
      });
      return wrapper;
    }
  }

  @ApiSchema({ name: 'User' }) // <- here
  class UserDto {
    @ApiProperty()
    firstName: string;

    // ...
  }

。将更改为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:

  1. Add a static name property to your DTO.
  class UserDto {
    static name = 'User';  // <- here

    @ApiProperty()
    firstName: string;

    // ...
  }

But in strict mode, TypeScript will show an error like:
Static property 'name' conflicts with built-in property 'Function.name' of constructor function 'UserDto'.

  1. Write a decorator with an interface as suggested in the pull request and use it until the desired functionality appears in Nest.js.
    The decorator adds the name property with the needed value to the wrapper class for the DTO.
  type Constructor<T = object> = new(...args: any[]) => T;
  type Wrapper<T = object> = { new(): (T & any), prototype: T };
  type DecoratorOptions = { name: string };
  type ApiSchemaDecorator = <T extends Constructor>(options: DecoratorOptions) => (constructor: T) => Wrapper<T>;

  const ApiSchema: ApiSchemaDecorator = ({ name }) => {
    return (constructor) => {
      const wrapper = class extends constructor { };
      Object.defineProperty(wrapper, 'name', {
        value: name,
        writable: false,
      });
      return wrapper;
    }
  }

Use as suggested in the proposal:

  @ApiSchema({ name: 'User' }) // <- here
  class UserDto {
    @ApiProperty()
    firstName: string;

    // ...
  }

And don't forget that in TypeScript 5 the decorator API will change to something close to the implementation in JavaScript ????

温柔戏命师 2025-02-14 09:43:25

使用apischema装饰器:

@ApiSchema({ name: 'User' })
class UserDto {

  @ApiProperty()
  firstName: string;

  // ...
}

Use the ApiSchema decorator:

@ApiSchema({ name: 'User' })
class UserDto {

  @ApiProperty()
  firstName: string;

  // ...
}
别低头,皇冠会掉 2025-02-14 09:43:25

我在我的情况下使用@apimodel

这样解决了

    @ApiModel(value="MeuLindoDto")
    public class NameOriginalClassResponseDto ...

I solved in my case using @ApiModel

like this

    @ApiModel(value="MeuLindoDto")
    public class NameOriginalClassResponseDto ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文