Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。Swagger 让部署管理和使用功能强大的 API 从未如此简单。
回顾
首先,新建一个 ASP.NET Core 3.1 的项目,使用 nuget 安装 Swashbuckle.AspNetCore 包,命令如下:
在 startup.cs 文件配置 Swagger 服务,如下:
ConfigureServices 方法
Configure 方法
新建一个 Test 控制器,代码如下:
Test 控制器 Create 接口 Product 参数模型对象如下:
启动项目后,访问:http://localhost:18979/swagger/index.html,如下图:
swagger.json 内容如下:
{
"openapi": "3.0.1",
"info": {
"title": "My API",
"version": "v1"
},
"paths": {
"/api/Test/Create": {
"post": {
"tags": [
"Test"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
},
"application/json": {
"schema": {
"type": "string"
}
},
"text/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Information": {
"type": "object",
"properties": {
"isDiscount": {
"type": "boolean"
},
"isVip": {
"type": "boolean",
"nullable": true
}
},
"additionalProperties": false
},
"Product": {
"type": "object",
"properties": {
"name": {
"type": "string",
"nullable": true
},
"disabled": {
"type": "boolean"
},
"info": {
"$ref": "#/components/schemas/Information"
}
},
"additionalProperties": false
}
}
}
}
我们发现 Boolean 类型和可空 Boolean 类型,默认都是:true,在日常开发中 bool 如果没有赋值默认应该是 false,可空类型默认应该是 null,我们尝试使用 swagger 模拟提交的话,参数不注意的话,很容易出现事故。(上周,我就使用 swagger 模拟提交一个 bool 参数默认是 true 导致将微信消息推送给全体人员)
如何将 Boolean 类型默认赋值成 false 呢?可空类型赋值成 null 呢?
新建一个类继承于 ISchemaFilter,代码如下:
并且在添加 swagger 服务的时候,添加该过滤器 AddSwaggerGen 修改如下:
对于某些特殊 bool 字段,默认值需要设置为 true 的,可以添加 [DefaultValue(true)] 特性即可,如下图:
{
"name": "string",
"disabled": true,
"info": {
"isDiscount": false,
"isVip": null
}
} swagger.json 文件如下:
{
"openapi": "3.0.1",
"info": {
"title": "My API",
"version": "v1"
},
"paths": {
"/api/Test/Create": {
"post": {
"tags": [
"Test"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
},
"application/json": {
"schema": {
"type": "string"
}
},
"text/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Information": {
"type": "object",
"properties": {
"isDiscount": {
"type": "boolean",
"default": false
},
"isVip": {
"type": "boolean",
"default": null,
"nullable": true
}
},
"additionalProperties": false
},
"Product": {
"type": "object",
"properties": {
"name": {
"type": "string",
"nullable": true
},
"disabled": {
"type": "boolean",
"default": true
},
"info": {
"$ref": "#/components/schemas/Information"
}
},
"additionalProperties": false
}
}
}
}
(完)
|