OpenAPI (Swagger) 规范是否支持模式的通用对象?



我正在研究要在C#项目中使用的OpenAPI规范。我的 API 使用泛型类型,例如

class Message<T>
{
public string Id { get; set; }
public T Payload { get; set; }
}
class BasicClientState
{
public int Count { get; set; }
public double FaultPercentage { get; set; }
}

喜欢这个

class SomeApiController
{
public Message<DateTime> GetLatestRebootMessage() 
{
throw new NotImplementedException();
}
public Message<BasicClientState> GetLatestBasicClientStateMessage()
{
throw new NotImplementedException();
}
}

像这样的泛型是否可以在OpenAPI模式中表示,以便为具有泛型支持的语言(即Java,TypeScript(生成的DTO也将具有泛型类型?

是的,但您将丢失有关这些类的泛型来源的信息。

使用swashbuckle,这是从生成的swagger.json摘录的:

"DateTimeMessage": {
"type": "object",
"properties": {
"id": {
"type": "string",
"nullable": true
},
"payload": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": false
},
"BasicClientState": {
"type": "object",
"properties": {
"count": {
"type": "integer",
"format": "int32"
},
"faultPercentage": {
"type": "number",
"format": "double"
}
},
"additionalProperties": false
},
"BasicClientStateMessage": {
"type": "object",
"properties": {
"id": {
"type": "string",
"nullable": true
},
"payload": {
"$ref": "#/components/schemas/BasicClientState"
}
},
"additionalProperties": false
}

最新更新