我正在與下列數據契約的一個更復雜的版本工作,但是這應足以作爲一個例子:生成swagger文件時如何讓Swashbuckle尊重DataType?
using System;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
[DataContract(Namespace = "https://schemas.company.name/api-name/")]
public class Subscription
{
[DataMember(IsRequired = true)]
public long SubscriptionID { get; set; }
[DataMember(IsRequired = true)]
public long ProductID { get; set; }
[DataMember(IsRequired = true)]
public long AccountID { get; set; }
[DataMember(IsRequired = true), DataType(DataType.Date)]
public DateTime StartDate { get; set; }
[DataMember(IsRequired = true), DataType(DataType.Date)]
public DateTime EndDate { get; set; }
}
通過Swashbuckle用於上述數據合同生成的招搖JSON定義變成這樣:
...
"Subscription": {
"required": ["subscriptionID", "productID", "accountID", "startDate", "endDate"],
"type": "object",
"properties": {
"subscriptionID": {
"format": "int64",
"type": "integer"
},
"productID": {
"format": "int64",
"type": "integer"
},
"accountID": {
"format": "int64",
"type": "integer"
},
"startDate": {
"format": "date-time",
"type": "string"
},
"endDate": {
"format": "date-time",
"type": "string"
}
}
},
...
但是,你會發現JSON definitions.Subscription.properties.startDate.format
爲"date-time"
,但DateTypeAttribute
註釋在C#代碼DataType.Date
,不DataType.DateTime
。
在生成swagger文件時,如何使Swashbuckle尊重System.ComponentModel.DataAnnotations.DataTypeAttribute
?或者更具體地說,使用[DataType(DataType.Date]
註釋的類屬性生成format
的"date"
?
我希望有這是所有類的默認行爲,因爲我有太多具體的類屬性的硬編碼的詳細信息,並使用Swashbuckle基礎上從內同其他註釋產生招搖JSON整點命名空間(如System.ComponentModel.DataAnnotations.StringLengthAttribute
)。
我最初的嘗試是在我Startup.cs使用ISchemaFilter,如嘗試:
services.AddSwaggerGen(options =>
{
...
options.SchemaFilter<DataTypeSchemaFilter>();
...
});
如果實施過濾器類應用:
public class DataTypeSchemaFilter : ISchemaFilter
{
public void Apply(Schema model, SchemaFilterContext context)
{
???
}
}
不過,我看不出有什麼能力使用提供的Schema model
和SchemaFilterContext context
參數從過濾器內調查類屬性屬性。
如前所述,我知道Swashbuckle在處理類屬性時會查看同一名稱空間中的屬性,所以我希望有人知道我可以在哪裏綁定Swashbuckle並執行類似的任務。
你能提供你的ISchemaFilter來自哪個命名空間嗎?我在'Swashbuckle.SwaggerGen.Generator.ISchemaFilter'中找到的那個沒有相同的函數原型。 – Sybeus
Swashbuckle.Swagger.ISchemaFilter。我正在使用v5.4.0。 – fbhdev
固定用於AspNetCore。 – fbhdev