如何在 Swagger 3.0 中为组件编写嵌套对象
How to write a nested object in Swagger 3.0 for components
所以我正在开发数据传输对象文件中的组件 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: {}
我还是一无所获。
在 OpenAPI 中,示例嵌套对象的指定方式与 root example object 相同。例如,作为 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:
所以我正在开发数据传输对象文件中的组件 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: {}
我还是一无所获。
在 OpenAPI 中,示例嵌套对象的指定方式与 root example object 相同。例如,作为 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: