1

I'm creating automatic type-safe REST API (Refit) from Swagger document. I'm Creating Interface and model class manually like below. Is there any tool to to generate like this?

Swagger file:

"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"
                }
              }
            }
          }
        }
      }
    }
}

Interface code:

        /// <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);

Model Class:

[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!;
    }

I'm having huge number of Swagger files. So I want to do this dynamically, Please help me if anyone know.

2 Answers2

2

You can Generate Refit interfaces from OpenAPI specifications using a tool called Refitter.

You can do this via the command line using Refitter directly or from Visual Studio using the extension REST API Client Code Generator

1

The easiest way is to use https://editor.swagger.io/ to generate full server or client code from a open API documentation including all used models. But as it is with every online tool you should not copy-paste any sensitive data to the site. If there is sensitive data in your swagger documentation either remove it first or use a offline code generator like https://swagger.io/tools/swagger-codegen/

René
  • 100
  • 1
  • 8