【发布时间】:2021-05-21 11:52:05
【问题描述】:
我遇到了 OpenApi json 数据验证问题。基于 OpenApi 验证(或 editor.swagger.io),我的 JSON 文件有未使用的模型。我应该摆脱警告,几乎所有警告都来自 System.Reflection 命名空间。
例子:
: System.Reflection
- Unused model: PropertyInfo
- Unused model: MethodImplAttributes
- Unused model: FieldAttributes
- Unused model: EventAttributes
- Unused model: MethodAttributes
- Unused model: FieldInfo
- Unused model: PropertyAttributes
: 其余的
- Unused model: Type --system
- Unused model: SecurityRuleSet --system.security
json 是在我运行应用程序时生成的(带有 swagger 包的 .NET Core 3.1)
Install-Package Swashbuckle.AspNetCore -Version 5.6.3
Startup.cs
// Clears servers list and adds default server entry with relative url respecting
app.UseSwagger(c=>c.ForceSingleServerWithRelativeAddress());
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
c.EnableDeepLinking();
});
如果 System.Reflection 包在项目的其他地方(或任何其他命名空间,如 System.Security 等)使用,我如何才能摆脱这些警告。我不能只是自定义 MS 包来摆脱未使用的模型,它就是这样。
我在 Swagger 设置期间是否遗漏了任何启动选项,是否会导致问题?
警告(未使用的模型)没有给我任何堆栈跟踪、root 或任何东西,仅仅是因为项目中不存在未使用的模型?
在项目某处使用 system.reflection 的示例方法:
public async Task<IEnumerable<IssueSimple>> GetSimpleIssues(int id)
{
string method = MethodBase.GetCurrentMethod().Name; //that one using System.Reflection
return null; // not to expose business logic
}
用于验证的Open Api命令(json和open api文件必须在同一个文件夹中)
java -jar openapi-generator-cli-5.1.1.jar validate -i gittesting.json
部分json文件:
"PropertyInfo": {
"type": "object",
"properties": {
"name": {
"type": "string",
"nullable": true,
"readOnly": true
},
"declaringType": {
"$ref": "#/components/schemas/Type"
},
"reflectedType": {
"$ref": "#/components/schemas/Type"
},
"customAttributes": {
"type": "array",
"items": {
"$ref": "#/components/schemas/CustomAttributeData"
},
"nullable": true,
"readOnly": true
},
"isCollectible": {
"type": "boolean",
"readOnly": true
},
"metadataToken": {
"type": "integer",
"format": "int32",
"readOnly": true
},
"memberType": {
"$ref": "#/components/schemas/MemberTypes"
},
"propertyType": {
"$ref": "#/components/schemas/Type"
},
"attributes": {
"$ref": "#/components/schemas/PropertyAttributes"
},
"isSpecialName": {
"type": "boolean",
"readOnly": true
},
"canRead": {
"type": "boolean",
"readOnly": true
},
"canWrite": {
"type": "boolean",
"readOnly": true
}
},
"additionalProperties": false
////////////////////////////////////////////////////
////////////////////////////////////////////////////
"PropertyAttributes": {
"enum": [
"None",
"SpecialName",
"RTSpecialName",
"HasDefault",
"Reserved2",
"Reserved3",
"Reserved4",
"ReservedMask"
],
"type": "string",
"format": "int32"
},
一般问题是,为什么在未使用此模型时添加此模型?当 MS 包全局添加未使用的模型时,如何摆脱它们?
【问题讨论】:
标签: c# asp.net-core openapi swashbuckle.aspnetcore