C#重新安装-是否有转换器用于从Swagger文档创建C#接口和模型



我正在从Swagger文档中创建自动类型安全的REST API(重新安装(。我正在手动创建接口和模型类,如下所示。有什么工具可以像这样生成吗?

Swagger文件:

"paths": {
"/configuration/v1/devices/{device_serial}": {
"get": {
"tags": [
"Devices"
],
"summary": "Get variablised template for a Switch.",
"description": " Response information.",
"x-deployed": true,
"operationId": "api.devices.get_device",
"produces": [
"multipart/form-data"
],
"parameters": [
{
"in": "path",
"name": "device_serial",
"description": "Serial number of the device.",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Successful operation.",
"schema": {
"type": "object",
"properties": {
"total": {
"type": "integer"
},
"data": {
"type": "string"
}
}
}
}
}
}
}
}

接口代码:

/// <summary>
/// Get variablised template for a Switch.
/// </summary>
/// <remarks>Response information</remarks>
/// <exception cref="Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="deviceSerial"></param>
/// <param name="cancellationToken"></param>
/// <returns>Task of String</returns>
[Get("/configuration/v1/devices/{device_serial}")]
Task<DevicesResponse> GetDeviceVariabilisedTemplateAsync(
[AliasAs("device_serial")] string deviceSerial,
CancellationToken cancellationToken = default);

型号类别:

[DataContract]
public class DevicesResponse
{
[DataMember(Name = "total", EmitDefaultValue = false)]
public int? Total { get; set; } = default!;
[DataMember(Name = "data", EmitDefaultValue = false)]
public string? Data { get; set; } = default!;
}

我有大量的Swagger文件。所以我想动态地做这件事,如果有人知道,请帮助我。

您可以使用名为Refutter的工具从OpenAPI规范生成重新安装接口。

您可以使用Refutter直接通过命令行执行此操作,也可以使用扩展REST API客户端代码生成器从Visual Studio执行此操作

最简单的方法是使用https://editor.swagger.io/从开放的API文档中生成完整的服务器或客户端代码,包括所有使用的模型。但正如每一个在线工具一样,你不应该将任何敏感数据复制粘贴到网站上。如果您的swagger文档中有敏感数据,请先将其删除,或者使用类似的离线代码生成器https://swagger.io/tools/swagger-codegen/

最新更新