架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 19106|回复: 4

[ASP.NET] ASP.NET Core(八) 之 Swagger UI 默认参数的坑

[复制链接]
发表于 2021-5-8 13:27:02 | 显示全部楼层 |阅读模式
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。

总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。Swagger 让部署管理和使用功能强大的 API 从未如此简单。

回顾

ASP.NET Core(七)深入刨析框架源码
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core(六)DI 手动获取注入对象的方法
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core(五)基于 CAP 分布式事务
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(四)之过滤器统一 ModelState 模型验证
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core(三)使用 ActivatorUtilities 动态创建实例
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core(二)通过代码自重启应用
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core(一)使用 Redis 缓存
https://www.itsvse.com/thread-9393-1-1.html

首先,新建一个 ASP.NET Core 3.1 的项目,使用 nuget 安装 Swashbuckle.AspNetCore 包,命令如下:

在 startup.cs 文件配置 Swagger 服务,如下:

ConfigureServices 方法

Configure 方法

新建一个 Test 控制器,代码如下:

Test 控制器 Create 接口 Product 参数模型对象如下:

启动项目后,访问:http://localhost:18979/swagger/index.html,如下图:

QQ截图20210508130622.jpg

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
      }
    }
  }
}

QQ截图20210508131222.jpg

我们发现 Boolean 类型和可空 Boolean 类型,默认都是:true,在日常开发中 bool 如果没有赋值默认应该是 false,可空类型默认应该是 null,我们尝试使用 swagger 模拟提交的话,参数不注意的话,很容易出现事故。(上周,我就使用 swagger 模拟提交一个 bool 参数默认是 true 导致将微信消息推送给全体人员)

如何将 Boolean 类型默认赋值成 false 呢?可空类型赋值成 null 呢?

新建一个类继承于 ISchemaFilter,代码如下:

并且在添加 swagger 服务的时候,添加该过滤器 AddSwaggerGen 修改如下:

对于某些特殊 bool 字段,默认值需要设置为 true 的,可以添加 [DefaultValue(true)] 特性即可,如下图:

QQ截图20210508132343.jpg

{
  "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
      }
    }
  }
}

(完)






上一篇:Java 使用 JDBC 连接 sqlite URL 问题
下一篇:Kafka 手动设置 offset 偏移量
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2021-9-22 20:45:47 | 显示全部楼层
学习学习。。。
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2021-9-22 20:59:49 | 显示全部楼层
学习学习,老铁,更新这么多,跟不上了啊
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2023-5-16 11:22:31 | 显示全部楼层
RE: ASP.NET Core(八) 之 Swagger UI 默认参数的坑 [修改]
高级模式
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2023-5-16 17:20:36 | 显示全部楼层
学习学习
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-4-17 05:29

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表