1

所以我正在开发数据传输对象文件中的组件 yaml 文件,以便我可以引用它们。

这是我到目前为止所拥有的:

/**
* @openapi
* components:
*   schemas:
*     VerifiedEmailAddressDto:
*       type: object
*       required:
*         - email
*       properties:
*         _type:
*           type: string
*         email:
*           type: string
*           description: a users email.
*         reset:
*           type: boolean
*         passwordRules:
*           type: object
*           properties:
*             minLength:
*               type: number
*             maxLength:
*               type: number
*             minRequiredUppercase:
*               type: number
*       example:
*         _type: VerifiedEmailAddressDto
*         email: pablo+test_pab001@alunacare.com
*         reset: false
*         passwordRules:
*/
export class VerifiedEmailAddressDto {
  readonly _type = "VerifiedEmailAddressDto";
  readonly email: string;
  readonly reset: boolean;
  readonly passwordRules: { minLength: number; maxLength: number; minRequiredUppercase: number; minRequiredLowerCase: number; minRequiredSymbols: number };

  constructor(email: string, reset: boolean, passwordRules: { minLength: number; maxLength: number; minRequiredUppercase: number; minRequiredLowerCase: number; minRequiredSymbols: number }) {
   this.email = email;
   this.reset = reset;
   this.passwordRules = passwordRules;
  }
}

的最后一个属性passwordRules是这个对象内部的一个对象。所以它是一个嵌套对象,但到目前为止我所拥有的并没有给我这个:

{
 "_type": "VerifiedEmailAddressDto",
 "email": "pablo+test_pab001@alunacare.com",
 "reset": false,
 "passwordRules": {
   "minLength": 8,
   "maxLength": 25,
   "minRequiredUppercase": 1,
   "minRequiredLowerCase": 1,
   "minRequiredSymbols": 0
  }
}

但老实说,我不确定如何完成这个,我假设这部分:

*         passwordRules:
*           type: object
*           properties:
*             minLength:
*               type: number
*             maxLength:
*               type: number
*             minRequiredUppercase:
*               type: number

是正确的,但是在示例中提供的是我坚持的内容,在这种情况下,甚至上述内容可能也不正确。

这个想法是这样我最终可以在这里引用它:

/**
* @openapi
* /api/v2/auth/check_mail:
*   post:
*     tags: [Auth]
*     description: This endpoint checks to see if an email is unique or is in use.
*     requestBody:
*       required: true
*       content:
*         application/json:
*           schema:
*           type: object
*           $ref: '#/components/schemas/VerifiedEmailAddressDto'
*     responses:
*       201:
*         description: Get permissions.
*         content:
*           application/json:
*             schema:
*             $ref: '#/components/schemas/VerifiedEmailAddressDto'
*/
this.app.post(p().api.v2.auth.check_email.$url, [
// TODO restrict number of queries by IP by period of time.
authMiddleware.validateEmailQuery,
credentialsController.verifyEmailAddress
]);

所以我能够得到一个空对象,passwordRules如下所示:

/**
* @openapi
* components:
*   schemas:
*     VerifiedEmailAddressDto:
*       type: object
*       required:
*         - email
*       properties:
*         _type:
*           type: string
*         email:
*           type: string
*           description: a users email.
*         reset:
*           type: boolean
*         passwordRules:
*           type: object
*           properties:
*             minLength:
*               type: number
*             maxLength:
*               type: number
*             minRequiredUppercase:
*               type: number
*       example:
*         _type: VerifiedEmailAddressDto
*         email: pablo+test_pab001@alunacare.com
*         reset: false
*         passwordRules: {}
*/
export class VerifiedEmailAddressDto {
  readonly _type = "VerifiedEmailAddressDto";
  readonly email: string;
  readonly reset: boolean;
  readonly passwordRules: { minLength: number; maxLength: number; minRequiredUppercase: number; minRequiredLowerCase: number; minRequiredSymbols: number };

  constructor(email: string, reset: boolean, passwordRules: { minLength: number; maxLength: number; minRequiredUppercase: number; minRequiredLowerCase: number; minRequiredSymbols: number }) {
   this.email = email;
   this.reset = reset;
   this.passwordRules = passwordRules;
  }
}

但是如果我尝试像这样在对象中添加它的属性:

passwordRules: {
  minLength: 8
  maxLength: 25
}

如果我尝试像这样放置示例,我什么也得不到:

  *         passwordRules:
    *           type: object
    *           properties:
    *             minLength:
    *               type: number
    *               example: 8
    *             maxLength:
    *               type: number
    *               example: 25
    *             minRequiredUppercase:
    *               type: number
    *               example: 1
    *       example:
    *         _type: VerifiedEmailAddressDto
    *         email: pablo+test_pab001@alunacare.com
    *         reset: false
    *         passwordRules: {}

我仍然一无所获。

4

1 回答 1

0

在 OpenAPI 中,可以像指定根示例对象一样指定示例嵌套对象。例如,作为 YAML 键值对。

所以以下example规格:

...
*         passwordRules:
*           type: object
*           properties:
*             minLength:
*               type: number
*             maxLength:
*               type: number
*             minRequiredUppercase:
*               type: number
*             minRequiredLowerCase:
*               type: number
*             minRequiredSymbols:
*               type: number
*       example:
*         _type: VerifiedEmailAddressDto
*         email: pablo+test_pab001@alunacare.com
*         reset: false
*         passwordRules:
*           minLength: 8
*           maxLength: 25
*           minRequiredUppercase: 1
*           minRequiredLowerCase: 1
*           minRequiredSymbols: 0
...

..在 Swagger-UI 中看起来像这样:

招摇-ui

于 2021-12-02T04:55:45.090 回答