有沒有辦法從您的示例值中排除/洗掉屬性?
我在我的模型上使用 XML 注釋來提供關于 swagger 頁面的資訊,c.IncludeXmlComments
我使用///<example>Example Value</example> XML 標記來設定示例值。我的請求模型默認情況下不需要設定所有欄位,但如果我沒有設定示例XML 標記,示例值將轉換為它的型別。看起來像這樣
{
"ID": "string",
"ExampleSetValue": "Example Value"
}
我希望我的示例值只包含 ExampleSetValue 屬性,所以我的示例值看起來像這樣
{
"ExampleSetValue": "Example Value"
}
這是我啟動時的招搖設定
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", openApiInfo);
c.AddSecurityDefinition("Bearer", openApiSecurityScheme);
c.AddSecurityRequirement(openApiSecurityRequirement);
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
我的請求模型
public class CreateRequest
{
/// <summary>
/// ID of the user if available
/// </summary>
public string ID { get; set; }
/// <summary>
/// ExampleSetValue
/// </summary>
/// <example>Example Value</example>
public string ExampleSetValue { get; set; }
}
uj5u.com熱心網友回復:
xml 注釋似乎不能用于排除該屬性。
如果你想忽略屬性,我建議你使用[System.Text.Json.Serialization.JsonIgnore]屬性。但是通過這種方式,它也會在您序列化或反序列化資料時隱藏您的屬性。
public class CreateRequest
{
/// <summary>
/// ID of the user if available
/// </summary>
[JsonIgnore] //add this..........
public string ID { get; set; }
/// <summary>
/// ExampleSetValue
/// </summary>
/// <example>Example Value</example>
public string ExampleSetValue { get; set; }
}
如果您只想從示例值中排除該屬性,則需要自定義 ISchemaFilter:
模型:
public class CreateRequest
{
/// <summary>
/// ID of the user if available
/// </summary>
[IgnoreDataMember] //add this..........
public string ID { get; set; }
/// <summary>
/// ExampleSetValue
/// </summary>
/// <example>Example Value</example>
public string ExampleSetValue { get; set; }
}
自定義 ISchemaFilter:
public class MySwaggerSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (schema?.Properties == null)
{
return;
}
var ignoreDataMemberProperties = context.Type.GetProperties()
.Where(t => t.GetCustomAttribute<IgnoreDataMemberAttribute>() != null);
foreach (var ignoreDataMemberProperty in ignoreDataMemberProperties)
{
var propertyToHide = schema.Properties.Keys
.SingleOrDefault(x => x.ToLower() == ignoreDataMemberProperty.Name.ToLower());
if (propertyToHide != null)
{
schema.Properties.Remove(propertyToHide);
}
}
}
}
注冊過濾器:
services.AddSwaggerGen(c =>
{
//......
c.SchemaFilter<MySwaggerSchemaFilter>();
});
uj5u.com熱心網友回復:
我找到了一種完全適合我的情況的不同方法。您可以在您的架構上設定一個 OpenApiObject 型別的示例物件。
我創建了一個檔案過濾器,它在我的招搖中回圈遍歷模式。
public class AddExamplesFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
foreach (var schema in context.SchemaRepository.Schemas)
{
schema.Value.Example = ExampleManager.GetExample(schema.Key);
}
}
}
創建了 ExampleManager 類,它根據模式名稱回傳我想要的示例物件。
public static OpenApiObject GetExample(string requestModelName)
{
return requestModelName switch
{
"ObjectExample" => ObjectExample.GetExample(),
_ => null
};
}
最后一步創建了一個示例類,它完全符合我希望我的示例的外觀。我在哪里使用 OpenApiObject 類
public static class ObjectExample
{
public static OpenApiObject GetExample()
{
return new OpenApiObject
{
["ObjectInsideObject"] = new OpenApiObject
{
["Name"] = new OpenApiString("name"),
},
["ArrayWithObjects"] = new OpenApiArray
{
new OpenApiObject
{
["Object"] = new OpenApiObject
{
["integer"] = new OpenApiInteger(15),
},
["Description"] = new OpenApiString("description")
}
},
["date"] = new OpenApiDate(new DateTime().AddMonths(1)),
["Description"] = new OpenApiString("description"),
};
}
}
Swagger 頁面的最終外觀

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/511745.html
